KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > fs > FSMangedConnectionFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.fs;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.security.acl.Group JavaDoc;
32 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
33 import javax.resource.spi.ConnectionManager JavaDoc;
34 import javax.resource.spi.ManagedConnection JavaDoc;
35 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
36 import javax.resource.ResourceException JavaDoc;
37 import javax.security.auth.Subject JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.security.SimplePrincipal;
41
42 /**
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 37406 $
45  */

46 public class FSMangedConnectionFactory
47    implements ManagedConnectionFactory JavaDoc, Serializable JavaDoc
48 {
49    private static long serialVersionUID = 100000;
50    private static Logger log = Logger.getLogger(FSMangedConnectionFactory.class);
51
52    private String JavaDoc userName;
53    private String JavaDoc password;
54    private Set JavaDoc roles;
55    private transient File JavaDoc rootDir;
56
57    /** Creates new FSMangedConnectionFactory */
58    public FSMangedConnectionFactory()
59    {
60    }
61
62    public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
63    {
64       log.debug("createConnectionFactory");
65       throw new UnsupportedOperationException JavaDoc("Cannot be used in unmanaed env");
66    }
67    public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cm) throws ResourceException JavaDoc
68    {
69       log.debug("createConnectionFactory, cm="+cm, new Exception JavaDoc("CalledBy:"));
70       FSRequestInfo fsInfo = new FSRequestInfo(rootDir);
71       return new DirContextFactoryImpl(cm, this, fsInfo);
72    }
73    public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject,
74       ConnectionRequestInfo JavaDoc info)
75       throws ResourceException JavaDoc
76    {
77       log.debug("createManagedConnection, subject="+subject+", info="+info,
78          new Exception JavaDoc("CalledBy:"));
79       FSRequestInfo fsInfo = (FSRequestInfo) info;
80       if( roles != null && roles.size() > 0 )
81       {
82          validateRoles(subject);
83       }
84       return new FSManagedConnection(subject, fsInfo);
85    }
86
87    public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connectionSet, Subject JavaDoc subject,
88       ConnectionRequestInfo JavaDoc info)
89       throws ResourceException JavaDoc
90    {
91       log.debug("matchManagedConnections, connectionSet="+connectionSet+", subject="+subject+", info="+info);
92       return (ManagedConnection JavaDoc) connectionSet.iterator().next();
93    }
94    public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
95    {
96       return null;
97    }
98    public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
99    {
100    }
101    public boolean equals(Object JavaDoc other)
102    {
103       return super.equals(other);
104    }
105    public int hashCode()
106    {
107       return super.hashCode();
108    }
109
110    public String JavaDoc getUserName()
111    {
112       return userName;
113    }
114    public void setUserName(String JavaDoc userName)
115    {
116       this.userName = userName;
117    }
118
119    public String JavaDoc getPassword()
120    {
121       return password;
122    }
123    public void setPassword(String JavaDoc password)
124    {
125       this.password = password;
126    }
127
128    public String JavaDoc getRoles()
129    {
130       return roles.toString();
131    }
132    public void setRoles(String JavaDoc roles)
133    {
134       this.roles = new HashSet JavaDoc();
135       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(roles, ",");
136       while( st.hasMoreTokens() )
137       {
138          String JavaDoc role = st.nextToken();
139          this.roles.add(role);
140       }
141    }
142
143    public void setFileSystemRootDir(String JavaDoc rootDirPath)
144    {
145       rootDir = new File JavaDoc(rootDirPath);
146       if( rootDir.exists() == false )
147          rootDir.mkdirs();
148       log.debug("setFileSystemRootDir, rootDir="+rootDir.getAbsolutePath(),
149          new Exception JavaDoc("CalledBy:"));
150    }
151
152    private void validateRoles(Subject JavaDoc theSubject)
153       throws ResourceException JavaDoc
154    {
155       Set JavaDoc subjectGroups = theSubject.getPrincipals(Group JavaDoc.class);
156       Iterator JavaDoc iter = subjectGroups.iterator();
157       Group JavaDoc roleGrp = null;
158       while (iter.hasNext())
159       {
160          Group JavaDoc grp = (Group JavaDoc) iter.next();
161          String JavaDoc name = grp.getName();
162          if (name.equals("Roles"))
163             roleGrp = grp;
164       }
165       if( roleGrp == null )
166          throw new ResourceException JavaDoc("Subject has not Roles");
167
168       boolean isValid = false;
169       iter = roles.iterator();
170       while( iter.hasNext() && isValid == false )
171       {
172          String JavaDoc name = (String JavaDoc) iter.next();
173          SimplePrincipal role = new SimplePrincipal(name);
174          isValid = roleGrp.isMember(role);
175       }
176       if( isValid == false )
177       {
178          String JavaDoc msg = "Authorization failure, subjectRoles="+roleGrp
179             + ", requiredRoles="+roles;
180          throw new ResourceException JavaDoc(msg);
181       }
182    }
183 }
184
Popular Tags