1 17 18 19 20 package org.apache.lenya.ac.file; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.configuration.Configurable; 31 import org.apache.avalon.framework.parameters.ParameterException; 32 import org.apache.avalon.framework.parameters.Parameterizable; 33 import org.apache.avalon.framework.parameters.Parameters; 34 import org.apache.avalon.framework.service.ServiceException; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.framework.service.Serviceable; 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.source.SourceResolver; 39 import org.apache.lenya.ac.AccessControlException; 40 import org.apache.lenya.ac.GroupManager; 41 import org.apache.lenya.ac.IPRangeManager; 42 import org.apache.lenya.ac.RoleManager; 43 import org.apache.lenya.ac.UserManager; 44 import org.apache.lenya.ac.UserType; 45 import org.apache.lenya.ac.impl.AbstractAccreditableManager; 46 47 import org.apache.cocoon.util.NetUtils; 48 49 52 public class FileAccreditableManager extends AbstractAccreditableManager implements Serviceable, 53 Configurable, Parameterizable { 54 55 60 public FileAccreditableManager() { 61 } 62 63 69 public FileAccreditableManager(File configurationDirectory, UserType[] userTypes) { 70 assert configurationDirectory != null; 71 assert configurationDirectory.exists(); 72 assert configurationDirectory.isDirectory(); 73 this.configurationDirectory = configurationDirectory; 74 this.userTypes = new HashSet (Arrays.asList(userTypes)); 75 } 76 77 private File configurationDirectory; 78 private Set userTypes; 79 80 85 public UserType[] getUserTypes() throws AccessControlException { 86 if (userTypes == null) 87 throw new AccessControlException("User types not initialized"); 88 return (UserType[]) userTypes.toArray(new UserType[userTypes.size()]); 89 } 90 91 97 public File getConfigurationDirectory() throws AccessControlException { 98 99 if (configurationDirectory == null) { 100 101 if (configurationDirectoryPath == null) { 102 throw new AccessControlException("Configuration directory not set!"); 103 } 104 105 Source source = null; 106 SourceResolver resolver = null; 107 File directory; 108 try { 109 110 getLogger().debug( 111 "Configuration directory Path: [" + configurationDirectoryPath + "]"); 112 113 resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE); 114 source = resolver.resolveURI(configurationDirectoryPath); 115 116 getLogger().debug("Configuration directory URI: " + source.getURI()); 117 directory = new File (new URI (NetUtils.encodePath(source.getURI()))); 118 } catch (Exception e) { 119 throw new AccessControlException(e); 120 } finally { 121 if (resolver != null) { 122 if (source != null) { 123 resolver.release(source); 124 } 125 getManager().release(resolver); 126 } 127 } 128 setConfigurationDirectory(directory); 129 } 130 131 return configurationDirectory; 132 } 133 134 protected static final String DIRECTORY = "directory"; 135 136 139 public void parameterize(Parameters parameters) throws ParameterException { 140 if (parameters.isParameter(DIRECTORY)) { 141 configurationDirectoryPath = parameters.getParameter(DIRECTORY); 142 getLogger().debug("Configuration directory: [" + configurationDirectoryPath + "]"); 143 } 144 } 145 146 protected static final String A_M_TAG = "accreditable-manager"; 147 protected static final String U_M_CHILD_TAG = "user-manager"; 148 protected static final String U_T_CHILD_TAG = "user-type"; 149 protected static final String U_T_CLASS_ATTRIBUTE = "class"; 150 protected static final String U_T_CREATE_ATTRIBUTE = "create-use-case"; 151 protected static final String DEFAULT_USER_TYPE_CLASS = FileUser.class.getName(); 153 protected static final String DEFAULT_USER_TYPE_KEY = "Local User"; 154 protected static final String DEFAULT_USER_CREATE_USE_CASE = "userAddUser"; 155 156 160 public void configure(Configuration configuration) throws ConfigurationException { 161 if (A_M_TAG.equals(configuration.getName())) { 169 userTypes = new HashSet (); 170 Configuration umConf = configuration.getChild(U_M_CHILD_TAG, false); 171 if (umConf != null) { 172 Configuration[] typeConfs = umConf.getChildren(); 173 for (int i = 0; i < typeConfs.length; i++) { 174 userTypes.add(new UserType(typeConfs[i].getValue(), typeConfs[i] 175 .getAttribute(U_T_CLASS_ATTRIBUTE), typeConfs[i] 176 .getAttribute(U_T_CREATE_ATTRIBUTE))); 177 } 178 } else { 179 getLogger().debug( 180 "FileAccreditableManager: using default configuration for user types"); 181 userTypes.add(getDefaultUserType()); 184 } 185 } 188 } 189 190 194 public static UserType getDefaultUserType() { 195 return new UserType(DEFAULT_USER_TYPE_KEY, DEFAULT_USER_TYPE_CLASS, 196 DEFAULT_USER_CREATE_USE_CASE); 197 } 198 199 private String configurationDirectoryPath; 200 201 208 public void setConfigurationDirectory(File file) throws AccessControlException { 209 if (file == null || !file.isDirectory()) { 210 throw new AccessControlException("Configuration directory [" + file 211 + "] does not exist!"); 212 } 213 configurationDirectory = file; 214 } 215 216 private ServiceManager manager; 217 218 224 public void service(ServiceManager manager) throws ServiceException { 225 this.manager = manager; 226 } 227 228 233 protected ServiceManager getManager() { 234 return manager; 235 } 236 237 240 protected GroupManager initializeGroupManager() throws AccessControlException { 241 return FileGroupManager.instance(getConfigurationDirectory()); 242 } 243 244 247 protected IPRangeManager initializeIPRangeManager() throws AccessControlException { 248 return FileIPRangeManager.instance(getConfigurationDirectory()); 249 } 250 251 254 protected RoleManager initializeRoleManager() throws AccessControlException { 255 return FileRoleManager.instance(getConfigurationDirectory()); 256 } 257 258 261 protected UserManager initializeUserManager() throws AccessControlException { 262 return FileUserManager.instance(getConfigurationDirectory(), getUserTypes()); 263 } 264 265 } | Popular Tags |