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

jadex.base.service.deployment.DeploymentService Maven / Gradle / Ivy

Go to download

The Jadex platform base package contains functionality useful for constructing platforms.

The newest version!
package jadex.base.service.deployment;

import jadex.bridge.IExternalAccess;
import jadex.bridge.IInputConnection;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.context.IContextService;
import jadex.bridge.service.types.deployment.FileData;
import jadex.bridge.service.types.deployment.IDeploymentService;
import jadex.commons.SReflect;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.ITerminableIntermediateFuture;
import jadex.commons.future.TerminableIntermediateFuture;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *  Service for deployment files on file system.
 */
@Service
public class DeploymentService implements IDeploymentService
{
	/** The agent. */
	@ServiceComponent
	protected IExternalAccess agent; 
	
	/**
	 *  Put a file.
	 *  @param file The file data.
	 *  @param path The target path.
	 *  @return True, when the file has been copied.
	 */
	public ITerminableIntermediateFuture uploadFile(IInputConnection con, String path, String name)
	{
		try
		{
			return con.writeToOutputStream(new FileOutputStream(path+File.separator+name), agent);
		}
		catch(Exception e)
		{
			return new TerminableIntermediateFuture(e);
		}
	}
	
	/**
	 *  Rename a file.
	 *  @param path The target path.
	 *  @return True, if rename was successful.
	 */
	public IFuture renameFile(String path, String name)
	{
		Future ret = new Future();
		try
		{
			File file = new File(path);
			String newname = file.getParent()+"/"+name;
			if(file.renameTo(new File(newname)))
			{
				ret.setResult(name);
			}
			else
			{
				ret.setException(new RuntimeException());
			}
		}
		catch(Exception e)
		{
			ret.setException(e);
		}
		return ret;
	}
	
	/**
	 *  Delete a file.
	 *  @param path The target path.
	 *  @return True, if delete was successful.
	 */
	public IFuture deleteFile(String path)
	{
		Future ret = new Future();
		try
		{
			// file.toPath().delete(); since 1.7 throws Exception
			File file = new File(path);
			if(file.delete())
			{
				ret.setResult(null);
			}
			else
			{
				ret.setException(new RuntimeException());
			}
		}
		catch(Exception e)
		{
			ret.setException(e);
		}
		return ret;
	}
	
	/**
	 *  Get the root devices.
	 *  @return The root device files.
	 */
	public IFuture getRoots()
	{
		File[] roots = File.listRoots();
		return new Future(FileData.convertToRemoteFiles(roots));
	}
	
	/**
	 *  Execute a file.
	 *  @param path The filename to execute.
	 */
	public IFuture openFile(final String path)
	{
		final Future ret = new Future();
		SServiceProvider.getService(agent.getServiceProvider(), IContextService.class).addResultListener(
				new DefaultResultListener()
				{
					@Override
					public void resultAvailable(IContextService cs)
					{
						try
						{
							cs.openFile(path);
						}
						catch (IOException e)
						{
							e.printStackTrace();
							ret.setException(e);
						}
					}
				});

		// exec produces strange exceptions?!
		// Runtime.getRuntime().exec(path);
		ret.setResult(null);
		return ret;
	}
	
	/**
	 * 
	 */
	public static void main(String[] args)
	{
		try
		{
			Runtime.getRuntime().exec("notepad.exe");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy