package edu.usc.glidein.service.impl;

import java.io.File;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.UUID;

import org.apache.axis.MessageContext;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.apache.log4j.Logger;
import org.globus.wsrf.ResourceContext;
import org.globus.wsrf.ResourceKey;
import org.globus.wsrf.container.ServiceHost;
import org.globus.wsrf.utils.AddressingUtils;

import edu.usc.glidein.GlideinConfiguration;
import edu.usc.glidein.GlideinException;
import edu.usc.glidein.stubs.types.Sites;
import edu.usc.glidein.stubs.types.Site;
import edu.usc.glidein.stubs.types.SiteStatus;

public class SiteFactoryService
{
	private Logger logger = Logger.getLogger(SiteFactoryService.class);

	public SiteFactoryService() { }
	
	public EndpointReferenceType createSite(Site site)
	throws RemoteException
	{
		// Initialize site
		try {
			GlideinConfiguration config = GlideinConfiguration.getInstance();
			
			// Set status
			site.setStatus(SiteStatus.NEW);
			site.setStatusMessage("Created");
			
			// Set path
			File path = null;
			String var = config.getProperty("glidein.var","/tmp/glidein");
			do {
				UUID uid = UUID.randomUUID();
				String submitPath = var + File.separator + uid.toString();
				site.setSubmitPath(submitPath);
				path = new File(submitPath);
			} while (path.exists());
			
			// Check for Condor version and set reasonable default
			String ver = site.getCondorVersion();
			ver = "".equalsIgnoreCase(ver) ? null : ver;
			String pkg = site.getCondorPackage();
			pkg = "".equalsIgnoreCase(pkg) ? null : pkg;
			if (ver==null && pkg==null) {
				site.setCondorVersion("7.0.0");
			}
		} catch (GlideinException e) {
			String message = "Unable to initialize site";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
		
		// Create a new resource
		ResourceKey key = null;
		try {
			ResourceContext rctx = ResourceContext.getResourceContext();
			SiteResourceHome home = (SiteResourceHome) rctx.getResourceHome();
			key = home.create(site);
		} catch (Exception e) {
			String message = "Unable to create site resource";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
		
		// Create an endpoint reference for the new resource
		EndpointReferenceType epr = null;
		try {
			URL baseURL = ServiceHost.getBaseURL();
			MessageContext mctx = MessageContext.getCurrentContext();
			String svc = (String) mctx.getService().getOption("instance");
			String instanceURI = baseURL.toString() + svc;
			epr = AddressingUtils.createEndpointReference(instanceURI, key);
		} catch (Exception e) {
			String message = "Unable to create endpoint reference";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
		
		// Return the endpoint reference to the client
		return epr;
	}
	
	public Sites listSites(boolean longFormat)
	throws RemoteException
	{
		try {
			ResourceContext rctx = ResourceContext.getResourceContext();
			SiteResourceHome home = (SiteResourceHome) rctx.getResourceHome();
			Site[] sites = home.list(longFormat);
			return new Sites(sites);
		} catch (Exception e) {
			String message = "Unable to list sites";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
	}
}
@@ -36,29 +36,8 @@
 l;%0A%0A
-import java.io.File;%0A
 impo
@@ -89,31 +89,8 @@
 ion;
-%0Aimport java.util.UUID;
 %0A%0Aim
@@ -396,94 +396,8 @@
 s;%0A%0A
-import edu.usc.glidein.GlideinConfiguration;%0Aimport edu.usc.glidein.GlideinException;%0A
 impo
@@ -749,111 +749,8 @@
 %09%09//
- Initialize site%0A%09%09try %7B%0A%09%09%09GlideinConfiguration config = GlideinConfiguration.getInstance();%0A%09%09%09%0A%09%09%09//
  Set
@@ -757,17 +757,16 @@
  status%0A
-%09
 %09%09site.s
@@ -791,17 +791,16 @@
 s.NEW);%0A
-%09
 %09%09site.s
@@ -835,317 +835,8 @@
 %09%09%09%0A
-%09%09%09// Set path%0A%09%09%09File path = null;%0A%09%09%09String var = config.getProperty(%22glidein.var%22,%22/tmp/glidein%22);%0A%09%09%09do %7B%0A%09%09%09%09UUID uid = UUID.randomUUID();%0A%09%09%09%09String submitPath = var + File.separator + uid.toString();%0A%09%09%09%09site.setSubmitPath(submitPath);%0A%09%09%09%09path = new File(submitPath);%0A%09%09%09%7D while (path.exists());%0A%09%09%09%0A%09
 %09%09//
@@ -888,17 +888,16 @@
 default%0A
-%09
 %09%09String
@@ -930,17 +930,16 @@
 on();%0A%09%09
-%09
 ver = %22%22
@@ -973,25 +973,24 @@
 ll : ver;%0A%09%09
-%09
 String pkg =
@@ -1015,17 +1015,16 @@
 kage();%0A
-%09
 %09%09pkg = 
@@ -1064,17 +1064,16 @@
  pkg;%0A%09%09
-%09
 if (ver=
@@ -1094,17 +1094,16 @@
 null) %7B%0A
-%09
 %09%09%09site.
@@ -1133,165 +1133,8 @@
 %22);%0A
-%09%09%09%7D%0A%09%09%7D catch (GlideinException e) %7B%0A%09%09%09String message = %22Unable to initialize site%22;%0A%09%09%09logger.error(message,e);%0A%09%09%09throw new RemoteException(message, e);%0A
 %09%09%7D%0A

Remove submitPath