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