KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > plugins > api > ServerProgress


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.deployment.plugins.api;
21
22 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
23 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
24 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
25 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
26 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
27 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
28 import javax.enterprise.deploy.shared.ActionType JavaDoc;
29 import javax.enterprise.deploy.shared.CommandType JavaDoc;
30 import javax.enterprise.deploy.shared.StateType JavaDoc;
31 import javax.enterprise.deploy.spi.exceptions.*;
32
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * This is an utility class to avoid exposing deployment interface
37  * {@link javax.enterprise.deploy.spi.status.ProgressObject} directly in
38  * server management SPI {@link StartServer}.
39  * <P>
40  * Typical usage is for plugin StartServer implementation to create
41  * instance of ServerProgress and return it to caller of
42  * startDeploymentManager, stopDeploymentManager and startDebugging.
43  * Plugin will update caller on progress of the operation through
44  * method calls to set status.
45  * <P>
46  * @author nn136682
47  */

48
49 public class ServerProgress implements ProgressObject JavaDoc {
50     private Object JavaDoc server;
51     private java.util.Vector JavaDoc listeners = new java.util.Vector JavaDoc();
52     private DeploymentStatus JavaDoc status;
53     
54     /** Creates a new instance of StartServerProgress */
55     public ServerProgress(Object JavaDoc server) {
56         this.server = server;
57         createRunningProgressEvent(CommandType.START, ""); //NOI18N
58
}
59
60     public static final Command START_SERVER = new Command(25, "START SERVER"); //NOI18N
61
public static final Command STOP_SERVER = new Command(26, "STOP SERVER"); //NOI18N
62

63     public static class Command extends CommandType JavaDoc {
64         String JavaDoc commandString;
65         public Command(int val, String JavaDoc commandString) {
66             super(val);
67             this.commandString = commandString;
68         }
69         public String JavaDoc toString() {
70             return commandString;
71         }
72     }
73     
74     public void setStatusStartRunning(String JavaDoc message) {
75         notify(createRunningProgressEvent(START_SERVER, message));
76     }
77     public void setStatusStartFailed(String JavaDoc message) {
78         notify(createFailedProgressEvent(START_SERVER, message));
79     }
80     public void setStatusStartCompleted(String JavaDoc message) {
81         notify(createCompletedProgressEvent(START_SERVER, message));
82     }
83     public void setStatusStopRunning(String JavaDoc message) {
84         notify(createRunningProgressEvent(STOP_SERVER, message));
85     }
86     public void setStatusStopFailed(String JavaDoc message) {
87         notify(createFailedProgressEvent(STOP_SERVER, message));
88     }
89     public void setStatusStopCompleted(String JavaDoc message) {
90         notify(createCompletedProgressEvent(CommandType.STOP, message));
91     }
92     protected synchronized void notify(ProgressEvent JavaDoc pe) {
93         for (Iterator JavaDoc i=listeners.iterator(); i.hasNext();) {
94             ProgressListener JavaDoc pol = (ProgressListener JavaDoc) i.next();
95             pol.handleProgressEvent(pe);
96         }
97     }
98
99     protected DeploymentStatus JavaDoc createDeploymentStatus(final CommandType JavaDoc comtype, final String JavaDoc msg, final StateType JavaDoc state) {
100         return new DeploymentStatus JavaDoc() {
101             public ActionType JavaDoc getAction() { return ActionType.EXECUTE; }
102             public CommandType JavaDoc getCommand() { return comtype; }
103             public String JavaDoc getMessage() { return msg; }
104             public StateType JavaDoc getState() { return state; }
105
106             public boolean isCompleted () {
107                 return StateType.COMPLETED.equals(state);
108             }
109
110             public boolean isFailed () {
111                 return StateType.FAILED.equals(state);
112             }
113
114             public boolean isRunning () {
115                 return StateType.RUNNING.equals(state);
116             }
117         };
118     }
119     protected ProgressEvent JavaDoc createCompletedProgressEvent(CommandType JavaDoc command, String JavaDoc message) {
120         status = createDeploymentStatus(command, message, StateType.COMPLETED);
121         return new ProgressEvent JavaDoc(server, null, status);
122     }
123     
124     protected ProgressEvent JavaDoc createFailedProgressEvent(CommandType JavaDoc command, String JavaDoc message) {
125         status = createDeploymentStatus(command, message, StateType.FAILED);
126         return new ProgressEvent JavaDoc(server, null, status);
127     }
128
129     protected ProgressEvent JavaDoc createRunningProgressEvent(CommandType JavaDoc command, String JavaDoc message) {
130         status = createDeploymentStatus(command, message, StateType.RUNNING);
131         return new ProgressEvent JavaDoc(server, null, status);
132     }
133 //-------------- JSR88 ProgressObject -----------------
134
public synchronized void addProgressListener(ProgressListener JavaDoc pol) {
135         listeners.add(pol);
136     }
137     public synchronized void removeProgressListener(ProgressListener JavaDoc pol) {
138         /*for (Iterator i=listeners.iterator(); i.hasNext();) {
139             if(i.next().equals(pol))
140                 i.remove();
141         }*/

142         listeners.remove(pol);
143     }
144     
145     public boolean isCancelSupported() { return true; }
146     public void cancel() throws OperationUnsupportedException {
147         //noop
148
}
149     public boolean isStopSupported() { return false; }
150     public void stop() throws OperationUnsupportedException {
151         //noop
152
}
153     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
154         return null;
155     }
156     public DeploymentStatus JavaDoc getDeploymentStatus() {
157         return status;
158     }
159     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
160         return new TargetModuleID JavaDoc[0];
161     }
162 }
163
164
Popular Tags