45
45
46
46
import de .tsl2 .nano .core .classloader .LibClassLoader ;
47
47
import de .tsl2 .nano .core .classloader .NetworkClassLoader ;
48
- //import de.tsl2.nano.collection.PersistableSingelton;
49
- //import de.tsl2.nano.collection.PersistentCache;
50
48
import de .tsl2 .nano .core .cls .BeanClass ;
51
49
import de .tsl2 .nano .core .exception .ExceptionHandler ;
52
50
import de .tsl2 .nano .core .exception .Message ;
@@ -103,7 +101,8 @@ public class ENV implements Serializable {
103
101
private static final long serialVersionUID = 5988200267214868670L ;
104
102
105
103
public static final String PATH_TEMP = "temp/" ;
106
- private static ENV self ;
104
+ // workaround on new threads that should inherit the ENV from last creation -> thread unsafe
105
+ private static ENV lastCreated ;
107
106
private static ThreadLocal <ENV > selfThread = new ThreadLocal <ENV >();
108
107
109
108
@ SuppressWarnings ("rawtypes" )
@@ -149,7 +148,7 @@ private ENV() {
149
148
* @return environment name - equal to the name of the configuration directory, defined in {@link #getConfigPath()}.
150
149
*/
151
150
public static String getName () {
152
- String path = self .getConfigPath ().replace (File .separator , DEF_PATHSEPRATOR );
151
+ String path = self () .getConfigPath ().replace (File .separator , DEF_PATHSEPRATOR );
153
152
if (path .lastIndexOf (DEF_PATHSEPRATOR ) == path .length () - 1 ) {
154
153
path = path .substring (0 , path .length () - 1 );
155
154
}
@@ -201,16 +200,17 @@ public static String getBuildInformations() {
201
200
public static synchronized <T > T get (Class <T > service ) {
202
201
T serviceImpl = (T ) services ().get (service );
203
202
if (serviceImpl == null ) {
203
+ ENV self = self ();
204
204
debug (self , "no service found for " + service );
205
205
debug (self , "available services:\n " + StringUtil .toFormattedString (services (), 500 , true ));
206
206
String path = getConfigPath (service ) + getFileExtension ();
207
207
if (new File (path ).canRead ()) {
208
- self () .info ("loading service from " + path );
209
- serviceImpl = self () .addService (service , self () .get (XmlUtil .class ).loadXml (path , service ));
208
+ self .info ("loading service from " + path );
209
+ serviceImpl = self .addService (service , self .get (XmlUtil .class ).loadXml (path , service ));
210
210
} else if (!service .isInterface ()
211
211
&& BeanClass .hasDefaultConstructor (service , !Util .isFrameworkClass (service ))) {
212
- self () .info ("trying to create service " + service + " through default construction" );
213
- serviceImpl = self () .addService (BeanClass .createInstance (service ));
212
+ self .info ("trying to create service " + service + " through default construction" );
213
+ serviceImpl = self .addService (BeanClass .createInstance (service ));
214
214
if (serviceImpl instanceof Serializable ) {
215
215
get (XmlUtil .class ).saveXml (path , serviceImpl );
216
216
}
@@ -225,27 +225,28 @@ public static synchronized <T> T get(Class<T> service) {
225
225
* @return true, if environment was already created
226
226
*/
227
227
public final static boolean isAvailable () {
228
+ ENV self = selfThread .get () != null ? selfThread .get () : lastCreated ; // the lastCreated is a workaround...
228
229
return self != null && self .properties != null && self .services != null ;
229
230
}
230
231
231
232
protected final static ENV self () {
232
- if (selfThread .get () != null )
233
- return selfThread .get ();
234
- // else // clean version
235
- // throw new IllegalStateException("no environment available. please call ENV.create(...) before!");
236
- if (self == null ) { //dirty version: used in cause of lots of testcases and legacy applications
233
+ if (selfThread .get () != null ) {
234
+ return selfThread .get ();
235
+ } else if (lastCreated != null ) {
236
+ selfThread .set (lastCreated );
237
+ return lastCreated ;
238
+ } else if (lastCreated == null || lastCreated .properties == null || lastCreated .services == null ) {
237
239
System .out .println ("WARN: NO ENV was created before. creating a default instance" );
238
- self = create (System .getProperty (KEY_CONFIG_PATH , System .getProperty ("user.dir" ).replace ('\\' , '/' )));
239
- selfThread .set (self );
240
- } else if (isTestMode () && self .properties == null || self .services == null ) {
241
- System .out .println ("WARN: NO ENV was created before. creating a default instance" );
242
- self = create (System .getProperty (KEY_CONFIG_PATH , System .getProperty ("user.dir" ).replace ('\\' , '/' )));
243
- selfThread .set (self );
244
- }
245
- return self ;
240
+ lastCreated = create (
241
+ System .getProperty (KEY_CONFIG_PATH , System .getProperty ("user.dir" ).replace ('\\' , '/' )));
242
+ selfThread .set (lastCreated );
243
+ return lastCreated ;
244
+ } else // will never be reached ;-) - unclean
245
+ throw new IllegalStateException ("no environment available. please call ENV.create(...) before!" );
246
246
}
247
247
248
248
public static ENV create (String dir ) {
249
+ ENV self ;
249
250
FileUtil .userDirFile (dir ).mkdirs ();
250
251
dir = dir .endsWith ("/" ) ? dir : dir + "/" ;
251
252
String name = dir + StringUtil .substring (dir , PREFIX_ENVNAME , "/" );
@@ -322,7 +323,7 @@ public static ENV create(String dir) {
322
323
// BeanUtil.addStandardTypePackages("de.tsl2.nano.bean.def");
323
324
// self.persist();
324
325
LogFactory .log ("==> ENV " + name + " created successful!" );
325
- return self ;
326
+ return lastCreated = self ;
326
327
}
327
328
328
329
private void update (File configFile , String buildInfo ) {
@@ -408,7 +409,7 @@ public static void reset() {
408
409
// PersistableSingelton.clearCache();
409
410
// PersistentCache.clearCache();
410
411
selfThread .set (null );
411
- self = null ;
412
+ lastCreated = null ;
412
413
}
413
414
414
415
/**
@@ -629,7 +630,9 @@ public String transform(String toTransform) {
629
630
* @return formatted object
630
631
*/
631
632
public static String format (Object obj ) {
632
- return self != null && self .services != null ? ((Format ) services ().getOrDefault (Format .class , new DefaultFormat ())).format (obj ) : new DefaultFormat ().format (obj );
633
+ return self () != null && self ().services != null
634
+ ? ((Format ) services ().getOrDefault (Format .class , new DefaultFormat ())).format (obj )
635
+ : new DefaultFormat ().format (obj );
633
636
}
634
637
635
638
/**
@@ -677,7 +680,7 @@ public static String getTempPath() {
677
680
* @return
678
681
*/
679
682
public static String getTempPathRel () {
680
- self .getTempPath (); // -> mkdir()
683
+ self () .getTempPath (); // -> mkdir()
681
684
return getConfigPathRel () + PATH_TEMP ;
682
685
}
683
686
0 commit comments