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: */package org.apache.geronimo.deployment.plugin.jmx;
017:
018: import java.io.File;
019: import java.io.InputStream;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.List;
023: import java.util.Locale;
024:
025: import javax.enterprise.deploy.model.DeployableObject;
026: import javax.enterprise.deploy.shared.DConfigBeanVersionType;
027: import javax.enterprise.deploy.shared.ModuleType;
028: import javax.enterprise.deploy.spi.DeploymentConfiguration;
029: import javax.enterprise.deploy.spi.DeploymentManager;
030: import javax.enterprise.deploy.spi.Target;
031: import javax.enterprise.deploy.spi.TargetModuleID;
032: import javax.enterprise.deploy.spi.exceptions.DConfigBeanVersionUnsupportedException;
033: import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
034: import javax.enterprise.deploy.spi.exceptions.TargetException;
035: import javax.enterprise.deploy.spi.status.ProgressObject;
036:
037: import org.apache.geronimo.deployment.ModuleConfigurer;
038: import org.apache.geronimo.deployment.plugin.TargetImpl;
039: import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
040: import org.apache.geronimo.deployment.plugin.local.CommandSupport;
041: import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
042: import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
043: import org.apache.geronimo.deployment.plugin.local.StartCommand;
044: import org.apache.geronimo.deployment.plugin.local.StopCommand;
045: import org.apache.geronimo.deployment.plugin.local.UndeployCommand;
046: import org.apache.geronimo.gbean.AbstractName;
047: import org.apache.geronimo.kernel.Kernel;
048: import org.apache.geronimo.kernel.config.ConfigurationInfo;
049: import org.apache.geronimo.kernel.config.ConfigurationManager;
050: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
051: import org.apache.geronimo.kernel.config.ConfigurationUtil;
052: import org.apache.geronimo.kernel.management.State;
053:
054: /**
055: * @version $Rev: 597690 $ $Date: 2007-11-23 07:43:02 -0800 (Fri, 23 Nov 2007) $
056: */
057: public abstract class JMXDeploymentManager implements DeploymentManager {
058:
059: protected Kernel kernel;
060: private ConfigurationManager configurationManager;
061: protected CommandContext commandContext;
062: private final Collection<ModuleConfigurer> moduleConfigurers;
063:
064: public JMXDeploymentManager(
065: Collection<ModuleConfigurer> moduleConfigurers) {
066: if (null == moduleConfigurers) {
067: throw new IllegalArgumentException(
068: "moduleConfigurers is required");
069: }
070: this .moduleConfigurers = moduleConfigurers;
071: }
072:
073: protected void initialize(Kernel kernel) {
074: this .kernel = kernel;
075: configurationManager = ConfigurationUtil
076: .getConfigurationManager(kernel);
077: commandContext = new CommandContext(true, true, null, null,
078: false);
079: }
080:
081: public void setAuthentication(String username, String password) {
082: commandContext.setUsername(username);
083: commandContext.setPassword(password);
084: }
085:
086: public void release() {
087: if (kernel != null && configurationManager != null) {
088: try {
089: ConfigurationUtil.releaseConfigurationManager(kernel,
090: configurationManager);
091: } finally {
092: configurationManager = null;
093: kernel = null;
094: }
095: }
096: }
097:
098: public Target[] getTargets() {
099: if (kernel == null) {
100: throw new IllegalStateException("Disconnected");
101: }
102: List stores = configurationManager.listStores();
103: if (stores.isEmpty()) {
104: return null;
105: }
106:
107: Target[] targets = new Target[stores.size()];
108: for (int i = 0; i < stores.size(); i++) {
109: AbstractName storeName = (AbstractName) stores.get(i);
110: targets[i] = new TargetImpl(storeName, null);
111: }
112: return targets;
113: }
114:
115: public TargetModuleID[] getAvailableModules(
116: final ModuleType moduleType, Target[] targetList)
117: throws TargetException {
118: ConfigFilter filter = new ConfigFilter() {
119: public boolean accept(ConfigurationInfo info) {
120: return moduleType == null
121: || info.getType() == ConfigurationModuleType
122: .getFromValue(moduleType.getValue());
123: }
124: };
125: return getModules(targetList, filter);
126: }
127:
128: public TargetModuleID[] getNonRunningModules(
129: final ModuleType moduleType, Target[] targetList)
130: throws TargetException {
131: ConfigFilter filter = new ConfigFilter() {
132: public boolean accept(ConfigurationInfo info) {
133: return info.getState() != State.RUNNING
134: && (moduleType == null || info.getType() == ConfigurationModuleType
135: .getFromValue(moduleType.getValue()));
136: }
137: };
138: return getModules(targetList, filter);
139: }
140:
141: public TargetModuleID[] getRunningModules(
142: final ModuleType moduleType, Target[] targetList)
143: throws TargetException {
144: ConfigFilter filter = new ConfigFilter() {
145: public boolean accept(ConfigurationInfo info) {
146: return info.getState() == State.RUNNING
147: && (moduleType == null || info.getType() == ConfigurationModuleType
148: .getFromValue(moduleType.getValue()));
149: }
150: };
151: return getModules(targetList, filter);
152: }
153:
154: private static interface ConfigFilter {
155: boolean accept(ConfigurationInfo info);
156: }
157:
158: private TargetModuleID[] getModules(Target[] targetList,
159: ConfigFilter filter) throws TargetException {
160: if (kernel == null) {
161: throw new IllegalStateException("Disconnected");
162: }
163:
164: if (targetList == null) {
165: return null;
166: }
167:
168: try {
169: ArrayList<TargetModuleIDImpl> result = new ArrayList<TargetModuleIDImpl>();
170: for (Target aTargetList : targetList) {
171: TargetImpl target = (TargetImpl) aTargetList;
172: AbstractName storeName = target.getAbstractName();
173: List infos = configurationManager
174: .listConfigurations(storeName);
175: for (Object info1 : infos) {
176: ConfigurationInfo info = (ConfigurationInfo) info1;
177: if (filter.accept(info)) {
178: String name = info.getConfigID().toString();
179: List list = CommandSupport.loadChildren(kernel,
180: name);
181: TargetModuleIDImpl moduleID = new TargetModuleIDImpl(
182: target, name,
183: (String[]) list.toArray(new String[list
184: .size()]));
185: moduleID.setType(CommandSupport
186: .convertModuleType(info.getType()));
187: if (moduleID.getChildTargetModuleID() != null) {
188: for (int k = 0; k < moduleID
189: .getChildTargetModuleID().length; k++) {
190: TargetModuleIDImpl child = (TargetModuleIDImpl) moduleID
191: .getChildTargetModuleID()[k];
192: if (CommandSupport.isWebApp(kernel,
193: child.getModuleID())) {
194: child.setType(ModuleType.WAR);
195: }
196: }
197: }
198: result.add(moduleID);
199: }
200: }
201: }
202: CommandSupport.addWebContextPaths(kernel, result);
203: return result.size() == 0 ? null : result
204: .toArray(new TargetModuleID[result.size()]);
205: } catch (Exception e) {
206: throw (TargetException) new TargetException(e.getMessage())
207: .initCause(e);
208: }
209: }
210:
211: public ProgressObject distribute(Target[] targetList,
212: File moduleArchive, File deploymentPlan) {
213: if (kernel == null) {
214: throw new IllegalStateException("Disconnected");
215: }
216: DistributeCommand command = createDistributeCommand(targetList,
217: moduleArchive, deploymentPlan);
218: command.setCommandContext(commandContext);
219: new Thread(command).start();
220: return command;
221: }
222:
223: /**
224: * @deprecated
225: */
226: public ProgressObject distribute(Target[] targetList,
227: InputStream moduleArchive, InputStream deploymentPlan) {
228: return distribute(targetList, null, moduleArchive,
229: deploymentPlan);
230: }
231:
232: public ProgressObject distribute(Target[] targetList,
233: ModuleType moduleType, InputStream moduleArchive,
234: InputStream deploymentPlan) throws IllegalStateException {
235: if (kernel == null) {
236: throw new IllegalStateException("Disconnected");
237: }
238: DistributeCommand command = createDistributeCommand(targetList,
239: moduleType, moduleArchive, deploymentPlan);
240: command.setCommandContext(commandContext);
241: new Thread(command).start();
242: return command;
243: }
244:
245: public ProgressObject start(TargetModuleID[] moduleIDList) {
246: if (kernel == null) {
247: throw new IllegalStateException("Disconnected");
248: }
249: StartCommand command = new StartCommand(kernel, moduleIDList);
250: command.setCommandContext(commandContext);
251: new Thread(command).start();
252: return command;
253: }
254:
255: public ProgressObject stop(TargetModuleID[] moduleIDList) {
256: if (kernel == null) {
257: throw new IllegalStateException("Disconnected");
258: }
259: StopCommand command = new StopCommand(kernel, moduleIDList);
260: command.setCommandContext(commandContext);
261: new Thread(command).start();
262: return command;
263: }
264:
265: public ProgressObject undeploy(TargetModuleID[] moduleIDList) {
266: if (kernel == null) {
267: throw new IllegalStateException("Disconnected");
268: }
269: UndeployCommand command = new UndeployCommand(kernel,
270: moduleIDList);
271: command.setCommandContext(commandContext);
272: new Thread(command).start();
273: return command;
274: }
275:
276: public boolean isRedeploySupported() {
277: return true;
278: }
279:
280: public ProgressObject redeploy(TargetModuleID[] moduleIDList,
281: File moduleArchive, File deploymentPlan) {
282: if (kernel == null) {
283: throw new IllegalStateException("Disconnected");
284: }
285: RedeployCommand command = createRedeployCommand(moduleIDList,
286: moduleArchive, deploymentPlan);
287: command.setCommandContext(commandContext);
288: new Thread(command).start();
289: return command;
290: }
291:
292: public ProgressObject redeploy(TargetModuleID[] moduleIDList,
293: InputStream moduleArchive, InputStream deploymentPlan) {
294: if (kernel == null) {
295: throw new IllegalStateException("Disconnected");
296: }
297: RedeployCommand command = createRedeployCommand(moduleIDList,
298: moduleArchive, deploymentPlan);
299: command.setCommandContext(commandContext);
300: new Thread(command).start();
301: return command;
302: }
303:
304: public Locale[] getSupportedLocales() {
305: return new Locale[] { getDefaultLocale() };
306: }
307:
308: public Locale getCurrentLocale() {
309: return getDefaultLocale();
310: }
311:
312: public Locale getDefaultLocale() {
313: return Locale.getDefault();
314: }
315:
316: public boolean isLocaleSupported(Locale locale) {
317: return getDefaultLocale().equals(locale);
318: }
319:
320: public void setLocale(Locale locale) {
321: throw new UnsupportedOperationException("Cannot set Locale");
322: }
323:
324: public DConfigBeanVersionType getDConfigBeanVersion() {
325: return DConfigBeanVersionType.V1_4;
326: }
327:
328: public boolean isDConfigBeanVersionSupported(
329: DConfigBeanVersionType version) {
330: return DConfigBeanVersionType.V1_4.equals(version);
331: }
332:
333: public void setDConfigBeanVersion(DConfigBeanVersionType version)
334: throws DConfigBeanVersionUnsupportedException {
335: if (!isDConfigBeanVersionSupported(version)) {
336: throw new DConfigBeanVersionUnsupportedException(
337: "Version not supported " + version);
338: }
339: }
340:
341: public DeploymentConfiguration createConfiguration(
342: DeployableObject dObj) throws InvalidModuleException {
343: if (dObj == null) {
344: throw new NullPointerException(
345: "No deployable object supplied to configure");
346: }
347: ModuleConfigurer configurer = null;
348: for (ModuleConfigurer moduleConfigurer : moduleConfigurers) {
349: if (moduleConfigurer.getModuleType() == dObj.getType()) {
350: configurer = moduleConfigurer;
351: break;
352: }
353: }
354: if (configurer == null) {
355: throw new InvalidModuleException(
356: "No configurer for module type: " + dObj.getType()
357: + " registered");
358: }
359: return configurer.createConfiguration(dObj);
360: }
361:
362: protected DistributeCommand createDistributeCommand(
363: Target[] targetList, File moduleArchive, File deploymentPlan) {
364: return new DistributeCommand(kernel, targetList, moduleArchive,
365: deploymentPlan);
366: }
367:
368: protected DistributeCommand createDistributeCommand(
369: Target[] targetList, ModuleType moduleType,
370: InputStream moduleArchive, InputStream deploymentPlan) {
371: return new DistributeCommand(kernel, targetList, moduleType,
372: moduleArchive, deploymentPlan);
373: }
374:
375: protected RedeployCommand createRedeployCommand(
376: TargetModuleID[] moduleIDList, File moduleArchive,
377: File deploymentPlan) {
378: return new RedeployCommand(kernel, moduleIDList, moduleArchive,
379: deploymentPlan);
380: }
381:
382: protected RedeployCommand createRedeployCommand(
383: TargetModuleID[] moduleIDList, InputStream moduleArchive,
384: InputStream deploymentPlan) {
385: return new RedeployCommand(kernel, moduleIDList, moduleArchive,
386: deploymentPlan);
387: }
388:
389: public void setLogConfiguration(boolean shouldLog,
390: boolean verboseStatus) {
391: commandContext.setLogErrors(shouldLog);
392: commandContext.setVerbose(verboseStatus);
393: }
394:
395: public void setInPlace(boolean inPlace) {
396: commandContext.setInPlace(inPlace);
397: }
398: }
|