KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > plugin > local > CommandSupport


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 JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.enterprise.deploy.shared.ActionType JavaDoc;
31 import javax.enterprise.deploy.shared.CommandType JavaDoc;
32 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
33 import javax.enterprise.deploy.shared.StateType JavaDoc;
34 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
35 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
36 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
37 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
38 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
39 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
40 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
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: 482741 $ $Date: 2006-12-05 14:04:39 -0500 (Tue, 05 Dec 2006) $
51  */

52 public abstract class CommandSupport implements ProgressObject JavaDoc, Runnable JavaDoc {
53     private final CommandType JavaDoc command;
54     private ActionType JavaDoc action;
55     private StateType JavaDoc state;
56     private String JavaDoc message;
57     private final Set JavaDoc listeners = new HashSet JavaDoc();
58     private final List JavaDoc moduleIDs = new ArrayList JavaDoc();
59     protected CommandContext commandContext = null; //todo: this is pretty bad; should add it into constructor
60

61     private ProgressEvent JavaDoc event = null;
62
63     protected CommandSupport(CommandType JavaDoc 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 JavaDoc moduleID) {
71         moduleIDs.add(moduleID);
72     }
73
74     protected synchronized int getModuleCount() {
75         return moduleIDs.size();
76     }
77
78     public synchronized TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
79         return (TargetModuleID JavaDoc[]) moduleIDs.toArray(new TargetModuleID JavaDoc[moduleIDs.size()]);
80     }
81
82     public synchronized DeploymentStatus JavaDoc getDeploymentStatus() {
83         return new Status JavaDoc(command, action, state, message);
84     }
85
86     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc id) {
87         return null;
88     }
89
90     public boolean isCancelSupported() {
91         return false;
92     }
93
94     public void cancel() throws OperationUnsupportedException JavaDoc {
95         throw new OperationUnsupportedException JavaDoc("cancel not supported");
96     }
97
98     public boolean isStopSupported() {
99         return false;
100     }
101
102     public void stop() throws OperationUnsupportedException JavaDoc {
103         throw new OperationUnsupportedException JavaDoc("stop not supported");
104     }
105
106     public void addProgressListener(ProgressListener JavaDoc pol) {
107         ProgressEvent JavaDoc 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 JavaDoc pol) {
118         listeners.remove(pol);
119     }
120
121     public final void fail(String JavaDoc message) {
122         sendEvent(message, StateType.FAILED);
123     }
124
125     protected final void complete(String JavaDoc message) {
126         sendEvent(message, StateType.COMPLETED);
127     }
128
129     public final void updateStatus(String JavaDoc message) {
130         sendEvent(message, state);
131     }
132
133     public void doFail(Exception JavaDoc e) {
134         if (e instanceof InternalKernelException) {
135             Exception JavaDoc test = (Exception JavaDoc)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 JavaDoc writer = new StringWriter JavaDoc();
149         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(writer);
150         printWriter.println(e.getMessage());
151         if (commandContext.isVerbose()) {
152             e.printStackTrace(printWriter);
153         } else {
154             Throwable JavaDoc 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 JavaDoc message, StateType JavaDoc state) {
163         assert !Thread.holdsLock(this) : "Trying to send event whilst holding lock";
164
165         ProgressListener JavaDoc[] toNotify;
166         DeploymentStatus JavaDoc newStatus;
167         synchronized (this) {
168             this.message = message;
169             this.state = state;
170             newStatus = new Status JavaDoc(command, action, state, message);
171             toNotify = (ProgressListener JavaDoc[]) listeners.toArray(new ProgressListener JavaDoc[listeners.size()]);
172             event = new ProgressEvent JavaDoc(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 JavaDoc clean(String JavaDoc 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 JavaDoc {
188         private final CommandType JavaDoc command;
189         private final ActionType JavaDoc action;
190         private final StateType JavaDoc state;
191         private final String JavaDoc message;
192
193         public Status(CommandType JavaDoc command, ActionType JavaDoc action, StateType JavaDoc state, String JavaDoc message) {
194             this.command = command;
195             this.action = action;
196             this.state = state;
197             this.message = message;
198         }
199
200         public CommandType JavaDoc getCommand() {
201             return command;
202         }
203
204         public ActionType JavaDoc getAction() {
205             return action;
206         }
207
208         public String JavaDoc getMessage() {
209             return message;
210         }
211
212         public StateType JavaDoc 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 JavaDoc toString() {
229             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc configName) {
269         Map JavaDoc filter = new HashMap JavaDoc();
270         filter.put("j2eeType", "WebModule");
271         filter.put("name", configName);
272         Set JavaDoc set = kernel.listGBeans(new AbstractNameQuery(null, filter));
273         return set.size() > 0;
274     }
275
276     protected void addWebURLs(Kernel kernel) throws Exception JavaDoc{
277         addWebURLs(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 addWebURLs(Kernel kernel, List JavaDoc moduleIDs) throws Exception JavaDoc{
285         Set JavaDoc 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 JavaDoc 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 JavaDoc loadChildren(Kernel kernel, String JavaDoc configName) {
306         List JavaDoc kids = new ArrayList JavaDoc();
307
308         Map JavaDoc filter = new HashMap JavaDoc();
309         filter.put("J2EEApplication", configName);
310
311         filter.put("j2eeType", "WebModule");
312         Set JavaDoc test = kernel.listGBeans(new AbstractNameQuery(null, filter));
313         for (Iterator JavaDoc it = test.iterator(); it.hasNext();) {
314             AbstractName child = (AbstractName) it.next();
315             String JavaDoc 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 JavaDoc it = test.iterator(); it.hasNext();) {
322             AbstractName child = (AbstractName) it.next();
323             String JavaDoc 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 JavaDoc it = test.iterator(); it.hasNext();) {
330             AbstractName child = (AbstractName) it.next();
331             String JavaDoc 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 JavaDoc it = test.iterator(); it.hasNext();) {
338             AbstractName child = (AbstractName) it.next();
339             String JavaDoc childName = child.getNameProperty("name");
340             kids.add(childName);
341         }
342         return kids;
343     }
344 }
345
Popular Tags