KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > ide > JBStartServer


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 package org.netbeans.modules.j2ee.jboss4.ide;
20
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.netbeans.modules.j2ee.deployment.profiler.api.ProfilerServerSettings;
25 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager;
26 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties;
27 import java.io.IOException JavaDoc;
28 import java.io.File JavaDoc;
29 import javax.enterprise.deploy.shared.ActionType JavaDoc;
30 import javax.enterprise.deploy.shared.CommandType JavaDoc;
31 import javax.enterprise.deploy.shared.StateType JavaDoc;
32 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
33 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
34 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer;
35 import org.openide.ErrorManager;
36 import org.openide.util.RequestProcessor;
37 import java.util.Vector JavaDoc;
38 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
39 import javax.enterprise.deploy.spi.Target JavaDoc;
40 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
41 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
42 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
43 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
44 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
45 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
46 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
47 import org.openide.util.NbBundle;
48 import org.netbeans.modules.j2ee.jboss4.nodes.Util;
49
50 /**
51  *
52  * @author Kirill Sorokin
53  */

54 public class JBStartServer extends StartServer implements ProgressObject JavaDoc{
55     
56     static enum MODE { RUN, DEBUG, PROFILE };
57     
58     static enum ACTION_STATUS { SUCCESS, FAILURE, UNKNOWN };
59     
60     private MODE mode;
61     
62     private JBDeploymentManager dm;
63     private static Map JavaDoc isDebugModeUri = Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
64     
65     public JBStartServer(DeploymentManager dm) {
66         if (!(dm instanceof JBDeploymentManager)) {
67             throw new IllegalArgumentException JavaDoc("");
68         }
69         this.dm = (JBDeploymentManager) dm;
70     }
71     
72     private void addDebugModeUri() {
73         isDebugModeUri.put(dm.getUrl(), new Object JavaDoc());
74     }
75     
76     private void removeDebugModeUri() {
77         isDebugModeUri.remove(dm.getUrl());
78     }
79     
80     private boolean existsDebugModeUri() {
81         return isDebugModeUri.containsKey(dm.getUrl());
82     }
83     
84     public ProgressObject JavaDoc startDebugging(Target JavaDoc target) {
85         String JavaDoc serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
86         fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING, NbBundle.getMessage(JBStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName))); //NOI18N
87
mode = MODE.DEBUG;
88         RequestProcessor.getDefault().post(new JBStartRunnable(null, dm, this), 0, Thread.NORM_PRIORITY);
89         addDebugModeUri();
90         return this;
91     }
92     
93     public boolean isDebuggable(Target JavaDoc target) {
94         if (!existsDebugModeUri()) {
95             return false;
96         }
97         if (!isRunning()) {
98             return false;
99         }
100         return true;
101     }
102     
103     public boolean supportsStartDebugging(Target JavaDoc target) {
104         return true;
105     }
106     
107     public boolean supportsStartProfiling(Target JavaDoc target) {
108         return true;
109     }
110     
111     public boolean isAlsoTargetServer(Target JavaDoc target) {
112         return true;
113     }
114     
115     public ServerDebugInfo getDebugInfo(Target JavaDoc target) {
116         return new ServerDebugInfo("localhost", dm.getDebuggingPort());
117     }
118     
119     /**
120      * Starts the server in profiling mode.
121      */

122     public ProgressObject JavaDoc startProfiling(Target JavaDoc target, ProfilerServerSettings settings) {
123         String JavaDoc serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
124         fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING, NbBundle.getMessage(JBStartServer.class, "MSG_START_PROFILED_SERVER_IN_PROGRESS", serverName))); //NOI18N
125
mode = MODE.PROFILE;
126         RequestProcessor.getDefault().post(new JBStartRunnable(settings, dm, this), 0, Thread.NORM_PRIORITY);
127         removeDebugModeUri();
128         return this;
129     }
130     
131     
132     /**
133      * Indicates whether this server supports start/stop.
134      *
135      * @return true/false - supports/does not support
136      */

137     public boolean supportsStartDeploymentManager() {
138         return true;
139     }
140     
141     /**
142      * Stops the server.
143      */

144     public ProgressObject JavaDoc stopDeploymentManager() {
145         String JavaDoc serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
146         fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.STOP, StateType.RUNNING, NbBundle.getMessage(JBStartServer.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)));//NOI18N
147
RequestProcessor.getDefault().post(new JBStopRunnable(dm, this), 0, Thread.NORM_PRIORITY);
148         removeDebugModeUri();
149         return this;
150     }
151     
152     /**
153      * Starts the server
154      */

155     public ProgressObject JavaDoc startDeploymentManager() {
156         String JavaDoc serverName = dm.getInstanceProperties().getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
157         fireHandleProgressEvent(null, new JBDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING, NbBundle.getMessage(JBStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName)));//NOI18N
158
mode = MODE.RUN;
159         RequestProcessor.getDefault().post(new JBStartRunnable(null, dm, this), 0, Thread.NORM_PRIORITY);
160         removeDebugModeUri();
161         return this;
162     }
163     
164     
165     public boolean needsStartForTargetList() {
166         return false;
167     }
168     
169     public boolean needsStartForConfigure() {
170         return false;
171     }
172     
173     public boolean needsStartForAdminConfig() {
174         return false;
175     }
176     
177     private boolean isReallyRunning(){
178         final InstanceProperties ip = dm.getInstanceProperties();
179         if (ip == null) {
180             return false; // finish, it looks like this server instance has been unregistered
181
}
182         // this should prevent the thread from getting stuck if the server is in suspended state
183
SafeTrueTest test = new SafeTrueTest() {
184             public void run() {
185                 ClassLoader JavaDoc oldLoader = null;
186                 String JavaDoc checkingConfigName = ip.getProperty(JBPluginProperties.PROPERTY_SERVER);
187                 String JavaDoc checkingServerDir = null;
188                 
189                 try {
190                     String JavaDoc serverDir = ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR);
191                     
192                     if(serverDir == null) {
193                         result = false;
194                         return;
195                     }
196                     
197                     checkingServerDir = new File JavaDoc(serverDir).getCanonicalPath();
198                 } catch (IllegalStateException JavaDoc ex) {
199                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
200                     result = false;
201                 } catch (IOException JavaDoc ex) {
202                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
203                     result = false;
204                 }
205                 
206                 Object JavaDoc serverName = Util.getMBeanParameter(dm, "ServerName", "jboss.system:type=ServerConfig");
207                 Object JavaDoc serverHome = Util.getMBeanParameter(dm, "ServerHomeDir", "jboss.system:type=ServerConfig");
208                 
209                 if(serverName == null || serverHome == null) {
210                     result = false;
211                     return;
212                 }
213                 
214                 serverHome = ((File JavaDoc)serverHome).getAbsolutePath();
215                 
216                 if (checkingConfigName.equals(serverName) && checkingServerDir.equals(serverHome))
217                     result = true;
218             }
219         };
220         
221         return safeTrueTest(test, 10000);
222     }
223     
224     /** Safe true/false test useful. */
225     private abstract static class SafeTrueTest implements Runnable JavaDoc {
226         protected boolean result = false;
227         
228         public abstract void run();
229         
230         public final boolean result() {
231             return result;
232         }
233     };
234     
235     /** Return the result of the test or false if the given time-out ran out. */
236     private boolean safeTrueTest(SafeTrueTest test, int timeout) {
237         try {
238             new RequestProcessor().post(test).waitFinished(timeout);
239         } catch (InterruptedException JavaDoc ie) {
240             // no op
241
} finally {
242             return test.result();
243         }
244     }
245     
246     public boolean isRunning() {
247         
248         InstanceProperties ip = dm.getInstanceProperties();
249         if (ip == null) {
250             return false; // finish, it looks like this server instance has been unregistered
251
}
252         
253         if (!isReallyRunning()){
254             dm.setRunningLastCheck(ip, Boolean.FALSE);
255             return false;
256         }
257         
258         dm.setRunningLastCheck(ip, Boolean.TRUE);
259         return true;
260     }
261     
262     // ---------- Implementation of ProgressObject interface
263
private Vector JavaDoc listeners = new Vector JavaDoc();
264     private DeploymentStatus JavaDoc deploymentStatus;
265     
266     public void addProgressListener(ProgressListener JavaDoc pl) {
267         listeners.add(pl);
268     }
269     
270     public void removeProgressListener(ProgressListener JavaDoc pl) {
271         listeners.remove(pl);
272     }
273     
274     public void stop() throws OperationUnsupportedException JavaDoc {
275         throw new OperationUnsupportedException JavaDoc("");
276     }
277     
278     public boolean isStopSupported() {
279         return false;
280     }
281     
282     public void cancel() throws OperationUnsupportedException JavaDoc {
283         throw new OperationUnsupportedException JavaDoc("");
284     }
285     
286     public boolean isCancelSupported() {
287         return false;
288     }
289     
290     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
291         return null;
292     }
293     
294     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
295         return new TargetModuleID JavaDoc[]{};
296     }
297     
298     public DeploymentStatus JavaDoc getDeploymentStatus() {
299         return deploymentStatus;
300     }
301     
302     /** Report event to any registered listeners. */
303     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID, DeploymentStatus JavaDoc deploymentStatus) {
304         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(this, targetModuleID, deploymentStatus);
305         
306         this.deploymentStatus = deploymentStatus;
307         
308         java.util.Vector JavaDoc targets = null;
309         synchronized (this) {
310             if (listeners != null) {
311                 targets = (java.util.Vector JavaDoc) listeners.clone();
312             }
313         }
314         
315         if (targets != null) {
316             for (int i = 0; i < targets.size(); i++) {
317                 ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
318                 target.handleProgressEvent(evt);
319             }
320         }
321     }
322
323     MODE getMode() {
324         return mode;
325     }
326     
327     
328 }
329
Popular Tags