1 17 18 package org.apache.geronimo.deployment.plugin.local; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import javax.enterprise.deploy.shared.ActionType ; 31 import javax.enterprise.deploy.shared.CommandType ; 32 import javax.enterprise.deploy.shared.ModuleType ; 33 import javax.enterprise.deploy.shared.StateType ; 34 import javax.enterprise.deploy.spi.TargetModuleID ; 35 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 36 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 37 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 38 import javax.enterprise.deploy.spi.status.ProgressEvent ; 39 import javax.enterprise.deploy.spi.status.ProgressListener ; 40 import javax.enterprise.deploy.spi.status.ProgressObject ; 41 import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl; 42 import org.apache.geronimo.deployment.plugin.jmx.CommandContext; 43 import org.apache.geronimo.gbean.AbstractName; 44 import org.apache.geronimo.gbean.AbstractNameQuery; 45 import org.apache.geronimo.kernel.InternalKernelException; 46 import org.apache.geronimo.kernel.Kernel; 47 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 48 49 52 public abstract class CommandSupport implements ProgressObject , Runnable { 53 private final CommandType command; 54 private ActionType action; 55 private StateType state; 56 private String message; 57 private final Set listeners = new HashSet (); 58 private final List moduleIDs = new ArrayList (); 59 protected CommandContext commandContext = null; 61 private ProgressEvent event = null; 62 63 protected CommandSupport(CommandType command) { 64 this.command = command; 65 this.action = ActionType.EXECUTE; 66 this.state = StateType.RUNNING; 67 this.message = null; 68 } 69 70 protected synchronized void addModule(TargetModuleID moduleID) { 71 moduleIDs.add(moduleID); 72 } 73 74 protected synchronized int getModuleCount() { 75 return moduleIDs.size(); 76 } 77 78 public synchronized TargetModuleID [] getResultTargetModuleIDs() { 79 return (TargetModuleID []) moduleIDs.toArray(new TargetModuleID [moduleIDs.size()]); 80 } 81 82 public synchronized DeploymentStatus getDeploymentStatus() { 83 return new Status (command, action, state, message); 84 } 85 86 public ClientConfiguration getClientConfiguration(TargetModuleID id) { 87 return null; 88 } 89 90 public boolean isCancelSupported() { 91 return false; 92 } 93 94 public void cancel() throws OperationUnsupportedException { 95 throw new OperationUnsupportedException ("cancel not supported"); 96 } 97 98 public boolean isStopSupported() { 99 return false; 100 } 101 102 public void stop() throws OperationUnsupportedException { 103 throw new OperationUnsupportedException ("stop not supported"); 104 } 105 106 public void addProgressListener(ProgressListener pol) { 107 ProgressEvent event; 108 synchronized (this) { 109 listeners.add(pol); 110 event = this.event; 111 } 112 if(event != null) { 113 pol.handleProgressEvent(event); 114 } 115 } 116 117 public synchronized void removeProgressListener(ProgressListener pol) { 118 listeners.remove(pol); 119 } 120 121 public final void fail(String message) { 122 sendEvent(message, StateType.FAILED); 123 } 124 125 protected final void complete(String message) { 126 sendEvent(message, StateType.COMPLETED); 127 } 128 129 public final void updateStatus(String message) { 130 sendEvent(message, state); 131 } 132 133 public void doFail(Exception e) { 134 if (e instanceof InternalKernelException) { 135 Exception test = (Exception )e.getCause(); 136 if(test != null) { 137 e = test; 138 } 139 } 140 141 if (commandContext.isLogErrors()) { 142 System.err.println("Deployer operation failed: " + e.getMessage()); 143 if (commandContext.isVerbose()) { 144 e.printStackTrace(System.err); 145 } 146 } 147 148 StringWriter writer = new StringWriter (); 149 PrintWriter printWriter = new PrintWriter (writer); 150 printWriter.println(e.getMessage()); 151 if (commandContext.isVerbose()) { 152 e.printStackTrace(printWriter); 153 } else { 154 Throwable throwable = e; 155 while (null != (throwable = throwable.getCause())) { 156 printWriter.println("\t" + throwable.getMessage()); 157 } 158 } 159 fail(writer.toString()); 160 } 161 162 private void sendEvent(String message, StateType state) { 163 assert !Thread.holdsLock(this) : "Trying to send event whilst holding lock"; 164 165 ProgressListener [] toNotify; 166 DeploymentStatus newStatus; 167 synchronized (this) { 168 this.message = message; 169 this.state = state; 170 newStatus = new Status (command, action, state, message); 171 toNotify = (ProgressListener []) listeners.toArray(new ProgressListener [listeners.size()]); 172 event = new ProgressEvent (this, null, newStatus); 173 } 174 175 for (int i = 0; i < toNotify.length; i++) { 176 toNotify[i].handleProgressEvent(event); 177 } 178 } 179 180 protected static String clean(String value) { 181 if(value.startsWith("\"") && value.endsWith("\"")) { 182 return value.substring(1, value.length()-1); 183 } 184 return value; 185 } 186 187 private static class Status implements DeploymentStatus { 188 private final CommandType command; 189 private final ActionType action; 190 private final StateType state; 191 private final String message; 192 193 public Status(CommandType command, ActionType action, StateType state, String message) { 194 this.command = command; 195 this.action = action; 196 this.state = state; 197 this.message = message; 198 } 199 200 public CommandType getCommand() { 201 return command; 202 } 203 204 public ActionType getAction() { 205 return action; 206 } 207 208 public String getMessage() { 209 return message; 210 } 211 212 public StateType getState() { 213 return state; 214 } 215 216 public boolean isRunning() { 217 return StateType.RUNNING.equals(state); 218 } 219 220 public boolean isCompleted() { 221 return StateType.COMPLETED.equals(state); 222 } 223 224 public boolean isFailed() { 225 return StateType.FAILED.equals(state); 226 } 227 228 public String toString() { 229 StringBuffer buf = new StringBuffer (); 230 buf.append("DeploymentStatus[").append(command).append(','); 231 buf.append(action).append(','); 232 buf.append(state); 233 if (message != null) { 234 buf.append(',').append(message); 235 } 236 buf.append(']'); 237 return buf.toString(); 238 } 239 } 240 241 public CommandContext getCommandContext() { 242 return commandContext; 243 } 244 245 public void setCommandContext(CommandContext commandContext) { 246 this.commandContext = new CommandContext(commandContext); 247 } 248 249 public static ModuleType convertModuleType(ConfigurationModuleType type) { 250 if(type.getValue() == ConfigurationModuleType.WAR.getValue()) { 251 return ModuleType.WAR; 252 } 253 if(type.getValue() == ConfigurationModuleType.RAR.getValue()) { 254 return ModuleType.RAR; 255 } 256 if(type.getValue() == ConfigurationModuleType.EJB.getValue()) { 257 return ModuleType.EJB; 258 } 259 if(type.getValue() == ConfigurationModuleType.EAR.getValue()) { 260 return ModuleType.EAR; 261 } 262 if(type.getValue() == ConfigurationModuleType.CAR.getValue()) { 263 return ModuleType.CAR; 264 } 265 return null; 266 } 267 268 public static boolean isWebApp(Kernel kernel, String configName) { 269 Map filter = new HashMap (); 270 filter.put("j2eeType", "WebModule"); 271 filter.put("name", configName); 272 Set set = kernel.listGBeans(new AbstractNameQuery(null, filter)); 273 return set.size() > 0; 274 } 275 276 protected void addWebURLs(Kernel kernel) throws Exception { 277 addWebURLs(kernel, moduleIDs); 278 } 279 280 284 public static void addWebURLs(Kernel kernel, List moduleIDs) throws Exception { 285 Set webApps = null; 286 for (int i = 0; i < moduleIDs.size(); i++) { 287 TargetModuleIDImpl id = (TargetModuleIDImpl) moduleIDs.get(i); 288 if(id.getType() != null && id.getType().getValue() == ModuleType.WAR.getValue()) { 289 if(webApps == null) { 290 webApps = kernel.listGBeans(new AbstractNameQuery("org.apache.geronimo.management.geronimo.WebModule")); 291 } 292 for (Iterator it = webApps.iterator(); it.hasNext();) { 293 AbstractName name = (AbstractName) it.next(); 294 if(name.getName().get("name").equals(id.getModuleID())) { 295 id.setWebURL(kernel.getAttribute(name, "URLFor").toString()); 296 } 297 } 298 } 299 if(id.getChildTargetModuleID() != null) { 300 addWebURLs(kernel, Arrays.asList(id.getChildTargetModuleID())); 301 } 302 } 303 } 304 305 public static List loadChildren(Kernel kernel, String configName) { 306 List kids = new ArrayList (); 307 308 Map filter = new HashMap (); 309 filter.put("J2EEApplication", configName); 310 311 filter.put("j2eeType", "WebModule"); 312 Set test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 313 for (Iterator it = test.iterator(); it.hasNext();) { 314 AbstractName child = (AbstractName) it.next(); 315 String childName = child.getNameProperty("name"); 316 kids.add(childName); 317 } 318 319 filter.put("j2eeType", "EJBModule"); 320 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 321 for (Iterator it = test.iterator(); it.hasNext();) { 322 AbstractName child = (AbstractName) it.next(); 323 String childName = child.getNameProperty("name"); 324 kids.add(childName); 325 } 326 327 filter.put("j2eeType", "AppClientModule"); 328 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 329 for (Iterator it = test.iterator(); it.hasNext();) { 330 AbstractName child = (AbstractName) it.next(); 331 String childName = child.getNameProperty("name"); 332 kids.add(childName); 333 } 334 335 filter.put("j2eeType", "ResourceAdapterModule"); 336 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 337 for (Iterator it = test.iterator(); it.hasNext();) { 338 AbstractName child = (AbstractName) it.next(); 339 String childName = child.getNameProperty("name"); 340 kids.add(childName); 341 } 342 return kids; 343 } 344 } 345 | Popular Tags |