001: package org.objectweb.salome_tmf.ihm.main;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.net.JarURLConnection;
009: import java.net.URL;
010: import java.net.URLConnection;
011: import java.util.Properties;
012: import java.util.jar.JarFile;
013: import java.util.jar.Manifest;
014:
015: import javax.jnlp.BasicService;
016: import javax.jnlp.ServiceManager;
017: import javax.jnlp.UnavailableServiceException;
018:
019: public class SalomeJWS_BootStrap {
020: URL urlSalome;
021: BasicService bs;
022:
023: String tmpDir = System.getProperties()
024: .getProperty("java.io.tmpdir");
025: String fs = System.getProperties().getProperty("file.separator");
026: Properties salomeProperties = new Properties();
027: String propertiesFile;
028:
029: String commons_jars = "commons-logging.jar, jfreechart-1.0.1.jar, log4j-1.2.6.jar, driver.jar, jcommon-1.0.0.jar, jpf-0.3.jar, pg74.216.jdbc3.jar ";
030: String jar_salome = "salome_tmf_data.jar,salome_tmf_api.jar,salome_tmf_ihm.jar,salome_tmf_sqlengine.jar,salome_tmf_coreplug.jar,salome_tmf_login.jar,salome_tmf_tools.jar";
031: String htmlfiles = "index.html, Salome.html, Salome2.html";
032: String configs = "cfg/DB_Connexion.properties,cfg/key.txt";
033:
034: String install_path = "SalomeV2";
035:
036: SalomeJWS_BootStrap(String installDir) {
037: bootStrap(installDir);
038: }
039:
040: void bootStrap(String installDir) {
041: try {
042: /*JFrame f = new JFrame();
043: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
044: f.show();
045: */
046: if (installDir != null) {
047: install_path += fs + installDir;
048: }
049: bs = (BasicService) ServiceManager
050: .lookup("javax.jnlp.BasicService");
051: urlSalome = bs.getCodeBase();
052: propertiesFile = tmpDir + fs + install_path + fs
053: + "cfg/DB_Connexion.properties";
054:
055: System.out.println("Installed Salome is = " + urlSalome);
056: System.out.println("Installation Salome is = "
057: + install_path);
058: if (create_salomeInstall()) {
059: /* Install Resources */
060: installRessource();
061: } else {
062: /* Install Resources if needded HTML */
063: updateRessource();
064: }
065: updateProperties();
066:
067: /* Start Salome TMF */
068: displayURL(new File(tmpDir + fs + install_path + fs
069: + "Salome2.html").toURL().toString());
070:
071: /* Exit boot Strap */
072: System.exit(0);
073:
074: } catch (Exception exc) {
075: exc.printStackTrace();
076: System.exit(0);
077: }
078: }
079:
080: /****************************** Properties ****************************************/
081: void updateProperties() throws Exception {
082: if (loadPluginPropertie()) {
083: savePluginPropertie();
084: }
085: }
086:
087: boolean loadPluginPropertie() throws Exception {
088: boolean needUpdate = false;
089: FileInputStream ins = new FileInputStream(propertiesFile);
090: salomeProperties.load(ins);
091: ins.close();
092: try {
093: String SalomeCodeBase = salomeProperties
094: .getProperty("CodeBase");
095: if (!(SalomeCodeBase.trim().equals(urlSalome.toString()
096: .trim()))) {
097: salomeProperties.setProperty("CodeBase", urlSalome
098: .toString().trim());
099: needUpdate = true;
100: System.out.println("Code base in properties file"
101: + SalomeCodeBase + "need Update");
102: } else {
103: needUpdate = false;
104: System.out
105: .println("Code base in properties file is OK");
106: }
107: } catch (Exception e) {
108: needUpdate = true;
109: salomeProperties.setProperty("CodeBase", urlSalome
110: .toString().trim());
111: System.out
112: .println("Code base in properties file need Update with"
113: + urlSalome.toString().trim());
114: }
115: return needUpdate;
116: }
117:
118: void savePluginPropertie() throws Exception {
119:
120: FileOutputStream out = new FileOutputStream(propertiesFile);
121: salomeProperties.store(out, "");
122: out.close();
123: }
124:
125: /******************************* JAR/HTML ******************************************/
126: void updateRessource() throws Exception {
127: String[] jar_list = jar_salome.split(",");
128: for (int i = 0; i < jar_list.length; i++) {
129: String url = urlSalome.toString() + jar_list[i].trim();
130:
131: String urlRemote = "jar:" + urlSalome.toString();
132: urlRemote += jar_list[i].trim() + "!/";
133: File fileLocal = new File(tmpDir + fs + install_path + fs
134: + jar_list[i].trim());
135: if (needUpdate(urlRemote, fileLocal)) {
136: installFile(new URL(url), jar_list[i].trim());
137: }
138: }
139: }
140:
141: boolean needUpdate(String remoteUrl, File localFile) {
142: boolean ret = false;
143: try {
144: URL jar = new URL(remoteUrl);
145: JarURLConnection jarConnection = (JarURLConnection) jar
146: .openConnection();
147: Manifest remoteManifest = jarConnection.getManifest();
148:
149: JarFile localeJar = new JarFile(localFile);
150: Manifest localManifest = localeJar.getManifest();
151:
152: if (!remoteManifest.equals(localManifest)) {
153: System.out.println("Need to Update : " + localFile);
154: ret = true;
155: } else {
156: System.out.println("No Update for : " + localFile);
157: }
158:
159: //System.out.println("JarManifest = " + manifest + " for " + jar);
160: } catch (Exception e) {
161: ret = true;
162: }
163: return ret;
164: }
165:
166: void installRessource() throws Exception {
167: /* Les Jars*/
168: String[] jar_list = commons_jars.split(",");
169: for (int i = 0; i < jar_list.length; i++) {
170: String urlJar = urlSalome.toString() + jar_list[i].trim();
171: System.out.println("Install JAR = " + urlJar);
172: URL url = new URL(urlJar);
173: try {
174: installFile(url, jar_list[i].trim());
175: } catch (Exception e) {
176:
177: }
178: }
179:
180: jar_list = jar_salome.split(",");
181: for (int i = 0; i < jar_list.length; i++) {
182: String urlJar = urlSalome.toString() + jar_list[i].trim();
183: System.out.println("Install JAR = " + urlJar);
184: URL url = new URL(urlJar);
185: installFile(url, jar_list[i].trim());
186: }
187:
188: /*Les htmls*/
189: String[] text_files = htmlfiles.split(",");
190: for (int i = 0; i < text_files.length; i++) {
191: String urlfile = urlSalome.toString()
192: + text_files[i].trim();
193: URL url = new URL(urlfile);
194: System.out.println("Install HTML = " + url);
195: installFile(url, text_files[i].trim());
196: }
197: /*Les config*/
198: String[] config_files = configs.split(",");
199: for (int i = 0; i < config_files.length; i++) {
200: String urlfile = urlSalome.toString()
201: + config_files[i].trim();
202: URL url = new URL(urlfile);
203: System.out.println("Install Config = " + url);
204: installFile(url, config_files[i].trim());
205: }
206: }
207:
208: void installFile(URL pUrl, String name) throws Exception {
209: try {
210: URLConnection urlconn = pUrl.openConnection();
211: InputStream is = urlconn.getInputStream();
212: File file = new File(tmpDir + fs + install_path + fs + name);
213: System.out.println("Write file " + file);
214: writeStream(is, file);
215: } catch (Exception e) {
216: e.printStackTrace();
217: }
218: }
219:
220: void writeStream(InputStream is, File f) throws Exception {
221: byte[] buf = new byte[102400];
222: f.createNewFile();
223: FileOutputStream fos = new FileOutputStream(f);
224: int len = 0;
225: while ((len = is.read(buf)) != -1) {
226: fos.write(buf, 0, len);
227: }
228: fos.close();
229: is.close();
230: }
231:
232: boolean showDocument(URL toShow) {
233: boolean ret = false;
234: try {
235: // Lookup the javax.jnlp.BasicService object
236: BasicService bs = (BasicService) ServiceManager
237: .lookup("javax.jnlp.BasicService");
238: // Invoke the showDocument method
239: ret = bs.showDocument(toShow);
240:
241: } catch (UnavailableServiceException ue) {
242: // Service is not supported
243: }
244: return ret;
245: }
246:
247: public static void main(String arg[]) {
248: String install_dir = null;
249: if (arg.length > 0) {
250: install_dir = arg[0];
251: }
252: new SalomeJWS_BootStrap(install_dir);
253: }
254:
255: /**********************************************************************************************/
256:
257: boolean create_salomeInstall() throws Exception {
258: boolean ret = false;
259: File file = new File(tmpDir + fs + install_path);
260: if (!file.exists()) {
261: file.mkdirs();
262: ret = true;
263: }
264: file = new File(tmpDir + fs + install_path + fs + "cfg");
265: if (!file.exists()) {
266: file.mkdirs();
267: }
268: return ret;
269: }
270:
271: /*************************************************************************************************/
272: public boolean displayURL(String url) {
273:
274: // Opening a browser, even when running sandbox-restricted
275: // in JavaWebStart.
276: System.out.println("Try to open url with differents methode: "
277: + url);
278:
279: try {
280: if (showDocument(new URL(url))) {
281: System.out.println("Url open with JavaWebStart");
282: return true;
283: }
284: } catch (Exception e) {
285: e.printStackTrace();
286: // Not running in JavaWebStart or service is not supported.
287: // We continue with the methods below ...
288: }
289:
290: //String[] cmd = null;
291:
292: switch (getPlatform()) {
293: case (WIN_ID): {
294: System.out.println("Try Windows Command Line");
295: return runCmdLine(replaceToken(WIN_CMDLINE, URLTOKEN, url));
296: }
297: case (MAC_ID): {
298: System.out.println("Try Mac Command Line");
299: return runCmdLine(replaceToken(MAC_CMDLINE, URLTOKEN, url));
300: }
301: default:
302: System.out.println("Try Unix Command Line");
303: for (int i = 0; i < OTHER_CMDLINES.length; i++) {
304: if (runCmdLine(replaceToken(OTHER_CMDLINES[i],
305: URLTOKEN, url), replaceToken(
306: OTHER_FALLBACKS[i], URLTOKEN, url)))
307: return true;
308: }
309: }
310:
311: return false;
312: }
313:
314: /**
315: * Try to determine whether this application is running under Windows or
316: * some other platform by examing the "os.name" property.
317: *
318: * @return the ID of the platform
319: */
320: private int getPlatform() {
321: String os = System.getProperty("os.name");
322: if (os != null && os.startsWith(WIN_PREFIX))
323: return WIN_ID;
324: if (os != null && os.startsWith(MAC_PREFIX))
325: return MAC_ID;
326: return OTHER_ID;
327: }
328:
329: private String[] replaceToken(String[] target, String token,
330: String replacement) {
331: if (null == target)
332: return null;
333: String[] result = new String[target.length];
334:
335: for (int i = 0; i < target.length; i++)
336: result[i] = target[i].replaceAll(token, replacement);
337:
338: return result;
339: }
340:
341: private boolean runCmdLine(String[] cmdLine) {
342: System.out.println("Try to execute commande line = "
343: + cmdLine);
344: return runCmdLine(cmdLine, null);
345: }
346:
347: private boolean runCmdLine(String[] cmdLine,
348: String[] fallBackCmdLine) {
349: try {
350: System.out.println("Try to execute commande line = "
351: + cmdLine + " with " + fallBackCmdLine);
352: /*
353: * System.out.println( "Trying to invoke browser, cmd='" +
354: * connectStringArray(cmdLine) + "' ... ");
355: */
356: Process p = Runtime.getRuntime().exec(cmdLine);
357:
358: if (null != fallBackCmdLine) {
359: // wait for exit code -- if it's 0, command worked,
360: // otherwise we need to start fallBackCmdLine.
361: int exitCode = p.waitFor();
362: if (exitCode != 0) {
363: /*
364: * System.out.println(exitCode); System.out.println();
365: */
366:
367: /*
368: * System.out.println( "Trying to invoke browser, cmd='" +
369: * connectStringArray(fallBackCmdLine) + "' ...");
370: */
371: Runtime.getRuntime().exec(fallBackCmdLine);
372:
373: }
374: }
375:
376: System.out.println();
377: return true;
378:
379: } catch (InterruptedException e) {
380: System.out.println("Caught: " + e);
381: } catch (IOException e) {
382: System.out.println("Caught: " + e);
383: }
384:
385: return false;
386: }
387:
388: // This token is a placeholder for the actual URL
389: private final String URLTOKEN = "%URLTOKEN%";
390:
391: // Used to identify the windows platform.
392: private final int WIN_ID = 1;
393:
394: // Used to discover the windows platform.
395: private final String WIN_PREFIX = "Windows";
396:
397: // The default system browser under windows.
398: // Once upon a time:
399: // for 'Windows 9' and 'Windows M': start
400: // for 'Windows': cmd /c start
401: private final String[] WIN_CMDLINE = { "rundll32",
402: "url.dll,FileProtocolHandler", URLTOKEN };
403:
404: // Used to identify the mac platform.
405: private final int MAC_ID = 2;
406:
407: // Used to discover the mac platform.
408: private final String MAC_PREFIX = "Mac";
409:
410: // The default system browser under mac.
411: private final String[] MAC_CMDLINE = { "open", URLTOKEN };
412:
413: // Used to identify the mac platform.
414: private final int OTHER_ID = -1;
415:
416: private final String[][] OTHER_CMDLINES = {
417:
418: // The first guess for a browser under other systems (and unix):
419: // Remote controlling firefox
420: // (http://www.mozilla.org/unix/remote.html)
421: { "firefox", "-remote",
422: "openURL(" + URLTOKEN + ",new-window)" },
423:
424: // Remote controlling mozilla
425: // (http://www.mozilla.org/unix/remote.html)
426: { "mozilla", "-remote",
427: "openURL(" + URLTOKEN + ",new-window)" },
428: // The second guess for a browser under other systems (and unix):
429: // The RedHat skript htmlview
430: { "htmlview", URLTOKEN },
431:
432: // The third guess for a browser under other systems (and unix):
433: // Remote controlling netscape
434: // (http://wp.netscape.com/newsref/std/x-remote.html)
435: { "netscape", "-remote", "openURL(" + URLTOKEN + ")" }
436:
437: };
438:
439: private final String[][] OTHER_FALLBACKS = {
440:
441: // Fallback for remote controlling mozilla:
442: //Starting up a new mozilla
443: { "firefox", URLTOKEN },
444:
445: // Starting up a new mozilla
446: { "mozilla", URLTOKEN },
447:
448: // No fallback for htmlview
449: null,
450:
451: // Fallback for remote controlling netscape:
452: // Starting up a new netscape
453: { "netscape", URLTOKEN }
454:
455: };
456:
457: }
|