1 17 package org.apache.geronimo.deployment.plugin.local; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.util.Iterator ; 22 23 import javax.enterprise.deploy.shared.CommandType ; 24 import javax.enterprise.deploy.spi.Target ; 25 import javax.enterprise.deploy.spi.TargetModuleID ; 26 27 import org.apache.geronimo.deployment.plugin.ConfigIDExtractor; 28 import org.apache.geronimo.deployment.plugin.TargetImpl; 29 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl; 30 import org.apache.geronimo.gbean.AbstractName; 31 import org.apache.geronimo.kernel.InternalKernelException; 32 import org.apache.geronimo.kernel.Kernel; 33 import org.apache.geronimo.kernel.config.ConfigurationManager; 34 import org.apache.geronimo.kernel.config.ConfigurationUtil; 35 import org.apache.geronimo.kernel.config.LifecycleResults; 36 import org.apache.geronimo.kernel.config.NoSuchConfigException; 37 import org.apache.geronimo.kernel.repository.Artifact; 38 39 42 public class RedeployCommand extends AbstractDeployCommand { 43 private static final String [] IS_IN_PLACE_CONFIGURATION_SIG = {Artifact.class.getName()}; 44 private static final String IS_IN_PLACE_CONFIGURATION_METH = "isInPlaceConfiguration"; 45 46 private final TargetModuleID [] modules; 47 48 public RedeployCommand(Kernel kernel, TargetModuleID [] moduleIDList, File moduleArchive, File deploymentPlan) { 49 super(CommandType.REDEPLOY, kernel, moduleArchive, deploymentPlan, null, null, false); 50 this.modules = moduleIDList; 51 } 52 53 public RedeployCommand(Kernel kernel, TargetModuleID [] moduleIDList, InputStream moduleArchive, InputStream deploymentPlan) { 54 super(CommandType.REDEPLOY, kernel, null, null, moduleArchive, deploymentPlan, true); 55 this.modules = moduleIDList; 56 } 57 58 public void run() { 59 if (deployer == null) { 60 return; 61 } 62 63 try { 64 if (spool) { 65 if (moduleStream != null) { 66 moduleArchive = createTempFile(); 67 copyTo(moduleArchive, moduleStream); 68 } 69 if (deploymentStream != null) { 70 deploymentPlan = createTempFile(); 71 copyTo(deploymentPlan, deploymentStream); 72 } 73 } 74 Artifact configID = null; 75 if(deploymentPlan != null) { 76 String extracted = ConfigIDExtractor.extractModuleIdFromPlan(deploymentPlan); 77 if(extracted != null) { 78 configID = Artifact.create(extracted); 79 } 80 } else { 81 String extracted = ConfigIDExtractor.extractModuleIdFromArchive(moduleArchive); 82 if(extracted != null) { 83 configID = Artifact.create(extracted); 84 } 85 } 86 if(configID != null && configID.getGroupId() == null) { 87 configID = new Artifact(Artifact.DEFAULT_GROUP_ID, configID.getArtifactId(), 88 configID.getVersion(), configID.getType()); 89 } 90 91 ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel); 92 try { 93 for (int i = 0; i < modules.length; i++) { 94 TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i]; 95 Artifact artifact = Artifact.create(module.getModuleID()); 96 if(configID != null && configID.isResolved()) { 97 if(configID.getGroupId().equals(artifact.getGroupId()) && 98 configID.getArtifactId().equals(artifact.getArtifactId()) && 99 configID.getVersion().equals(artifact.getVersion())) { 100 redeploySameConfiguration(configurationManager, artifact, module.getTarget()); 101 } else { 102 redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget()); 103 } 104 } else { 105 redeployUpdatedConfiguration(configurationManager, artifact, module.getTarget()); 106 } 107 } 108 } finally { 109 ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager); 110 } 111 addWebURLs(kernel); 112 complete("Completed"); 113 } catch (Exception e) { 114 doFail(e); 115 } finally { 116 if (spool) { 117 if (moduleArchive != null) { 118 moduleArchive.delete(); 119 } 120 if (deploymentPlan != null) { 121 deploymentPlan.delete(); 122 } 123 } 124 } 125 } 126 127 private void redeployUpdatedConfiguration(ConfigurationManager manager, Artifact previous, Target target) throws Exception , NoSuchConfigException { 128 130 TargetImpl impl = (TargetImpl) target; 133 AbstractName storeName = impl.getAbstractName(); 134 Boolean inPlaceConfiguration = (Boolean ) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object []{previous}, IS_IN_PLACE_CONFIGURATION_SIG); 135 commandContext.setInPlace(inPlaceConfiguration.booleanValue()); 136 doDeploy(target, false); 137 Artifact configID = Artifact.create(getResultTargetModuleIDs()[0].getModuleID()); 138 LifecycleResults results = manager.reloadConfiguration(previous, configID.getVersion()); 139 140 boolean newStarted = false; 143 for (Iterator it = results.getStopped().iterator(); it.hasNext();) { 144 Artifact name = (Artifact) it.next(); 145 updateStatus("Stopped "+name); 146 } 147 for (Iterator it = results.getUnloaded().iterator(); it.hasNext();) { 148 Artifact name = (Artifact) it.next(); 149 updateStatus("Unloaded "+name); 150 } 151 for (Iterator it = results.getLoaded().iterator(); it.hasNext();) { 152 Artifact name = (Artifact) it.next(); 153 updateStatus("Loaded "+name); 154 } 155 for (Iterator it = results.getStarted().iterator(); it.hasNext();) { 156 Artifact name = (Artifact) it.next(); 157 updateStatus("Started "+name); 158 if(configID.matches(name)) { 159 newStarted = true; 160 } 161 } 162 for (Iterator it = results.getFailed().keySet().iterator(); it.hasNext();) { 163 Artifact name = (Artifact) it.next(); 164 updateStatus("Failed on "+name+": "+results.getFailedCause(name).getMessage()); 165 doFail((Exception )results.getFailedCause(name)); 166 } 167 if(results.getFailed().size() == 0 && !newStarted) { 168 updateStatus("Note: new module was not started (probably because old module was not running)."); 169 } 170 } 171 172 private void redeploySameConfiguration(ConfigurationManager configurationManager, Artifact configID, Target target) throws Exception { 173 if(!configID.isResolved()) { 174 throw new IllegalStateException ("Cannot redeploy same module when module ID is not fully resolved ("+configID+")"); 175 } 176 try { 177 configurationManager.stopConfiguration(configID); 178 updateStatus("Stopped "+configID); 179 } catch (InternalKernelException e) { 180 Exception cause = (Exception )e.getCause(); 181 if(cause instanceof NoSuchConfigException) { 182 } else { 184 throw cause; 185 } 186 } catch(NoSuchConfigException e) { 187 } 189 try { 190 configurationManager.unloadConfiguration(configID); 191 updateStatus("Unloaded "+configID); 192 } catch(InternalKernelException e) { 193 Exception cause = (Exception )e.getCause(); 194 if(cause instanceof NoSuchConfigException) { 195 } else { 197 throw cause; 198 } 199 } catch (NoSuchConfigException e) { 200 } 202 203 TargetImpl impl = (TargetImpl) target; 206 AbstractName storeName = impl.getAbstractName(); 207 Boolean inPlaceConfiguration = (Boolean ) kernel.invoke(storeName, IS_IN_PLACE_CONFIGURATION_METH, new Object []{configID}, IS_IN_PLACE_CONFIGURATION_SIG); 208 commandContext.setInPlace(inPlaceConfiguration.booleanValue()); 209 210 try { 211 configurationManager.uninstallConfiguration(configID); 212 updateStatus("Uninstalled "+configID); 213 } catch(InternalKernelException e) { 214 Exception cause = (Exception )e.getCause(); 215 if(cause instanceof NoSuchConfigException) { 216 throw new IllegalStateException ("Module "+configID+" is not installed!"); 217 } else { 218 throw cause; 219 } 220 } catch (NoSuchConfigException e) { 221 throw new IllegalStateException ("Module "+configID+" is not installed!"); 222 } 223 224 doDeploy(target, false); 225 updateStatus("Deployed "+configID); 226 227 configurationManager.loadConfiguration(configID); 228 configurationManager.startConfiguration(configID); 229 updateStatus("Started " + configID); 230 } 231 } 232 | Popular Tags |