All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.activecomponents.webservice.SWebSocket Maven / Gradle / Ivy

There is a newer version: 4.0.267
Show newest version
package org.activecomponents.webservice;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletContext;

import jadex.base.IPlatformConfiguration;
import jadex.base.PlatformConfigurationHandler;
import jadex.base.Starter;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.FutureBarrier;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;

/**
 *  Static helper methods for Jadex websocket integration.
 */
public class SWebSocket
{
	//-------- constants --------
	
	/** Attribute name for platform in context. */
	public static final String	ATTR_PLATFORM	= "org.activecomponents.webservice.platform";
	
	//-------- methods --------
	
	/**
	 *  Initialize a context. Should be called only once per context at startup.
	 *  Creates platform and initial agents, if necessary.
	 *  @param context	The servlet context.
	 */
	public static IFuture	initContext(ServletContext context)
	{
		System.out.println("Init context for: "+context.getContextPath());
		
		FutureBarrier	agents	= new FutureBarrier();

		// Create components specified in web.xml
		Enumeration pnames = context.getInitParameterNames();
		if(pnames!=null)
		{
			while(pnames.hasMoreElements())
			{
				String pname = pnames.nextElement();
				if(pname!=null && pname.startsWith("ws_component"))
				{
					final String model = context.getInitParameter(pname);
					IFuture fut = createAgent(context, model);
					agents.addFuture(fut);
					fut.addResultListener(new IResultListener()
					{
						public void resultAvailable(IComponentIdentifier result)
						{
						}
						
						public void exceptionOccurred(Exception exception)
						{
							System.out.println("Exception in component start: "+model+" "+exception);
						}
					});
				}
			}
		}
		
		return agents.waitForIgnoreFailures(null);
	}
	
	/**
	 *  Cleanup a context. Should be called only once per context at shutdown.
	 *  Removes platform with all agents, if necessary.
	 *  @param context	The servlet context.
	 */
	public static IFuture	cleanupContext(ServletContext context)
	{
		// Terminates the platform of the application/context
		
		IFuture fut = (IFuture)context.getAttribute(ATTR_PLATFORM);
		if(fut!=null)
		{
			final Future	ret	= new Future();
			fut.addResultListener(new IResultListener()
			{
				@Override
				public void resultAvailable(IExternalAccess platform)
				{
					platform.killComponent().addResultListener(new IResultListener>()
					{
						@Override
						public void resultAvailable(Map result)
						{
							ret.setResult(null);
						}
						
						@Override
						public void exceptionOccurred(Exception exception)
						{
							// Platform shutdown failed -> no more cleanup possible.
							ret.setResult(null);
						}
					});
				}
				
				@Override
				public void exceptionOccurred(Exception exception)
				{
					// Platform startup failed -> no cleanup necessary/possible.
					ret.setResult(null);
				}
			});
			
			return ret;
		}
		else
		{
			return IFuture.DONE;
		}
	}
	
	/**
	 *  Get or create a Jadex platform for the given context.
	 *  @param context	The servlet context.
	 */
	public static IFuture getPlatform(ServletContext context)
	{
		IFuture ret = (IFuture)context.getAttribute(ATTR_PLATFORM);
	    if(ret==null)
	    {
	    	IPlatformConfiguration pc = PlatformConfigurationHandler.getDefault();
		    pc.setGui(false);
//		    pc.getRootConfig().setGui(true);
//		    pc.getRootConfig().setLogging(true);
//		    pc.getRootConfig().setRsPublish(true);	
		    ret = Starter.createPlatform(pc);
			context.setAttribute(ATTR_PLATFORM, ret);		    
	    }
	    return ret;
	}
	
	/**
	 *  Create a agent.
	 *  @param context	The servlet context.
	 *  @param model	The agent model.
	 *  @return Future for the component identifier of the created agent. 
	 */
	public static IFuture	createAgent(ServletContext context, final String model)
	{
		final Future	ret	= new Future();
		getPlatform(context).addResultListener(new ExceptionDelegationResultListener(ret)
		{
			@Override
			public void customResultAvailable(IExternalAccess platform) throws Exception
			{
				platform.createComponent(new CreationInfo().setFilename(model)).addResultListener(new IResultListener()
				{
					public void resultAvailable(IExternalAccess result)
					{
						ret.setResult(result.getId());
						System.out.println("Created component: "+result.getId());
					}
					public void exceptionOccurred(Exception exception)
					{
						ret.setExceptionIfUndone(exception);
					}
				});
			}
		});
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy