001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.deployment.plugin.jmx;
019    
020    import java.io.File;
021    import java.io.InputStream;
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.Locale;
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    import org.apache.geronimo.connector.deployment.RARConfigurer;
037    import org.apache.geronimo.deployment.plugin.TargetImpl;
038    import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
039    import org.apache.geronimo.deployment.plugin.local.CommandSupport;
040    import org.apache.geronimo.deployment.plugin.local.DistributeCommand;
041    import org.apache.geronimo.deployment.plugin.local.RedeployCommand;
042    import org.apache.geronimo.deployment.plugin.local.StartCommand;
043    import org.apache.geronimo.deployment.plugin.local.StopCommand;
044    import org.apache.geronimo.deployment.plugin.local.UndeployCommand;
045    import org.apache.geronimo.gbean.AbstractName;
046    import org.apache.geronimo.kernel.Kernel;
047    import org.apache.geronimo.kernel.config.ConfigurationInfo;
048    import org.apache.geronimo.kernel.config.ConfigurationManager;
049    import org.apache.geronimo.kernel.config.ConfigurationModuleType;
050    import org.apache.geronimo.kernel.config.ConfigurationUtil;
051    import org.apache.geronimo.kernel.config.NoSuchStoreException;
052    import org.apache.geronimo.kernel.management.State;
053    import org.apache.geronimo.web.deployment.WARConfigurer;
054    import org.apache.commons.logging.Log;
055    import org.apache.commons.logging.LogFactory;
056    
057    
058    /**
059     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
060     */
061    public abstract class JMXDeploymentManager implements DeploymentManager {
062        private static final Log log = LogFactory.getLog(JMXDeploymentManager.class);
063    
064        protected Kernel kernel;
065        private ConfigurationManager configurationManager;
066        private CommandContext commandContext;
067    
068        protected void initialize(Kernel kernel) {
069            this.kernel = kernel;
070            configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
071            commandContext = new CommandContext(true, true, null, null, false);
072        }
073    
074        public void setAuthentication(String username, String password) {
075            commandContext.setUsername(username);
076            commandContext.setPassword(password);
077        }
078    
079        public void release() {
080            if(kernel != null && configurationManager != null) {
081                try {
082                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
083                } finally {
084                    configurationManager = null;
085                    kernel = null;
086                }
087            }
088        }
089    
090        public Target[] getTargets() {
091            if (kernel == null) {
092                throw new IllegalStateException("Disconnected");
093            }
094            List stores = configurationManager.listStores();
095            if (stores.size() == 0) {
096                return null;
097            }
098    
099            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    }