KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > DeploymentStatus


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.backend;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34
35 import com.sun.appserv.management.base.MapCapable;
36
37
38 /**
39  * This class encapsulates all status related to backend deployment
40  * Backend deployement can consist of several stages. Those stages
41  * are organized in a parent/children relationship where the children
42  * are the sub stages of a parent stage. For instance, deployment stage
43  * consist of j2eec stage and application loading stage. For a stage to
44  * be sucessful, its status and all its children's status must be warning
45  * or success
46  *
47  * @author Jerome Dochez
48  *
49  */

50 public class DeploymentStatus implements java.io.Serializable JavaDoc, MapCapable {
51
52         // declare SUID for class versioning compatibility
53
// generated using pe build fcs-b50
54
// this value should stay the same for all
55
// 8.x releases
56
static final long serialVersionUID = -6130876961172599515L;
57
58     /**
59      * Possible status for a stage or overall deployment status
60      */

61     public static final int FAILURE = 0;
62     public static final int WARNING = 1;
63     public static final int SUCCESS=2;
64     public static final int NOTINITIALIZED = 3;
65     
66
67     /*
68      * Possible properties for the additional status
69      */

70     public static final String JavaDoc CONTEXT_ROOT = "ContextRoot";
71     public static final String JavaDoc KEY_SEPARATOR = "_";
72     public static final String JavaDoc MODULE_ID = "moduleid";
73     public static final String JavaDoc MODULE_TYPE = "ModuleType";
74     public static final String JavaDoc SUBMODULE_COUNT = "NumberOfSubModules";
75     public static final String JavaDoc WSDL_PUBLISH_URL = "ClientPublishURL";
76     public static final String JavaDoc WSDL_LOCATION = "WsdlFileLocation";
77     public static final String JavaDoc WSDL_DIRECTORY = "WsdlDirectory";
78     public static final String JavaDoc WSDL_FILE_ENTRIES = "WsdlFileEntries";
79     public static final String JavaDoc COUNT = "NumberOfEntries";
80
81     /**
82      * instance information :
83      * - information about this stage
84      * - information about sub stages
85      */

86     private String JavaDoc stageDescription;
87     private int stageStatus = NOTINITIALIZED;
88     private String JavaDoc stageStatusMessage = "";
89     private Throwable JavaDoc stageException;
90     
91     private List JavaDoc subStages = new ArrayList JavaDoc();
92         private DeploymentStatus parent = null;
93         
94         /**
95          * backend deployment can transfer some information back to the
96          * client process through these properties.
97          */

98         private Map JavaDoc additionalStatus = new Properties JavaDoc();
99
100         // this field is still kept for backward compatibility reason
101
private Properties JavaDoc props=null;
102         
103     
104     /**
105      * Creates a new uninitialized DeploymentStatus instance
106      */

107     public DeploymentStatus() {
108     }
109     
110     /**
111      * Creates a new uninitialized DeploymentStatus instance as a
112      * sub stage deployment status of the passed DeploymentStatus
113      * @param parent DeploymentStatus
114      */

115     public DeploymentStatus(DeploymentStatus parent) {
116         if (parent==null) {
117             throw new IllegalArgumentException JavaDoc("parent deployment status cannot be null");
118         }
119         parent.addSubStage(this);
120     }
121     
122     /**
123      * @return the combined status for the stage and its children,
124      * it will return the lowest status as defined in the constants
125      */

126     public int getStatus() {
127         
128         int currentStatus = stageStatus;
129         
130         // iterate over all sub stages to get their status
131
for (Iterator JavaDoc stageItr = subStages.iterator();stageItr.hasNext();) {
132             DeploymentStatus subStage = (DeploymentStatus) stageItr.next();
133             int subStageStatus= subStage.getStatus();
134             // if the sub stage status is a lower number than our current, something
135
// went horribly wrong in the substage, update ours
136
if (subStageStatus<currentStatus) {
137                 currentStatus = subStageStatus;
138             }
139         }
140         return currentStatus;
141     }
142     
143     /**
144      * Add a sub stage to this deployment status
145      * @param the sub stage deployment status
146      */

147     public void addSubStage(DeploymentStatus subStage) {
148         subStages.add(subStage);
149                 subStage.setParent(this);
150     }
151     
152     /**
153      * Get the list of sub stages for this deployment status
154      * @return an Iterator for the sub stages
155      */

156     public Iterator JavaDoc getSubStages() {
157         return subStages.iterator();
158     }
159         
160     /**
161      * Set the status for this stage
162      * @param the status as defined in the constants
163      */

164     public void setStageStatus(int status) {
165         stageStatus = status;
166     }
167     
168     /**
169      * @return the status for this stage (ignoring sub stages status)
170      */

171     public int getStageStatus() {
172         return stageStatus;
173     }
174     
175     /**
176      * @return the exception if an exception was thrown during
177      * the execution of the stage
178      */

179     public Throwable JavaDoc getStageException() {
180         return stageException;
181     }
182
183     /**
184      * @return a meaningful i18ned stage description
185      */

186     public String JavaDoc getStageIdentifier() {
187         return stageDescription;
188     }
189
190         /**
191          * @return a meaningful i18ned stage description
192          */

193         public String JavaDoc getStageDescription() {
194                 return stageDescription;
195         }
196
197
198     /**
199      * @return a meaningful i18ned reason for failure or warning
200      */

201     public String JavaDoc getStageStatusMessage() {
202         return stageStatusMessage;
203     }
204
205     /**
206      * When the stage throws an exception, it should store it here in
207      * the assiciated deployment status
208      * @param throwable
209      */

210     public void setStageException(Throwable JavaDoc throwable) {
211             stageException = new Throwable JavaDoc(throwable.getMessage());
212             stageException.setStackTrace(throwable.getStackTrace());
213     }
214
215     /**
216      * @param string for a meaningful i18ned stage description
217      */

218     public void setStageDescription(String JavaDoc string) {
219         stageDescription = string;
220     }
221
222     /**
223      * @param string for a meaningful i18ned reason for stage
224      * warning or failure
225      */

226     public void setStageStatusMessage(String JavaDoc string) {
227         stageStatusMessage = string;
228     }
229         
230         /**
231          * @return the current deployment status or one of the sub stage
232          * deployment status with a status equal to the provided level.
233          */

234         public DeploymentStatus getStageStatusForLevel(int level) {
235             if (stageStatus == level) {
236                 return this;
237             } else {
238                 for (Iterator JavaDoc itr = subStages.iterator();itr.hasNext();) {
239                     DeploymentStatus subStage = (DeploymentStatus) itr.next();
240                     if (subStage.getStatus()==level) {
241                         return subStage;
242                     }
243                 }
244             }
245             return null;
246         }
247
248         /**
249          * @return the parent status for this status if any
250          */

251         public DeploymentStatus getParent() {
252             return parent;
253         }
254         
255         /**
256          * Setthe parent status for this status
257          */

258         public void setParent(DeploymentStatus parent) {
259             this.parent = parent;
260         }
261         
262         /**
263          * @return the main status for this deployment operation, which is
264          * the parent of all status in this hierarchy
265          */

266         public DeploymentStatus getMainStatus() {
267             if (parent!=null) {
268                 return parent.getMainStatus();
269             }
270             return this;
271         }
272         
273         /**
274          * Add a new property to this status object
275          */

276         public void addProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
277             additionalStatus.put(propertyName, propertyValue);
278
279             // the name-value pair is also stored in props
280
// for backward compatibility
281
if (props==null) {
282                 props = new Properties JavaDoc();
283             }
284             props.put(propertyName, propertyValue);
285         }
286         
287         /**
288          * @return the value of for property name
289          */

290         public String JavaDoc getProperty(String JavaDoc propertyName) {
291             if (additionalStatus.get(propertyName) != null) {
292                 return (String JavaDoc)additionalStatus.get(propertyName);
293             } else {
294             // we also try to retrieve from props
295
// for backward compatibility
296
if (props==null) {
297                     return null;
298                 }
299                 return props.getProperty(propertyName);
300             }
301         }
302
303         /**
304          * @return the additional status map
305          */

306         public Map JavaDoc getAdditionalStatus() {
307             return additionalStatus;
308         }
309
310         /**
311          * Set the additional status for this status
312          */

313         public void setAdditionalStatus(Map JavaDoc additionalStatus) {
314             this.additionalStatus = additionalStatus;
315         }
316         
317         /**
318          * @return a meaningful string about mysefl
319          */

320         public String JavaDoc toString() {
321             return "Status " + stageStatus + " message " + stageStatusMessage
322                 + " \nException " + stageException;
323         }
324
325         /**
326          * Get all stages with this deployment status level
327          * @param parent status it needs to iterate through
328          * @param status level it's looking for
329          * @return an iterator for the stages with the level
330          */

331         public static Iterator JavaDoc getAllStageStatusForLevel(
332             DeploymentStatus status,
333             int level) {
334             List JavaDoc stages = new ArrayList JavaDoc();
335             if (status.getStageStatus() == level) {
336                 stages.add(status);
337             }
338             // need to iterate through the rest of status hierarchy
339
for (Iterator JavaDoc itr = status.getSubStages();itr.hasNext();) {
340                 DeploymentStatus subStage = (DeploymentStatus) itr.next();
341                 if (subStage.getStageStatus() == level) {
342                     stages.add(subStage);
343                 }
344                 for (Iterator JavaDoc itr2 = subStage.getSubStages();itr2.hasNext();) {
345                     DeploymentStatus subStage2 = (DeploymentStatus) itr2.next();
346                     if (subStage2.getStageStatus() == level) {
347                         stages.add(subStage2);
348                     }
349
350                     for (Iterator JavaDoc itr3 = subStage2.getSubStages();
351                         itr3.hasNext();) {
352                         DeploymentStatus subStage3 =
353                             (DeploymentStatus) itr3.next();
354                         if (subStage3.getStageStatus() == level) {
355                             stages.add(subStage3);
356                         }
357                     }
358                 }
359             }
360             return stages.iterator();
361         }
362
363         /**
364          * Implement the MapCapable interface so DeploymentStatus
365          * can be converted back and forth with CompositeData
366          */

367         public Map JavaDoc asMap() {
368             HashMap JavaDoc m = new HashMap JavaDoc();
369             m.put(MapCapable.MAP_CAPABLE_CLASS_NAME_KEY, com.sun.appserv.management.deploy.DeploymentStatus.DEPLOYMENT_STATUS_CLASS_NAME );
370             m.put(com.sun.appserv.management.deploy.DeploymentStatus.STAGE_STATUS_KEY, new Integer JavaDoc(stageStatus));
371             m.put(com.sun.appserv.management.deploy.DeploymentStatus.STAGE_STATUS_MESSAGE_KEY, stageStatusMessage);
372             m.put(com.sun.appserv.management.deploy.DeploymentStatus.STAGE_DESCRIPTION_KEY, stageDescription);
373             m.put(com.sun.appserv.management.deploy.DeploymentStatus.SUB_STAGES_KEY, subStagesToMapList());
374             m.put(com.sun.appserv.management.deploy.DeploymentStatus.STAGE_THROWABLE_KEY, stageException);
375             m.put(com.sun.appserv.management.deploy.DeploymentStatus.ADDITIONAL_STATUS_KEY, additionalStatus);
376             return( m );
377
378         }
379
380         /**
381          * DeploymentStatus class may not be available on the client side.
382          *
383          * Converts the list of DeploymentStatus objects to list of Maps.
384          * Iterates through the substages list and calls asMap().
385          */

386         List JavaDoc subStagesToMapList() {
387             final List JavaDoc l = new ArrayList JavaDoc(subStages.size());
388             final Iterator JavaDoc it = subStages.iterator();
389             while (it.hasNext()) {
390                 final MapCapable mc = (MapCapable)it.next();
391                 l.add(mc.asMap());
392             }
393             return l;
394         }
395
396         /**
397                 Return the interface that this Map represents
398                 (the Java classname).
399          */

400         public String JavaDoc getMapClassName() {
401         return com.sun.appserv.management.deploy.DeploymentStatus.DEPLOYMENT_STATUS_CLASS_NAME;
402     }
403
404
405         /**
406          * Traverse through the DeploymenStatus hierarchy and
407          * write failure/warning msgs to the print writer
408          */

409         public static void parseDeploymentStatus(DeploymentStatus status,
410             PrintWriter JavaDoc pw) {
411             if (status != null) {
412                 
413                 // if it's falure case, print all exceptions
414
if (status.getStatus() == DeploymentStatus.FAILURE) {
415                     for (Iterator JavaDoc itr = getAllStageStatusForLevel(status, DeploymentStatus.FAILURE); itr.hasNext();) {
416                         DeploymentStatus stage = (DeploymentStatus) itr.next();
417                         printFailure(pw, stage);
418                     }
419                 }
420
421                 // if it's warning case, print all warnings
422
else if (status.getStatus() == DeploymentStatus.WARNING) {
423                     for (Iterator JavaDoc itr = getAllStageStatusForLevel(status, DeploymentStatus.WARNING); itr.hasNext();) {
424                         DeploymentStatus stage = (DeploymentStatus) itr.next();
425                         String JavaDoc msg = stage.getStageStatusMessage();
426                         if (msg != null) {
427                             pw.println(msg);
428                         }
429                     }
430                 }
431                 pw.flush();
432             }
433         }
434
435         /**
436         * Prints the status string and/or status exception
437         * @param pw PrintWriter to which info is printed.
438         * @param status DeploymentStatus
439         * @param t Throwable to print
440         */

441         private static void printFailure(PrintWriter JavaDoc pw,
442             DeploymentStatus status) {
443             String JavaDoc msg = status.getStageStatusMessage();
444             Throwable JavaDoc t = status.getStageException();
445             if (msg != null && msg.trim().length() > 0) {
446                 pw.println(msg);
447                 // only print the exception if it's not the same as the
448
// the status message
449
if (t != null && t.getMessage() != null &&
450                     !t.getMessage().equals(msg)) {
451                     pw.println(t);
452                 }
453             } else {
454                 if (t != null) {
455                     pw.println(t);
456                 }
457             }
458         }
459
460 }
461
Popular Tags