blob: 046e67435854e39f059874c2a58c3c5b09c611a5 [file] [log] [blame]
gercanbe0fd052005-11-08 21:47:59 +00001/***************************************************************************************************
2 * Copyright (c) 2005 Eteration A.S. and Gorkem Ercan. All rights reserved. This program and the
3 * accompanying materials are made available under the terms of the Eclipse Public License v1.0
gercan53c00bb2005-05-18 19:54:52 +00004 * which accompanies this distribution, and is available at
gercanbe0fd052005-11-08 21:47:59 +00005 * http://www.eclipse.org/legal/epl-v10.html
gercan53c00bb2005-05-18 19:54:52 +00006 *
gercanbe0fd052005-11-08 21:47:59 +00007 * Contributors: Gorkem Ercan - initial API and implementation
8 *
9 **************************************************************************************************/
gercan66440342005-03-08 22:20:47 +000010package org.eclipse.jst.server.generic.core.internal;
gercan202cf232004-12-04 11:58:11 +000011
12import java.util.ArrayList;
gercana8a90f42005-08-30 21:47:29 +000013import java.util.HashMap;
gercan202cf232004-12-04 11:58:11 +000014import java.util.Iterator;
15import java.util.List;
gercana8a90f42005-08-30 21:47:29 +000016import java.util.Map;
gercan202cf232004-12-04 11:58:11 +000017import org.eclipse.core.runtime.CoreException;
gercan202cf232004-12-04 11:58:11 +000018import org.eclipse.core.runtime.IProgressMonitor;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.NullProgressMonitor;
21import org.eclipse.core.runtime.Path;
22import org.eclipse.core.runtime.Status;
23import org.eclipse.debug.core.DebugEvent;
24import org.eclipse.debug.core.DebugPlugin;
25import org.eclipse.debug.core.IDebugEventSetListener;
26import org.eclipse.debug.core.ILaunch;
27import org.eclipse.debug.core.ILaunchConfiguration;
28import org.eclipse.debug.core.ILaunchConfigurationType;
29import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
30import org.eclipse.debug.core.ILaunchManager;
31import org.eclipse.debug.core.model.IProcess;
32import org.eclipse.debug.ui.IDebugUIConstants;
33import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
34import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
35import org.eclipse.jdt.launching.IVMInstall;
36import org.eclipse.jdt.launching.JavaRuntime;
gercan202cf232004-12-04 11:58:11 +000037import org.eclipse.jst.server.generic.servertype.definition.ArchiveType;
gercana8a90f42005-08-30 21:47:29 +000038import org.eclipse.jst.server.generic.servertype.definition.ArgumentPair;
gercan202cf232004-12-04 11:58:11 +000039import org.eclipse.jst.server.generic.servertype.definition.Classpath;
gercana8a90f42005-08-30 21:47:29 +000040import org.eclipse.jst.server.generic.servertype.definition.LaunchConfiguration;
gercan202cf232004-12-04 11:58:11 +000041import org.eclipse.jst.server.generic.servertype.definition.ServerRuntime;
gercand6f4c0d2006-02-20 18:48:49 +000042import org.eclipse.osgi.util.NLS;
gercan202cf232004-12-04 11:58:11 +000043import org.eclipse.wst.server.core.IModule;
44import org.eclipse.wst.server.core.IServer;
gercan4b6c9a62005-03-23 06:08:30 +000045import org.eclipse.wst.server.core.ServerPort;
gercan8995a6c2005-11-17 20:42:39 +000046import org.eclipse.wst.server.core.internal.DeletedModule;
gercan202cf232004-12-04 11:58:11 +000047import org.eclipse.wst.server.core.model.ServerBehaviourDelegate;
48import org.eclipse.wst.server.core.model.ServerDelegate;
gercan202cf232004-12-04 11:58:11 +000049import org.eclipse.wst.server.core.util.SocketUtil;
50
51/**
gercande114e12004-12-06 23:07:27 +000052 * Server behaviour delegate implementation for generic server.
gercan202cf232004-12-04 11:58:11 +000053 *
54 * @author Gorkem Ercan
55 */
56public class GenericServerBehaviour extends ServerBehaviourDelegate {
57
gercan8995a6c2005-11-17 20:42:39 +000058 private static final String ATTR_STOP = "stop-server"; //$NON-NLS-1$
gercan202cf232004-12-04 11:58:11 +000059
60 // the thread used to ping the server to check for startup
61 protected transient PingThread ping = null;
62 protected transient IDebugEventSetListener processListener;
63 protected transient IProcess process;
64
65 /* (non-Javadoc)
66 * @see org.eclipse.wst.server.core.model.ServerBehaviourDelegate#publishServer(org.eclipse.core.runtime.IProgressMonitor)
67 */
gercanc380c802005-02-02 21:20:40 +000068 public void publishServer(int kind, IProgressMonitor monitor) throws CoreException {
gercan53c00bb2005-05-18 19:54:52 +000069 // do nothing
gercan202cf232004-12-04 11:58:11 +000070 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.wst.server.core.model.ServerBehaviourDelegate#publishModule(org.eclipse.wst.server.core.IModule[], org.eclipse.wst.server.core.IModule, org.eclipse.core.runtime.IProgressMonitor)
74 */
gercan980ddcb2005-03-16 17:58:09 +000075 public void publishModule(int kind, int deltaKind, IModule[] module,
gercan202cf232004-12-04 11:58:11 +000076 IProgressMonitor monitor) throws CoreException {
gercand7422432005-01-30 21:54:27 +000077
gercan7c052dd2006-06-04 19:01:29 +000078
gercan4b6c9a62005-03-23 06:08:30 +000079 if(REMOVED == deltaKind){
gercan1a498772005-03-07 22:07:00 +000080 removeFromServer(module,monitor);
gercand7422432005-01-30 21:54:27 +000081 }
gercan1a498772005-03-07 22:07:00 +000082 else{
gercan7c052dd2006-06-04 19:01:29 +000083 checkClosed(module);
gercan68b3acf2005-10-24 18:40:50 +000084 String publisherId = ServerTypeDefinitionUtil.getPublisherID(module[0], getServerDefinition());
gercan1a498772005-03-07 22:07:00 +000085 GenericPublisher publisher = PublishManager.getPublisher(publisherId);
86 if(publisher==null){
gercand6f4c0d2006-02-20 18:48:49 +000087 IStatus status = new Status(IStatus.ERROR,CorePlugin.PLUGIN_ID,0, NLS.bind(GenericServerCoreMessages.unableToCreatePublisher,publisherId),null);
gercan1a498772005-03-07 22:07:00 +000088 throw new CoreException(status);
89 }
gercan91da9202005-06-22 20:44:10 +000090 publisher.initialize(module,getServer());
gercan1a498772005-03-07 22:07:00 +000091 IStatus[] status= publisher.publish(null,monitor);
gercan726fc722005-07-18 18:34:17 +000092 if(status==null){
gercan980ddcb2005-03-16 17:58:09 +000093 setModulePublishState(module, IServer.PUBLISH_STATE_NONE);
gercan726fc722005-07-18 18:34:17 +000094 }else {
95 for (int i=0; i < status.length; i++) {
96 if (IStatus.ERROR == status[i].getSeverity()){
97 setModulePublishState(module, IServer.PUBLISH_STATE_UNKNOWN);
98 throw new CoreException(status[i]);
99 }
100 }
101 }
gercan1a498772005-03-07 22:07:00 +0000102 }
gercan202cf232004-12-04 11:58:11 +0000103 }
104
gercan8995a6c2005-11-17 20:42:39 +0000105 private void checkClosed(IModule[] module) throws CoreException
106 {
107 for(int i=0;i<module.length;i++)
108 {
109 if(module[i] instanceof DeletedModule)
110 {
gercand6f4c0d2006-02-20 18:48:49 +0000111 IStatus status = new Status(IStatus.ERROR,CorePlugin.PLUGIN_ID,0, NLS.bind(GenericServerCoreMessages.canNotPublishDeletedModule,module[i].getName()),null);
gercan8995a6c2005-11-17 20:42:39 +0000112 throw new CoreException(status);
113 }
114 }
115 }
gercan980ddcb2005-03-16 17:58:09 +0000116 private void removeFromServer(IModule[] module, IProgressMonitor monitor) throws CoreException
gercan1a498772005-03-07 22:07:00 +0000117 {
gercan68b3acf2005-10-24 18:40:50 +0000118 String publisherId = ServerTypeDefinitionUtil.getPublisherID(module[0], getServerDefinition());
gercan1a498772005-03-07 22:07:00 +0000119 GenericPublisher publisher = PublishManager.getPublisher(publisherId);
120 if(publisher==null){
gercand6f4c0d2006-02-20 18:48:49 +0000121 IStatus status = new Status(IStatus.ERROR,CorePlugin.PLUGIN_ID,0,NLS.bind(GenericServerCoreMessages.unableToCreatePublisher,publisherId),null);
gercan1a498772005-03-07 22:07:00 +0000122 throw new CoreException(status);
123 }
gercan91da9202005-06-22 20:44:10 +0000124 publisher.initialize(module,getServer());
gercan1a498772005-03-07 22:07:00 +0000125 publisher.unpublish(monitor);
126 }
127
128
gercan202cf232004-12-04 11:58:11 +0000129 /* (non-Javadoc)
130 * @see org.eclipse.wst.server.core.model.ServerBehaviourDelegate#stop(boolean)
131 */
gercan202cf232004-12-04 11:58:11 +0000132 public void stop(boolean force) {
133 if (force) {
134 terminate();
135 return;
136 }
137
138 int state = getServer().getServerState();
139 if (state == IServer.STATE_STOPPED)
140 return;
141 else if (state == IServer.STATE_STARTING || state == IServer.STATE_STOPPING) {
142 terminate();
143 return;
144 }
gercanc7334922005-06-16 15:51:50 +0000145
146 shutdown(state);
147 }
148
149 /**
150 * Shuts down the server via the launch configuration.
151 */
152 protected void shutdown(int state) {
gercanf97307f2005-11-16 21:54:19 +0000153 GenericServerRuntime runtime = getRuntimeDelegate();
gercan202cf232004-12-04 11:58:11 +0000154 try {
gercan8995a6c2005-11-17 20:42:39 +0000155 Trace.trace(Trace.FINEST, "Stopping Server"); //$NON-NLS-1$
gercan202cf232004-12-04 11:58:11 +0000156 if (state != IServer.STATE_STOPPED)
157 setServerState(IServer.STATE_STOPPING);
gercanc7334922005-06-16 15:51:50 +0000158 String configTypeID = getConfigTypeID();
gercan202cf232004-12-04 11:58:11 +0000159 ILaunchManager mgr = DebugPlugin.getDefault().getLaunchManager();
gercanc7334922005-06-16 15:51:50 +0000160 ILaunchConfigurationType type = mgr.getLaunchConfigurationType(configTypeID);
161 String launchName = getStopLaunchName();
162 String uniqueLaunchName = mgr.generateUniqueLaunchConfigurationNameFrom(launchName);
gercan202cf232004-12-04 11:58:11 +0000163 ILaunchConfiguration conf = null;
gercan202cf232004-12-04 11:58:11 +0000164 ILaunchConfiguration[] lch = mgr.getLaunchConfigurations(type);
165 for (int i = 0; i < lch.length; i++) {
166 if (launchName.equals(lch[i].getName())) {
167 conf = lch[i];
168 break;
169 }
170 }
171
172 ILaunchConfigurationWorkingCopy wc = null;
173 if (conf != null) {
174 wc = conf.getWorkingCopy();
175 } else {
176 wc = type.newInstance(null, uniqueLaunchName);
177 }
gercanc7334922005-06-16 15:51:50 +0000178
179 // To stop from appearing in history lists
180 wc.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true);
181 // Set the stop attribute so that we know we are stopping
gercan8995a6c2005-11-17 20:42:39 +0000182 wc.setAttribute(ATTR_STOP, "true"); //$NON-NLS-1$
gercanc7334922005-06-16 15:51:50 +0000183
184 // Setup the launch config for stopping the server
185 setupStopLaunchConfiguration(runtime, wc);
186
187 // Launch the stop launch config
gercan202cf232004-12-04 11:58:11 +0000188 wc.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
gercanc7334922005-06-16 15:51:50 +0000189
gercan202cf232004-12-04 11:58:11 +0000190 } catch (Exception e) {
gercan8995a6c2005-11-17 20:42:39 +0000191 Trace.trace(Trace.SEVERE, "Error stopping Server", e); //$NON-NLS-1$
gercan202cf232004-12-04 11:58:11 +0000192 }
gercan202cf232004-12-04 11:58:11 +0000193 }
194
gercanc7334922005-06-16 15:51:50 +0000195 /**
196 * Returns the String ID of the launch configuration type.
gercan51230b92006-04-02 21:15:34 +0000197 * @return id
gercanc7334922005-06-16 15:51:50 +0000198 */
199 protected String getConfigTypeID() {
200 return IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION;
201 }
202
203 /**
204 * Returns the String name of the stop launch configuration.
gercan51230b92006-04-02 21:15:34 +0000205 * @return launchname
gercanc7334922005-06-16 15:51:50 +0000206 */
207 protected String getStopLaunchName() {
gercan8995a6c2005-11-17 20:42:39 +0000208 return "GenericServerStopper"; //$NON-NLS-1$
gercanc7334922005-06-16 15:51:50 +0000209 }
210
gercanf97307f2005-11-16 21:54:19 +0000211 private boolean isRemote(){
212 return (getServer().getServerType().supportsRemoteHosts()&& !SocketUtil.isLocalhost(getServer().getHost()) );
213 }
gercanc7334922005-06-16 15:51:50 +0000214 /**
215 * Sets up the launch configuration for stopping the server.
gercanc7334922005-06-16 15:51:50 +0000216 */
217 protected void setupStopLaunchConfiguration(GenericServerRuntime runtime, ILaunchConfigurationWorkingCopy wc) {
gercanf97307f2005-11-16 21:54:19 +0000218 if(isRemote())// Do not launch for remote servers.
219 return;
220
gercanc7334922005-06-16 15:51:50 +0000221 wc.setAttribute(
222 IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
223 getServerDefinition().getResolver().resolveProperties(this.getServerDefinition().getStop().getMainClass()));
224
225 IVMInstall vmInstall = runtime.getVMInstall();
226 wc.setAttribute(
227 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, runtime
228 .getVMInstallTypeId());
229 wc.setAttribute(
230 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME,
231 vmInstall.getName());
232
233 setupLaunchClasspath(wc, vmInstall, getStopClasspath());
234
gercana8a90f42005-08-30 21:47:29 +0000235 Map environVars = getEnvironmentVariables(getServerDefinition().getStop());
236 if(!environVars.isEmpty()){
237 wc.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES,environVars);
238 }
239
gercanc7334922005-06-16 15:51:50 +0000240 wc.setAttribute(
241 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
242 getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStop().getWorkingDirectory()));
243 wc.setAttribute(
244 IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
gercana8a90f42005-08-30 21:47:29 +0000245 getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStop().getProgramArgumentsAsString()));
gercanc7334922005-06-16 15:51:50 +0000246 wc.setAttribute(
247 IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
gercana8a90f42005-08-30 21:47:29 +0000248 getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStop().getVmParametersAsString()));
gercanc7334922005-06-16 15:51:50 +0000249 }
250
gercan51230b92006-04-02 21:15:34 +0000251 /**
252 * Start class name
253 * @return name
254 */
gercan202cf232004-12-04 11:58:11 +0000255 public String getStartClassName() {
gercan2284dc22005-03-27 12:57:33 +0000256 return getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStart().getMainClass());
gercan202cf232004-12-04 11:58:11 +0000257 }
258
gercan51230b92006-04-02 21:15:34 +0000259 /**
260 * Server definition
261 * @return serverdef
262 */
gercan202cf232004-12-04 11:58:11 +0000263 public ServerRuntime getServerDefinition() {
gercan3a097732005-06-14 04:16:07 +0000264 GenericServer server = (GenericServer)getServer().loadAdapter(ServerDelegate.class, null);
gercan202cf232004-12-04 11:58:11 +0000265 return server.getServerDefinition();
266 }
267
gercanc7334922005-06-16 15:51:50 +0000268 protected GenericServerRuntime getRuntimeDelegate() {
gercan14d0e652005-06-12 19:35:28 +0000269 return (GenericServerRuntime)getServer().getRuntime().loadAdapter(GenericServerRuntime.class,null);
gercan202cf232004-12-04 11:58:11 +0000270 }
271
gercan202cf232004-12-04 11:58:11 +0000272 private List getStartClasspath() {
273 String cpRef = getServerDefinition().getStart().getClasspathReference();
274 return serverClasspath(cpRef);
275 }
276
277 /**
278 * @param cpRef
gercan51230b92006-04-02 21:15:34 +0000279 * @return classpath
gercan202cf232004-12-04 11:58:11 +0000280 */
gercan412f7272005-05-26 22:28:45 +0000281 protected List serverClasspath(String cpRef) {
gercan202cf232004-12-04 11:58:11 +0000282 Classpath classpath = getServerDefinition().getClasspath(cpRef);
283
gercan2dfb4882005-04-18 19:32:07 +0000284 List cpEntryList = new ArrayList(classpath.getArchive().size());
gercan202cf232004-12-04 11:58:11 +0000285 Iterator iterator= classpath.getArchive().iterator();
286 while(iterator.hasNext())
287 {
288 ArchiveType archive = (ArchiveType)iterator.next();
289 String cpath = getServerDefinition().getResolver().resolveProperties(archive.getPath());
gercan2dfb4882005-04-18 19:32:07 +0000290
291 cpEntryList.add(JavaRuntime.newArchiveRuntimeClasspathEntry(
292 new Path(cpath)));
293 }
294 return cpEntryList;
gercan202cf232004-12-04 11:58:11 +0000295 }
296
297 /**
298 * @param wc
299 * @param vmInstall
300 */
gercan412f7272005-05-26 22:28:45 +0000301 protected void setupLaunchClasspath(ILaunchConfigurationWorkingCopy wc, IVMInstall vmInstall, List cp) {
gercan2dfb4882005-04-18 19:32:07 +0000302 //merge existing classpath with server classpath
303 try {
304 IRuntimeClasspathEntry[] existingCps = JavaRuntime.computeUnresolvedRuntimeClasspath(wc);
305 for (int i = 0; i < existingCps.length; i++) {
306 if(cp.contains(existingCps[i])==false){
307 cp.add(existingCps[i]);
308 }
309 }
310 } catch (CoreException e) {
311 // ignore
312 }
313
314 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, convertCPEntryToMemento(cp));
315 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,false);
316 }
317
318 private List convertCPEntryToMemento(List cpEntryList)
319 {
320 List list = new ArrayList(cpEntryList.size());
321 Iterator iterator = cpEntryList.iterator();
322 while(iterator.hasNext())
323 {
324 IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)iterator.next();
325 try {
326 list.add(entry.getMemento());
327 } catch (CoreException e) {
328 // ignore
329 }
330 }
331 return list;
332 }
gercan202cf232004-12-04 11:58:11 +0000333
334 private String getWorkingDirectory() {
335 return getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStart().getWorkingDirectory());
336 }
337
gercanfbe9cb42006-01-21 13:14:53 +0000338 protected String getProgramArguments() {
gercana8a90f42005-08-30 21:47:29 +0000339 return getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStart().getProgramArgumentsAsString());
gercan202cf232004-12-04 11:58:11 +0000340 }
341
gercana8a90f42005-08-30 21:47:29 +0000342 protected Map getEnvironmentVariables(LaunchConfiguration config){
343 List variables = config.getEnvironmentVariable();
344 Map varsMap = new HashMap(variables.size());
345 Iterator iterator= variables.iterator();
346 while(iterator.hasNext()){
347 ArgumentPair pair = (ArgumentPair)iterator.next();
348 varsMap.put(pair.getName(),getServerDefinition().getResolver().resolveProperties(pair.getValue()));
349 }
350 return varsMap;
351 }
352
gercan202cf232004-12-04 11:58:11 +0000353 private String getVmArguments() {
gercana8a90f42005-08-30 21:47:29 +0000354 return getServerDefinition().getResolver().resolveProperties(getServerDefinition().getStart().getVmParametersAsString());
gercan202cf232004-12-04 11:58:11 +0000355 }
356
gercanc7334922005-06-16 15:51:50 +0000357 public void setupLaunchConfiguration(ILaunchConfigurationWorkingCopy workingCopy, IProgressMonitor monitor) throws CoreException {
gercanf97307f2005-11-16 21:54:19 +0000358 if(isRemote())// No launch for remote servers.
359 return;
360
361 workingCopy.setAttribute(
gercand7422432005-01-30 21:54:27 +0000362 IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
363 getStartClassName());
364
gercanf97307f2005-11-16 21:54:19 +0000365 GenericServerRuntime runtime = getRuntimeDelegate();
gercand7422432005-01-30 21:54:27 +0000366
367 IVMInstall vmInstall = runtime.getVMInstall();
368 workingCopy.setAttribute(
369 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, runtime
370 .getVMInstallTypeId());
371 workingCopy.setAttribute(
372 IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME,
373 vmInstall.getName());
374
375 setupLaunchClasspath(workingCopy, vmInstall, getStartClasspath());
376
377
378 workingCopy.setAttribute(
379 IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
380 getWorkingDirectory());
gercanec6ef212005-02-17 06:14:42 +0000381
gercana8a90f42005-08-30 21:47:29 +0000382
383 Map environVars = getEnvironmentVariables(getServerDefinition().getStart());
384 if(!environVars.isEmpty()){
385 workingCopy.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES,environVars);
386 }
387
gercanec6ef212005-02-17 06:14:42 +0000388 String existingProgArgs = workingCopy.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, (String)null);
389 String serverProgArgs = getProgramArguments();
390 if(existingProgArgs==null || existingProgArgs.indexOf(serverProgArgs)<0) {
391 workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,serverProgArgs);
392 }
393 String existingVMArgs = workingCopy.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,(String)null);
394 String serverVMArgs= getVmArguments();
395 if(existingVMArgs==null || existingVMArgs.indexOf(serverVMArgs)<0) {
396 workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,serverVMArgs);
397 }
gercan879fdab2005-06-13 21:30:14 +0000398 }
gercan202cf232004-12-04 11:58:11 +0000399
gercanc7334922005-06-16 15:51:50 +0000400 /**
gercan714985d2006-04-29 21:08:06 +0000401 * Setup for starting the server. Checks all ports available
402 * and sets server state and mode.
gercanc7334922005-06-16 15:51:50 +0000403 *
404 * @param launch ILaunch
405 * @param launchMode String
406 * @param monitor IProgressMonitor
407 */
408 protected void setupLaunch(ILaunch launch, String launchMode, IProgressMonitor monitor) throws CoreException {
gercan8995a6c2005-11-17 20:42:39 +0000409 if ("true".equals(launch.getLaunchConfiguration().getAttribute(ATTR_STOP, "false"))) //$NON-NLS-1$ //$NON-NLS-2$
gercanc7334922005-06-16 15:51:50 +0000410 return;
gercane231f4f2005-08-10 20:08:06 +0000411
gercand9cdddc2005-10-06 03:41:05 +0000412 String host = getServer().getHost();
gercanc7334922005-06-16 15:51:50 +0000413 ServerPort[] ports = getServer().getServerPorts(null);
414 ServerPort sp = null;
gercana479bb42006-05-13 22:01:37 +0000415 if(SocketUtil.isLocalhost(host)){
gercand9cdddc2005-10-06 03:41:05 +0000416 for(int i=0;i<ports.length;i++){
417 sp= ports[i];
418 if (SocketUtil.isPortInUse(ports[i].getPort(), 5))
gercand6f4c0d2006-02-20 18:48:49 +0000419 throw new CoreException(new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID, 0, NLS.bind(GenericServerCoreMessages.errorPortInUse,Integer.toString(sp.getPort()),sp.getName()),null));
gercand9cdddc2005-10-06 03:41:05 +0000420 }
gercan202cf232004-12-04 11:58:11 +0000421 }
gercanc7334922005-06-16 15:51:50 +0000422 setServerState(IServer.STATE_STARTING);
423 setMode(launchMode);
gercan714985d2006-04-29 21:08:06 +0000424 }
425 /**
426 * Call to start Ping thread that will check for startup of the server.
427 *
428 */
429 protected void startPingThread()
430 {
gercanc7334922005-06-16 15:51:50 +0000431 try {
gercan714985d2006-04-29 21:08:06 +0000432 String url = "http://"+getServer().getHost();; //$NON-NLS-1$
433 ServerPort[] ports = getServer().getServerPorts(null);
434 ServerPort sp = null;
435 for(int i=0;i<ports.length;i++){
436 if(ports[i].getProtocol().equalsIgnoreCase("http")){//$NON-NLS-1$
437 sp=ports[i];
438 }
439 }
440 if(sp==null){
441 Trace.trace(Trace.SEVERE, "Can't ping for server startup."); //$NON-NLS-1$
442 return;
443 }
444 int port = sp.getPort();
gercanc7334922005-06-16 15:51:50 +0000445 if (port != 80)
gercan8995a6c2005-11-17 20:42:39 +0000446 url += ":" + port; //$NON-NLS-1$
gercanc5d7c032005-06-21 22:20:17 +0000447 ping = new PingThread(getServer(), url, this);
gercanc7334922005-06-16 15:51:50 +0000448 } catch (Exception e) {
gercan8995a6c2005-11-17 20:42:39 +0000449 Trace.trace(Trace.SEVERE, "Can't ping for server startup."); //$NON-NLS-1$
gercan714985d2006-04-29 21:08:06 +0000450 }
gercanc7334922005-06-16 15:51:50 +0000451 }
gercan714985d2006-04-29 21:08:06 +0000452
gercan412f7272005-05-26 22:28:45 +0000453 protected void setProcess(final IProcess newProcess) {
gercan202cf232004-12-04 11:58:11 +0000454 if (process != null)
455 return;
gercancbfe4a52005-06-05 21:37:17 +0000456 if(processListener!=null)
457 DebugPlugin.getDefault().removeDebugEventListener(processListener);
458 if (newProcess==null)
459 return;
gercan202cf232004-12-04 11:58:11 +0000460 process = newProcess;
461 processListener = new IDebugEventSetListener() {
462 public void handleDebugEvents(DebugEvent[] events) {
463 if (events != null) {
464 int size = events.length;
465 for (int i = 0; i < size; i++) {
gercancbfe4a52005-06-05 21:37:17 +0000466 if (process!= null && process.equals(events[i].getSource()) && events[i].getKind() == DebugEvent.TERMINATE) {
gercan202cf232004-12-04 11:58:11 +0000467 DebugPlugin.getDefault().removeDebugEventListener(this);
468 stopImpl();
469 }
470 }
471 }
472 }
473 };
474 DebugPlugin.getDefault().addDebugEventListener(processListener);
475 }
476
477 protected void stopImpl() {
478 if (ping != null) {
gercan53c00bb2005-05-18 19:54:52 +0000479 ping.stop();
gercan202cf232004-12-04 11:58:11 +0000480 ping = null;
481 }
482 if (process != null) {
483 process = null;
484 DebugPlugin.getDefault().removeDebugEventListener(processListener);
485 processListener = null;
486 }
487 setServerState(IServer.STATE_STOPPED);
488 }
489
490 /**
491 * Terminates the server.
gercane231f4f2005-08-10 20:08:06 +0000492 * This method may be called before a process created while setting up the
493 * launch config.
gercan202cf232004-12-04 11:58:11 +0000494 */
gercanc7334922005-06-16 15:51:50 +0000495 protected void terminate() {
gercan202cf232004-12-04 11:58:11 +0000496 if (getServer().getServerState() == IServer.STATE_STOPPED)
497 return;
498
499 try {
500 setServerState(IServer.STATE_STOPPING);
gercan8995a6c2005-11-17 20:42:39 +0000501 Trace.trace(Trace.FINEST, "Killing the Server process"); //$NON-NLS-1$
gercan202cf232004-12-04 11:58:11 +0000502 if (process != null && !process.isTerminated()) {
503 process.terminate();
gercane231f4f2005-08-10 20:08:06 +0000504
gercan202cf232004-12-04 11:58:11 +0000505 }
gercane231f4f2005-08-10 20:08:06 +0000506 stopImpl();
gercan202cf232004-12-04 11:58:11 +0000507 } catch (Exception e) {
gercan8995a6c2005-11-17 20:42:39 +0000508 Trace.trace(Trace.SEVERE, "Error killing the process", e); //$NON-NLS-1$
gercan202cf232004-12-04 11:58:11 +0000509 }
510 }
511
512 private List getStopClasspath() {
513 String cpRef = getServerDefinition().getStop().getClasspathReference();
514 return serverClasspath(cpRef);
515 }
gercan7b2057b2005-06-14 04:53:15 +0000516
gercanec6ef212005-02-17 06:14:42 +0000517 public void publishFinish(IProgressMonitor monitor) throws CoreException {
518 IModule[] modules = this.getServer().getModules();
519 boolean allpublished= true;
520 for (int i = 0; i < modules.length; i++) {
gercan6400a652005-07-08 22:25:46 +0000521 if(this.getServer().getModulePublishState(new IModule[]{modules[i]})!=IServer.PUBLISH_STATE_NONE)
gercanec6ef212005-02-17 06:14:42 +0000522 allpublished=false;
523 }
524 if(allpublished)
525 setServerPublishState(IServer.PUBLISH_STATE_NONE);
526 }
gercan53c00bb2005-05-18 19:54:52 +0000527
528 protected void setServerStarted() {
529 setServerState(IServer.STATE_STARTED);
530 }
531}