Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: de/danet/an/workflow/assignment/StandardResourceAssignmentServiceFactory.java


1   /*
2    * This file is part of the WfMCore/WfMOpen project.
3    * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
4    * All rights reserved.
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   * $Id: StandardResourceAssignmentServiceFactory.java,v 1.15 2003/09/08 20:59:22 lipp Exp $
21   *
22   * $Log: StandardResourceAssignmentServiceFactory.java,v $
23   * Revision 1.15  2003/09/08 20:59:22  lipp
24   * Javadoc fix.
25   *
26   * Revision 1.14  2003/06/27 08:51:46  lipp
27   * Fixed copyright/license information.
28   *
29   * Revision 1.13  2003/04/25 14:50:59  lipp
30   * Fixed javadoc errors and warnings.
31   *
32   * Revision 1.12  2002/12/19 21:37:43  lipp
33   * Reorganized interfaces.
34   *
35   * Revision 1.11  2002/06/27 10:55:59  lipp
36   * Delayed initialization even more.
37   *
38   * Revision 1.10  2002/01/09 14:00:01  lipp
39   * Cleaned up relation between wfcore, resource assignment and resource
40   * management service.
41   *
42   * Revision 1.9  2002/01/09 11:53:01  lipp
43   * Clarified documentation.
44   *
45   * Revision 1.8  2001/12/17 19:56:03  lipp
46   * Implementation of equality checking improved.
47   *
48   * Revision 1.7  2001/12/17 12:14:04  lipp
49   * Adapted to configurable ResourceManagement/AssignmentServices.
50   *
51   * Revision 1.6  2001/12/17 10:52:13  lipp
52   * Added configurable resource management service.
53   *
54   * Revision 1.5  2001/12/17 09:15:50  lipp
55   * Javadoc fixes.
56   *
57   * Revision 1.4  2001/12/17 08:43:58  lipp
58   * Added configurable resource management service.
59   *
60   * Revision 1.3  2001/12/16 21:48:57  lipp
61   * addAssignment implemented.
62   *
63   * Revision 1.2  2001/12/16 10:37:35  lipp
64   * Assignment service implemented.
65   *
66   * Revision 1.1  2001/12/09 20:43:57  lipp
67   * First try.
68   *
69   */
70  package de.danet.an.workflow.assignment;
71  
72  import java.io.IOException;
73  import java.io.InputStream;
74  
75  import java.util.Properties;
76  
77  import java.rmi.RemoteException;
78  
79  import javax.ejb.CreateException;
80  import javax.naming.NamingException;
81  
82  import de.danet.an.util.EJBUtil;
83  
84  import de.danet.an.workflow.ejbs.assignment.AssignmentService;
85  import de.danet.an.workflow.ejbs.assignment.AssignmentServiceHome;
86  import de.danet.an.workflow.spis.ras.FactoryConfigurationError;
87  import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
88  import de.danet.an.workflow.spis.ras.ResourceAssignmentServiceFactory;
89  import de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory;
90  
91  /**
92   * Implements a simple resource assignment service factory.<P>
93   *
94   * Usage of this class as service factory requires an additional
95   * configuration parameter. The service factory implementation needs to
96   * connect to the assignment service EJB. In order to do so, it needs a
97   * JNDI name to look up the home interface. As JNDI names must be changeable
98   * by the application deployer, the name can't be hard coded.<P>
99   *
100  * This factory therefore uses the following ordered lookup procedure 
101  * to determine the JNDI name of the assignment service EJB home interface:
102  * <ul>
103  *   <li>Look for a name in
104  *  <code>java:comp/env/de.danet.an.workflow.assignment.assignmentService</code>. 
105  *     The configuration for
106  *     <code>StandardResourceAssignmentServiceFactory</code> using 
107  *     this mechanism thus looks like:
108  * <PRE> &lt;env-entry&gt;
109  *   &lt;description&gt;Configure the chosen factory&lt;/description&gt;
110  *   &lt;env-entry-name&gt;de.danet.an.workflow.assignment.assignmentService&lt;/env-entry-name&gt;
111  *   &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
112  *   &lt;env-entry-value&gt;<i>JNDI name of assignment service EJB home</i>&lt;/env-entry-value&gt;
113  * &lt;/env-entry&gt;</PRE>
114  *     Note that this environment entry must be inserted in the
115  *     <code>ejb-jar.xml</code> or <code>web.xml</code> for every EJB
116  *     resp. servlet that calls the
117  *     {@link de.danet.an.workflow.spis.ras.ResourceAssignmentServiceFactory#newInstance
118  *     <code>newInstance</code>} method of 
119  *     <code>ResourceAssignmentServiceFactory</code>.</li>
120  *
121  *   <li>Find the application resource file 
122  *     <code>de/danet/an/workflow/ras.properties</code>
123  *     and look for an entry "<code>assignmentService 
124  *     = <i>JNDI name of assignment service EJB home</i></code>".</li>
125  *
126  *   <li>Use the fallback JNDI name
127  *     <code>de.danet.an.workflow.assignment.AssignmentService</code>.</li>
128  * </ul>
129  *
130  * This implementation uses an instance of {@link
131  * de.danet.an.workflow.spis.rms.ResourceManagementService
132  * <code>ResourceManagementService</code>} to access a resource
133  * management facility. To obtain the service it calls the {@link
134  * de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory#newInstance
135  * <code>newInstance</code>} method of {@link
136  * de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory
137  * <code>ResourceManagementServiceFactory</code>}. Thus if this
138  * factory (<code>StandardResourceAssignmentServiceFactory</code>) is
139  * configured as resource assignment service factory, all
140  * configuration information required by
141  * <code>ResourceManagementServiceFactory.newInstance</code> (and by
142  * the actually configured resource management service factory
143  * implementation) must be available when the {@link
144  * de.danet.an.workflow.spis.ras.ResourceAssignmentServiceFactory#newInstance
145  * <code>newInstance</code>} method of
146  * <code>ResourceAssignmentServiceFactory</code> is called.
147  */
148 public class StandardResourceAssignmentServiceFactory
149     extends ResourceAssignmentServiceFactory {
150     
151     /** The configured resource management factory. */
152     private ResourceManagementServiceFactory rmsf = null;
153 
154     /** The assignment service passed to service instances. */
155     private AssignmentService as = null;
156 
157     /** The home interface name of the assignment service. */
158     private static String homeName = null;
159 
160     /**
161      * Constructor.
162      *
163      * @throws FactoryConfigurationError if the required resources cannot
164      * be obtained.
165      */
166     public StandardResourceAssignmentServiceFactory ()
167   throws FactoryConfigurationError {
168   // obtain base resource management service factory
169   try {
170       rmsf = ResourceManagementServiceFactory.newInstance();
171   } catch (de.danet.an.workflow.spis.rms.FactoryConfigurationError rme) {
172       throw new FactoryConfigurationError 
173     ("Required ResourceManagementServiceFactory not configured "
174      + "properly: " + rme.getMessage());
175   }
176 
177   // get home name of associated assignment EJB
178   if (homeName == null) {
179       homeName = findHomeName();
180   }
181   // Lookup assignment service home JNDI name in JNDI
182   try {
183       AssignmentServiceHome ash
184     = (AssignmentServiceHome)EJBUtil.lookupEJBHome
185     (AssignmentServiceHome.class, homeName);
186       as = ash.create();
187   } catch (NamingException nex) {
188       throw new FactoryConfigurationError 
189     ("ActivityServiceEJB home not bound: " + nex.getMessage());
190   } catch (CreateException cex) {
191       throw new FactoryConfigurationError 
192     ("Cannot create ActivityServiceEJB: " + cex.getMessage());
193   } catch (RemoteException rex) {
194       throw new FactoryConfigurationError 
195     ("Cannot create ActivityServiceEJB: " + rex.getMessage());
196   }
197     }
198 
199     /**
200      * Tries to find the name of the assignment service home interface
201      * as described {@link StandardResourceAssignmentServiceFactory
202      * for the class}.
203      */
204     private static String findHomeName () {
205   String home = null;
206   try {
207       home = (String)EJBUtil.lookupJNDIEntry
208     ("java:comp/env/de.danet.an.workflow.assignment"
209      + ".assignmentService");
210       return home;
211   } catch (NamingException ne) {
212       // Name not defined
213   }
214   // try properties
215   try {
216       InputStream is = StandardResourceAssignmentServiceFactory.class
217     .getResourceAsStream ("/de/danet/an/workflow/ras.properties");
218       if (is != null) {
219     Properties props = new Properties();
220     props.load (is);
221     home = props.getProperty ("assignmentService");
222     if (home != null) {
223         return home;
224     }
225       }
226   } catch (IOException ex) {
227   }
228   // fallback
229   return "de.danet.an.workflow.assignment.AssignmentService";
230     }
231 
232     /* Comment copied from interface. */
233     public ResourceAssignmentService newResourceAssignmentService ()
234   throws FactoryConfigurationError {
235   if (rmsf == null) {
236       throw new FactoryConfigurationError
237     ("Resource management service factory not configured.");
238   }
239   return new StandardResourceAssignmentService(as, rmsf);
240     }
241 
242     /**
243      * Two resource assignment service factories are equal if they are
244      * identically configured.
245      *
246      * @param obj the factory to compare with.
247      * @return <code>true</code> if the objects are equal.
248      */
249     public boolean equals (Object obj) {
250   StandardResourceAssignmentServiceFactory other
251       = (StandardResourceAssignmentServiceFactory)obj;
252   return (rmsf == other.rmsf);
253     }
254 
255     /**
256      * Generate a hash code.
257      *
258      * @return the hash code.
259      */
260     public int hashCode () {
261   return homeName.hashCode();
262     }
263 }