View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.system.main;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Enumeration;
26  import java.util.Set;
27  import java.util.Collection;
28  import java.net.URL;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.geronimo.common.GeronimoEnvironment;
33  import org.apache.geronimo.kernel.GBeanNotFoundException;
34  import org.apache.geronimo.kernel.InternalKernelException;
35  import org.apache.geronimo.kernel.Kernel;
36  import org.apache.geronimo.kernel.KernelFactory;
37  import org.apache.geronimo.kernel.config.ConfigurationManager;
38  import org.apache.geronimo.kernel.config.ConfigurationUtil;
39  import org.apache.geronimo.kernel.config.NoSuchConfigException;
40  import org.apache.geronimo.kernel.config.LifecycleException;
41  import org.apache.geronimo.kernel.config.ConfigurationData;
42  import org.apache.geronimo.kernel.log.GeronimoLogging;
43  import org.apache.geronimo.kernel.repository.Artifact;
44  import org.apache.geronimo.kernel.repository.MissingDependencyException;
45  import org.apache.geronimo.gbean.AbstractName;
46  import org.apache.geronimo.gbean.AbstractNameQuery;
47  
48  
49  /**
50   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
51   */
52  public class CommandLine {
53      protected static final Log log;
54  
55      static {
56          // Perform initialization tasks common with the various Geronimo environments.
57          GeronimoEnvironment.init();
58  
59          // This MUST be done before the first log is acquired
60          GeronimoLogging.initialize(GeronimoLogging.ERROR);
61          log = LogFactory.getLog(CommandLine.class.getName());
62      }
63  
64      /**
65       * Command line entry point called by executable jar
66       * @param args command line args
67       */
68      public static void main(String[] args) {
69          log.info("Server startup begun");
70          try {
71              // the interesting entries from the manifest
72              CommandLineManifest manifest = CommandLineManifest.getManifestEntries();
73              List configurations = manifest.getConfigurations();
74              AbstractNameQuery mainGBean = manifest.getMainGBeanQuery();
75              String mainMethod = manifest.getMainMethod();
76  
77              new CommandLine().invokeMainGBean(configurations, mainGBean, mainMethod, args);
78  
79              log.info("Server shutdown completed");
80          } catch (Exception e) {
81              ExceptionUtil.trimStackTrace(e);
82              e.printStackTrace();
83              System.exit(2);
84              throw new AssertionError();
85          }
86      }
87  
88      private Kernel kernel;
89      private AbstractName configurationName;
90  
91      public void invokeMainGBean(List configurations, AbstractNameQuery mainGBeanQuery, String mainMethod, String[] args) throws Exception {
92          startKernel();
93          Runtime.getRuntime().addShutdownHook(new Thread("Geronimo shutdown thread") {
94              public void run() {
95                  log.info("Server shutdown begun");
96                  try {
97                      stopKernel();
98                  } catch (GBeanNotFoundException e) {
99  
100                 }
101             }
102         });
103         loadConfigurations(configurations);
104 
105         log.info("Server startup completed");
106         Set matches = kernel.listGBeans(mainGBeanQuery);
107         if (matches.isEmpty()) {
108             throw new Exception("No match for AbstractNameQuery: " + mainGBeanQuery);
109         }
110         if (matches.size() > 1) {
111             throw new Exception("Ambiguous AbstractNameQuery: " + mainGBeanQuery + " matches: " + matches);
112         }
113         AbstractName mainGBean = (AbstractName) matches.iterator().next();
114 
115         // invoke the main method
116         kernel.invoke(
117                 mainGBean,
118                 mainMethod,
119                 new Object[]{args},
120                 new String[]{String[].class.getName()});
121 
122     }
123 
124     protected void startKernel() throws Exception {
125         ClassLoader classLoader = CommandLine.class.getClassLoader();
126         InputStream in = classLoader.getResourceAsStream("META-INF/config.ser");
127         try {
128             // boot the kernel
129             kernel = KernelFactory.newInstance().createKernel("geronimo");
130             kernel.boot();
131     
132             // load the configuration
133             configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader);
134         } finally {
135             if (in != null) {
136                 try {
137                     in.close();
138                 } catch (IOException ignored) {
139                     // ignored
140                 }
141             }
142         }
143     }
144 
145     protected void startKernel(Artifact moduleId) throws Exception {
146         // boot the kernel
147         kernel = KernelFactory.newInstance().createKernel("geronimo");
148         kernel.boot();
149         ClassLoader classLoader = CommandLine.class.getClassLoader();
150         for (Enumeration modules = classLoader.getResources("META-INF/config.ser"); modules.hasMoreElements(); ) {
151             URL moduleDataURL = (URL) modules.nextElement();
152             InputStream in = moduleDataURL.openStream();
153             try {
154                 ConfigurationData moduleData = ConfigurationUtil.readConfigurationData(in);
155                 if (moduleId.matches(moduleData.getId())) {
156                     // load the configuration
157                     configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, moduleData, classLoader);
158                     return;
159                 }
160             } finally {
161                 in.close();
162             }
163         }
164         throw new NoSuchConfigException(moduleId);
165     }
166 
167     protected void loadConfigurations(List configurations) throws NoSuchConfigException, LifecycleException, MissingDependencyException {
168         // load and start the configurations
169         ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
170         Collection resolvedConfigurations = configurationManager.getArtifactResolver().resolveInClassLoader(configurations);
171         try {
172             for (Iterator i = resolvedConfigurations.iterator(); i.hasNext();) {
173                 Artifact configID = (Artifact) i.next();
174                 configurationManager.loadConfiguration(configID);
175                 configurationManager.startConfiguration(configID);
176             }
177         } finally {
178             ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
179         }
180     }
181 
182     protected Kernel getKernel() {
183         return kernel;
184     }
185 
186     protected void stopKernel() throws GBeanNotFoundException, InternalKernelException {
187         // stop this configuration
188         kernel.stopGBean(configurationName);
189 
190         // shutdown the kernel
191         kernel.shutdown();
192     }
193 }