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    
018    package org.apache.geronimo.system.main;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    
023    import org.apache.geronimo.cli.CLParserException;
024    import org.apache.geronimo.cli.daemon.DaemonCLParser;
025    import org.apache.geronimo.kernel.KernelFactory;
026    import org.apache.geronimo.kernel.config.ConfigurationUtil;
027    
028    
029    /**
030     * @version $Rev:385659 $ $Date: 2007-06-23 23:48:40 -0400 (Sat, 23 Jun 2007) $
031     */
032    public class Daemon extends EmbeddedDaemon {
033    
034        private Daemon() {
035            super(KernelFactory.newInstance().createKernel("geronimo"));
036        }
037    
038        @Override
039        protected int initializeKernel() throws Exception {
040            ClassLoader classLoader = EmbeddedDaemon.class.getClassLoader();
041    
042            // boot the kernel
043            try {
044                kernel.boot();
045            } catch (Exception e) {
046                e.printStackTrace();
047                return 1;
048            }
049    
050            // add our shutdown hook
051            Runtime.getRuntime().addShutdownHook(new Thread("Geronimo shutdown thread") {
052                public void run() {
053                    System.out.println("");
054                    System.out.println("Server shutdown started");
055                    kernel.shutdown();
056                    System.out.println("Server shutdown completed");
057                }
058            });
059    
060            // load this configuration
061            InputStream in = classLoader.getResourceAsStream("META-INF/config.ser");
062            try {
063                ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader);
064            } finally {
065                if (in != null) {
066                    try {
067                        in.close();
068                    } catch (IOException ignored) {
069                        // ignored
070                    }
071                }
072            }
073            return 0;
074        }
075    
076        /**
077         * Static entry point allowing a Kernel to be run from the command line.
078         *
079         * Once the Kernel is booted and the configuration is loaded, the process
080         * will remain running until the shutdown() method on the kernel is
081         * invoked or until the JVM exits.
082         *
083         * @param args the command line arguments
084         */
085        public static void main(String[] args) {
086            DaemonCLParser parser = new DaemonCLParser(System.out);
087            try {
088                parser.parse(args);
089            } catch (CLParserException e) {
090                System.err.println(e.getMessage());
091                parser.displayHelp();
092                System.exit(1);
093            }
094            new Daemon().execute(parser);
095        }
096    }