1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 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 /** 50 * @version $Rev: 686633 $ $Date: 2008-08-17 12:04:44 -0700 (Sun, 17 Aug 2008) $ 51 */ 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; //todo: this is pretty bad; should add it into constructor 60 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(Throwable 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 addWebContextPaths(kernel, moduleIDs); 278 } 279 280 /** 281 * Given a list of TargetModuleIDs, figure out which ones represent web 282 * modules and add a WebURL to each if possible. 283 */ 284 public static void addWebContextPaths(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, "contextPath").toString()); 296 break; 297 } 298 } 299 } 300 if(id.getChildTargetModuleID() != null) { 301 addWebContextPaths(kernel, Arrays.asList(id.getChildTargetModuleID())); 302 } 303 } 304 } 305 306 public static List loadChildren(Kernel kernel, String configName) { 307 List kids = new ArrayList(); 308 309 Map filter = new HashMap(); 310 filter.put("J2EEApplication", configName); 311 312 filter.put("j2eeType", "WebModule"); 313 Set test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 314 for (Iterator it = test.iterator(); it.hasNext();) { 315 AbstractName child = (AbstractName) it.next(); 316 String childName = child.getNameProperty("name"); 317 kids.add(childName); 318 } 319 320 filter.put("j2eeType", "EJBModule"); 321 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 322 for (Iterator it = test.iterator(); it.hasNext();) { 323 AbstractName child = (AbstractName) it.next(); 324 String childName = child.getNameProperty("name"); 325 kids.add(childName); 326 } 327 328 filter.put("j2eeType", "AppClientModule"); 329 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 330 for (Iterator it = test.iterator(); it.hasNext();) { 331 AbstractName child = (AbstractName) it.next(); 332 String childName = child.getNameProperty("name"); 333 kids.add(childName); 334 } 335 336 filter.put("j2eeType", "ResourceAdapterModule"); 337 test = kernel.listGBeans(new AbstractNameQuery(null, filter)); 338 for (Iterator it = test.iterator(); it.hasNext();) { 339 AbstractName child = (AbstractName) it.next(); 340 String childName = child.getNameProperty("name"); 341 kids.add(childName); 342 } 343 return kids; 344 } 345 }