001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
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: RmsService.java,v 1.3 2006/09/29 12:32:13 drmlipp Exp $
021: *
022: * $Log: RmsService.java,v $
023: * Revision 1.3 2006/09/29 12:32:13 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.2 2005/11/22 21:48:46 mlipp
027: * Continued.
028: *
029: * Revision 1.1 2005/11/16 16:35:09 drmlipp
030: * Started Liferay based RMS.
031: *
032: */
033: package de.danet.an.workflow.liferayrms;
034:
035: import java.rmi.RemoteException;
036: import java.security.Principal;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.List;
040:
041: import com.liferay.portal.PortalException;
042: import com.liferay.portal.SystemException;
043: import com.liferay.portal.model.User;
044: import com.liferay.portal.service.persistence.GroupUtil;
045: import com.liferay.portal.service.persistence.RoleUtil;
046: import com.liferay.portal.service.persistence.UserUtil;
047:
048: import de.danet.an.workflow.jetspeedrms.Resource;
049: import de.danet.an.workflow.omgcore.WfResource;
050: import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
051: import de.danet.an.workflow.spis.rms.FactoryConfigurationError;
052: import de.danet.an.workflow.spis.rms.ResourceManagementService;
053: import de.danet.an.workflow.spis.rms.ResourceNotFoundException;
054:
055: /**
056: * Implements the {@link
057: * de.danet.an.workflow.spis.rms.ResourceManagementService resource
058: * management service} based on the Jetspeed2 user management components.
059: *
060: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
061: * @version $Revision: 1.3 $
062: */
063:
064: public class RmsService implements ResourceManagementService {
065:
066: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
067: .getLog(RmsService.class);
068:
069: private ResourceAssignmentService ras = null;
070:
071: /**
072: * Constructs a new resource management service.
073: *
074: * @param ctx the application context
075: * @param rasvc the resource assignment service
076: * @throws FactoryConfigurationError if the required resources
077: * cannot be obtained
078: */
079: public RmsService(ResourceAssignmentService rasvc)
080: throws FactoryConfigurationError {
081: ras = rasvc;
082: init();
083: }
084:
085: /**
086: * Constructs a new resource management service. A context and resource
087: * assignment service must be set and init must be called after creating
088: * an instance with this constructor.
089: *
090: * @param ctx the application context
091: * @param rasvc the resource assignment service
092: * @throws FactoryConfigurationError if the required resources
093: * cannot be obtained
094: */
095: public RmsService() {
096: }
097:
098: /**
099: * Initialize the RMS. Called autromatically by the constructor
100: * that has the parameters. When the constructor without parameters
101: * is used, this must be called after setting the context and the
102: * resource assignment service.
103: */
104: public void init() throws FactoryConfigurationError {
105: }
106:
107: /**
108: * @return Returns the resource assignment service.
109: */
110: public ResourceAssignmentService getResourceAssignmentService() {
111: return ras;
112: }
113:
114: /**
115: * @param ras The resource assignment service to set.
116: */
117: public void setResourceAssignmentService(
118: ResourceAssignmentService ras) {
119: this .ras = ras;
120: }
121:
122: /**
123: * Returns a nice representation of a user.
124: * @param the user
125: */
126: private String userToName(User user) {
127: return user.getFullName();
128: }
129:
130: /**
131: * Given a {@link java.security.Principal principal}, return the
132: * workflow resource associated with this principal by the
133: * resource management facility.<P>
134: *
135: * The implementation assumes that the given principal is the
136: * value assigned as principal by the associated
137: * {@link de.danet.an.staffmgmt.jbossx.StaffMemberLoginModule
138: * authentication component}.
139: *
140: * @param principal the principal.
141: * @return a <code>WfResource</code> object corresponding to the
142: * given principal.
143: * @throws ResourceNotFoundException if the StaffMember with the given key
144: * can't be found or the key is not associate with an StaffMember object.
145: * @throws RemoteException if a system-level error occurs.
146: */
147: public WfResource asResource(Principal principal)
148: throws ResourceNotFoundException, RemoteException {
149: String name = principal.getName();
150: try {
151: User user = UserUtil.findByPrimaryKey(name);
152: return new Resource(ras, "U:" + name, userToName(user));
153: } catch (PortalException e) {
154: throw new ResourceNotFoundException("No user with name \""
155: + name + "\"");
156: } catch (SystemException e) {
157: throw new ResourceNotFoundException("No user with name \""
158: + name + "\"");
159: }
160: }
161:
162: /* Comment copied from interface */
163: public Collection authorizers(WfResource wfResource)
164: throws RemoteException {
165: throw new UnsupportedOperationException();
166: }
167:
168: /* Comment copied from interface */
169: public WfResource resourceByKey(String key)
170: throws ResourceNotFoundException, RemoteException {
171: if (key.startsWith("U:")) {
172: String name = key.substring(2);
173: try {
174: User user = UserUtil.findByPrimaryKey(name);
175: return new Resource(ras, key, userToName(user));
176: } catch (PortalException e) {
177: // fall through to "not found"
178: } catch (SystemException e) {
179: // fall through to "not found"
180: }
181: } else if (key.startsWith("G:")) {
182: String name = key.substring(2);
183: try {
184: GroupUtil.findByPrimaryKey(name);
185: return new Resource(ras, key, name);
186: } catch (PortalException e) {
187: } catch (SystemException e) {
188: }
189: } else if (key.startsWith("R:")) {
190: String name = key.substring(2);
191: try {
192: RoleUtil.findByPrimaryKey(name);
193: return new Resource(ras, key, name);
194: } catch (PortalException e) {
195: } catch (SystemException e) {
196: }
197: } else {
198: throw new IllegalArgumentException(
199: "Resource with invalid key " + key);
200: }
201: throw new ResourceNotFoundException("Unknown resource: \""
202: + key + "\"");
203: }
204:
205: /* Comment copied from interface. */
206: public Collection listResources() throws RemoteException {
207: List res = new ArrayList();
208: return res;
209: }
210:
211: /**
212: * The <code>resSel</code> paramter is evaluated if it is of type
213: * <code>java.lang.String</code> only. It takes the
214: * following format:
215: * <dl>
216: * <dt><code>R:<i>role name</i></code></dt>
217: * <dd>Selects the role with the given name.</dd>
218: * <dt><code>G:<i>group name</i></code></dt>
219: * <dd>Selects the group with the given name.</dd>
220: * <dt><code>M:<i>member id</i></code></dt>
221: * <dd>Selects the member with the given id.</dd>
222: * </dl>
223: *
224: * @param resSel an object that describes resource selection criteria.
225: * @return collection of <code>WfResource</code> objects.
226: * @throws RemoteException if a system-level error occurs.
227: * @throws UnsupportedOperationException if the resource management
228: * service does not support this feature.
229: */
230: public Collection selectResources(Object resSel)
231: throws RemoteException, UnsupportedOperationException {
232: Collection res = new ArrayList();
233: if (resSel == null || !(resSel instanceof String)) {
234: return res;
235: }
236: String crit = (String) resSel;
237: if (crit.startsWith("U:")) {
238: String name = crit.substring(2);
239: try {
240: User user = UserUtil.findByPrimaryKey(name);
241: res.add(new Resource(ras, crit, userToName(user)));
242: } catch (PortalException e) {
243: // fall through to "not found"
244: } catch (SystemException e) {
245: // fall through to "not found"
246: }
247: } else if (crit.startsWith("G:")) {
248: String name = crit.substring(2);
249: try {
250: GroupUtil.findByPrimaryKey(name);
251: res.add(new Resource(ras, crit, name));
252: } catch (PortalException e) {
253: } catch (SystemException e) {
254: }
255: } else if (crit.startsWith("R:")) {
256: String name = crit.substring(2);
257: try {
258: RoleUtil.findByPrimaryKey(name);
259: res.add(new Resource(ras, crit, name));
260: } catch (PortalException e) {
261: } catch (SystemException e) {
262: }
263: }
264: return res;
265: }
266: }
|