001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.kernel.util;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    
022    import org.apache.geronimo.kernel.Kernel;
023    import org.apache.geronimo.kernel.KernelFactory;
024    import org.apache.geronimo.kernel.config.ConfigurationUtil;
025    
026    /**
027     *
028     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
029     */
030    public class MainConfigurationBootstrapper {
031    
032        public static void main(String[] args) {
033            int status = main(new MainConfigurationBootstrapper(), args);
034            System.exit(status);
035        }
036    
037        public static int main(MainConfigurationBootstrapper bootstrapper, Object opaque) {
038            Main main = bootstrapper.getMain(MainConfigurationBootstrapper.class.getClassLoader());
039    
040            int exitCode;
041            ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();
042            try {
043                ClassLoader newTCCL = main.getClass().getClassLoader();
044                Thread.currentThread().setContextClassLoader(newTCCL);
045                exitCode = main.execute(opaque);
046            } finally {
047                Thread.currentThread().setContextClassLoader(oldTCCL);
048            }
049            return exitCode;
050        }
051    
052        protected Kernel kernel;
053    
054        public Main getMain(ClassLoader classLoader) {
055            try {
056                bootKernel();
057                loadBootConfiguration(classLoader);
058                loadPersistentConfigurations();
059                return getMain();
060            } catch (Exception e) {
061                if (null != kernel) {
062                    kernel.shutdown();
063                }
064                e.printStackTrace();
065                System.exit(1);
066                throw new AssertionError();
067            }
068        }
069        
070        public void bootKernel() throws Exception {
071            kernel = KernelFactory.newInstance().createKernel("MainBootstrapper");
072            kernel.boot();
073    
074            Runtime.getRuntime().addShutdownHook(new Thread("MainBootstrapper shutdown thread") {
075                public void run() {
076                    kernel.shutdown();
077                }
078            });
079        }
080        
081        public void loadBootConfiguration(ClassLoader classLoader) throws Exception {
082            InputStream in = classLoader.getResourceAsStream("META-INF/config.ser");
083            try {
084                ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader, true);
085            } finally {
086                if (in != null) {
087                    try {
088                        in.close();
089                    } catch (IOException ignored) {
090                        // ignored
091                    }
092                }
093            }
094        }
095        
096        public void loadPersistentConfigurations() throws Exception {
097        }
098    
099        public Main getMain() throws Exception {
100            return (Main) kernel.getGBean(Main.class);
101        }
102    
103        public Kernel getKernel() {
104            return kernel;
105        }
106        
107    }