/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
* 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: PropsRmsManagedConnection.java 2372 2007-05-17 21:53:00Z mlipp $
*
* $Log$
* Revision 1.2 2006/09/29 12:32:07 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1 2006/09/24 20:57:16 mlipp
* Moved RMS implementations in own sub-package.
*
* Revision 1.8 2006/09/13 12:20:22 drmlipp
* Fixed listResources.
*
* Revision 1.7 2006/07/11 12:49:46 drmlipp
* Several problems fixed.
*
* Revision 1.6 2006/07/11 11:13:13 drmlipp
* Props RMS running.
*
* Revision 1.5 2006/07/11 08:58:15 drmlipp
* Added another invalidation.
*
* Revision 1.4 2006/07/10 20:09:33 mlipp
* Fixed comment.
*
* Revision 1.3 2006/07/10 20:07:00 mlipp
* Fixed connection handling.
*
* Revision 1.2 2006/07/10 13:31:30 drmlipp
* Fixed problem with unspecified subject.
*
* Revision 1.1 2006/07/05 10:58:52 drmlipp
* Renamed package.
*
* Revision 1.1 2006/07/05 10:53:26 drmlipp
* Separated generic RMS adapter client from properties based adapter.
*
* Revision 1.1 2006/07/04 16:28:30 drmlipp
* Started.
*
*/
package de.danet.an.workflow.rmsimpls.propsrmsra;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import javax.naming.NameNotFoundException;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionRequestInfo;
import javax.security.auth.Subject;
import de.danet.an.util.ra.ConnectionSupport;
import de.danet.an.util.ra.ManagedConnectionFactorySupport;
import de.danet.an.util.ra.ManagedConnectionSupport;
import de.danet.an.workflow.rmsimpls.eisrms.aci.RmsEntry;
/**
* This class provides the managed connection used by the resource adapter
* for the properties files based RMS.
* @author Michael Lipp
*/
public class PropsRmsManagedConnection extends ManagedConnectionSupport {
private Properties usersProps;
private Properties groupsProps;
private Properties rolesProps;
/**
* Create a new instance.
* @param subject the subject
*/
public PropsRmsManagedConnection
(ManagedConnectionFactorySupport mcf, Subject subject,
Properties usersProps, Properties groupsProps, Properties rolesProps) {
super (mcf, subject);
this.usersProps = usersProps;
this.groupsProps = groupsProps;
this.rolesProps = rolesProps;
}
/* (non-Javadoc)
* Comment copied from interface or superclass.
*/
protected void doDestroy() {
usersProps = null;
groupsProps = null;
rolesProps = null;
}
/* (non-Javadoc)
* Comment copied from interface or superclass.
*/
protected ConnectionSupport createConnection
(Subject subject, ConnectionRequestInfo cxRequestInfo) {
return new PropsRmsConnection();
}
/* (non-Javadoc)
* @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#lookupResource
*/
public RmsEntry lookupResource(String key)
throws ResourceException, NameNotFoundException {
RmsEntry entry = findResource(key);
if (entry == null) {
throw new NameNotFoundException("Unknown key \"" + key + "\"");
}
return entry;
}
/**
* @param key
* @return
* @throws NameNotFoundException
*/
private RmsEntry findResource(String key) {
Properties props = null;
int type = 0;
if (key.startsWith("M:")) {
String propKey = key.substring(2);
if (!usersProps.containsKey(propKey)) {
return null;
}
return new RmsEntry (type, key, propKey);
} else if (key.startsWith("G:")) {
props = groupsProps;
type = RmsEntry.RESOURCE_TYPE_GROUP;
} else if (key.startsWith("R:")) {
props = rolesProps;
type = RmsEntry.RESOURCE_TYPE_ROLE;
} else {
return null;
}
String propKey = key.substring(2);
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
String user = (String)e.nextElement();
String val = props.getProperty(user);
for (StringTokenizer st = new StringTokenizer(val, ",");
st.hasMoreTokens();) {
String entry = st.nextToken().trim();
if (entry.equals(propKey)) {
return new RmsEntry(type, key, propKey);
}
}
}
return null;
}
/* (non-Javadoc)
* @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#findResource
*/
public RmsEntry lookupUserByName(String name)
throws NameNotFoundException {
if (!usersProps.containsKey(name)) {
throw new NameNotFoundException("Unknown user \"" + name + "\"");
}
return new RmsEntry (RmsEntry.RESOURCE_TYPE_USER, "M:" + name, name);
}
/* (non-Javadoc)
* @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#authorizers
*/
public Collection authorizers(String key) throws ResourceException {
Collection res = new ArrayList ();
String user = key.substring(2);
res.addAll(getAuths
(user, RmsEntry.RESOURCE_TYPE_GROUP, "G:", groupsProps));
res.addAll(getAuths
(user, RmsEntry.RESOURCE_TYPE_ROLE, "R:", rolesProps));
return res;
}
private Collection getAuths
(String user, int type, String prefix, Properties props) {
Collection res = new ArrayList ();
if (props.containsKey(user)) {
String val = props.getProperty(user);
for (StringTokenizer st = new StringTokenizer(val, ",");
st.hasMoreTokens();) {
String entry = st.nextToken().trim();
res.add(new RmsEntry(type, prefix + entry, entry));
}
}
return res;
}
/* (non-Javadoc)
* @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#listResources()
*/
public Collection listResources() throws ResourceException {
Collection res = new ArrayList ();
Set keys = new HashSet ();
res.addAll
(listEntries(keys, RmsEntry.RESOURCE_TYPE_GROUP, groupsProps));
res.addAll(listEntries(keys, RmsEntry.RESOURCE_TYPE_ROLE, rolesProps));
for (Iterator i = keys.iterator(); i.hasNext();) {
String user = (String)i.next();
res.add (new RmsEntry
(RmsEntry.RESOURCE_TYPE_USER, "M:" + user, user));
}
return res;
}
private Collection listEntries (Set keys, int type, Properties props) {
Set entries = new HashSet ();
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
String user = (String)e.nextElement();
keys.add (user);
String val = props.getProperty(user);
for (StringTokenizer st = new StringTokenizer(val, ",");
st.hasMoreTokens();) {
entries.add(st.nextToken().trim());
}
}
Collection res = new ArrayList ();
for (Iterator i = entries.iterator(); i.hasNext();) {
String entry = (String)i.next();
String prefix = (type==RmsEntry.RESOURCE_TYPE_GROUP ? "G:" : "R:");
res.add(new RmsEntry(type, prefix + entry, entry));
}
return res;
}
/* (non-Javadoc)
* @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#selectResources
*/
public Collection selectResources(Object resSel) throws ResourceException {
Collection res = new ArrayList ();
if (!(resSel instanceof String)) {
return res;
}
RmsEntry entry = findResource((String)resSel);
if (entry != null) {
res.add(entry);
}
return res;
}
}