KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > jmx > JMXDeploymentManager


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.deployment.plugin.jmx;
18
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
26 import javax.enterprise.deploy.shared.DConfigBeanVersionType JavaDoc;
27 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
28 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
29 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
30 import javax.enterprise.deploy.spi.Target JavaDoc;
31 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
32 import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.InvalidModuleException JavaDoc;
34 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
35 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.geronimo.deployment.plugin.TargetImpl;
40 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
41 import org.apache.geronimo.deployment.plugin.local.CommandSupport;
42 import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
43 import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
44 import org.apache.geronimo.deployment.plugin.local.StartCommand;
45 import org.apache.geronimo.deployment.plugin.local.StopCommand;
46 import org.apache.geronimo.deployment.plugin.local.UndeployCommand;
47 import org.apache.geronimo.gbean.AbstractName;
48 import org.apache.geronimo.kernel.Kernel;
49 import org.apache.geronimo.kernel.config.ConfigurationInfo;
50 import org.apache.geronimo.kernel.config.ConfigurationManager;
51 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
52 import org.apache.geronimo.kernel.config.ConfigurationUtil;
53 import org.apache.geronimo.kernel.management.State;
54
55
56 /**
57  * @version $Rev: 482870 $ $Date: 2006-12-05 21:50:50 -0500 (Tue, 05 Dec 2006) $
58  */

59 public abstract class JMXDeploymentManager implements DeploymentManager JavaDoc {
60     private static final Log log = LogFactory.getLog(JMXDeploymentManager.class);
61
62     protected Kernel kernel;
63     private ConfigurationManager configurationManager;
64     private CommandContext commandContext;
65
66     protected void initialize(Kernel kernel) {
67         this.kernel = kernel;
68         configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
69         commandContext = new CommandContext(true, true, null, null, false);
70     }
71
72     public void setAuthentication(String JavaDoc username, String JavaDoc password) {
73         commandContext.setUsername(username);
74         commandContext.setPassword(password);
75     }
76
77     public void release() {
78         if(kernel != null && configurationManager != null) {
79             try {
80                 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
81             } finally {
82                 configurationManager = null;
83                 kernel = null;
84             }
85         }
86     }
87
88     public Target JavaDoc[] getTargets() {
89         if (kernel == null) {
90             throw new IllegalStateException JavaDoc("Disconnected");
91         }
92         List JavaDoc stores = configurationManager.listStores();
93         if (stores.size() == 0) {
94             return null;
95         }
96
97         Target JavaDoc[] targets = new Target JavaDoc[stores.size()];
98         for (int i = 0; i < stores.size(); i++) {
99             AbstractName storeName = (AbstractName) stores.get(i);
100             targets[i] = new TargetImpl(storeName, null);
101         }
102         return targets;
103     }
104
105     public TargetModuleID JavaDoc[] getAvailableModules(final ModuleType JavaDoc moduleType, Target JavaDoc[] targetList) throws TargetException JavaDoc {
106         ConfigFilter filter = new ConfigFilter() {
107             public boolean accept(ConfigurationInfo info) {
108                 return moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue());
109             }
110         };
111         return getModules(targetList, filter);
112     }
113
114     public TargetModuleID JavaDoc[] getNonRunningModules(final ModuleType JavaDoc moduleType, Target JavaDoc[] targetList) throws TargetException JavaDoc {
115         ConfigFilter filter = new ConfigFilter() {
116             public boolean accept(ConfigurationInfo info) {
117                 return info.getState() != State.RUNNING && (moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue()));
118             }
119         };
120         return getModules(targetList, filter);
121     }
122
123     public TargetModuleID JavaDoc[] getRunningModules(final ModuleType JavaDoc moduleType, Target JavaDoc[] targetList) throws TargetException JavaDoc {
124         ConfigFilter filter = new ConfigFilter() {
125             public boolean accept(ConfigurationInfo info) {
126                 return info.getState() == State.RUNNING && (moduleType == null || info.getType() == ConfigurationModuleType.getFromValue(moduleType.getValue()));
127             }
128         };
129         return getModules(targetList, filter);
130     }
131
132     private static interface ConfigFilter {
133         boolean accept(ConfigurationInfo info);
134     }
135
136     private TargetModuleID JavaDoc[] getModules(Target JavaDoc[] targetList, ConfigFilter filter) throws TargetException JavaDoc {
137         if (kernel == null) {
138             throw new IllegalStateException JavaDoc("Disconnected");
139         }
140         try {
141             ArrayList JavaDoc result = new ArrayList JavaDoc();
142             for (int i = 0; i < targetList.length; i++) {
143                 TargetImpl target = (TargetImpl) targetList[i];
144                 AbstractName storeName = target.getAbstractName();
145                 List JavaDoc infos = configurationManager.listConfigurations(storeName);
146                 for (int j = 0; j < infos.size(); j++) {
147                     ConfigurationInfo info = (ConfigurationInfo) infos.get(j);
148                     if (filter.accept(info)) {
149                         String JavaDoc name = info.getConfigID().toString();
150                         List JavaDoc list = CommandSupport.loadChildren(kernel, name);
151                         TargetModuleIDImpl moduleID = new TargetModuleIDImpl(target, name, (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]));
152                         moduleID.setType(CommandSupport.convertModuleType(info.getType()));
153                         if(moduleID.getChildTargetModuleID() != null) {
154                             for (int k = 0; k < moduleID.getChildTargetModuleID().length; k++) {
155                                 TargetModuleIDImpl child = (TargetModuleIDImpl) moduleID.getChildTargetModuleID()[k];
156                                 if(CommandSupport.isWebApp(kernel, child.getModuleID())) {
157                                     child.setType(ModuleType.WAR);
158                                 }
159                             }
160                         }
161                         result.add(moduleID);
162                     }
163                 }
164             }
165             CommandSupport.addWebURLs(kernel, result);
166             return result.size() == 0 ? null : (TargetModuleID JavaDoc[]) result.toArray(new TargetModuleID JavaDoc[result.size()]);
167         } catch (Exception JavaDoc e) {
168             throw (TargetException JavaDoc) new TargetException JavaDoc(e.getMessage()).initCause(e);
169         }
170     }
171
172     public ProgressObject JavaDoc distribute(Target JavaDoc[] targetList, File JavaDoc moduleArchive, File JavaDoc deploymentPlan) {
173         if (kernel == null) {
174             throw new IllegalStateException JavaDoc("Disconnected");
175         }
176         DistributeCommand command = createDistributeCommand(targetList, moduleArchive, deploymentPlan);
177         command.setCommandContext(commandContext);
178         new Thread JavaDoc(command).start();
179         return command;
180     }
181
182     public ProgressObject JavaDoc distribute(Target JavaDoc[] targetList, InputStream JavaDoc moduleArchive, InputStream JavaDoc deploymentPlan) {
183         if (kernel == null) {
184             throw new IllegalStateException JavaDoc("Disconnected");
185         }
186         DistributeCommand command = createDistributeCommand(targetList, moduleArchive, deploymentPlan);
187         command.setCommandContext(commandContext);
188         new Thread JavaDoc(command).start();
189         return command;
190     }
191
192     public ProgressObject JavaDoc start(TargetModuleID JavaDoc[] moduleIDList) {
193         if (kernel == null) {
194             throw new IllegalStateException JavaDoc("Disconnected");
195         }
196         StartCommand command = new StartCommand(kernel, moduleIDList);
197         command.setCommandContext(commandContext);
198         new Thread JavaDoc(command).start();
199         return command;
200     }
201
202     public ProgressObject JavaDoc stop(TargetModuleID JavaDoc[] moduleIDList) {
203         if (kernel == null) {
204             throw new IllegalStateException JavaDoc("Disconnected");
205         }
206         StopCommand command = new StopCommand(kernel, moduleIDList);
207         command.setCommandContext(commandContext);
208         new Thread JavaDoc(command).start();
209         return command;
210     }
211
212     public ProgressObject JavaDoc undeploy(TargetModuleID JavaDoc[] moduleIDList) {
213         if (kernel == null) {
214             throw new IllegalStateException JavaDoc("Disconnected");
215         }
216         UndeployCommand command = new UndeployCommand(kernel, moduleIDList);
217         command.setCommandContext(commandContext);
218         new Thread JavaDoc(command).start();
219         return command;
220     }
221
222     public boolean isRedeploySupported() {
223         return true;
224     }
225
226     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] moduleIDList, File JavaDoc moduleArchive, File JavaDoc deploymentPlan) {
227         if (kernel == null) {
228             throw new IllegalStateException JavaDoc("Disconnected");
229         }
230         RedeployCommand command = createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
231         command.setCommandContext(commandContext);
232         new Thread JavaDoc(command).start();
233         return command;
234     }
235
236     public ProgressObject JavaDoc redeploy(TargetModuleID JavaDoc[] moduleIDList, InputStream JavaDoc moduleArchive, InputStream JavaDoc deploymentPlan) {
237         if (kernel == null) {
238             throw new IllegalStateException JavaDoc("Disconnected");
239         }
240         RedeployCommand command = createRedeployCommand(moduleIDList, moduleArchive, deploymentPlan);
241         command.setCommandContext(commandContext);
242         new Thread JavaDoc(command).start();
243         return command;
244     }
245
246     public Locale JavaDoc[] getSupportedLocales() {
247         return new Locale JavaDoc[]{getDefaultLocale()};
248     }
249
250     public Locale JavaDoc getCurrentLocale() {
251         return getDefaultLocale();
252     }
253
254     public Locale JavaDoc getDefaultLocale() {
255         return Locale.getDefault();
256     }
257
258     public boolean isLocaleSupported(Locale JavaDoc locale) {
259         return getDefaultLocale().equals(locale);
260     }
261
262     public void setLocale(Locale JavaDoc locale) {
263         throw new UnsupportedOperationException JavaDoc("Cannot set Locale");
264     }
265
266     public DConfigBeanVersionType JavaDoc getDConfigBeanVersion() {
267         return DConfigBeanVersionType.V1_4;
268     }
269
270     public boolean isDConfigBeanVersionSupported(DConfigBeanVersionType JavaDoc version) {
271         return DConfigBeanVersionType.V1_4.equals(version);
272     }
273
274     public void setDConfigBeanVersion(DConfigBeanVersionType JavaDoc version) throws DConfigBeanVersionUnsupportedException JavaDoc {
275         if (!isDConfigBeanVersionSupported(version)) {
276             throw new DConfigBeanVersionUnsupportedException JavaDoc("Version not supported " + version);
277         }
278     }
279
280     public DeploymentConfiguration JavaDoc createConfiguration(DeployableObject JavaDoc dObj) throws InvalidModuleException JavaDoc {
281         if(dObj.getType().equals(ModuleType.CAR)) {
282             //todo: need a client configurer
283
} else if(dObj.getType().equals(ModuleType.EAR)) {
284             //todo: need an EAR configurer
285
} else if(dObj.getType().equals(ModuleType.EJB)) {
286             try {
287                 Class JavaDoc cls = Class.forName("org.apache.openejb.deployment.EJBConfigurer");
288                 return (DeploymentConfiguration JavaDoc)cls.getMethod("createConfiguration", new Class JavaDoc[]{DeployableObject JavaDoc.class}).invoke(cls.newInstance(), new Object JavaDoc[]{dObj});
289             } catch (Exception JavaDoc e) {
290                 log.error("Unable to invoke EJB deployer", e);
291             }
292         } else if(dObj.getType().equals(ModuleType.RAR)) {
293 // return new RARConfigurer().createConfiguration(dObj);
294
} else if(dObj.getType().equals(ModuleType.WAR)) {
295 // return new WARConfigurer().createConfiguration(dObj);
296
}
297         throw new InvalidModuleException JavaDoc("Not supported");
298     }
299
300     protected DistributeCommand createDistributeCommand(Target JavaDoc[] targetList, File JavaDoc moduleArchive, File JavaDoc deploymentPlan) {
301         return new DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan);
302     }
303
304     protected DistributeCommand createDistributeCommand(Target JavaDoc[] targetList, InputStream JavaDoc moduleArchive, InputStream JavaDoc deploymentPlan) {
305         return new DistributeCommand(kernel, targetList, moduleArchive, deploymentPlan);
306     }
307
308     protected RedeployCommand createRedeployCommand(TargetModuleID JavaDoc[] moduleIDList, File JavaDoc moduleArchive, File JavaDoc deploymentPlan) {
309         return new RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
310     }
311
312     protected RedeployCommand createRedeployCommand(TargetModuleID JavaDoc[] moduleIDList, InputStream JavaDoc moduleArchive, InputStream JavaDoc deploymentPlan) {
313         return new RedeployCommand(kernel, moduleIDList, moduleArchive, deploymentPlan);
314     }
315
316     public void setLogConfiguration(boolean shouldLog, boolean verboseStatus) {
317         commandContext.setLogErrors(shouldLog);
318         commandContext.setVerbose(verboseStatus);
319     }
320
321     public void setInPlace(boolean inPlace) {
322         commandContext.setInPlace(inPlace);
323     }
324 }
325
Popular Tags