package edu.usc.glidein.service.impl;

import java.net.URL;
import java.rmi.RemoteException;

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.stubs.types.Sites;
import edu.usc.glidein.stubs.types.Site;
import edu.usc.glidein.stubs.types.SiteState;

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

	public SiteFactoryService() { }
	
	public EndpointReferenceType createSite(Site site)
	throws RemoteException
	{
		// Set state
		site.setState(SiteState.NEW);
		site.setShortMessage("Created");
			
		// 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");
		}
		
		// 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);
		}
	}
}
@@ -87,16 +87,54 @@
 ption;%0A%0A
+import javax.naming.NamingException;%0A%0A
 import o
@@ -233,40 +233,8 @@
 pe;%0A
-import org.apache.log4j.Logger;%0A
 impo
@@ -485,817 +485,196 @@
 te;%0A
-import edu.usc.glidein.stubs.types.SiteState;%0A%0Apublic class SiteFactoryService%0A%7B%0A%09private Logger logger = Logger.getLogger(SiteFactoryService.class);%0A%0A%09public SiteFactoryService() %7B %7D%0A%09%0A%09public EndpointReferenceType createSite(Site site)%0A%09throws RemoteException%0A%09%7B%0A%09%09// Set state%0A%09%09site.setState(SiteState.NEW);%0A%09%09site.setShortMessage(%22Created%22);%0A%09%09%09%0A%09%09// Check for Condor version and s
+%0Apublic class SiteFactoryService%0A%7B%0A%09public EndpointReferenceType createSite(Site site)%0A%09throws RemoteException%0A%09%7B%09%0A%09%09// G
 et re
-a
 so
-nable default%0A%09%09String ver = site.getCondorVersion();%0A%09%09ver = %22%22.equalsIgnoreCase(ver) ? null : ver;%0A%09%09String pkg = site.getCondorPackage();%0A%09%09pkg = %22%22.equalsIgnoreCase(pkg) ? null : pkg;%0A%09%09if (ver==null && pkg==null) %7B%0A%09%09%09site.setCondorVersion(%227.0.0%22);%0A%09%09%7D%0A%09%09%0A%09%09// Create a new resource%0A%09%09ResourceKey key = null;%0A%09%09try %7B%0A%09%09%09ResourceContext rctx = ResourceContext.getResourceContext();%0A%09%09%09SiteResourceHome 
+urce home%0A%09%09SiteResourceHome home = null;%0A%09%09try %7B%0A%09%09%09
 home = 
-(
 Site
@@ -689,59 +689,21 @@
 Home
-) rctx.getResourceHome();%0A%09%09%09key = home.create(site
+.getInstance(
 );%0A%09
@@ -704,32 +704,38 @@
 e();%0A%09%09%7D catch (
+Naming
 Exception e) %7B%0A%09
@@ -724,32 +724,33 @@
 ngException 
+n
 e) %7B%0A%09%09%09
 String messa
@@ -737,33 +737,42 @@
 e) %7B%0A%09%09%09
-String message = 
+throw new RemoteException(
 %22Unable 
@@ -778,104 +778,102 @@
  to 
-create site r
+get SiteR
 esource
-%22;%0A%09%09%09logger.error(message,e);%0A%09%09%09throw new RemoteException(message, e);%0A%09%09%7D
+Home%22, ne);%0A%09%09%7D%0A%09%09%0A%09%09// Create resource%0A%09%09ResourceKey key = home.create(site);
 %0A%09%09%0A
@@ -1288,33 +1288,42 @@
 e) %7B%0A%09%09%09
-String message = 
+throw new RemoteException(
 %22Unable 
@@ -1355,74 +1355,8 @@
 nce%22
-;%0A%09%09%09logger.error(message,e);%0A%09%09%09throw new RemoteException(message
 , e)
@@ -1744,80 +1744,8 @@
 ) %7B%0A
-%09%09%09String message = %22Unable to list sites%22;%0A%09%09%09logger.error(message,e);%0A
 %09%09%09t
@@ -1769,23 +1769,38 @@
 ception(
-message
+%22Unable to list sites%22
 , e);%0A%09%09

Move Site initialization to SiteResource