001: package org.objectweb.salome_tmf.miniboot;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Rectangle;
006: import java.io.File;
007: import java.io.FileOutputStream;
008: import java.io.InputStream;
009: import java.lang.reflect.Method;
010: import java.net.URL;
011: import java.net.URLConnection;
012: import java.util.jar.JarFile;
013: import java.util.jar.JarInputStream;
014: import java.util.jar.Manifest;
015:
016: import javax.swing.JApplet;
017: import javax.swing.JFrame;
018: import javax.swing.JLabel;
019: import javax.swing.JPanel;
020: import javax.swing.JProgressBar;
021:
022: public class SalomeTMFBoot extends JApplet {
023:
024: String tmpDir = System.getProperties()
025: .getProperty("java.io.tmpdir");
026: String fs = System.getProperties().getProperty("file.separator");
027: String install_path = "SalomeV3_Boot";
028: URL urlSalome;
029:
030: 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, dom4j-1.5.jar";
031: String jar_salome = "salome_tmf_data.jar,salome_tmf_api.jar,salome_tmf_ihm.jar,salome_tmf_sqlengine.jar,salome_tmf_coreplug.jar";
032:
033: static boolean doInit = false;
034: ClassLoader pClassLoader;
035:
036: static JApplet pApplet;
037: static Class classSalomeTMF_AppJWS;
038: static String projectName = null;
039: LoadingBar pLoadingBar;
040:
041: public void init() {
042: if (!doInit) {
043: urlSalome = getCodeBase();
044: System.out.println("Code base is " + urlSalome);
045: //projectName = this.getParameter("projectname");
046: String installDir = chooseProject(getDocumentBase());
047: if (installDir != null) {
048: install_path += fs + installDir;
049: }
050: pClassLoader = this .getClass().getClassLoader();
051: pLoadingBar = new LoadingBar("Loading Salome-TMF",
052: "Install Resources", 13);
053: pLoadingBar.show();
054: try {
055: if (create_salomeInstall()) {
056: installRessource();
057: } else {
058: updateRessource();
059: }
060: create_salomeInstall();
061: } catch (Exception e) {
062:
063: }
064: pLoadingBar.doTask("Launch SalomeTMF");
065: if (pApplet == null) {
066: try {
067: classSalomeTMF_AppJWS = Class
068: .forName("org.objectweb.salome_tmf.ihm.main.SalomeTMF_AppJWS");
069: pApplet = (JApplet) classSalomeTMF_AppJWS
070: .newInstance();
071: Method meth_setParentApplet = classSalomeTMF_AppJWS
072: .getMethod("setParentApplet",
073: new Class[] { JApplet.class });
074: meth_setParentApplet.invoke(pApplet,
075: new Object[] { this });
076: } catch (Exception e) {
077:
078: }
079: }
080: pLoadingBar.dispose();
081: pApplet.init();
082: doInit = true;
083: } else {
084: System.out.println("Reload : " + pApplet.getContentPane());
085: try {
086: Method meth_reloadPanel = classSalomeTMF_AppJWS
087: .getMethod("reloadPanel",
088: new Class[] { JApplet.class });
089: meth_reloadPanel.invoke(pApplet, new Object[] { this });
090: } catch (Exception e) {
091: e.printStackTrace();
092: }
093: //pApplet.getContentPane().validate();
094: //pApplet.getContentPane().repaint();
095: //setContentPane(pApplet.getContentPane());
096: validate();
097: repaint();
098: }
099: }
100:
101: public String chooseProject(URL url) {
102: String urlString = url.toString();
103: String[] tab = urlString.split("[?=]");
104: if (tab.length == 3 && tab[1].equals("project")) {
105: return tab[2];
106: }
107: return null;
108: }
109:
110: public void start() {
111: System.out.println("[SalomeTMFBoot]->start");
112: /*if (!doInit)
113: pApplet.start();*/
114: }
115:
116: public void destroy() {
117: System.out.println("[SalomeTMFBoot]->destroy");
118: /*
119: if (!doInit)
120: pApplet.destroy();
121: */
122: }
123:
124: public void stop() {
125: System.out.println("[SalomeTMFBoot]->stop");
126: /*
127: if (!doInit)
128: pApplet.destroy();
129: */
130: }
131:
132: /******************************************************************************************/
133:
134: private void addJar(URL jar) {
135: try {
136: Class pclC = pClassLoader.getClass();
137: pclC.getMethod("addLocalJar", new Class[] { URL.class })
138: .invoke(pClassLoader, new Object[] { jar });
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142: }
143:
144: /******************************************************************************************/
145:
146: void updateRessource() throws Exception {
147: String[] jar_list = commons_jars.split(",");
148: for (int i = 0; i < jar_list.length; i++) {
149: File fileLocal = new File(tmpDir + fs + install_path + fs
150: + jar_list[i].trim());
151: pLoadingBar.doTask("Update : " + fileLocal);
152: addJar(fileLocal.toURL());
153: }
154: jar_list = jar_salome.split(",");
155: for (int i = 0; i < jar_list.length; i++) {
156: String url = urlSalome.toString() + jar_list[i].trim();
157: URL urlRemote = new URL(url);
158: File fileLocal = new File(tmpDir + fs + install_path + fs
159: + jar_list[i].trim());
160: pLoadingBar.doTask("Update : " + fileLocal);
161: if (needUpdate(urlRemote, fileLocal)) {
162: installFile(urlRemote, fileLocal);
163: }
164: addJar(fileLocal.toURL());
165: }
166:
167: }
168:
169: boolean needUpdate(URL pJarURL, File localFile) {
170: boolean ret = false;
171: if (!localFile.exists()) {
172: return true;
173: }
174: try {
175: JarInputStream jarInput = new JarInputStream(pJarURL
176: .openStream());
177: Manifest remoteManifest = jarInput.getManifest();
178:
179: JarFile localeJar = new JarFile(localFile);
180: Manifest localManifest = localeJar.getManifest();
181:
182: if (!localManifest.equals(remoteManifest)) {
183: ret = true;
184: } else {
185: if (localManifest.getEntries().size() == 0) {
186: ret = true;
187: System.out.println("Need to Update : " + localFile);
188: }
189: }
190: } catch (Exception e) {
191: e.printStackTrace();
192: ret = true;
193: }
194: return ret;
195: }
196:
197: void installRessource() throws Exception {
198: /* Les Jars*/
199: String[] jar_list = commons_jars.split(",");
200: for (int i = 0; i < jar_list.length; i++) {
201: String urlJar = urlSalome.toString() + jar_list[i].trim();
202: URL url = new URL(urlJar);
203: try {
204: File file = new File(tmpDir + fs + install_path + fs
205: + jar_list[i].trim());
206: pLoadingBar.doTask("Install : " + file);
207: installFile(url, file);
208: addJar(file.toURL());
209: } catch (Exception e) {
210:
211: }
212: }
213:
214: jar_list = jar_salome.split(",");
215: for (int i = 0; i < jar_list.length; i++) {
216: String urlJar = urlSalome.toString() + jar_list[i].trim();
217: //System.out.println("Install JAR = " + urlJar);
218: URL url = new URL(urlJar);
219: File file = new File(tmpDir + fs + install_path + fs
220: + jar_list[i].trim());
221: pLoadingBar.doTask("Install : " + file);
222: installFile(url, file);
223: addJar(file.toURL());
224: }
225:
226: }
227:
228: void installFile(URL pUrl, File f) throws Exception {
229: try {
230: URLConnection urlconn = pUrl.openConnection();
231: InputStream is = urlconn.getInputStream();
232: writeStream(is, f);
233: } catch (Exception e) {
234: e.printStackTrace();
235: }
236: }
237:
238: void writeStream(InputStream is, File f) throws Exception {
239: byte[] buf = new byte[102400];
240: f.createNewFile();
241: FileOutputStream fos = new FileOutputStream(f);
242: int len = 0;
243: while ((len = is.read(buf)) != -1) {
244: fos.write(buf, 0, len);
245: }
246: fos.close();
247: is.close();
248: }
249:
250: boolean create_salomeInstall() throws Exception {
251: boolean ret = false;
252: File file = new File(tmpDir + fs + install_path);
253: if (!file.exists()) {
254: file.mkdirs();
255: ret = true;
256: }
257: return ret;
258: }
259:
260: class LoadingBar extends JFrame {
261: private JProgressBar progress;
262: private JLabel label1;
263: private JPanel topPanel;
264:
265: int iCtr = 0;
266: int nbTask = 0;
267:
268: public LoadingBar(String title, String label, int nbTask) {
269: setTitle(title);
270: setSize(310, 130);
271: setResizable(false);
272: setBackground(Color.gray);
273: this .nbTask = nbTask;
274: topPanel = new JPanel();
275: topPanel.setPreferredSize(new Dimension(310, 130));
276: getContentPane().add(topPanel);
277:
278: // Create a label and progress bar
279: label1 = new JLabel(label);
280: label1.setPreferredSize(new Dimension(280, 24));
281: topPanel.add(label1);
282:
283: progress = new JProgressBar();
284: progress.setPreferredSize(new Dimension(300, 20));
285: progress.setMinimum(0);
286: progress.setMaximum(nbTask);
287: progress.setValue(0);
288: progress.setBounds(20, 35, 260, 20);
289: topPanel.add(progress);
290: //this.setLocation(300,300);
291: this .setLocationRelativeTo(this .getParent());
292: //pack();
293: }
294:
295: public void doTask(String task) {
296: iCtr++;
297: // Update the progress indicator and label
298: label1.setText(task);
299: Rectangle labelRect = label1.getBounds();
300: labelRect.x = 0;
301: labelRect.y = 0;
302: label1.paintImmediately(labelRect);
303:
304: progress.setValue(iCtr);
305: Rectangle progressRect = progress.getBounds();
306: progressRect.x = 0;
307: progressRect.y = 0;
308: progress.paintImmediately(progressRect);
309: }
310:
311: /*public synchronized void show(){
312: super.show();
313: }*/
314:
315: public void finishAllTask() {
316: dispose();
317: }
318: }
319: }
|