[jboss-cvs] jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/runtime/server/polling ...

Robert Stryker rawblem at gmail.com
Wed Nov 15 17:26:55 EST 2006


  User: rawb    
  Date: 06/11/15 17:26:55

  Added:       as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/runtime/server/polling   
                        PollThread.java TwiddlePoller.java
                        TimeoutPoller.java
  Log:
  Rebuilt the event log API and fixed some UI bugs preventing an efficient refresh of the tree. 
  
  Revision  Changes    Path
  1.1      date: 2006/11/15 22:26:55;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/runtime/server/polling/PollThread.java
  
  Index: PollThread.java
  ===================================================================
  /**
   * JBoss, a Division of Red Hat
   * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
  * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.runtime.server.polling;
  
  import java.util.Date;
  
  import org.eclipse.wst.server.core.IServer;
  import org.eclipse.wst.server.core.internal.ServerType;
  import org.jboss.ide.eclipse.as.core.model.EventLogModel;
  import org.jboss.ide.eclipse.as.core.model.EventLogModel.EventLogRoot;
  import org.jboss.ide.eclipse.as.core.model.EventLogModel.EventLogTreeItem;
  import org.jboss.ide.eclipse.as.core.runtime.server.IServerStatePoller;
  import org.jboss.ide.eclipse.as.core.server.JBossServer;
  import org.jboss.ide.eclipse.as.core.server.JBossServerBehavior;
  import org.jboss.ide.eclipse.as.core.server.ServerAttributeHelper;
  import org.jboss.ide.eclipse.as.core.server.attributes.IServerPollingAttributes;
  import org.jboss.ide.eclipse.as.core.util.SimpleTreeItem;
  
  /**
   *
   * @author rob.stryker at jboss.com
   */
  public class PollThread extends Thread {
  	public static final String SERVER_STARTING = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.server.starting";
  	public static final String SERVER_STOPPING = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.server.stopping";
  	public static final String FAILURE = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.failure";
  	public static final String SUCCESS = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.success";
  	public static final String POLL_THREAD_ABORTED = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.aborted";
  	public static final String POLL_THREAD_TIMEOUT = "org.jboss.ide.eclipse.as.core.runtime.server.PollThread.timeout";
  	
  	public static final String EXPECTED_STATE = "org.jboss.ide.eclipse.as.core.runtime.server.PollThreadEvent.expectedState";
  
  	
  	private boolean expectedState;
  	private IServerStatePoller poller;
  	private boolean abort;
  	private JBossServerBehavior behavior;
  	private EventLogRoot eventRoot;
  	
  	private PollThreadEvent activeEvent;
  	
  	public PollThread(String name, IServerStatePoller poller, boolean expectedState, JBossServerBehavior behavior) {
  		super(name);
  		this.expectedState = expectedState;
  		this.poller = poller;
  		this.abort = false;
  		this.behavior = behavior;
  		eventRoot = EventLogModel.getModel(behavior.getServer()).getRoot();
  	}
  	
  	public void cancel() {
  		abort = true;
  	}
  
  	
  	// Getting the timeouts. First from plugin.xml as default, or from user settings.
  	public int getTimeout() {
  		int timeout;
  		JBossServer jbs = ((JBossServer)getServer().loadAdapter(JBossServer.class, null));
  		ServerAttributeHelper helper = (ServerAttributeHelper)jbs.getAttributeHelper();
  		if( expectedState == IServerStatePoller.SERVER_UP) {
  			int def = ((ServerType)getServer().getServerType()).getStartTimeout();
  			timeout = helper.getAttribute(IServerPollingAttributes.START_TIMEOUT, def);
  		} else {
  			int def = ((ServerType)getServer().getServerType()).getStopTimeout();
  			timeout = helper.getAttribute(IServerPollingAttributes.STOP_TIMEOUT, def);
  		}
  		return timeout;
  	}
  	
  	
  	public void run() {
  		int maxWait = getTimeout();
  		alertEventLogStarting();
  		
  		long startTime = new Date().getTime();
  		boolean done = false;
  		poller.beginPolling(getServer(), expectedState, this);
  		while( !abort && !done && new Date().getTime() < startTime + maxWait ) {
  			try {
  				Thread.sleep(100);
  				done = poller.isComplete();
  			} catch( InterruptedException ie ) { }
  		}
  		boolean currentState = !expectedState;
  		if( abort ) {
  			poller.cancel(IServerStatePoller.CANCEL);
  			poller.cleanup();
  			alertEventLogAbort();
  		} else {
  		
  			if( done ) {
  				// the poller has an answer
  				currentState = poller.getState();
  				poller.cleanup();
  			} else {
  				// we timed out.  get response from preferences
  				poller.cancel(IServerStatePoller.TIMEOUT_REACHED);
  				currentState = getTimeoutBehavior();
  				poller.cleanup();
  				alertEventLogTimeout();
  				fireTimeoutEvent();
  			}
  			
  			if( currentState != expectedState ) {
  				// it didnt work... cancel all processes! force stop
  				behavior.stop(true);
  				alertEventLogFailure();
  			} else {
  				if( currentState == IServerStatePoller.SERVER_UP ) 
  					behavior.setServerStarted();
  				else
  					behavior.setServerStopped();
  				alertEventLogSuccess(currentState);
  			}
  		}
  	}
  	
  	protected boolean getTimeoutBehavior() {
  		// timeout has been reached, so let the user's preferences override
  		JBossServer jbs = ((JBossServer)getServer().loadAdapter(JBossServer.class, null));
  		ServerAttributeHelper helper = (ServerAttributeHelper)jbs.getAttributeHelper();
  			
  		boolean behavior = helper.getAttribute(IServerPollingAttributes.TIMEOUT_BEHAVIOR, IServerPollingAttributes.TIMEOUT_IGNORE);
  		if( behavior == IServerPollingAttributes.TIMEOUT_ABORT ) 
  			return !expectedState;
  
  		return expectedState;
  	}
  	
  	protected void fireTimeoutEvent() {
  //		IServerPollerTimeoutListener[] listeners = 
  //			JBossServerCore.getDefault().getTimeoutListeners(poller.getClass().getName());
  //		for( int i = 0; i < listeners.length; i++ ) {
  //			listeners[i].serverTimedOut(getServer(), expectedState);
  //		}
  	}
  	
  	protected IServer getServer() {
  		 return behavior.getServer();
  	}
  	
  
  	/*
  	 * Event Log Stuff here!
  	 */
  	protected void alertEventLogStarting() {
  		if( expectedState == IServerStatePoller.SERVER_UP) {
  			activeEvent = new PollThreadEvent(eventRoot, SERVER_STARTING, expectedState);
  		} else {
  			activeEvent = new PollThreadEvent(eventRoot, SERVER_STOPPING, expectedState);
  		}
  		EventLogModel.markChanged(eventRoot);
  	}
  	
  	public PollThreadEvent getActiveEvent() {
  		return activeEvent;
  	}
  	
  	protected void alertEventLogAbort() {
  		PollThreadEvent event = new PollThreadEvent(eventRoot, POLL_THREAD_ABORTED, expectedState);
  		EventLogModel.markChanged(eventRoot);
  	}
  	protected void alertEventLogTimeout() {
  		PollThreadEvent event = new PollThreadEvent(eventRoot, POLL_THREAD_TIMEOUT, expectedState);
  		EventLogModel.markChanged(eventRoot);
  	}
  	protected void alertEventLogFailure() {
  		PollThreadEvent event = new PollThreadEvent(eventRoot, FAILURE, expectedState);
  		EventLogModel.markChanged(eventRoot);
  	}
  	protected void alertEventLogSuccess(boolean currentState) {
  		PollThreadEvent event = new PollThreadEvent(eventRoot, SUCCESS, expectedState);
  		EventLogModel.markChanged(eventRoot);
  	}
  
  	public class PollThreadEvent extends EventLogTreeItem {
  		public PollThreadEvent(SimpleTreeItem parent, String type, boolean expectedState) {
  			super(parent, type);
  			setProperty(EXPECTED_STATE, new Boolean(expectedState));
  			//System.out.println("type:state = " + type + ":" + expectedState);
  		}
  		
  		public boolean getExpectedState() {
  			return ((Boolean) getProperty(EXPECTED_STATE)).booleanValue();
  		}
  	}
  }
  
  
  
  1.1      date: 2006/11/15 22:26:55;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/runtime/server/polling/TwiddlePoller.java
  
  Index: TwiddlePoller.java
  ===================================================================
  /**
   * JBoss, a Division of Red Hat
   * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
  * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.runtime.server.polling;
  
  import org.eclipse.debug.core.model.IProcess;
  import org.eclipse.wst.server.core.IServer;
  import org.jboss.ide.eclipse.as.core.model.EventLogModel;
  import org.jboss.ide.eclipse.as.core.model.ServerProcessModel;
  import org.jboss.ide.eclipse.as.core.model.EventLogModel.EventLogTreeItem;
  import org.jboss.ide.eclipse.as.core.model.ServerProcessModel.ServerProcessModelEntity;
  import org.jboss.ide.eclipse.as.core.runtime.server.IServerStatePoller;
  import org.jboss.ide.eclipse.as.core.server.JBossServerLaunchConfiguration;
  import org.jboss.ide.eclipse.as.core.server.TwiddleLauncher;
  import org.jboss.ide.eclipse.as.core.server.TwiddleLauncher.ProcessData;
  import org.jboss.ide.eclipse.as.core.util.SimpleTreeItem;
  
  public class TwiddlePoller implements IServerStatePoller {
  
  	public static final String STATUS = "org.jboss.ide.eclipse.as.core.runtime.server.internal.TwiddlePoller.status";
  	public static final String EXPECTED_STATE = "org.jboss.ide.eclipse.as.core.runtime.server.internal.TwiddlePoller.expectedState";
  
  	public static final String TYPE_TERMINATED = "org.jboss.ide.eclipse.as.core.runtime.server.internal.TwiddlePoller.TYPE_TERMINATED";
  	public static final String TYPE_RESULT = "org.jboss.ide.eclipse.as.core.runtime.server.internal.TwiddlePoller.TYPE_RESULT";
  	
  	public static final int STATE_STARTED = 1;
  	public static final int STATE_STOPPED = 0;
  	public static final int STATE_TRANSITION = -1;
  	
  	private boolean expectedState;
  	private int started; 
  	private boolean canceled;
  	private boolean done;
  	private IServer server;
  	
  	private PollerRunnable currentRunnable;
  	private EventLogTreeItem event;
  	public void beginPolling(IServer server, boolean expectedState, PollThread pt) {
  		this.expectedState = expectedState;
  		this.canceled = false;
  		this.done = false;
  		this.server = server;
  		event = pt.getActiveEvent();
  		launchTwiddlePoller();
  	}
  
  	private class PollerRunnable implements Runnable {
  		private TwiddleLauncher launcher;
  		public void run() {
  			String args = "get \"jboss.system:type=Server\" Started";
  			while( !canceled && !done ) {
  				// are start processes still going?
  				ServerProcessModelEntity ent = ServerProcessModel.getDefault().getModel(server.getId());
  				IProcess[] processes = ent.getProcesses(JBossServerLaunchConfiguration.START);
  				if( ServerProcessModel.allProcessesTerminated(processes)) {
  					done = true;
  					started = 0;
  					eventAllProcessesTerminated();
  				} else {
  					launcher = new TwiddleLauncher();
  					ProcessData[] datas = launcher.getTwiddleResults(server, args, true);
  					if( datas.length == 1 ) {
  						ProcessData d = datas[0];
  						String out = d.getOut();
  						if( out.startsWith("Started=true")) {
  							started = STATE_STARTED;
  						} else if(out.startsWith("Started=false")) {
  							started = STATE_TRANSITION; // it's alive and responding
  						} else {
  							started = STATE_STOPPED; // It's fully down
  						}
  						
  						if( started == 1 && expectedState == SERVER_UP ) {
  							done = true;
  						} else if( started == 0 && expectedState == SERVER_DOWN) {
  							done = true;
  						} 
  					}
  				}
  				eventTwiddleExecuted();
  			}
  		}
  
  		public void setCanceled() {
  			if( launcher != null ) {
  				launcher.setCanceled();
  			}
  		}
  	}
  	public void eventTwiddleExecuted() {
  		TwiddlePollerEvent tpe = new TwiddlePollerEvent(event, TYPE_RESULT, started, expectedState);
  		EventLogModel.markChanged(event);
  	}
  	public void eventAllProcessesTerminated() {
  		TwiddlePollerEvent tpe = new TwiddlePollerEvent(event, TYPE_TERMINATED, started, expectedState);
  		EventLogModel.markChanged(event);
  	}
  	
  	private void launchTwiddlePoller() {
  		PollerRunnable run = new PollerRunnable();
  		Thread t = new Thread(run, "Twiddle Poller");
  		t.start();
  	}
  	
  	
  	public void cancel(int type) {
  		canceled = true;
  	}
  
  	public void cleanup() {
  		ServerProcessModelEntity ent = ServerProcessModel.getDefault().getModel(server.getId());
  		if( expectedState == SERVER_UP) {
  			ent.terminateProcesses(JBossServerLaunchConfiguration.TWIDDLE);
  		} else {
  			ent.terminateProcesses(JBossServerLaunchConfiguration.TWIDDLE);
  			ent.terminateProcesses(JBossServerLaunchConfiguration.STOP);
  		}
  	}
  
  	public boolean getState() {
  		if( started == 0 ) return SERVER_DOWN;
  		if( started == 1 ) return SERVER_UP;
  
  		if( !done && !canceled ) 
  			return !expectedState; // Not there yet.
  
  		return expectedState; // done or canceled, doesnt matter
  	}
  
  	public boolean isComplete() {
  		return done;
  	}
  	
  	
  	public class TwiddlePollerEvent extends EventLogTreeItem {
  		public TwiddlePollerEvent(SimpleTreeItem parent, String type, int status, boolean expectedState) {
  			super(parent, null, type);
  			setProperty(EXPECTED_STATE, new Boolean(expectedState));
  			setProperty(STATUS, new Integer(status));
  			//System.out.println("type:state:status = " + type + ":" + expectedState + ":" + status);
  		}
  	}
  	
  	
  
  }
  
  
  
  1.1      date: 2006/11/15 22:26:55;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.core/jbosscore/org/jboss/ide/eclipse/as/core/runtime/server/polling/TimeoutPoller.java
  
  Index: TimeoutPoller.java
  ===================================================================
  /**
   * JBoss, a Division of Red Hat
   * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
  * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.core.runtime.server.polling;
  
  import java.util.Date;
  
  import org.eclipse.wst.server.core.IServer;
  import org.jboss.ide.eclipse.as.core.model.ServerProcessModel;
  import org.jboss.ide.eclipse.as.core.runtime.server.IServerStatePoller;
  
  // Wait 10 seconds, then say it's at it's expected state
  public class TimeoutPoller implements IServerStatePoller {
  
  	private PollThread pollThread;
  	private boolean expectedState;
  	private long endTime;
  	private IServer server;
  	int timeout = -1;
  	
  	
  	public void beginPolling(IServer server, boolean expectedState, PollThread pt) {
  		this.server = server;
  		this.expectedState = expectedState;
  		this.endTime = new Date().getTime() + getTimeout();
  	}
  
  	protected int getTimeout() {
  		return 15000;
  	}
  	public void cancel(int type) {
  	}
  
  	public boolean getState() {
  		if( new Date().getTime() > endTime ) return expectedState;
  		return !expectedState;
  	}
  
  	public boolean isComplete() {
  		if( new Date().getTime() > endTime ) return true;
  		return false;
  	}
  
  	public void cleanup() {
  		if( !expectedState ) {
  			ServerProcessModel.getDefault().getModel(server.getId()).clearAll();
  		} 
  	}
  
  	
  }
  
  
  



More information about the jboss-cvs-commits mailing list