001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: DatabaseRmsFactory.java,v 1.7 2006/10/15 19:29:51 mlipp Exp $
021: *
022: * $Log: DatabaseRmsFactory.java,v $
023: * Revision 1.7 2006/10/15 19:29:51 mlipp
024: * Merged changes from 1.4.x up to 1.4ea3pre1.
025: *
026: * Revision 1.6.2.1 2006/10/14 21:34:05 mlipp
027: * Simplified resource assignment service implementation.
028: *
029: * Revision 1.6 2006/10/03 11:13:05 mlipp
030: * Improved.
031: *
032: * Revision 1.5 2006/09/29 14:01:17 drmlipp
033: * Improved property names.
034: *
035: * Revision 1.4 2006/09/29 12:32:11 drmlipp
036: * Consistently using WfMOpen as projct name now.
037: *
038: * Revision 1.3 2006/09/29 11:39:16 drmlipp
039: * Various fixes.
040: *
041: * Revision 1.2 2006/09/28 21:28:09 mlipp
042: * Implementation continued.
043: *
044: * Revision 1.1 2006/09/28 15:03:56 drmlipp
045: * Getting started with db RMS.
046: *
047: */
048: package de.danet.an.workflow.rmsimpls.dbrms;
049:
050: import java.io.IOException;
051: import java.io.InputStream;
052: import java.util.Properties;
053:
054: import javax.naming.NamingException;
055:
056: import de.danet.an.util.EJBUtil;
057: import de.danet.an.workflow.spis.rms.FactoryConfigurationError;
058: import de.danet.an.workflow.spis.rms.ResourceManagementService;
059: import de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory;
060:
061: /**
062: * This class provides an implementaion of the factory API based on
063: * information obtained from a database.<P>
064: *
065: * Usage of this class as service factory requires several
066: * configuration parameters for accessing the database. The
067: * factory uses the following ordered lookup procedure to determine
068: * these parameters:
069: *
070: * <ul>
071: * <li>Look for parameters in JNDI. Lookup parameters in
072: * <code>java:comp/env/de.danet.an.workflow.rmsimpls.dbrms.<i>parameter</i></code>.
073: * The configuration for a parameter using
074: * this mechanism thus looks like:
075: * <PRE> <env-entry>
076: * <description>Configure database parameter <i>parameter</i></description>
077: * <env-entry-name>de.danet.an.workflow.rmsimpls.dbrms.<i>parameter</i></env-entry-name>
078: * <env-entry-type>java.lang.String</env-entry-type>
079: * <env-entry-value><i>Value of parameter</i></env-entry-value>
080: * </env-entry></PRE>
081: * Note that the environment entries must be inserted in the
082: * <code>ejb-jar.xml</code> or <code>web.xml</code> for every EJB
083: * resp. servlet that calls the
084: * {@link de.danet.an.workflow.spis.rms.ResourceManagementServiceFactory#newInstance
085: * <code>newInstance</code>} method of
086: * <code>ResourceManagementServiceFactory</code>.</li>
087: *
088: * <li>Find the application resource file
089: * <code>de.danet.an.workflow.rmsimpls.dbrms-factory.properties</code>
090: * and look for entries "<code><i>parameter</i>
091: * = <i>Value of parameter</i></code>".</li>
092: * </ul><P>
093: *
094: * The following parameters are used:
095: * <dl>
096: * <dt><code>dataSource</code></dt>
097: * <dd>The lookup name of the data source in JNDI
098: * (e.g. <code>java:/DefaultDS</code>).</dd>
099: * <dt><code>allUsersQuery</code>
100: * <dd>The query that returns the known users as a two column result with
101: * the user's entry's primary key in the first column and the user's
102: * display name in the second.</dd>
103: * <dt><code>allRolesQuery</code>
104: * <dd>The query that returns the known roles as a two column result with
105: * the role's entry's primary key in the first column and the role's
106: * display name in the second.</dd>
107: * <dt><code>allGroupsQuery</code>
108: * <dd>The query that returns the known groups as a two column result with
109: * the group's entry's primary key in the first column and group's
110: * display name in the second.</dd>
111: * <dt><code>userNameQuery</code></dt>
112: * <dd>The query that returns user data as a single column result with the
113: * user's display name in the only column. The user's entry's primary key
114: * is set as query parameter.</dd>
115: * <dt><code>roleNameQuery</code></dt>
116: * <dd>The query that returns role data as a single column result with the
117: * role's display name in the only column. The role's entry's primary key
118: * is set as query parameter.</dd>
119: * <dt><code>groupNameQuery</code></dt>
120: * <dd>The query that returns group data as a single column result with the
121: * group's display name in the only column. The group's entry's primary key
122: * is set as query parameter.</dd>
123: * <dt><code>userLookupQuery</code></dt>
124: * <dd>The query that returns a user's data as a two column result with the
125: * user's entry's primary key in the first column and the user's display
126: * name in the second. The user's account name (usually the login name) is
127: * set as query parameter.</dd>
128: * <dt><code>roleLookupQuery</code></dt>
129: * <dd>The query that returns a role's data as a two column result with the
130: * role's entry's primary key in the first column and the role's display
131: * name in the second. The role's "account" name (clear text name) is set
132: * as query parameter.</dd>
133: * <dt><code>groupLookupQuery</code></dt>
134: * <dd>The query that returns a group's data as a two column result with the
135: * group's entry's primary key in the first column and the group's name
136: * display in the second. The groups's "account" name (clear text name) is
137: * set as query parameter.</dd>
138: * <dt><code>rolesQuery</code></dt>
139: * <dd>The query that returns a user's roles as a two column result with the
140: * role's entry's primary key in the first column and the role's display
141: * name in the second. The user's entry's primary key is set as query
142: * parameter.</dd>
143: * <dt><code>groupsQuery</code></dt>
144: * <dd>The query that returns a user's groups as a two column result with the
145: * group's entry's primary key in the first column and the group's display
146: * name in the second. The user's entry's primary key is set as query
147: * parameter.</dd>
148: * </dl><P>
149: *
150: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
151: * @version $Revision: 1.7 $
152: */
153: public class DatabaseRmsFactory extends
154: ResourceManagementServiceFactory {
155:
156: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
157: .getLog(DatabaseRmsFactory.class);
158:
159: /**
160: * Create a new instance of this factory.
161: *
162: * @throws FactoryConfigurationError if configuration information is
163: * missing or invalid.
164: */
165: public DatabaseRmsFactory() throws FactoryConfigurationError {
166: }
167:
168: /* Comment copied from interface. */
169: public ResourceManagementService newResourceManagementService()
170: throws FactoryConfigurationError {
171: if (getResourceAssignmentContext() == null) {
172: throw new FactoryConfigurationError(
173: "Resource assignment context not configured.");
174: }
175:
176: // make fallbacks
177: Properties fallbacks = new Properties();
178:
179: // try to get properties from file
180: Properties baseProps = new Properties(fallbacks);
181: try {
182: InputStream is = DatabaseRmsFactory.class
183: .getResourceAsStream("/de.danet.an.workflow.rmsimpls.dbrms-factory.properties");
184: if (is != null) {
185: baseProps.load(is);
186: }
187: } catch (IOException ex) {
188: }
189:
190: // Overrides from JNDI
191: Properties props = new Properties(baseProps);
192: String[] knownProps = { "dataSource", "allUsersQuery",
193: "allRolesQuery", "allGroupsQuery", "userNameQuery",
194: "roleNameQuery", "groupNameQuery", "userLookupQuery",
195: "roleLookupQuery", "groupLookupQuery", "rolesQuery",
196: "groupsQuery" };
197: for (int i = 0; i < knownProps.length; i++) {
198: try {
199: String entry = (String) EJBUtil
200: .lookupJNDIEntry("java:comp/env/de.danet.an.workflow.rmsimpls.dbrms."
201: + knownProps[i]);
202: props.setProperty(knownProps[i], entry);
203: } catch (NamingException ne) {
204: // Name not defined
205: }
206: }
207: for (int i = 0; i < knownProps.length; i++) {
208: if (props.getProperty(knownProps[i]) == null) {
209: throw new FactoryConfigurationError(
210: "Missing property: " + knownProps[i]);
211: }
212: }
213:
214: return new DatabaseRmsService(props,
215: getResourceAssignmentContext());
216: }
217: }
|