001: //==============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.boot;
025:
026: import java.awt.Component;
027: import java.io.File;
028: import java.io.UnsupportedEncodingException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.net.URLClassLoader;
032: import java.net.URLDecoder;
033: import java.util.ArrayList;
034: import javax.swing.JOptionPane;
035:
036: //==============================================================================
037:
038: public class Util {
039: //---------------------------------------------------------------------------
040: //---
041: //--- Boot methods
042: //---
043: //---------------------------------------------------------------------------
044:
045: /** Returns the full path of the jar file that contains the running class
046: */
047:
048: public static String getJarFile(String classPath) {
049: String dir = ClassLoader.getSystemResource(classPath)
050: .toString();
051:
052: try {
053: dir = URLDecoder.decode(dir, "UTF-8");
054: } catch (UnsupportedEncodingException e) {
055: //--- this should not happen but ...
056:
057: e.printStackTrace();
058: }
059:
060: dir = dir.replace('\\', '/');
061:
062: if (dir.startsWith("jar:"))
063: dir = dir.substring(4);
064: if (dir.startsWith("file:"))
065: dir = dir.substring(5);
066:
067: //--- skip the ending string "/"+clazz
068:
069: dir = dir.substring(0, dir.length() - classPath.length() - 1);
070:
071: //--- we must skip the "xxx.jar!" string (if the case)
072:
073: if (dir.endsWith("!"))
074: dir = dir.substring(0, dir.length() - 1);
075:
076: //--- hack for windows : dirs like '/C:/...' must be changed to remove the
077: //--- starting slash
078:
079: if (dir.startsWith("/") && dir.indexOf(":") != -1)
080: dir = dir.substring(1);
081:
082: return dir;
083: }
084:
085: //---------------------------------------------------------------------------
086:
087: public static URL[] getJarUrls(String dir) throws Exception {
088: try {
089: String jars[] = new File(dir).list();
090:
091: ArrayList<String> al = new ArrayList<String>();
092:
093: for (String jar : jars)
094: if (jar.endsWith(".jar"))
095: al.add(jar);
096:
097: URL urls[] = new URL[al.size()];
098:
099: int pos = 0;
100:
101: for (String jar : al)
102: urls[pos++] = new URL("file:" + dir + "/" + jar);
103:
104: return urls;
105: }
106:
107: catch (MalformedURLException e) {
108: showError("Malformed URL --> " + e.getMessage());
109: throw e;
110: }
111:
112: catch (NullPointerException e) {
113: showError("Null pointer ex while scanning : " + dir);
114: throw e;
115: }
116: }
117:
118: //---------------------------------------------------------------------------
119:
120: public static void boot(String appPath, URL[] jarFiles,
121: String className, String args[]) throws Exception {
122: URLClassLoader mcl = new URLClassLoader(jarFiles);
123:
124: try {
125: Starter starter = (Starter) Class.forName(className, true,
126: mcl).newInstance();
127:
128: starter.start(appPath, args);
129: } catch (Throwable e) {
130: e.printStackTrace();
131: showError(e);
132:
133: while (e.getCause() != null) {
134: e = e.getCause();
135: showError(e);
136: }
137:
138: //--- this line is needed to exit in case of Errors
139: //--- (not Exceptions) when the GUI is up
140:
141: System.exit(-1);
142: }
143: }
144:
145: //---------------------------------------------------------------------------
146: //---
147: //--- GUI methods
148: //---
149: //---------------------------------------------------------------------------
150:
151: public static void showError(Throwable t) {
152: String message = t.getClass().getSimpleName() + "\n"
153: + t.getMessage();
154:
155: JOptionPane.showMessageDialog(null, message, "Error",
156: JOptionPane.ERROR_MESSAGE);
157: }
158:
159: //---------------------------------------------------------------------------
160:
161: public static void showError(String message) {
162: JOptionPane.showMessageDialog(null, message, "Error",
163: JOptionPane.ERROR_MESSAGE);
164: }
165:
166: //---------------------------------------------------------------------------
167:
168: public static void showInfo(String message) {
169: JOptionPane.showMessageDialog(null, message, "Information",
170: JOptionPane.INFORMATION_MESSAGE);
171: }
172: }
173:
174: //==============================================================================
|