40
40
import java .io .IOException ;
41
41
import java .io .InputStream ;
42
42
import java .io .InputStreamReader ;
43
+ import java .io .UnsupportedEncodingException ;
44
+ import org .apache .logging .log4j .Logger ;
43
45
import java .lang .management .ManagementFactory ;
46
+ import java .net .URISyntaxException ;
47
+ import java .net .URLDecoder ;
44
48
import java .nio .file .Files ;
45
49
import java .nio .file .Paths ;
50
+ import java .security .CodeSource ;
46
51
import java .util .ArrayList ;
47
52
import java .util .Map ;
48
53
49
54
import org .apache .commons .io .filefilter .WildcardFileFilter ;
55
+ import org .apache .logging .log4j .LogManager ;
50
56
import org .eclipse .swt .widgets .Display ;
51
57
import org .eclipse .swt .widgets .Shell ;
52
58
@@ -60,8 +66,11 @@ public class DataLoaderRunner extends Thread {
60
66
private static final String GMT_FOR_DATE_FIELD_VALUE = "datefield.usegmt" ;
61
67
private static final String SWT_NATIVE_LIB_IN_JAVA_LIB_PATH = "swt.nativelib.inpath" ;
62
68
private static final String LOCAL_SWT_DIR = "target/" ;
69
+ private static final String PATH_SEPARATOR = System .getProperty ("path.separator" );
70
+ private static final String FILE_SEPARATOR = System .getProperty ("file.separator" );
63
71
private static boolean useGMTForDateFieldValue = false ;
64
72
private static Map <String , String > argNameValuePair ;
73
+ private static Logger logger ;
65
74
66
75
private static boolean isBatchMode () {
67
76
return argNameValuePair .containsKey (RUN_MODE ) ?
@@ -90,28 +99,17 @@ public void run() {
90
99
}
91
100
92
101
public static void main (String [] args ) {
102
+ Controller .initializeConfigDirAndLog (args );
93
103
Runtime .getRuntime ().addShutdownHook (new DataLoaderRunner ());
94
104
argNameValuePair = Controller .getArgMapFromArgArray (args );
95
- Controller . setConfigDir ( args );
105
+ logger = LogManager . getLogger ( DataLoaderRunner . class );
96
106
setUseGMTForDateFieldValue ();
97
107
if (isBatchMode ()) {
98
108
ProcessRunner .runBatchMode (args );
99
109
} else if (argNameValuePair .containsKey (SWT_NATIVE_LIB_IN_JAVA_LIB_PATH )
100
110
&& "true" .equalsIgnoreCase (argNameValuePair .get (SWT_NATIVE_LIB_IN_JAVA_LIB_PATH ))){
101
111
/* Run in the UI mode, get the controller instance with batchMode == false */
102
112
try {
103
- String SWTDirStr = System .getProperty ("java.library.path" );
104
- if (SWTDirStr == null
105
- || SWTDirStr .isBlank ()
106
- || SWTDirStr .equalsIgnoreCase ("null" )
107
- || !(Files .exists (Paths .get (SWTDirStr )))) {
108
- System .err .println ("Unable to find SWT directory: " + SWTDirStr );
109
- System .err .println ("Native JRE for "
110
- + System .getProperty ("os.name" ) + " : "
111
- + System .getProperty ("os.arch" ) + " not supported." );
112
- System .err .println ("Try JRE for the supported platform in emulation mode." );
113
- System .exit (-1 );
114
- }
115
113
Controller controller = Controller .getInstance (UI , false , args );
116
114
controller .createAndShowGUI ();
117
115
} catch (ControllerInitializationException e ) {
@@ -123,8 +121,6 @@ public static void main(String[] args) {
123
121
}
124
122
125
123
private static void rerunWithSWTNativeLib (String [] args ) {
126
- String separator = System .getProperty ("file.separator" );
127
- String classpath = System .getProperty ("java.class.path" );
128
124
String javaExecutablePath = null ;
129
125
try {
130
126
javaExecutablePath = ProcessHandle .current ()
@@ -136,43 +132,58 @@ private static void rerunWithSWTNativeLib(String[] args) {
136
132
}
137
133
if (javaExecutablePath == null ) {
138
134
javaExecutablePath = System .getProperty ("java.home" )
139
- + separator + "bin" + separator + "java" ;
135
+ + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java" ;
140
136
}
141
137
// java command is the first argument
142
138
ArrayList <String > jvmArgs = new ArrayList <String >(128 );
139
+ logger .debug ("java executable path: " + javaExecutablePath );
143
140
jvmArgs .add (javaExecutablePath );
144
141
145
142
// JVM options
146
143
// set -XstartOnFirstThread for MacOS
147
144
String osName = System .getProperty ("os.name" ).toLowerCase ();
148
145
if ((osName .contains ("mac" )) || (osName .startsWith ("darwin" ))) {
149
146
jvmArgs .add ("-XstartOnFirstThread" );
147
+ logger .debug ("added JVM arg -XstartOnFirsThread" );
150
148
}
151
149
152
150
// set JVM arguments
153
- String SWTDir = getSWTDir ();
154
- jvmArgs .add ("-Djava.library.path=" + SWTDir );
151
+ // set library path
152
+ String librarypath = System .getProperty ("java.library.path" );
153
+ if (librarypath != null && !librarypath .isBlank ()) {
154
+ librarypath = getSWTDir () + PATH_SEPARATOR + librarypath ;
155
+ } else {
156
+ librarypath = getSWTDir ();
157
+ }
158
+ jvmArgs .add ("-Djava.library.path=" + librarypath );
159
+ logger .debug ("set java.library.path=" + librarypath );
155
160
jvmArgs .addAll (ManagementFactory .getRuntimeMXBean ().getInputArguments ());
156
161
157
162
// set classpath
158
- String pathSeparator = System .getProperty ("path.separator " );
163
+ String classpath = System .getProperty ("java.class.path " );
159
164
if (classpath != null && !classpath .isBlank ()) {
160
- classpath = classpath + pathSeparator ;
165
+ classpath = getSWTJarPath () + PATH_SEPARATOR + classpath ;
166
+ } else {
167
+ classpath = getSWTJarPath ();
161
168
}
162
- classpath = classpath + getSWTJarPath ();
163
169
jvmArgs .add ("-cp" );
164
170
jvmArgs .add (classpath );
171
+ logger .debug ("set java.class.path=" + classpath );
165
172
166
173
// specify name of the class with main method
167
174
jvmArgs .add (DataLoaderRunner .class .getName ());
175
+ logger .debug ("added class to execute - " + DataLoaderRunner .class .getName ());
168
176
169
177
// specify application arguments
178
+ logger .debug ("added following arguments:" );
170
179
for (int i = 0 ; i < args .length ; i ++) {
171
180
jvmArgs .add (args [i ]);
181
+ logger .debug (" " + args [i ]);
172
182
}
173
183
174
184
// add the argument to indicate that JAVA_LIB_PATH has the directory containing SWT native libraries
175
185
jvmArgs .add (SWT_NATIVE_LIB_IN_JAVA_LIB_PATH + "=true" );
186
+ logger .debug (" " + SWT_NATIVE_LIB_IN_JAVA_LIB_PATH + "=true" );
176
187
ProcessBuilder processBuilder = new ProcessBuilder (jvmArgs );
177
188
processBuilder .redirectErrorStream (true );
178
189
try {
@@ -234,7 +245,12 @@ private static String getOSName() {
234
245
}
235
246
236
247
private static String getSWTDir () {
237
- String SWTDirStr = buildPathStringFromOSAndArch ("swt" , "" , "" , "" );
248
+ String path = getDirContainingClassJar (DataLoaderRunner .class );
249
+ if (path == null ) {
250
+ path = "." ;
251
+ }
252
+
253
+ String SWTDirStr = buildPathStringFromOSAndArch (path + "/" +"swt" , "" , "" , "" );
238
254
if (Files .exists (Paths .get (SWTDirStr ))) {
239
255
return SWTDirStr ;
240
256
}
@@ -246,7 +262,6 @@ private static String getSWTDir() {
246
262
}
247
263
248
264
SWTDirStr = buildPathStringFromOSAndArch (LOCAL_SWT_DIR + "swt" , "" , "" , "" );
249
-
250
265
if (SWTDirStr == null ) {
251
266
System .err .println ("Unable to find SWT directory for "
252
267
+ System .getProperty ("os.name" ) + " : "
@@ -271,4 +286,28 @@ private static String getSWTJarPath() {
271
286
}
272
287
return files [0 ].getPath ();
273
288
}
289
+
290
+ private static String getDirContainingClassJar (Class aClass ) {
291
+ CodeSource codeSource = aClass .getProtectionDomain ().getCodeSource ();
292
+
293
+ File jarFile = null ;
294
+
295
+ if (codeSource != null && codeSource .getLocation () != null ) {
296
+ try {
297
+ jarFile = new File (codeSource .getLocation ().toURI ());
298
+ } catch (URISyntaxException e ) {
299
+ return null ;
300
+ }
301
+ } else {
302
+ String path = DataLoaderRunner .class .getResource (aClass .getSimpleName () + ".class" ).getPath ();
303
+ String jarFilePath = path .substring (path .indexOf (":" ) + 1 , path .indexOf ("!" ));
304
+ try {
305
+ jarFilePath = URLDecoder .decode (jarFilePath , "UTF-8" );
306
+ } catch (UnsupportedEncodingException e ) {
307
+ // fail silently;
308
+ }
309
+ jarFile = new File (jarFilePath );
310
+ }
311
+ return jarFile .getParentFile ().getAbsolutePath ();
312
+ }
274
313
}
0 commit comments