Example 7.1 Implementing Business Delegate pattern  ResourceDelegate

// imports
...

public class ResourceDelegate {

	// Remote reference for Session Facade
	private ResourceSession session;

	// Class for Session Facade's Home object
	private static final Class homeClazz =
		corepatterns.apps.psa.ejb.ResourceSessionHome.class;

	// Default Constructor. Looks up home and connects to
	// session by creating a new one.
	public ResourceDelegate() throws ResourceException {
		try {
			ResourceSessionHome home =
				(ResourceSessionHome) ServiceLocator.getInstance().
					getRemoteHome("Resource", homeClazz);
			session = home.create();
		} catch (ServiceLocatorException ex) {
			// Translate Service Locator exception into
			// application exception
			throw new ResourceException(...);
		} catch (CreateException ex) {
			// Translate the Session create exception into
			// application exception
			throw new ResourceException(...);
		} catch (RemoteException ex) {
			// Translate the Remote exception into application
			// exception
			throw new ResourceException(...);
		}
	}

	// Constructor that accepts an ID (Handle id) and
	// reconnects to the prior session bean instead of creating
	// a new one
	public ResourceDelegate(String id)
	throws ResourceException {
		// reconnect to the session bean for the given id
		reconnect(id);
	}

	// Returns a String ID the client can use at a later time
	// to reconnect to the session bean
	public String getID() {
		try {
			return ServiceLocator.getId(session);
		} catch (Exception e) {
			// Throw an application exception
		}
	}

	// method to reconnect using String ID
	public void reconnect(String id) throws ResourceException {
		try {
			session =
				(ResourceSession) ServiceLocator.getService(id);
		} catch (RemoteException ex) {
			// Translate the Remote exception into application
			// exception
			throw new ResourceException(...);
		}
	}

	// The following are the business methods proxied to the
	// Session Facade. If any service exception is encountered,
	// these methods convert them into application exceptions
	// such as ResourceException, SkillSetException, and so on.
	public ResourceTO setCurrentResource(String resourceId)
	throws ResourceException {
		try {
			return session.setCurrentResource(resourceId);
		} catch (RemoteException ex) {
			// Translate the service exception into
			// application exception
			throw new ResourceException(...);
		}
	}

	public ResourceTO getResourceDetails()
	throws ResourceException {

		try {
			return session.getResourceDetails();
		} catch (RemoteException ex) {
			// Translate the service exception into application
			// exception
			throw new ResourceException(...);
		}
	}

	public void setResourceDetails(ResourceTO to)
	throws ResourceException {
		try {
			session.setResourceDetails(to);
		} catch (RemoteException ex) {
			throw new ResourceException(...);
		}
	}

	public void addNewResource(ResourceTO to)
	throws ResourceException {
		try {
			session.addResource(to);
		} catch (RemoteException ex) {
			throw new ResourceException(...);
		}
	}

	// all other proxy method to session bean
	...
}