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