KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > ide > OC4JStartServer


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.oc4j.ide;
21
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26 import javax.enterprise.deploy.shared.ActionType JavaDoc;
27 import javax.enterprise.deploy.shared.CommandType JavaDoc;
28 import javax.enterprise.deploy.shared.StateType JavaDoc;
29 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
30 import javax.enterprise.deploy.spi.Target JavaDoc;
31 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
32 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException JavaDoc;
33 import javax.enterprise.deploy.spi.status.ClientConfiguration JavaDoc;
34 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
35 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
36 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
37 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
38 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
39 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
40 import org.netbeans.modules.j2ee.deployment.plugins.api.StartServer;
41 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager;
42 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties;
43 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils;
44 import org.openide.util.NbBundle;
45 import org.openide.util.RequestProcessor;
46
47 /**
48  *
49  * @author pblaha
50  */

51 public class OC4JStartServer extends StartServer implements ProgressObject JavaDoc {
52     
53     static enum MODE { RUN, DEBUG };
54     private MODE mode;
55     private DeploymentStatus JavaDoc deploymentStatus;
56     private OC4JDeploymentManager dm;
57     private String JavaDoc serverName;
58     private String JavaDoc serverHome;
59     private Vector JavaDoc <ProgressListener JavaDoc> listeners = new Vector JavaDoc<ProgressListener JavaDoc>();
60     private InstanceProperties ip;
61     private static Map JavaDoc isDebugModeUri = Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
62     private String JavaDoc url;
63     
64     public OC4JStartServer(DeploymentManager JavaDoc dm) {
65         if (!(dm instanceof OC4JDeploymentManager)) {
66             throw new IllegalArgumentException JavaDoc("Only OC4JDeplomentManager is supported"); //NOI18N
67
}
68         this.dm = (OC4JDeploymentManager) dm;
69         this.ip = ((OC4JDeploymentManager)dm).getProperties().getInstanceProperties();
70         serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
71         serverHome = ip.getProperty(OC4JPluginProperties.PROPERTY_OC4J_HOME);
72         url = ip.getProperty(InstanceProperties.URL_ATTR);
73     }
74     
75     public boolean supportsStartDebugging(Target JavaDoc target) {
76         return OC4JPluginUtils.isLocalServer(ip);
77     }
78     
79     public InstanceProperties getInstanceProperties() {
80         return ip;
81     }
82     
83     public ProgressObject JavaDoc startDebugging(Target JavaDoc target) {
84         mode = MODE.DEBUG;
85         String JavaDoc user = "oc4jadmin";
86         String JavaDoc serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
87         fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING,
88                 NbBundle.getMessage(OC4JStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName)));//NOI18N
89

90         // Check if the server is initialized
91
if(!OC4JPluginUtils.isUserActivated(serverHome, user)) {
92             String JavaDoc password = null;
93             
94             while(password == null || password.equals("")) {
95                 password = OC4JPluginUtils.requestPassword(user);
96             }
97             
98             if(!OC4JPluginUtils.activateUser(serverHome, user, password)) {
99                 fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.FAILED,
100                         NbBundle.getMessage(OC4JStartServer.class, "MSG_START_SERVER_NOT_INITIALIZED", serverName))); //NOI18N
101

102                 return this;
103             }
104         }
105         
106         RequestProcessor.getDefault().post(new OC4JStartRunnable(dm, this), 0, Thread.NORM_PRIORITY);
107         addDebugModeUri();
108         return this;
109     }
110     
111     private void addDebugModeUri() {
112         isDebugModeUri.put(url, new Object JavaDoc());
113     }
114     
115     private void removeDebugModeUri() {
116         isDebugModeUri.remove(url);
117     }
118     
119     private boolean existsDebugModeUri() {
120         return isDebugModeUri.containsKey(url);
121     }
122     
123     public boolean isDebuggable(Target JavaDoc target) {
124         if (!existsDebugModeUri()) {
125             return false;
126         }
127         if (!isRunning()) {
128             return false;
129         }
130         return true;
131     }
132     
133     public boolean isAlsoTargetServer(Target JavaDoc target) {
134         return true;
135     }
136     
137     public ServerDebugInfo getDebugInfo(Target JavaDoc target) {
138         return new ServerDebugInfo(ip.getProperty(OC4JPluginProperties.PROPERTY_HOST), dm.getProperties().getDebugPort());
139     }
140     
141     public boolean supportsStartDeploymentManager() {
142         return OC4JPluginUtils.isLocalServer(ip);
143     }
144     
145     public ProgressObject JavaDoc stopDeploymentManager() {
146         fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING,
147                 NbBundle.getMessage(OC4JStartServer.class, "MSG_STOP_SERVER_IN_PROGRESS", serverName)));//NOI18N
148
RequestProcessor.getDefault().post(new OC4JStopRunnable(dm, this), 0, Thread.NORM_PRIORITY);
149         removeDebugModeUri();
150         return this;
151     }
152     
153     // start server
154
public ProgressObject JavaDoc startDeploymentManager() {
155         mode = MODE.RUN;
156         String JavaDoc user = "oc4jadmin";
157         String JavaDoc serverName = ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
158         fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.RUNNING,
159                 NbBundle.getMessage(OC4JStartServer.class, "MSG_START_SERVER_IN_PROGRESS", serverName)));//NOI18N
160

161         // Check if the server is initialized
162
if(!OC4JPluginUtils.isUserActivated(serverHome, user)) {
163             String JavaDoc password = null;
164             
165             while(password == null || password.equals("")) {
166                 password = OC4JPluginUtils.requestPassword(user);
167             }
168             
169             if(!OC4JPluginUtils.activateUser(serverHome, user, password)) {
170                 fireHandleProgressEvent(null, new OC4JDeploymentStatus(ActionType.EXECUTE, CommandType.START, StateType.FAILED,
171                         NbBundle.getMessage(OC4JStartServer.class, "MSG_START_SERVER_NOT_INITIALIZED", serverName))); //NOI18N
172

173                 return this;
174             }
175         }
176         
177         RequestProcessor.getDefault().post(new OC4JStartRunnable(dm, this), 0, Thread.NORM_PRIORITY);
178         removeDebugModeUri();
179         return this;
180     }
181     
182     public boolean needsStartForTargetList() {
183         return false;
184     }
185     
186     public boolean needsStartForConfigure() {
187         return false;
188     }
189     
190     public boolean needsStartForAdminConfig() {
191         return false;
192     }
193     
194     public boolean isRunning() {
195         return OC4JPluginProperties.isRunning(ip.getProperty(OC4JPluginProperties.PROPERTY_HOST),
196                 ip.getProperty(InstanceProperties.HTTP_PORT_NUMBER));
197     }
198     
199     public DeploymentStatus JavaDoc getDeploymentStatus() {
200         return deploymentStatus;
201     }
202     
203     public TargetModuleID JavaDoc[] getResultTargetModuleIDs() {
204         return null;
205     }
206     
207     public ClientConfiguration JavaDoc getClientConfiguration(TargetModuleID JavaDoc targetModuleID) {
208         return null;
209     }
210     
211     public boolean isCancelSupported() {
212         return false;
213     }
214     
215     public void cancel() throws OperationUnsupportedException JavaDoc {
216     }
217     
218     public boolean isStopSupported() {
219         return OC4JPluginUtils.isLocalServer(ip);
220     }
221     
222     public void stop() throws OperationUnsupportedException JavaDoc {
223     }
224     
225     public void addProgressListener(ProgressListener JavaDoc progressListener) {
226         listeners.add(progressListener);
227     }
228     
229     public void removeProgressListener(ProgressListener JavaDoc progressListener) {
230         listeners.remove(progressListener);
231     }
232     
233     public void fireHandleProgressEvent(TargetModuleID JavaDoc targetModuleID, DeploymentStatus JavaDoc deploymentStatus) {
234         ProgressEvent JavaDoc evt = new ProgressEvent JavaDoc(this, targetModuleID, deploymentStatus);
235         this.deploymentStatus = deploymentStatus;
236         
237         java.util.Vector JavaDoc targets = null;
238         synchronized (this) {
239             if (listeners != null) {
240                 targets = (java.util.Vector JavaDoc) listeners.clone();
241             }
242         }
243         
244         if (targets != null) {
245             for (int i = 0; i < targets.size(); i++) {
246                 ProgressListener JavaDoc target = (ProgressListener JavaDoc)targets.elementAt(i);
247                 target.handleProgressEvent(evt);
248             }
249         }
250     }
251     
252     MODE getMode() {
253         return mode;
254     }
255 }
Popular Tags