Home > Articles > Software Development & Management

  • Print
  • + Share This
From the book Sample Code

Sample Code

Implementing the Business Delegate Pattern

Consider a professional services application (PSA), which has resources (consultants) assigned to projects (engagements). The presentation-tier component must access a Session Façde (341). Apply the Business Delegate pattern to implement a ResourceDelegate object, which encapsulates the details of dealing with the ResourceSession Session Façde (341).

The ResourceDelegate implementation for this example is shown in Example 7.1, and the corresponding remote interface for the ResourceSession Session Façde (341) is shown in Example 7.2.

Example 7.1 Implementing Business Delegate pattern – ResourceDelegate

1	
2	// imports
3	...
4	
5	public class ResourceDelegate {
6	
7		// Remote reference for Session Facade
8		private ResourceSession session;
9	
10		// Class for Session Facade's Home object
11		private static final Class homeClazz =
12			corepatterns.apps.psa.ejb.ResourceSessionHome.class;
13	
14		// Default Constructor. Looks up home and connects to
15		// session by creating a new one.
16		public ResourceDelegate() throws ResourceException {
17			try {
18				ResourceSessionHome home =
19					(ResourceSessionHome) ServiceLocator.getInstance().
20						getRemoteHome("Resource", homeClazz);
21				session = home.create();
22			} catch (ServiceLocatorException ex) {
23				// Translate Service Locator exception into
24				// application exception
25				throw new ResourceException(...);
26			} catch (CreateException ex) {
27				// Translate the Session create exception into
28				// application exception
29				throw new ResourceException(...);
30			} catch (RemoteException ex) {
31				// Translate the Remote exception into application
32				// exception
33				throw new ResourceException(...);
34			}
35		}
36	
37		// Constructor that accepts an ID (Handle id) and
38		// reconnects to the prior session bean instead of creating
39		// a new one
40		public ResourceDelegate(String id)
41		throws ResourceException {
42			// reconnect to the session bean for the given id
43			reconnect(id);
44		}
45	
46		// Returns a String ID the client can use at a later time
47		// to reconnect to the session bean
48		public String getID() {
49			try {
50				return ServiceLocator.getId(session);
51			} catch (Exception e) {
52				// Throw an application exception
53			}
54		}
55	
56		// method to reconnect using String ID
57		public void reconnect(String id) throws ResourceException {
58			try {
59				session =
60					(ResourceSession) ServiceLocator.getService(id);
61			} catch (RemoteException ex) {
62				// Translate the Remote exception into application
63				// exception
64				throw new ResourceException(...);
65			}
66		}
67	
68		// The following are the business methods proxied to the
69		// Session Facade. If any service exception is encountered,
70		// these methods convert them into application exceptions
71		// such as ResourceException, SkillSetException, and so on.
72		public ResourceTO setCurrentResource(String resourceId)
73		throws ResourceException {
74			try {
75				return session.setCurrentResource(resourceId);
76			} catch (RemoteException ex) {
77				// Translate the service exception into
78				// application exception
79				throw new ResourceException(...);
80			}
81		}
82	
83		public ResourceTO getResourceDetails()
84		throws ResourceException {
85	
86			try {
87				return session.getResourceDetails();
88			} catch (RemoteException ex) {
89				// Translate the service exception into application
90				// exception
91				throw new ResourceException(...);
92			}
93		}
94	
95		public void setResourceDetails(ResourceTO to)
96		throws ResourceException {
97			try {
98				session.setResourceDetails(to);
99			} catch (RemoteException ex) {
100				throw new ResourceException(...);
101			}
102		}
103	
104		public void addNewResource(ResourceTO to)
105		throws ResourceException {
106			try {
107				session.addResource(to);
108			} catch (RemoteException ex) {
109				throw new ResourceException(...);
110			}
111		}
112	
113		// all other proxy method to session bean
114		...
115	}

Example 7.2 Remote Interface for ResourceSession

1	// imports
2	...
3	public interface ResourceSession extends EJBObject {
4		
5		public ResourceTO setCurrentResource(String resourceId)
6		throws RemoteException, ResourceException;
7		
8		public ResourceTO getResourceDetails() 
9		throws RemoteException, ResourceException;
10	
11		public void setResourceDetails(ResourceTO resource) 
12		throws RemoteException, ResourceException;
13		
14		public void addResource(ResourceTO resource) 
15		throws RemoteException, ResourceException;
16		
17		public void removeResource() 
18		throws RemoteException, ResourceException;
19		
20		// methods for managing blockout time by the resource
21		public void addBlockoutTime(Collection blockoutTime)
22		throws RemoteException, BlockoutTimeException;
23		
24		public void updateBlockoutTime(Collection blockoutTime) 
25		throws RemoteException, BlockoutTimeException;
26		
27		public void removeBlockoutTime(	Collection blockoutTime) 
28		throws RemoteException, BlockoutTimeException;
29		
30		public void removeAllBlockoutTime() 
31		throws RemoteException, BlockoutTimeException;
32		
33		// methods for resource skillsets time by the resource
34		public void addSkillSets(Collection skillSet) 
35		throws RemoteException, SkillSetException;
36		
37		public void updateSkillSets(Collection skillSet) 
38		throws RemoteException, SkillSetException;
39		
40		public void removeSkillSet(Collection skillSet) 
41		throws RemoteException, SkillSetException;
42	
43		...
44	}
45	
  • + Share This
  • 🔖 Save To Your Account