1 19 package org.netbeans.modules.j2ee.jboss4.ide; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Map ; 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 ; 28 import java.io.File ; 29 import javax.enterprise.deploy.shared.ActionType ; 30 import javax.enterprise.deploy.shared.CommandType ; 31 import javax.enterprise.deploy.shared.StateType ; 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 ; 38 import javax.enterprise.deploy.spi.DeploymentManager ; 39 import javax.enterprise.deploy.spi.Target ; 40 import javax.enterprise.deploy.spi.TargetModuleID ; 41 import javax.enterprise.deploy.spi.status.ProgressEvent ; 42 import javax.enterprise.deploy.spi.status.ProgressListener ; 43 import javax.enterprise.deploy.spi.status.ProgressObject ; 44 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 45 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 46 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 47 import org.openide.util.NbBundle; 48 import org.netbeans.modules.j2ee.jboss4.nodes.Util; 49 50 54 public class JBStartServer extends StartServer implements ProgressObject { 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 isDebugModeUri = Collections.synchronizedMap((Map )new HashMap (2,1)); 64 65 public JBStartServer(DeploymentManager dm) { 66 if (!(dm instanceof JBDeploymentManager)) { 67 throw new IllegalArgumentException (""); 68 } 69 this.dm = (JBDeploymentManager) dm; 70 } 71 72 private void addDebugModeUri() { 73 isDebugModeUri.put(dm.getUrl(), new Object ()); 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 startDebugging(Target target) { 85 String 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))); 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 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 target) { 104 return true; 105 } 106 107 public boolean supportsStartProfiling(Target target) { 108 return true; 109 } 110 111 public boolean isAlsoTargetServer(Target target) { 112 return true; 113 } 114 115 public ServerDebugInfo getDebugInfo(Target target) { 116 return new ServerDebugInfo("localhost", dm.getDebuggingPort()); 117 } 118 119 122 public ProgressObject startProfiling(Target target, ProfilerServerSettings settings) { 123 String 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))); 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 137 public boolean supportsStartDeploymentManager() { 138 return true; 139 } 140 141 144 public ProgressObject stopDeploymentManager() { 145 String 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))); RequestProcessor.getDefault().post(new JBStopRunnable(dm, this), 0, Thread.NORM_PRIORITY); 148 removeDebugModeUri(); 149 return this; 150 } 151 152 155 public ProgressObject startDeploymentManager() { 156 String 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))); 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; } 182 SafeTrueTest test = new SafeTrueTest() { 184 public void run() { 185 ClassLoader oldLoader = null; 186 String checkingConfigName = ip.getProperty(JBPluginProperties.PROPERTY_SERVER); 187 String checkingServerDir = null; 188 189 try { 190 String serverDir = ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR); 191 192 if(serverDir == null) { 193 result = false; 194 return; 195 } 196 197 checkingServerDir = new File (serverDir).getCanonicalPath(); 198 } catch (IllegalStateException ex) { 199 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 200 result = false; 201 } catch (IOException ex) { 202 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 203 result = false; 204 } 205 206 Object serverName = Util.getMBeanParameter(dm, "ServerName", "jboss.system:type=ServerConfig"); 207 Object 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 )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 225 private abstract static class SafeTrueTest implements Runnable { 226 protected boolean result = false; 227 228 public abstract void run(); 229 230 public final boolean result() { 231 return result; 232 } 233 }; 234 235 236 private boolean safeTrueTest(SafeTrueTest test, int timeout) { 237 try { 238 new RequestProcessor().post(test).waitFinished(timeout); 239 } catch (InterruptedException ie) { 240 } 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; } 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 private Vector listeners = new Vector (); 264 private DeploymentStatus deploymentStatus; 265 266 public void addProgressListener(ProgressListener pl) { 267 listeners.add(pl); 268 } 269 270 public void removeProgressListener(ProgressListener pl) { 271 listeners.remove(pl); 272 } 273 274 public void stop() throws OperationUnsupportedException { 275 throw new OperationUnsupportedException (""); 276 } 277 278 public boolean isStopSupported() { 279 return false; 280 } 281 282 public void cancel() throws OperationUnsupportedException { 283 throw new OperationUnsupportedException (""); 284 } 285 286 public boolean isCancelSupported() { 287 return false; 288 } 289 290 public ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 291 return null; 292 } 293 294 public TargetModuleID [] getResultTargetModuleIDs() { 295 return new TargetModuleID []{}; 296 } 297 298 public DeploymentStatus getDeploymentStatus() { 299 return deploymentStatus; 300 } 301 302 303 public void fireHandleProgressEvent(TargetModuleID targetModuleID, DeploymentStatus deploymentStatus) { 304 ProgressEvent evt = new ProgressEvent (this, targetModuleID, deploymentStatus); 305 306 this.deploymentStatus = deploymentStatus; 307 308 java.util.Vector targets = null; 309 synchronized (this) { 310 if (listeners != null) { 311 targets = (java.util.Vector ) listeners.clone(); 312 } 313 } 314 315 if (targets != null) { 316 for (int i = 0; i < targets.size(); i++) { 317 ProgressListener target = (ProgressListener )targets.elementAt(i); 318 target.handleProgressEvent(evt); 319 } 320 } 321 } 322 323 MODE getMode() { 324 return mode; 325 } 326 327 328 } 329 | Popular Tags |