KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > util > ProgressEventSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.ws7.util;
21
22 import java.util.Vector JavaDoc;
23
24 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
25 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
26 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
27 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
28
29 import org.openide.ErrorManager;
30 import org.openide.util.RequestProcessor;
31 //The tomcat team will split the tomcat module in 2, so that this type of behaviour can be shared
32
// between web/app server plugins. This is really a shared utility class.
33
//
34
/**
35  * This is a utility class that can be used by ProgressObject's,
36  * You can use an instance of this class as a member field
37  * of your ProgressObject and delegate various work to it.
38  *
39  * @author Radim Kubacki
40  */

41 public class ProgressEventSupport {
42
43     /** Source object. */
44     private Object JavaDoc obj;
45     
46     private Vector JavaDoc listeners;
47     
48     private DeploymentStatus JavaDoc status;
49     
50     private TargetModuleID JavaDoc tmID;
51     
52     /**
53      * Constructs a <code>ProgressEventSupport</code> object.
54      *
55      * @param o Source for any events.
56      */

57     public ProgressEventSupport(Object JavaDoc o) {
58         if (o == null) {
59             throw new NullPointerException JavaDoc();
60         }
61
62         obj = o;
63     }
64     
65     /** Add a ProgressListener to the listener list. */
66     public synchronized void addProgressListener(ProgressListener JavaDoc lsnr) {
67         boolean notify = false;
68
69         if (listeners == null) {
70             listeners = new Vector JavaDoc();
71         }
72
73         listeners.addElement(lsnr);
74
75         if (status != null && !status.isRunning()) {
76             notify = true;
77         }
78
79         if (notify) {
80                                 // not to miss completion event
81
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
82                     public void run() {
83                         fireHandleProgressEvent(tmID, status);
84                     }
85                 });
86         }
87     }
88     
89     /** Remove a ProgressListener from the listener list. */
90     public synchronized void removeProgressListener(ProgressListener JavaDoc lsnr) {
91         if (listeners == null) {
92             return;
93         }
94
95         listeners.removeElement(lsnr);
96     }
97
98     /** Report event to any registered listeners. */
99     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID,
100                                         DeploymentStatus JavaDoc sCode) {
101         ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.sun.ws7");
102         if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
103             err.log("progress event from " + // NOI18N
104
obj +
105                                                 " status " + // NOI18N
106
sCode);
107         }
108
109         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(obj, targetModuleID, sCode);
110         status = sCode;
111         tmID = targetModuleID;
112         
113     Vector JavaDoc targets = null;
114     synchronized (this) {
115         if (listeners != null) {
116             targets = (Vector JavaDoc)listeners.clone();
117         }
118     }
119
120     if (targets != null) {
121         for (int i = 0; i < targets.size(); i++) {
122             ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
123             target.handleProgressEvent (evt);
124         }
125     }
126     }
127     
128     /** Returns last DeploymentStatus notified by {@link fireHandleProgressEvent}
129      */

130     public DeploymentStatus JavaDoc getDeploymentStatus() {
131         return status;
132     }
133 }
134
Popular Tags