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: */package org.apache.geronimo.deployment.plugin.local;
017:
018: import java.io.PrintWriter;
019: import java.io.StringWriter;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import javax.enterprise.deploy.shared.ActionType;
029: import javax.enterprise.deploy.shared.CommandType;
030: import javax.enterprise.deploy.shared.ModuleType;
031: import javax.enterprise.deploy.shared.StateType;
032: import javax.enterprise.deploy.spi.TargetModuleID;
033: import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
034: import javax.enterprise.deploy.spi.status.ClientConfiguration;
035: import javax.enterprise.deploy.spi.status.DeploymentStatus;
036: import javax.enterprise.deploy.spi.status.ProgressEvent;
037: import javax.enterprise.deploy.spi.status.ProgressListener;
038: import javax.enterprise.deploy.spi.status.ProgressObject;
039: import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
040: import org.apache.geronimo.deployment.plugin.jmx.CommandContext;
041: import org.apache.geronimo.gbean.AbstractName;
042: import org.apache.geronimo.gbean.AbstractNameQuery;
043: import org.apache.geronimo.kernel.InternalKernelException;
044: import org.apache.geronimo.kernel.Kernel;
045: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
046:
047: /**
048: * @version $Rev: 615085 $ $Date: 2008-01-24 16:27:32 -0800 (Thu, 24 Jan 2008) $
049: */
050: public abstract class CommandSupport implements ProgressObject,
051: Runnable {
052: private final CommandType command;
053: private ActionType action;
054: private StateType state;
055: private String message;
056: private final Set listeners = new HashSet();
057: private final List moduleIDs = new ArrayList();
058: protected CommandContext commandContext = null; //todo: this is pretty bad; should add it into constructor
059:
060: private ProgressEvent event = null;
061:
062: protected CommandSupport(CommandType command) {
063: this .command = command;
064: this .action = ActionType.EXECUTE;
065: this .state = StateType.RUNNING;
066: this .message = null;
067: }
068:
069: protected synchronized void addModule(TargetModuleID moduleID) {
070: moduleIDs.add(moduleID);
071: }
072:
073: protected synchronized int getModuleCount() {
074: return moduleIDs.size();
075: }
076:
077: public synchronized TargetModuleID[] getResultTargetModuleIDs() {
078: return (TargetModuleID[]) moduleIDs
079: .toArray(new TargetModuleID[moduleIDs.size()]);
080: }
081:
082: public synchronized DeploymentStatus getDeploymentStatus() {
083: return new Status(command, action, state, message);
084: }
085:
086: public ClientConfiguration getClientConfiguration(TargetModuleID id) {
087: return null;
088: }
089:
090: public boolean isCancelSupported() {
091: return false;
092: }
093:
094: public void cancel() throws OperationUnsupportedException {
095: throw new OperationUnsupportedException("cancel not supported");
096: }
097:
098: public boolean isStopSupported() {
099: 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: "
143: + e.getMessage());
144: if (commandContext.isVerbose()) {
145: e.printStackTrace(System.err);
146: }
147: }
148:
149: StringWriter writer = new StringWriter();
150: PrintWriter printWriter = new PrintWriter(writer);
151: printWriter.println(e.getMessage());
152: if (commandContext.isVerbose()) {
153: e.printStackTrace(printWriter);
154: } else {
155: Throwable throwable = e;
156: while (null != (throwable = throwable.getCause())) {
157: printWriter.println("\t" + throwable.getMessage());
158: }
159: }
160: fail(writer.toString());
161: }
162:
163: private void sendEvent(String message, StateType state) {
164: assert !Thread.holdsLock(this ) : "Trying to send event whilst holding lock";
165:
166: ProgressListener[] toNotify;
167: DeploymentStatus newStatus;
168: synchronized (this ) {
169: this .message = message;
170: this .state = state;
171: newStatus = new Status(command, action, state, message);
172: toNotify = (ProgressListener[]) listeners
173: .toArray(new ProgressListener[listeners.size()]);
174: event = new ProgressEvent(this , null, newStatus);
175: }
176:
177: for (int i = 0; i < toNotify.length; i++) {
178: toNotify[i].handleProgressEvent(event);
179: }
180: }
181:
182: protected static String clean(String value) {
183: if (value.startsWith("\"") && value.endsWith("\"")) {
184: return value.substring(1, value.length() - 1);
185: }
186: return value;
187: }
188:
189: private static class Status implements DeploymentStatus {
190: private final CommandType command;
191: private final ActionType action;
192: private final StateType state;
193: private final String message;
194:
195: public Status(CommandType command, ActionType action,
196: StateType state, String message) {
197: this .command = command;
198: this .action = action;
199: this .state = state;
200: this .message = message;
201: }
202:
203: public CommandType getCommand() {
204: return command;
205: }
206:
207: public ActionType getAction() {
208: return action;
209: }
210:
211: public String getMessage() {
212: return message;
213: }
214:
215: public StateType getState() {
216: return state;
217: }
218:
219: public boolean isRunning() {
220: return StateType.RUNNING.equals(state);
221: }
222:
223: public boolean isCompleted() {
224: return StateType.COMPLETED.equals(state);
225: }
226:
227: public boolean isFailed() {
228: return StateType.FAILED.equals(state);
229: }
230:
231: public String toString() {
232: StringBuffer buf = new StringBuffer();
233: buf.append("DeploymentStatus[").append(command).append(',');
234: buf.append(action).append(',');
235: buf.append(state);
236: if (message != null) {
237: buf.append(',').append(message);
238: }
239: buf.append(']');
240: return buf.toString();
241: }
242: }
243:
244: public CommandContext getCommandContext() {
245: return commandContext;
246: }
247:
248: public void setCommandContext(CommandContext commandContext) {
249: this .commandContext = new CommandContext(commandContext);
250: }
251:
252: public static ModuleType convertModuleType(
253: ConfigurationModuleType type) {
254: if (type.getValue() == ConfigurationModuleType.WAR.getValue()) {
255: return ModuleType.WAR;
256: }
257: if (type.getValue() == ConfigurationModuleType.RAR.getValue()) {
258: return ModuleType.RAR;
259: }
260: if (type.getValue() == ConfigurationModuleType.EJB.getValue()) {
261: return ModuleType.EJB;
262: }
263: if (type.getValue() == ConfigurationModuleType.EAR.getValue()) {
264: return ModuleType.EAR;
265: }
266: if (type.getValue() == ConfigurationModuleType.CAR.getValue()) {
267: return ModuleType.CAR;
268: }
269: return null;
270: }
271:
272: public static boolean isWebApp(Kernel kernel, String configName) {
273: Map filter = new HashMap();
274: filter.put("j2eeType", "WebModule");
275: filter.put("name", configName);
276: Set set = kernel
277: .listGBeans(new AbstractNameQuery(null, filter));
278: return set.size() > 0;
279: }
280:
281: protected void addWebURLs(Kernel kernel) throws Exception {
282: addWebContextPaths(kernel, moduleIDs);
283: }
284:
285: /**
286: * Given a list of TargetModuleIDs, figure out which ones represent web
287: * modules and add a WebURL to each if possible.
288: */
289: public static void addWebContextPaths(Kernel kernel, List moduleIDs)
290: throws Exception {
291: Set webApps = null;
292: for (int i = 0; i < moduleIDs.size(); i++) {
293: TargetModuleIDImpl id = (TargetModuleIDImpl) moduleIDs
294: .get(i);
295: if (id.getType() != null
296: && id.getType().getValue() == ModuleType.WAR
297: .getValue()) {
298: if (webApps == null) {
299: webApps = kernel
300: .listGBeans(new AbstractNameQuery(
301: "org.apache.geronimo.management.geronimo.WebModule"));
302: }
303: for (Iterator it = webApps.iterator(); it.hasNext();) {
304: AbstractName name = (AbstractName) it.next();
305: if (name.getName().get("name").equals(
306: id.getModuleID())) {
307: id.setWebURL(kernel.getAttribute(name,
308: "contextPath").toString());
309: }
310: }
311: }
312: if (id.getChildTargetModuleID() != null) {
313: addWebContextPaths(kernel, Arrays.asList(id
314: .getChildTargetModuleID()));
315: }
316: }
317: }
318:
319: public static List loadChildren(Kernel kernel, String configName) {
320: List kids = new ArrayList();
321:
322: Map filter = new HashMap();
323: filter.put("J2EEApplication", configName);
324:
325: filter.put("j2eeType", "WebModule");
326: Set test = kernel
327: .listGBeans(new AbstractNameQuery(null, filter));
328: for (Iterator it = test.iterator(); it.hasNext();) {
329: AbstractName child = (AbstractName) it.next();
330: String childName = child.getNameProperty("name");
331: kids.add(childName);
332: }
333:
334: filter.put("j2eeType", "EJBModule");
335: test = kernel.listGBeans(new AbstractNameQuery(null, filter));
336: for (Iterator it = test.iterator(); it.hasNext();) {
337: AbstractName child = (AbstractName) it.next();
338: String childName = child.getNameProperty("name");
339: kids.add(childName);
340: }
341:
342: filter.put("j2eeType", "AppClientModule");
343: test = kernel.listGBeans(new AbstractNameQuery(null, filter));
344: for (Iterator it = test.iterator(); it.hasNext();) {
345: AbstractName child = (AbstractName) it.next();
346: String childName = child.getNameProperty("name");
347: kids.add(childName);
348: }
349:
350: filter.put("j2eeType", "ResourceAdapterModule");
351: test = kernel.listGBeans(new AbstractNameQuery(null, filter));
352: for (Iterator it = test.iterator(); it.hasNext();) {
353: AbstractName child = (AbstractName) it.next();
354: String childName = child.getNameProperty("name");
355: kids.add(childName);
356: }
357: return kids;
358: }
359: }
|