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