001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/mailarchive/tags/sakai_2-4-1/mailarchive-james/james/src/java/org/sakaiproject/james/PhoenixLauncherMain.java $
003: * $Id: PhoenixLauncherMain.java 8186 2006-04-24 01:42:56Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.james;
021:
022: import java.io.File;
023: import java.lang.reflect.Method;
024: import java.net.URL;
025: import java.security.CodeSource;
026: import java.security.PermissionCollection;
027: import java.security.Permissions;
028: import java.security.Policy;
029: import java.util.ArrayList;
030: import java.util.HashMap;
031: import java.util.Map;
032: import java.util.StringTokenizer;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /** Taken from the phoenix 4.0.4 org.apache.avalon.phoenix.launcher.Main.java */
038: /*
039: * Copyright (C) The Apache Software Foundation. All rights reserved. This software is published under the terms of the Apache Software License version 1.1, a copy of which has been included with this distribution in the LICENSE.txt file.
040: */
041:
042: /**
043: * PhoenixLoader is the class that bootstraps and sets up engine ClassLoader. It also sets up a default policy that gives full permissions to engine code.
044: *
045: * @author <a href="mailto:peter at apache.org">Peter Donald</a>
046: */
047: public final class PhoenixLauncherMain {
048: /** Our logger. */
049: private static Log M_log = LogFactory
050: .getLog(PhoenixLauncherMain.class);
051:
052: private static final String MAIN_CLASS = "org.apache.avalon.phoenix.frontends.CLIMain";
053:
054: private static final String LOADER_JAR = "phoenix-loader.jar";
055:
056: private static Object c_frontend;
057:
058: /**
059: * Main entry point for Phoenix.
060: *
061: * @param args
062: * the command line arguments
063: * @throws Exception
064: * if an error occurs
065: */
066: public static final void main(final String[] args) throws Exception {
067: final int exitCode = startup(args, new HashMap(), true);
068: System.exit(exitCode);
069: }
070:
071: /**
072: * Method to call to startup Phoenix from an external (calling) application. Protected to allow access from DaemonLauncher.
073: *
074: * @param args
075: * the command line arg array
076: * @param data
077: * a set of extra parameters to pass to embeddor
078: * @param blocking
079: * false if the current thread is expected to return.
080: * @return the exit code which should be used to exit the JVM
081: * @throws Exception
082: * if an error occurs
083: */
084: protected static final int startup(final String[] args,
085: final Map data, final boolean blocking) throws Exception {
086: int exitCode;
087: try {
088: // setup new Policy manager
089: // TODO: (done) removed
090: // Policy.setPolicy( new FreeNEasyPolicy() );
091:
092: // Create engine ClassLoader
093: // TODO: (done) removed
094: // final URL[] urls = getEngineClassPath();
095: // final URLClassLoader classLoader = new URLClassLoader( urls );
096:
097: // TODO: add these extra paths to the existing classloader
098: final ClassLoader classLoader = Thread.currentThread()
099: .getContextClassLoader();
100:
101: // TODO: (done) use the one classloader
102: // data.put( "common.classloader", ClassLoader.getSystemClassLoader() );
103: data.put("common.classloader", classLoader);
104:
105: data.put("container.classloader", classLoader);
106: data.put("phoenix.home", new File(findPhoenixHome()));
107:
108: // Setup context classloader
109: // TODO: (done) removed
110: // Thread.currentThread().setContextClassLoader( classLoader );
111:
112: // Create main launcher
113: final Class clazz = classLoader.loadClass(MAIN_CLASS);
114: final Class[] paramTypes = new Class[] { args.getClass(),
115: Map.class, Boolean.TYPE };
116: final Method method = clazz.getMethod("main", paramTypes);
117: c_frontend = clazz.newInstance();
118:
119: // kick the tires and light the fires....
120: final Integer integer = (Integer) method.invoke(c_frontend,
121: new Object[] { args, data, new Boolean(blocking) });
122: exitCode = integer.intValue();
123: } catch (final Exception e) {
124: // TODO: (done) changed to a log
125: // e.printStackTrace();
126: M_log.warn("PhoenixLauncherMain.startup: " + e);
127: exitCode = 1;
128: }
129: return exitCode;
130: }
131:
132: /**
133: * Method to call to shutdown Phoenix from an external (calling) application. Protected to allow access from DaemonLauncher.
134: */
135: protected static final void shutdown() {
136: if (null == c_frontend) {
137: return;
138: }
139:
140: try {
141: final Class clazz = c_frontend.getClass();
142: final Method method = clazz.getMethod("shutdown",
143: new Class[0]);
144:
145: // Lets put this sucker to sleep
146: method.invoke(c_frontend, new Object[0]);
147: } catch (final Exception e) {
148: // TODO: (done) changed to a log
149: // e.printStackTrace();
150: M_log.warn("PhoenixLauncherMain.shutdown: " + e);
151: } finally {
152: c_frontend = null;
153: }
154: }
155:
156: /**
157: * Create a ClassPath for the engine.
158: *
159: * @return the set of URLs that engine uses to load
160: * @throws Exception
161: * if unable to aquire classpath
162: */
163: private static URL[] getEngineClassPath() throws Exception {
164: final ArrayList urls = new ArrayList();
165:
166: final File dir = findEngineLibDir();
167: final File[] files = dir.listFiles();
168: for (int i = 0; i < files.length; i++) {
169: final File file = files[i];
170: if (file.getName().endsWith(".jar")) {
171: urls.add(file.toURL());
172: }
173: }
174:
175: // TODO: (done) add also the phoenix libs
176: final File dir_p = findPhoenixLibDir();
177: final File[] files_p = dir_p.listFiles();
178: for (int i = 0; i < files_p.length; i++) {
179: final File file = files_p[i];
180: if (file.getName().endsWith(".jar")) {
181: urls.add(file.toURL());
182: }
183: }
184:
185: return (URL[]) urls.toArray(new URL[urls.size()]);
186: }
187:
188: /**
189: * Find directory to load engine specific libraries from.
190: *
191: * @return the lib dir
192: * @throws Exception
193: * if unable to aquire directory
194: */
195: private static File findEngineLibDir() throws Exception {
196: final String phoenixHome = findPhoenixHome();
197: final String engineLibDir = phoenixHome + File.separator
198: + "bin" + File.separator + "lib";
199: final File dir = new File(engineLibDir).getCanonicalFile();
200: if (!dir.exists()) {
201: throw new Exception(
202: "Unable to locate engine lib directory at "
203: + engineLibDir);
204: }
205: return dir;
206: }
207:
208: /**
209: * TODO: (done) added this method Find directory to load phoenix libraries from.
210: *
211: * @return the lib dir
212: * @throws Exception
213: * if unable to aquire directory
214: */
215: private static File findPhoenixLibDir() throws Exception {
216: final String phoenixHome = findPhoenixHome();
217: final String engineLibDir = phoenixHome + File.separator
218: + "lib";
219: final File dir = new File(engineLibDir).getCanonicalFile();
220: if (!dir.exists()) {
221: throw new Exception(
222: "Unable to locate engine lib directory at "
223: + engineLibDir);
224: }
225: return dir;
226: }
227:
228: /**
229: * Utility method to find the home directory of Phoenix and make sure system property is set to it.
230: *
231: * @return the location of phoenix directory
232: * @throws Exception
233: * if unable to locate directory
234: */
235: private static String findPhoenixHome() throws Exception {
236: String phoenixHome = System.getProperty("phoenix.home", null);
237: if (null == phoenixHome) {
238: final File loaderDir = findLoaderDir();
239: phoenixHome = loaderDir.getAbsoluteFile().getParentFile()
240: + File.separator;
241: }
242:
243: phoenixHome = (new File(phoenixHome)).getCanonicalFile()
244: .toString();
245: System.setProperty("phoenix.home", phoenixHome);
246: return phoenixHome;
247: }
248:
249: /**
250: * Finds the LOADER_JAR file in the classpath.
251: */
252: private static final File findLoaderDir() throws Exception {
253: final String classpath = System.getProperty("java.class.path");
254: final String pathSeparator = System
255: .getProperty("path.separator");
256: final StringTokenizer tokenizer = new StringTokenizer(
257: classpath, pathSeparator);
258:
259: while (tokenizer.hasMoreTokens()) {
260: final String element = tokenizer.nextToken();
261:
262: if (element.endsWith(LOADER_JAR)) {
263: File file = (new File(element)).getCanonicalFile();
264: file = file.getParentFile();
265: return file;
266: }
267: }
268:
269: throw new Exception("Unable to locate " + LOADER_JAR
270: + " in classpath");
271: }
272:
273: /**
274: * Default polic class to give every code base all permssions. Will be replaced once the kernel loads.
275: */
276: private static class FreeNEasyPolicy extends Policy {
277: public PermissionCollection getPermissions(
278: final CodeSource codeSource) {
279: final Permissions permissions = new Permissions();
280: permissions.add(new java.security.AllPermission());
281: return permissions;
282: }
283:
284: public void refresh() {
285: }
286: }
287: }
|