/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: RmsService.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.2 2005/11/22 21:48:46 mlipp
* Continued.
*
* Revision 1.1 2005/11/16 16:35:09 drmlipp
* Started Liferay based RMS.
*
*/
package de.danet.an.workflow.liferayrms;
import java.rmi.RemoteException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.persistence.GroupUtil;
import com.liferay.portal.service.persistence.RoleUtil;
import com.liferay.portal.service.persistence.UserUtil;
import de.danet.an.workflow.jetspeedrms.Resource;
import de.danet.an.workflow.omgcore.WfResource;
import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
import de.danet.an.workflow.spis.rms.FactoryConfigurationError;
import de.danet.an.workflow.spis.rms.ResourceManagementService;
import de.danet.an.workflow.spis.rms.ResourceNotFoundException;
/**
* Implements the {@link
* de.danet.an.workflow.spis.rms.ResourceManagementService resource
* management service} based on the Jetspeed2 user management components.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 1607 $
*/
public class RmsService implements ResourceManagementService {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog(RmsService.class);
private ResourceAssignmentService ras = null;
/**
* Constructs a new resource management service.
*
* @param ctx the application context
* @param rasvc the resource assignment service
* @throws FactoryConfigurationError if the required resources
* cannot be obtained
*/
public RmsService (ResourceAssignmentService rasvc)
throws FactoryConfigurationError {
ras = rasvc;
init ();
}
/**
* Constructs a new resource management service. A context and resource
* assignment service must be set and init must be called after creating
* an instance with this constructor.
*
* @param ctx the application context
* @param rasvc the resource assignment service
* @throws FactoryConfigurationError if the required resources
* cannot be obtained
*/
public RmsService () {
}
/**
* Initialize the RMS. Called autromatically by the constructor
* that has the parameters. When the constructor without parameters
* is used, this must be called after setting the context and the
* resource assignment service.
*/
public void init () throws FactoryConfigurationError {
}
/**
* @return Returns the resource assignment service.
*/
public ResourceAssignmentService getResourceAssignmentService() {
return ras;
}
/**
* @param ras The resource assignment service to set.
*/
public void setResourceAssignmentService(ResourceAssignmentService ras) {
this.ras = ras;
}
/**
* Returns a nice representation of a user.
* @param the user
*/
private String userToName (User user) {
return user.getFullName();
}
/**
* Given a {@link java.security.Principal principal}, return the
* workflow resource associated with this principal by the
* resource management facility.<P>
*
* The implementation assumes that the given principal is the
* value assigned as principal by the associated
* {@link de.danet.an.staffmgmt.jbossx.StaffMemberLoginModule
* authentication component}.
*
* @param principal the principal.
* @return a <code>WfResource</code> object corresponding to the
* given principal.
* @throws ResourceNotFoundException if the StaffMember with the given key
* can't be found or the key is not associate with an StaffMember object.
* @throws RemoteException if a system-level error occurs.
*/
public WfResource asResource (Principal principal)
throws ResourceNotFoundException, RemoteException {
String name = principal.getName();
try {
User user = UserUtil.findByPrimaryKey(name);
return new Resource(ras, "U:" + name, userToName(user));
} catch (PortalException e) {
throw new ResourceNotFoundException
("No user with name \"" + name + "\"");
} catch (SystemException e) {
throw new ResourceNotFoundException
("No user with name \"" + name + "\"");
}
}
/* Comment copied from interface */
public Collection authorizers (WfResource wfResource)
throws RemoteException {
throw new UnsupportedOperationException();
}
/* Comment copied from interface */
public WfResource resourceByKey (String key)
throws ResourceNotFoundException, RemoteException {
if (key.startsWith("U:")) {
String name = key.substring(2);
try {
User user = UserUtil.findByPrimaryKey(name);
return new Resource(ras, key, userToName(user));
} catch (PortalException e) {
// fall through to "not found"
} catch (SystemException e) {
// fall through to "not found"
}
} else if (key.startsWith("G:")) {
String name = key.substring(2);
try {
GroupUtil.findByPrimaryKey(name);
return new Resource(ras, key, name);
} catch (PortalException e) {
} catch (SystemException e) {
}
} else if (key.startsWith("R:")) {
String name = key.substring(2);
try {
RoleUtil.findByPrimaryKey(name);
return new Resource(ras, key, name);
} catch (PortalException e) {
} catch (SystemException e) {
}
} else {
throw new IllegalArgumentException
("Resource with invalid key " + key);
}
throw new ResourceNotFoundException
("Unknown resource: \"" + key + "\"");
}
/* Comment copied from interface. */
public Collection listResources () throws RemoteException {
List res = new ArrayList ();
return res;
}
/**
* The <code>resSel</code> paramter is evaluated if it is of type
* <code>java.lang.String</code> only. It takes the
* following format:
* <dl>
* <dt><code>R:<i>role name</i></code></dt>
* <dd>Selects the role with the given name.</dd>
* <dt><code>G:<i>group name</i></code></dt>
* <dd>Selects the group with the given name.</dd>
* <dt><code>M:<i>member id</i></code></dt>
* <dd>Selects the member with the given id.</dd>
* </dl>
*
* @param resSel an object that describes resource selection criteria.
* @return collection of <code>WfResource</code> objects.
* @throws RemoteException if a system-level error occurs.
* @throws UnsupportedOperationException if the resource management
* service does not support this feature.
*/
public Collection selectResources (Object resSel)
throws RemoteException, UnsupportedOperationException {
Collection res = new ArrayList();
if (resSel == null || !(resSel instanceof String)) {
return res;
}
String crit = (String)resSel;
if (crit.startsWith("U:")) {
String name = crit.substring(2);
try {
User user = UserUtil.findByPrimaryKey(name);
res.add(new Resource(ras, crit, userToName(user)));
} catch (PortalException e) {
// fall through to "not found"
} catch (SystemException e) {
// fall through to "not found"
}
} else if (crit.startsWith("G:")) {
String name = crit.substring(2);
try {
GroupUtil.findByPrimaryKey(name);
res.add(new Resource(ras, crit, name));
} catch (PortalException e) {
} catch (SystemException e) {
}
} else if (crit.startsWith("R:")) {
String name = crit.substring(2);
try {
RoleUtil.findByPrimaryKey(name);
res.add(new Resource(ras, crit, name));
} catch (PortalException e) {
} catch (SystemException e) {
}
}
return res;
}
}