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: PropsRmsManagedConnection.java,v 1.3 2007/05/17 21:53:00 mlipp Exp $
021: *
022: * $Log: PropsRmsManagedConnection.java,v $
023: * Revision 1.3 2007/05/17 21:53:00 mlipp
024: * Refactored resource adaptors.
025: *
026: * Revision 1.2 2006/09/29 12:32:07 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.1 2006/09/24 20:57:16 mlipp
030: * Moved RMS implementations in own sub-package.
031: *
032: * Revision 1.8 2006/09/13 12:20:22 drmlipp
033: * Fixed listResources.
034: *
035: * Revision 1.7 2006/07/11 12:49:46 drmlipp
036: * Several problems fixed.
037: *
038: * Revision 1.6 2006/07/11 11:13:13 drmlipp
039: * Props RMS running.
040: *
041: * Revision 1.5 2006/07/11 08:58:15 drmlipp
042: * Added another invalidation.
043: *
044: * Revision 1.4 2006/07/10 20:09:33 mlipp
045: * Fixed comment.
046: *
047: * Revision 1.3 2006/07/10 20:07:00 mlipp
048: * Fixed connection handling.
049: *
050: * Revision 1.2 2006/07/10 13:31:30 drmlipp
051: * Fixed problem with unspecified subject.
052: *
053: * Revision 1.1 2006/07/05 10:58:52 drmlipp
054: * Renamed package.
055: *
056: * Revision 1.1 2006/07/05 10:53:26 drmlipp
057: * Separated generic RMS adapter client from properties based adapter.
058: *
059: * Revision 1.1 2006/07/04 16:28:30 drmlipp
060: * Started.
061: *
062: */
063: package de.danet.an.workflow.rmsimpls.propsrmsra;
064:
065: import java.util.ArrayList;
066: import java.util.Collection;
067: import java.util.Enumeration;
068: import java.util.HashSet;
069: import java.util.Iterator;
070: import java.util.Properties;
071: import java.util.Set;
072: import java.util.StringTokenizer;
073:
074: import javax.naming.NameNotFoundException;
075: import javax.resource.ResourceException;
076: import javax.resource.spi.ConnectionRequestInfo;
077: import javax.security.auth.Subject;
078:
079: import de.danet.an.util.ra.ConnectionSupport;
080: import de.danet.an.util.ra.ManagedConnectionFactorySupport;
081: import de.danet.an.util.ra.ManagedConnectionSupport;
082: import de.danet.an.workflow.rmsimpls.eisrms.aci.RmsEntry;
083:
084: /**
085: * This class provides the managed connection used by the resource adapter
086: * for the properties files based RMS.
087: * @author Michael Lipp
088: */
089: public class PropsRmsManagedConnection extends ManagedConnectionSupport {
090:
091: private Properties usersProps;
092: private Properties groupsProps;
093: private Properties rolesProps;
094:
095: /**
096: * Create a new instance.
097: * @param subject the subject
098: */
099: public PropsRmsManagedConnection(
100: ManagedConnectionFactorySupport mcf, Subject subject,
101: Properties usersProps, Properties groupsProps,
102: Properties rolesProps) {
103: super (mcf, subject);
104:
105: this .usersProps = usersProps;
106: this .groupsProps = groupsProps;
107: this .rolesProps = rolesProps;
108: }
109:
110: /* (non-Javadoc)
111: * Comment copied from interface or superclass.
112: */
113: protected void doDestroy() {
114: usersProps = null;
115: groupsProps = null;
116: rolesProps = null;
117: }
118:
119: /* (non-Javadoc)
120: * Comment copied from interface or superclass.
121: */
122: protected ConnectionSupport createConnection(Subject subject,
123: ConnectionRequestInfo cxRequestInfo) {
124: return new PropsRmsConnection();
125: }
126:
127: /* (non-Javadoc)
128: * @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#lookupResource
129: */
130: public RmsEntry lookupResource(String key)
131: throws ResourceException, NameNotFoundException {
132: RmsEntry entry = findResource(key);
133: if (entry == null) {
134: throw new NameNotFoundException("Unknown key \"" + key
135: + "\"");
136: }
137: return entry;
138: }
139:
140: /**
141: * @param key
142: * @return
143: * @throws NameNotFoundException
144: */
145: private RmsEntry findResource(String key) {
146: Properties props = null;
147: int type = 0;
148: if (key.startsWith("M:")) {
149: String propKey = key.substring(2);
150: if (!usersProps.containsKey(propKey)) {
151: return null;
152: }
153: return new RmsEntry(type, key, propKey);
154: } else if (key.startsWith("G:")) {
155: props = groupsProps;
156: type = RmsEntry.RESOURCE_TYPE_GROUP;
157: } else if (key.startsWith("R:")) {
158: props = rolesProps;
159: type = RmsEntry.RESOURCE_TYPE_ROLE;
160: } else {
161: return null;
162: }
163: String propKey = key.substring(2);
164: for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
165: String user = (String) e.nextElement();
166: String val = props.getProperty(user);
167: for (StringTokenizer st = new StringTokenizer(val, ","); st
168: .hasMoreTokens();) {
169: String entry = st.nextToken().trim();
170: if (entry.equals(propKey)) {
171: return new RmsEntry(type, key, propKey);
172: }
173: }
174: }
175: return null;
176: }
177:
178: /* (non-Javadoc)
179: * @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#findResource
180: */
181: public RmsEntry lookupUserByName(String name)
182: throws NameNotFoundException {
183: if (!usersProps.containsKey(name)) {
184: throw new NameNotFoundException("Unknown user \"" + name
185: + "\"");
186: }
187: return new RmsEntry(RmsEntry.RESOURCE_TYPE_USER, "M:" + name,
188: name);
189: }
190:
191: /* (non-Javadoc)
192: * @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#authorizers
193: */
194: public Collection authorizers(String key) throws ResourceException {
195: Collection res = new ArrayList();
196: String user = key.substring(2);
197: res.addAll(getAuths(user, RmsEntry.RESOURCE_TYPE_GROUP, "G:",
198: groupsProps));
199: res.addAll(getAuths(user, RmsEntry.RESOURCE_TYPE_ROLE, "R:",
200: rolesProps));
201: return res;
202: }
203:
204: private Collection getAuths(String user, int type, String prefix,
205: Properties props) {
206: Collection res = new ArrayList();
207: if (props.containsKey(user)) {
208: String val = props.getProperty(user);
209: for (StringTokenizer st = new StringTokenizer(val, ","); st
210: .hasMoreTokens();) {
211: String entry = st.nextToken().trim();
212: res.add(new RmsEntry(type, prefix + entry, entry));
213: }
214: }
215: return res;
216: }
217:
218: /* (non-Javadoc)
219: * @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#listResources()
220: */
221: public Collection listResources() throws ResourceException {
222: Collection res = new ArrayList();
223: Set keys = new HashSet();
224: res.addAll(listEntries(keys, RmsEntry.RESOURCE_TYPE_GROUP,
225: groupsProps));
226: res.addAll(listEntries(keys, RmsEntry.RESOURCE_TYPE_ROLE,
227: rolesProps));
228: for (Iterator i = keys.iterator(); i.hasNext();) {
229: String user = (String) i.next();
230: res.add(new RmsEntry(RmsEntry.RESOURCE_TYPE_USER, "M:"
231: + user, user));
232: }
233: return res;
234: }
235:
236: private Collection listEntries(Set keys, int type, Properties props) {
237: Set entries = new HashSet();
238: for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
239: String user = (String) e.nextElement();
240: keys.add(user);
241: String val = props.getProperty(user);
242: for (StringTokenizer st = new StringTokenizer(val, ","); st
243: .hasMoreTokens();) {
244: entries.add(st.nextToken().trim());
245: }
246: }
247: Collection res = new ArrayList();
248: for (Iterator i = entries.iterator(); i.hasNext();) {
249: String entry = (String) i.next();
250: String prefix = (type == RmsEntry.RESOURCE_TYPE_GROUP ? "G:"
251: : "R:");
252: res.add(new RmsEntry(type, prefix + entry, entry));
253: }
254: return res;
255: }
256:
257: /* (non-Javadoc)
258: * @see de.danet.an.workflow.rmsimpls.eisrms.aci.RmsConnection#selectResources
259: */
260: public Collection selectResources(Object resSel)
261: throws ResourceException {
262: Collection res = new ArrayList();
263: if (!(resSel instanceof String)) {
264: return res;
265: }
266: RmsEntry entry = findResource((String) resSel);
267: if (entry != null) {
268: res.add(entry);
269: }
270: return res;
271: }
272:
273: }
|