KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > main > CommandLine


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
18 package org.apache.geronimo.system.main;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.geronimo.common.GeronimoEnvironment;
32 import org.apache.geronimo.kernel.GBeanNotFoundException;
33 import org.apache.geronimo.kernel.InternalKernelException;
34 import org.apache.geronimo.kernel.Kernel;
35 import org.apache.geronimo.kernel.KernelFactory;
36 import org.apache.geronimo.kernel.config.ConfigurationManager;
37 import org.apache.geronimo.kernel.config.ConfigurationUtil;
38 import org.apache.geronimo.kernel.config.NoSuchConfigException;
39 import org.apache.geronimo.kernel.config.LifecycleException;
40 import org.apache.geronimo.kernel.config.ConfigurationData;
41 import org.apache.geronimo.kernel.log.GeronimoLogging;
42 import org.apache.geronimo.kernel.repository.Artifact;
43 import org.apache.geronimo.kernel.repository.MissingDependencyException;
44 import org.apache.geronimo.gbean.AbstractName;
45 import org.apache.geronimo.gbean.AbstractNameQuery;
46
47
48 /**
49  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
50  */

51 public class CommandLine {
52     protected static final Log log;
53
54     static {
55         // Perform initialization tasks common with the various Geronimo environments.
56
GeronimoEnvironment.init();
57
58         // This MUST be done before the first log is acquired
59
GeronimoLogging.initialize(GeronimoLogging.ERROR);
60         log = LogFactory.getLog(CommandLine.class.getName());
61     }
62
63     /**
64      * Command line entry point called by executable jar
65      * @param args command line args
66      */

67     public static void main(String JavaDoc[] args) {
68         log.info("Server startup begun");
69         try {
70             // the interesting entries from the manifest
71
CommandLineManifest manifest = CommandLineManifest.getManifestEntries();
72             List JavaDoc configurations = manifest.getConfigurations();
73             AbstractNameQuery mainGBean = manifest.getMainGBeanQuery();
74             String JavaDoc mainMethod = manifest.getMainMethod();
75
76             new CommandLine().invokeMainGBean(configurations, mainGBean, mainMethod, args);
77
78             log.info("Server shutdown completed");
79         } catch (Exception JavaDoc e) {
80             ExceptionUtil.trimStackTrace(e);
81             e.printStackTrace();
82             System.exit(2);
83             throw new AssertionError JavaDoc();
84         }
85     }
86
87     private Kernel kernel;
88     private AbstractName configurationName;
89
90     public void invokeMainGBean(List JavaDoc configurations, AbstractNameQuery mainGBeanQuery, String JavaDoc mainMethod, String JavaDoc[] args) throws Exception JavaDoc {
91         startKernel();
92         Runtime.getRuntime().addShutdownHook(new Thread JavaDoc("Geronimo shutdown thread") {
93             public void run() {
94                 log.info("Server shutdown begun");
95                 try {
96                     stopKernel();
97                 } catch (GBeanNotFoundException e) {
98
99                 }
100             }
101         });
102         loadConfigurations(configurations);
103
104         log.info("Server startup completed");
105         Set JavaDoc matches = kernel.listGBeans(mainGBeanQuery);
106         if (matches.isEmpty()) {
107             throw new Exception JavaDoc("No match for AbstractNameQuery: " + mainGBeanQuery);
108         }
109         if (matches.size() > 1) {
110             throw new Exception JavaDoc("Ambiguous AbstractNameQuery: " + mainGBeanQuery + " matches: " + matches);
111         }
112         AbstractName mainGBean = (AbstractName) matches.iterator().next();
113
114         // invoke the main method
115
kernel.invoke(
116                 mainGBean,
117                 mainMethod,
118                 new Object JavaDoc[]{args},
119                 new String JavaDoc[]{String JavaDoc[].class.getName()});
120
121     }
122
123     protected void startKernel() throws Exception JavaDoc {
124         ClassLoader JavaDoc classLoader = CommandLine.class.getClassLoader();
125         InputStream JavaDoc in = classLoader.getResourceAsStream("META-INF/config.ser");
126         try {
127             // boot the kernel
128
kernel = KernelFactory.newInstance().createKernel("geronimo");
129             kernel.boot();
130     
131             // load the configuration
132
configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, in, classLoader);
133         } finally {
134             if (in != null) {
135                 try {
136                     in.close();
137                 } catch (IOException JavaDoc ignored) {
138                     // ignored
139
}
140             }
141         }
142     }
143
144     protected void startKernel(Artifact moduleId) throws Exception JavaDoc {
145         // boot the kernel
146
kernel = KernelFactory.newInstance().createKernel("geronimo");
147         kernel.boot();
148         ClassLoader JavaDoc classLoader = CommandLine.class.getClassLoader();
149         for (Enumeration JavaDoc modules = classLoader.getResources("META-INF/config.ser"); modules.hasMoreElements(); ) {
150             URL JavaDoc moduleDataURL = (URL JavaDoc) modules.nextElement();
151             InputStream JavaDoc in = moduleDataURL.openStream();
152             try {
153                 ConfigurationData moduleData = ConfigurationUtil.readConfigurationData(in);
154                 if (moduleId.matches(moduleData.getId())) {
155                     // load the configuration
156
configurationName = ConfigurationUtil.loadBootstrapConfiguration(kernel, moduleData, classLoader);
157                     return;
158                 }
159             } finally {
160                 in.close();
161             }
162         }
163         throw new NoSuchConfigException(moduleId);
164     }
165
166     protected void loadConfigurations(List JavaDoc configurations) throws NoSuchConfigException, LifecycleException, MissingDependencyException {
167         // load and start the configurations
168
ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
169         Collection JavaDoc resolvedConfigurations = configurationManager.getArtifactResolver().resolveInClassLoader(configurations);
170         try {
171             for (Iterator JavaDoc i = resolvedConfigurations.iterator(); i.hasNext();) {
172                 Artifact configID = (Artifact) i.next();
173                 configurationManager.loadConfiguration(configID);
174                 configurationManager.startConfiguration(configID);
175             }
176         } finally {
177             ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
178         }
179     }
180
181     protected Kernel getKernel() {
182         return kernel;
183     }
184
185     protected void stopKernel() throws GBeanNotFoundException, InternalKernelException {
186         // stop this configuration
187
kernel.stopGBean(configurationName);
188
189         // shutdown the kernel
190
kernel.shutdown();
191     }
192 }
193
Popular Tags