Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jadex.tools.daemon;
  
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
Daemon agent provides functionalities for managing platforms.
 
 public class DaemonAgent extends MicroAgent
 {	
 	//-------- attributes --------
 	
The started platforms.
 
 	protected Map platforms

The listeners.
 
 	protected List listeners;
 
 	//-------- methods --------
 	
Called once after agent creation.
 
 	public IFuture	agentCreated()
 	{
 		 = Collections.synchronizedMap(new HashMap());
 		 = Collections.synchronizedList(new ArrayList());
 		SwingUtilities.invokeLater(new Runnable()
 		{
 			public void run()
 			{
 			}
 		});
 		return .;
 	}

Start a platform using a configuration.

Parameters:
args The arguments.
 
 	{
 		final Future ret = new Future();
 		
 		final StartOptions options = opt==nullnew StartOptions(): opt;
 		
 //		if(options.getMain()==null)
 //		{
 //			options.setMain("jadex.base.Starter");
 //		}
 		
 		if(options.getClassPath()==null || options.getClassPath().length()==0)
 		{
 			{
 				public void customResultAvailable(Object result)
 				{
 					{
 						public void customResultAvailable(Object result)
 						{
 							List urls = (List)result;
 							List res = new ArrayList();
 							for(int i=0; i<urls.size(); i++)
 							{
 								URL url = (URL)urls.get(i);
								String name = SUtil.convertURLToString(url);
								if(name!=null)
									res.add(name);
								else
									..println("Cannot convert url to file: "+url);
							}
							StringBuffer buf = new StringBuffer();
							for(int i=0; i<res.size(); i++)
							{
								buf.append(res.get(i));
								if(i+1<res.size())
							}
							options.setClassPath(buf.toString());
						}
					}));
				}	
			}));
		}
		else
		{
		}
		return ret;
	}

Start a platform using a configuration.

Parameters:
args The arguments. / public IFuture startPlatform(StartOptions opt) { final Future ret = new Future(); // Start platform in the same VM. Starter.createPlatform(args).addResultListener(new IResultListener() { public void resultAvailable(Object source, Object result) { IExternalAccess platform = (IExternalAccess)result; platforms.put(platform.getComponentIdentifier(), platform); ret.setResult(result); } public void exceptionOccurred(Object source, Exception exception) { ret.setException(exception); } }); return ret;
	{
		final Future ret = new Future();
		// Start platform in another VM.
		try
		{
			// Can be called in another directory
			File newcurdir = new File(options.getStartDirectory());
			newcurdir.mkdirs();
			String cmd = options.getStartCommand();
			..println("Starting process: "+cmd);
			final Process proc = Runtime.getRuntime().exec(options.getStartCommand(), nullnewcurdir);
			{
				boolean fin = false;
				public void write(byte[] bthrows IOException
				{
					if(!)
					{
						.append(new String(b));
						for(int i=0; i<b.lengthi++)
						{
							if(' '==b[i])
							{
								 = true;
								.put(cidproc);
								ret.setResult(cid);
								break;
							}
						}
					}
					super.write(b);
				}
				public void write(byte[] bint offint lenthrows IOException
				{
					if(!)
					{
						for(int i=0; i<leni++)
						{
							.append((char)b[i]);
							if(' '==b[i])
							{
								 = true;
								.put(cidproc);
								ret.setResult(cid);
								break;
							}
						}
					}
					super.write(bofflen);
				}
				public void write(int bthrows IOException
				{
					if(!)
					{
						.append((char)b);
						if(' '==b)
						{
							 = true;
							.put(cidproc);
							ret.setResult(cid);
						}
					}
					super.write(b);
				}
			};
			new Thread(new StreamCopy(proc.getInputStream(), fos)).start();
	//		boolean finished = false;
	//		while(!finished && !abort)
	//		{
	//			try
	//			{
	//				Thread.sleep(300);
	//				if(proc.exitValue()!=0)
	//					throw new RuntimeException("Java returned an error or could not be invoked.");
	//				finished = true;
	//			}
	//			catch(IllegalThreadStateException ie)
	//			{
	//				if(abort)
	//					proc.destroy();
	//			}
	//		}
			/*Process proc = Runtime.getRuntime().exec(cmd+" "+cmdline);
			new Thread(new StreamCopy(proc.getInputStream(), System.out)).start();
			new Thread(new StreamCopy(proc.getErrorStream(), System.out)).start();
			if(proc.waitFor()!=0)
			// todo: does not work because process output stream is not read :-(
			//if(Runtime.getRuntime().exec(cmd+" -classpath "+cp+cmdline).waitFor()!=0)
			{
				throw new RuntimeException("Javadoc returned an error or could not be invoked.");
			}*/
		}
		catch(Exception ex)
		{
			throw new RuntimeException("Could not start process. Reason: "+ex.getMessage());
		}
		return ret;
	}

Shutdown a platform.

Parameters:
cid The platform id.
	{
		final Future ret = new Future();
		Process proc = (Process).get(cid);
		if(proc==null)
		{
			ret.setException(new RuntimeException("Platform not found: "+cid));
		}
		else
		{
			proc.destroy();
			ret.setResult(null);
		}
		return ret;
	}

Shutdown a platform.

Parameters:
cid The platform id. / public IFuture shutdownPlatform(final IComponentIdentifier cid) { final Future ret = new Future(); IExternalAccess platform = (IExternalAccess)platforms.get(cid); if(platform==null) { ret.setException(new RuntimeException("No platform found: "+cid)); } else { SServiceProvider.getService(platform.getServiceProvider(), IComponentManagementService.class) .addResultListener(new IResultListener() { public void resultAvailable(Object source, Object result) { IComponentManagementService cms = (IComponentManagementService)result; cms.destroyComponent(cid).addResultListener(new DelegationResultListener(ret)); } public void exceptionOccurred(Object source, Exception exception) { ret.setException(exception); } }); } return ret;
Get the component identifiers of all (managed) platforms.

Returns:
Collection of platform ids.
	{
		return new Future(.keySet());
	}

Add a change listener.

Parameters:
listener The change listener.
	{
		.add(listener);
	}

Remove a change listener.

Parameters:
listener The change listener.
	{
		.remove(listener);
	}

Notify the listeners.
	protected void notifyListeners(ChangeEvent event)
	{
		IRemoteChangeListener[] alisteners;
		synchronized(this)
		{
			alisteners.isEmpty()? null
		}
		if(alisteners!=null)
		{
			for(int i=0; i<alisteners.lengthi++)
			{
				final IRemoteChangeListener lis = alisteners[i];
				{
					public void resultAvailable(Object result)
					{
					}
					public void exceptionOccurred(Exception exception)
					{
//						System.out.println("Removing listener: "+lis);
					}
				}));
			}
		}
	}
	//-------- static methods --------
Get the meta information about the agent.
	{
		return new MicroAgentMetaInfo("This agent offers the daemon service."null
			new IArgument[]{}//new Argument("infos", "Initial information records.", "InformationEntry[]")}
nullnull, SUtil.createHashMap(new String[]{"componentviewer.viewerclass"}, new Object[]{"jadex.tools.daemon.DaemonViewerPanel"}),
			new RequiredServiceInfo[]{new RequiredServiceInfo("libservice"ILibraryService.class)}
	}
New to GrepCode? Check out our FAQ X