package edu.usc.glidein.service.impl;

import java.rmi.RemoteException;

import org.apache.log4j.Logger;
import org.globus.wsrf.ResourceContext;
import org.globus.wsrf.ResourceKey;

import edu.usc.glidein.stubs.types.EmptyObject;
import edu.usc.glidein.stubs.types.Glidein;

public class GlideinService
{
	private Logger logger = Logger.getLogger(GlideinService.class);
	
	private GlideinResourceHome getResourceHome() throws RemoteException
	{
		try {
			Object resourceHome = ResourceContext.getResourceContext().getResourceHome();
			GlideinResourceHome glideinResourceHome = (GlideinResourceHome) resourceHome;
			return glideinResourceHome;
		} catch (Exception e) {
			String message = "Unable to find glidein resource home";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
	}
	
	private GlideinResource getResource() throws RemoteException
	{
		try {
			Object resource = ResourceContext.getResourceContext().getResource();
			GlideinResource glideinResource = (GlideinResource) resource;
			return glideinResource;
		} catch (Exception e) {
			String message = "Unable to find glidein resource";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
	}
	
	private ResourceKey getResourceKey() throws RemoteException 
	{
		try {
			return ResourceContext.getResourceContext().getResourceKey();
		} catch (Exception e) {
			String message = "Unable to find glidein resource key";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
	}
	
	public Glidein getGlidein(EmptyObject empty) throws RemoteException {
		return getResource().getGlidein();
	}
	
	public EmptyObject start(EmptyObject empty) throws RemoteException {
		// TODO submit glidein job
		return new EmptyObject();
	}
	
	public EmptyObject cancel(EmptyObject empty) throws RemoteException {
		// TODO cancel glidein job
		return new EmptyObject();
	}
	
	public EmptyObject delete(EmptyObject empty) throws RemoteException {
		getResource().delete();
		getResourceHome().remove(getResourceKey());
		return new EmptyObject();
	}
}

@@ -1568,25 +1568,26 @@
 oteException
- 
+%0A%09
 %7B%0A%09%09return g
@@ -1643,11 +1643,12 @@
 ct s
-tar
+ubmi
 t(Em
@@ -1690,172 +1690,37 @@
 tion
- 
+%0A%09
 %7B%0A%09%09
-// TODO submit glidein job%0A%09%09return new EmptyObject();%0A%09%7D%0A%09%0A%09public EmptyObject cancel(EmptyObject empty) throws RemoteException %7B%0A%09%09// TODO cancel glidein job
+getResource().submit();
 %0A%09%09r
@@ -1769,21 +1769,21 @@
 yObject 
-delet
+remov
 e(EmptyO
@@ -1817,17 +1817,18 @@
 xception
- 
+%0A%09
 %7B%0A%09%09getR

Add submit Rename delete to remove