1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.kernel.util; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 import org.apache.geronimo.kernel.Kernel; 23 import org.apache.geronimo.kernel.KernelFactory; 24 import org.apache.geronimo.kernel.config.ConfigurationUtil; 25 26 /** 27 * 28 * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $ 29 */ 30 public class MainConfigurationBootstrapper { 31 32 public static void main(String[] args) { 33 int status = main(new MainConfigurationBootstrapper(), args); 34 System.exit(status); 35 } 36 37 public static int main(MainConfigurationBootstrapper bootstrapper, Object opaque) { 38 Main main = bootstrapper.getMain(MainConfigurationBootstrapper.class.getClassLoader()); 39 40 int exitCode; 41 ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader(); 42 try { 43 ClassLoader newTCCL = main.getClass().getClassLoader(); 44 Thread.currentThread().setContextClassLoader(newTCCL); 45 exitCode = main.execute(opaque); 46 } finally { 47 Thread.currentThread().setContextClassLoader(oldTCCL); 48 } 49 return exitCode; 50 } 51 52 protected Kernel kernel; 53 54 public Main getMain(ClassLoader classLoader) { 55 try { 56 bootKernel(); 57 loadBootConfiguration(classLoader); 58 loadPersistentConfigurations(); 59 return getMain(); 60 } catch (Exception e) { 61 if (null != kernel) { 62 kernel.shutdown(); 63 } 64 e.printStackTrace(); 65 System.exit(1); 66 throw new AssertionError(); 67 } 68 } 69 70 public void bootKernel() throws Exception { 71 kernel = KernelFactory.newInstance().createKernel("MainBootstrapper"); 72 kernel.boot(); 73 74 Runtime.getRuntime().addShutdownHook(new Thread("MainBootstrapper shutdown thread") { 75 public void run() { 76 kernel.shutdown(); 77 } 78 }); 79 } 80 81 public void loadBootConfiguration(ClassLoader classLoader) throws Exception { 82 InputStream in = classLoader.getResourceAsStream("META-INF/config.ser"); 83 try { 84 ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader, true); 85 } finally { 86 if (in != null) { 87 try { 88 in.close(); 89 } catch (IOException ignored) { 90 // ignored 91 } 92 } 93 } 94 } 95 96 public void loadPersistentConfigurations() throws Exception { 97 } 98 99 public Main getMain() throws Exception { 100 return (Main) kernel.getGBean(Main.class); 101 } 102 103 public Kernel getKernel() { 104 return kernel; 105 } 106 107 }