KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > j2ee > WS70StartServer


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 /*
21  * WS70StartServer.java
22  */

23
24 package org.netbeans.modules.j2ee.sun.ws7.j2ee;
25
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import javax.enterprise.deploy.spi.Target JavaDoc;
28 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
29
30 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
31 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer;
32 import org.openide.ErrorManager;
33 import org.openide.util.NbBundle;
34 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
35 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
36 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
37 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
38 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
39 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
40 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
41 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
42
43 import javax.enterprise.deploy.shared.ActionType JavaDoc;
44 import javax.enterprise.deploy.shared.CommandType JavaDoc;
45 import javax.enterprise.deploy.shared.StateType JavaDoc;
46
47 import org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentManager;
48 import org.netbeans.modules.j2ee.sun.ws7.util.ProgressEventSupport;
49 import org.netbeans.modules.j2ee.sun.ws7.util.Status;
50 import org.netbeans.modules.j2ee.sun.ws7.Constants;
51 import org.openide.util.RequestProcessor;
52
53 import java.io.File JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Collections JavaDoc;
57  /*
58  * @author Mukesh Garg
59  */

60 public class WS70StartServer extends StartServer implements ProgressObject JavaDoc, Runnable JavaDoc{
61     
62     private WS70SunDeploymentManager dm;
63     private ProgressEventSupport pes;
64     private WS70CommandType cmdType;
65     private Target JavaDoc target;
66     private static Map JavaDoc isDebugModeConfig = Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
67     
68     public WS70StartServer(DeploymentManager JavaDoc dm) {
69         this.dm = (WS70SunDeploymentManager)dm;
70         pes = new ProgressEventSupport(this);
71         
72     }
73     
74
75     /**
76      * Returns true if the admin server is also the given target server (share the same vm).
77      * Start/stopping/debug apply to both servers.
78      * @param the target server in question
79      * @return true when admin is also target server
80      */

81     public boolean isAlsoTargetServer(Target JavaDoc target) {
82         return false;
83     }
84     
85     
86     /**
87      * Returns true if the admin server can be started through this spi.
88      */

89     public boolean supportsStartDeploymentManager() {
90         if(dm.isLocalServer()){
91             return true;
92         }
93         return false;
94     }
95     
96     
97     /**
98      * Starts the admin server. All diagnostic should be communicated
99      * through ServerProgres with no exceptions thrown.
100      *
101      * @return ProgressObject object used to monitor start server progress
102      */

103     public ProgressObject JavaDoc startDeploymentManager() {
104         if(dm.isLocalServer()){
105             cmdType = WS70CommandType.START;
106             pes.fireHandleProgressEvent(null,
107                                     new Status JavaDoc(ActionType.EXECUTE, CommandType.START,
108                                                NbBundle.getMessage(WS70StartServer.class, "MSG_STARTING_ADMIN_SERVER"),
109                                                StateType.RUNNING));
110             
111             RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
112             return this;
113         }else{
114             return null;
115         }
116     }
117     
118
119     /**
120      * Stops the admin server.
121      * All diagnostic should be communicated through ServerProgres with no
122      * exceptions thrown.
123      * @return ServerProgress object used to monitor start server progress
124      */

125     public ProgressObject JavaDoc stopDeploymentManager() {
126          if(dm.isLocalServer()){
127             cmdType = WS70CommandType.STOP;
128             pes.fireHandleProgressEvent(null,
129                                     new Status JavaDoc(ActionType.EXECUTE, CommandType.STOP,
130                                                NbBundle.getMessage(WS70StartServer.class, "MSG_STOPPING_ADMIN_SERVER"),
131                                                StateType.RUNNING));
132
133             RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
134             return this;
135         }else{
136             return null;
137         }
138
139     }
140     
141     
142     /**
143      * Returns true if the admin server should be started before server deployment configuration.
144      */

145     public boolean needsStartForConfigure() {
146         return false;
147     }
148     
149     /**
150      * Returns true if the admin server should be started before asking for
151      * target list.
152      */

153     public boolean needsStartForTargetList() {
154         return false;
155     }
156     
157     
158     /**
159      * Returns true if the admin server should be started before admininistrative configuration.
160      */

161     public boolean needsStartForAdminConfig() {
162         return false;
163     }
164     
165     
166     /**
167      * Returns true if this admin server is running.
168      */

169     public boolean isRunning() {
170         return ((WS70SunDeploymentManager)dm).isRunning();
171     }
172     
173     
174     /**
175      * The method <code>needsRestart</code> is used to signal that
176      * there were some actions on the container which would need a
177      * restart to take effect.
178      * This method not abstract because not not all plugins might need
179      * to implement it.
180      *
181      * @return a <code>boolean</code> value
182      */

183     public boolean needsRestart() {
184         return false;
185     }
186     
187     /**
188      * Returns true if this target is in debug mode.
189      */

190     public boolean isDebuggable(Target JavaDoc t) {
191         if(t==null){
192             return false;
193         }
194         String JavaDoc config = null;
195         try{
196             config = this.getConfigNameFromTarget(t);
197         }catch(Exception JavaDoc ex){
198             ex.printStackTrace();
199             return false;
200         }
201         if(!isDebugModeConfig.containsKey(dm.getHost()+dm.getPort()+config)){
202             return false;
203         }
204         
205         if(!dm.isRunning(config)){
206             isDebugModeConfig.remove(dm.getHost()+dm.getPort()+config);
207             return false;
208         }
209         return true;
210     }
211     
212     
213     /**
214      * Start or restart the target in debug mode.
215      *
216      * All diagnostic should be communicated through ServerProgres with no exceptions thrown.
217      * @param target the target server
218      * @return ServerProgress object to monitor progress on start operation
219      */

220     public ProgressObject JavaDoc startDebugging(Target JavaDoc t) {
221          if(t!=null){
222             this.target = t;
223             cmdType = WS70CommandType.STARTTARGETDEBUG;
224             pes.fireHandleProgressEvent(null,
225                                     new Status JavaDoc(ActionType.EXECUTE, CommandType.START,
226                                                NbBundle.getMessage(WS70StartServer.class, "MSG_STARTING_TARGET_SERVER_DEBUG"),
227                                                StateType.RUNNING));
228             
229             RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
230             String JavaDoc config = null;
231             try{
232                 config = this.getConfigNameFromTarget(t);
233             }catch(Exception JavaDoc ex){
234                 ex.printStackTrace();
235             }
236             isDebugModeConfig.put(dm.getHost()+dm.getPort()+config, new Object JavaDoc());
237             return this;
238         }
239         return null;
240     }
241     
242
243     /**
244      * Returns the host/port necessary for connecting to the server's debug information.
245      */

246     
247     public ServerDebugInfo getDebugInfo(Target JavaDoc t) {
248         if(t==null){
249             return null;
250         }
251         String JavaDoc uri = ((WS70SunDeploymentManager)dm).getUri();
252         this.dm = org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentFactory.getConnectedCachedDeploymentManager(uri);
253         String JavaDoc debugOptions = this.dm.getDebugOptions();
254         if(debugOptions==null){
255             ErrorManager.getDefault().log(
256                     ErrorManager.ERROR, NbBundle.getMessage(WS70StartServer.class, "ERR_DEBUG_OPTIONS_NULL"));
257             return null;
258         }
259         String JavaDoc nodeName = dm.getNodeNameForTarget(t);
260         String JavaDoc address = debugOptions.substring(debugOptions.indexOf(Constants.DEBUG_OPTIONS_ADDRESS)+Constants.DEBUG_OPTIONS_ADDRESS.length(), debugOptions.length());
261         int hasMore = address.indexOf("]"); //NOI18N
262
if(hasMore != -1){
263             address = address.substring(0, hasMore);
264         }
265         if(debugOptions.indexOf(Constants.ISDTSOCKET)!=-1){
266             int debugport = -1;
267             try{
268                 debugport = Integer.parseInt(address.trim());
269             }catch(NumberFormatException JavaDoc ex){
270                 ErrorManager.getDefault().log(
271                     ErrorManager.ERROR, NbBundle.getMessage(WS70StartServer.class, "ERR_DTSOCKET_PORT_INVALID"));
272                 return null;
273             }
274             return new ServerDebugInfo(nodeName, debugport);
275         }else if(debugOptions.indexOf(Constants.ISSHMEM)!=-1){
276             return new ServerDebugInfo(nodeName, address.trim());
277         }else{
278             ErrorManager.getDefault().log(
279                     ErrorManager.ERROR, NbBundle.getMessage(WS70StartServer.class, "ERR_UNKNOW_DEBUG_OPTION"));
280             return null;
281         }
282     }
283     
284     
285     public boolean isRunning(Target JavaDoc t){
286         if(t==null){
287             return true;
288         }
289         String JavaDoc uri = ((WS70SunDeploymentManager)dm).getUri();
290         this.dm = org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentFactory.getConnectedCachedDeploymentManager(uri);
291         try{
292             String JavaDoc configName = this.getConfigNameFromTarget(t);
293             return ((WS70SunDeploymentManager)dm).isRunning(configName);
294         }catch(Exception JavaDoc ex){
295             ex.printStackTrace();
296         }
297         return true;
298     }
299     public ProgressObject JavaDoc startTarget(Target JavaDoc t){
300         if(t!=null){
301             this.target = t;
302             cmdType = WS70CommandType.STARTTARGET;
303             pes.fireHandleProgressEvent(null,
304                                     new Status JavaDoc(ActionType.EXECUTE, CommandType.START,
305                                                NbBundle.getMessage(WS70StartServer.class, "MSG_STARTING_TARGET_SERVER"),
306                                                StateType.RUNNING));
307             
308             RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
309             String JavaDoc config = null;
310             try{
311                 config = this.getConfigNameFromTarget(t);
312             }catch(Exception JavaDoc ex){
313                 ex.printStackTrace();
314             }
315             isDebugModeConfig.remove(dm.getHost()+dm.getPort()+config);
316             return this;
317         }
318         return null;
319     }
320     public ProgressObject JavaDoc stoptTarget(Target JavaDoc t){
321         if(t!=null){
322             this.target = t;
323             cmdType = WS70CommandType.STOPTARGET;
324             pes.fireHandleProgressEvent(null,
325                                     new Status JavaDoc(ActionType.EXECUTE, CommandType.START,
326                                                NbBundle.getMessage(WS70StartServer.class, "MSG_STOPPING_TARGET_SERVER"),
327                                                StateType.RUNNING));
328             
329             RequestProcessor.getDefault().post(this, 0, Thread.NORM_PRIORITY);
330             String JavaDoc config = null;
331             try{
332                 config = this.getConfigNameFromTarget(t);
333             }catch(Exception JavaDoc ex){
334                 ex.printStackTrace();
335             }
336             isDebugModeConfig.remove(dm.getHost()+dm.getPort()+config);
337             return this;
338         }
339         return null;
340     }
341     public boolean supportsStartTarget(Target JavaDoc t){
342         if(t==null){
343             return false;
344         }
345         return true;
346     }
347     public boolean supportsStartDebugging(Target JavaDoc t){
348         if(t==null){
349             return false;
350         }
351         return true;
352     }
353       
354     // Ruunable run implementation
355
public synchronized void run(){
356         if (cmdType.equals(WS70CommandType.START)) {
357             try{
358                 runProcess(makeProcessString("start"), true); //NO I18N
359
this.viewAdminLogs();
360                 pes.fireHandleProgressEvent(null,
361                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
362                                            NbBundle.getMessage(WS70StartServer.class, "MSG_ADMIN_SERVER_STARTED"),
363                                            StateType.COMPLETED));
364             }catch(Exception JavaDoc ex){
365                 pes.fireHandleProgressEvent(null,
366                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
367                                            ex.getLocalizedMessage(),
368                                            StateType.FAILED));
369
370             }
371         } else if(cmdType.equals(WS70CommandType.STOP)) {
372             try{
373                 runProcess(makeProcessString("stop"), true); //NO I18N
374
this.viewAdminLogs();
375                 pes.fireHandleProgressEvent(null,
376                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
377                                            NbBundle.getMessage(WS70StartServer.class, "MSG_ADMIN_SERVER_STOPPED"),
378                                            StateType.COMPLETED));
379             }catch(Exception JavaDoc ex){
380                 pes.fireHandleProgressEvent(null,
381                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
382                                            ex.getLocalizedMessage(),
383                                            StateType.FAILED));
384
385             }
386         }else if(cmdType.equals(WS70CommandType.STARTTARGET)) {
387             try{
388                 String JavaDoc configName = this.getConfigNameFromTarget(target);
389                 dm.startServer(configName);
390                 pes.fireHandleProgressEvent(null,
391                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
392                                            NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_STARTED"),
393                                            StateType.COMPLETED));
394             }catch(Exception JavaDoc ex){
395                 pes.fireHandleProgressEvent(null,
396                                  new Status JavaDoc(ActionType.EXECUTE, cmdType,
397                                             ex.getLocalizedMessage(),
398                                             StateType.FAILED));
399             }
400             
401         }else if(cmdType.equals(WS70CommandType.STOPTARGET)) {
402             try{
403                 String JavaDoc configName = this.getConfigNameFromTarget(target);
404                 dm.stopServer(configName);
405                 pes.fireHandleProgressEvent(null,
406                                 new Status JavaDoc(ActionType.EXECUTE, cmdType,
407                                            NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_STOPPED"),
408                                            StateType.COMPLETED));
409             }catch(Exception JavaDoc ex){
410                 pes.fireHandleProgressEvent(null,
411                                new Status JavaDoc(ActionType.EXECUTE, cmdType,
412                                           ex.getLocalizedMessage(),
413                                           StateType.FAILED));
414             }
415             
416         }else if(cmdType.equals(WS70CommandType.STARTTARGETDEBUG)) {
417             try{
418                 String JavaDoc configName = this.getConfigNameFromTarget(target);
419                 if(dm.isDebugModeEnabled()){
420                     if(!dm.isRunning(configName)){
421                         ErrorManager.getDefault().log(
422                             ErrorManager.INFORMATIONAL, NbBundle.getMessage(WS70StartServer.class, "MSG_STARTING_TARGET_SERVER_DEBUG"));
423                         //start server
424
dm.startServer(configName);
425                         pes.fireHandleProgressEvent(null,
426                                  new Status JavaDoc(ActionType.EXECUTE, cmdType,
427                                             NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_STARTED_DEBUG"),
428                                             StateType.COMPLETED));
429                     }else{
430                         pes.fireHandleProgressEvent(null,
431                                  new Status JavaDoc(ActionType.EXECUTE, cmdType,
432                                             NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_STARTED_DEBUG"),
433                                             StateType.COMPLETED));
434                     }
435                 }else{
436                     if(dm.isRunning(configName)){ //stop target
437
ErrorManager.getDefault().log(
438                             ErrorManager.INFORMATIONAL, NbBundle.getMessage(WS70StartServer.class, "MSG_STOPPING_TARGET_SERVER"));
439                         pes.fireHandleProgressEvent(null,
440                                  new Status JavaDoc(ActionType.EXECUTE, cmdType,
441                                             NbBundle.getMessage(WS70StartServer.class, "MSG_RESTARTING_TARGET_SERVER_DEBUG"),
442                                             StateType.RUNNING));
443                         dm.stopServer(configName);
444                         
445                     }
446                     // enable debug mode
447
ErrorManager.getDefault().log(
448                         ErrorManager.INFORMATIONAL, NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_SETTING_DEBUG_MODE"));
449                     dm.changeDebugStatus(configName, true);
450                     // start target
451
ErrorManager.getDefault().log(
452                         ErrorManager.INFORMATIONAL, NbBundle.getMessage(WS70StartServer.class, "MSG_STARTING_TARGET_SERVER_DEBUG"));
453                     dm.startServer(configName);
454                     pes.fireHandleProgressEvent(null,
455                              new Status JavaDoc(ActionType.EXECUTE, cmdType,
456                                         NbBundle.getMessage(WS70StartServer.class, "MSG_TARGET_SERVER_STARTED_DEBUG"),
457                                         StateType.COMPLETED));
458                     
459                 }
460             }catch(Exception JavaDoc ex){
461                 pes.fireHandleProgressEvent(null,
462                                         new Status JavaDoc(ActionType.EXECUTE, cmdType,
463                                                    ex.getLocalizedMessage(),
464                                                    StateType.FAILED));
465
466             }
467         }
468     }
469
470     /** JSR88 method. */
471     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc t) {
472         return null;
473     }
474
475     /** JSR88 method. */
476     public DeploymentStatus JavaDoc getDeploymentStatus() {
477         return pes.getDeploymentStatus();
478     }
479
480     /** JSR88 method. */
481     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
482         return new TargetModuleID JavaDoc [] {};
483     }
484
485     /** JSR88 method. */
486     public boolean isCancelSupported() {
487         return false;
488     }
489
490     /** JSR88 method. */
491     public void cancel() throws OperationUnsupportedException JavaDoc {
492         throw new OperationUnsupportedException JavaDoc("cancel not supported in WS deployment"); // NOI18N
493
}
494
495     /** JSR88 method. */
496     public boolean isStopSupported() {
497         return false;
498     }
499
500     /** JSR88 method. */
501     public void stop() throws OperationUnsupportedException JavaDoc {
502         throw new OperationUnsupportedException JavaDoc("stop not supported in WS deployment"); // NOI18N
503
}
504
505     /** JSR88 method. */
506     public void addProgressListener(ProgressListener JavaDoc l) {
507         pes.addProgressListener(l);
508     }
509     
510     /** JSR88 method. */
511     public void removeProgressListener(ProgressListener JavaDoc l) {
512         pes.removeProgressListener(l);
513     }
514   
515     private int runProcess(String JavaDoc str, boolean wait) throws Exception JavaDoc {
516         Process JavaDoc child = Runtime.getRuntime().exec(str);
517         if (wait)
518             child.waitFor();
519         return child.exitValue();
520     }
521     private String JavaDoc makeProcessString(String JavaDoc str) {
522         if (org.openide.util.Utilities.isWindows()){
523             return "net " + str + " " + "https-admserv70"; // NOI18N
524
}else{
525             String JavaDoc process = str+"serv";
526             return ((WS70SunDeploymentManager)dm).getServerLocation()+File.separator +
527                     "admin-server" + File.separator+"bin" + File.separator + process; //NO I18N
528
}
529     }
530     private void viewAdminLogs(){
531         String JavaDoc uri = dm.getUri();
532         String JavaDoc location = dm.getServerLocation();
533         location = location+File.separator+"admin-server"+
534                 File.separator+"logs"+File.separator+"errors";
535
536         WS70LogViewer logViewer = new WS70LogViewer(new File JavaDoc(location));
537         
538         try{
539             logViewer.showLogViewer(UISupport.getServerIO(uri));
540         }catch(Exception JavaDoc ex){
541             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
542         }
543     }
544     private String JavaDoc getConfigNameFromTarget(Target JavaDoc t) throws Exception JavaDoc{
545         try{
546             java.lang.reflect.Method JavaDoc getConfigName = t.getClass().getDeclaredMethod("getConfigName", new Class JavaDoc[]{});
547             return (String JavaDoc)getConfigName.invoke(t, new Object JavaDoc[]{});
548         }catch(Exception JavaDoc ex){
549             throw ex;
550         }
551     }
552 }
553
Popular Tags