1 9 package org.jboss.portal.core.plugins.security; 10 11 import java.util.ArrayList ; 12 import java.util.Collections ; 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import javax.servlet.ServletContext ; 21 import javax.naming.InitialContext ; 22 import javax.naming.NamingException ; 23 24 import org.apache.log4j.Logger; 25 import org.jboss.portal.common.FQN; 26 import org.jboss.portal.common.metadata.MetaData; 27 import org.jboss.portal.core.metadata.ItemMetaData; 28 import org.jboss.portal.core.metadata.ModelMetaData; 29 import org.jboss.portal.core.metadata.PermissionDescriptionMetaData; 30 import org.jboss.portal.core.metadata.PermissionMetaData; 31 import org.jboss.portal.core.metadata.SchemeMetaData; 32 import org.jboss.portal.core.security.Item; 33 import org.jboss.portal.core.security.Model; 34 import org.jboss.portal.core.security.PermissionDescription; 35 import org.jboss.portal.core.security.Scheme; 36 import org.jboss.portal.core.security.SchemeStore; 37 import org.jboss.portal.core.modules.SecurityModule; 38 import org.jboss.portal.core.modules.ModuleConstants; 39 import org.jboss.portal.server.Application; 40 import org.jboss.portal.server.Component; 41 import org.jboss.portal.server.kernel.Service; 42 import org.jboss.portal.server.kernel.ServiceImplementation; 43 import org.jboss.portal.server.plugins.PluginService; 44 45 49 public class ModelPlugin extends PluginService implements Model 50 { 51 52 private static final Logger log = Logger.getLogger(ModelPlugin.class); 53 54 55 private ModelMetaData metaData; 56 57 58 private ModelContentProvider contentProvider; 59 60 61 private Component component; 62 63 64 private Map permissionDescriptions; 65 66 67 private Map impliedMap; 68 69 70 private Scheme defaultScheme; 71 72 73 private SecurityModule module; 74 75 public ModelPlugin() 76 { 77 permissionDescriptions = new HashMap (); 78 impliedMap = new HashMap (); 79 defaultScheme = new Scheme(); 80 } 81 82 public void create() throws Exception 83 { 84 String contentProviderClass = metaData.getContentProviderClass(); 86 if (contentProviderClass != null) 87 { 88 Application app = component.getApplication(); 89 ClassLoader loader = app.getClassLoader(); 90 try 91 { 92 Class clazz = loader.loadClass(contentProviderClass); 93 contentProvider = (ModelContentProvider)clazz.newInstance(); 94 } 95 catch (ClassNotFoundException e) 96 { 97 log.error("Class not found for model content provider = " + contentProviderClass); 98 } 99 catch (InstantiationException e) 100 { 101 log.error("Cannot create instance of model content provider = " + contentProviderClass); 102 } 103 catch (IllegalAccessException e) 104 { 105 log.error("Cannot create instance of model content provider = " + contentProviderClass); 106 } 107 } 108 109 for (Iterator i = metaData.getPermissionDescriptions().iterator();i.hasNext();) 111 { 112 PermissionDescriptionMetaData permissionDescriptionMD = (PermissionDescriptionMetaData)i.next(); 113 114 String name = permissionDescriptionMD.getName(); 115 String description = permissionDescriptionMD.getDescription(); 116 117 PermissionDescription permissionDescription = new PermissionDescription(name, description); 118 permissionDescriptions.put(name, permissionDescription); 119 impliedMap.put(name, new HashSet (permissionDescriptionMD.getImpliedSet())); 120 } 121 122 for (Iterator i = impliedMap.entrySet().iterator();i.hasNext();) 124 { 125 Map.Entry entry = (Map.Entry )i.next(); 126 String source = (String )entry.getKey(); 127 Set targets = (Set )entry.getValue(); 128 129 while (true) 131 { 132 if (targets.contains(source)) 134 { 135 throw new Exception ("Not an acyclic graph"); 136 } 137 int size = targets.size(); 138 for (Iterator j = new HashSet (targets).iterator();j.hasNext();) 139 { 140 String target = (String )j.next(); 141 Set targetTargets = (Set )impliedMap.get(target); 142 143 if (targetTargets == null) 145 { 146 throw new Exception ("Missing permission " + target); 147 } 148 targets.addAll(targetTargets); 149 } 150 if (size == targets.size()) 151 { 152 break; 153 } 154 } 155 } 156 157 defaultScheme = createScheme(metaData.getScheme()); 158 } 159 160 public static Scheme createScheme(SchemeMetaData schemeMD) throws Exception 161 { 162 Scheme scheme = new Scheme(); 163 final Map pathToRolesToPermissions = new HashMap (); 164 for (Iterator i = schemeMD.getItems().iterator();i.hasNext();) 165 { 166 ItemMetaData itemMD = (ItemMetaData)i.next(); 167 String path = itemMD.getPath(); 168 if (path.charAt(0) != '/') 169 { 170 throw new Exception ("Invalid path " + path); 172 } 173 List tmp = new ArrayList (); 174 int index = path.indexOf('/'); 175 while (index != -1) 176 { 177 String s = path.substring(0, index); 178 if (s.length() > 0) 179 { 180 tmp.add(s); 181 } 182 path = path.substring(index + 1); 183 index = path.indexOf('/'); 184 } 185 FQN fqn = new FQN((String [])tmp.toArray(new String [tmp.size()])); 186 Map roleNameToPermissionNames = new HashMap (); 187 for (Iterator j = itemMD.getPermissions().iterator();j.hasNext();) 188 { 189 PermissionMetaData permissionMD = (PermissionMetaData)j.next(); 190 String permissionName = permissionMD.getName(); 191 Set roleNames = permissionMD.getRoleNames(); 192 for (Iterator k = roleNames.iterator();k.hasNext();) 193 { 194 String roleName = (String )k.next(); 195 Set permissionNames = (Set )roleNameToPermissionNames.get(roleName); 196 if (permissionNames == null) 197 { 198 permissionNames = new HashSet (); 199 roleNameToPermissionNames.put(roleName, permissionNames); 200 } 201 permissionNames.add(permissionName); 202 scheme.add((String [])tmp.toArray(new String [tmp.size()]), roleName, permissionName); 203 } 204 } 205 pathToRolesToPermissions.put(fqn, roleNameToPermissionNames); 206 } 207 return scheme; 208 } 209 210 public void start() throws Exception 211 { 212 if (contentProvider != null) 213 { 214 try 215 { 216 Application app = component.getApplication(); 217 ServletContext ctx = app.getServletContext(); 218 contentProvider.init(ctx); 219 } 220 catch (Exception e) 221 { 222 log.error("Error during content model provider initialialization", e); 223 } 224 } 225 } 226 227 public void stop() 228 { 229 if (contentProvider != null) 230 { 231 try 232 { 233 contentProvider.destroy(); 234 } 235 catch (Exception e) 236 { 237 log.error("Error during content model provider destructio", e); 238 } 239 } 240 } 241 242 public void destroy() 243 { 244 permissionDescriptions.clear(); 245 impliedMap.clear(); 246 defaultScheme = null; 247 contentProvider = null; 248 } 249 250 public Item getItem(String [] path) 251 { 252 if (contentProvider == null) 253 { 254 return null; 255 } 256 else 257 { 258 return contentProvider.getItem(path); 259 } 260 } 261 262 public Iterator getChildren(String [] path) 263 { 264 if (contentProvider == null) 265 { 266 return null; 267 } 268 else 269 { 270 return contentProvider.getChildren(path); 271 } 272 } 273 274 public Map getPermissionDescriptions() 275 { 276 return Collections.unmodifiableMap(permissionDescriptions); 277 } 278 279 public boolean implies(String sourcePermission, String targetPermission) 280 { 281 if (sourcePermission == null) 282 { 283 throw new IllegalArgumentException ("source must not be null"); 284 } 285 if (targetPermission == null) 286 { 287 throw new IllegalArgumentException ("target must not be null"); 288 } 289 if (sourcePermission.equals(targetPermission)) 290 { 291 return true; 292 } 293 Set impliedSet = (Set )impliedMap.get(sourcePermission); 294 if (impliedSet == null) 295 { 296 return false; 297 } 298 boolean implies = impliedSet.contains(targetPermission); 299 return implies; 300 } 301 302 public Scheme getDefaultScheme() 303 { 304 return defaultScheme; 305 } 306 307 public void setMetaData(MetaData metaData) 308 { 309 this.metaData = (ModelMetaData)metaData; 310 } 311 312 public MetaData getMetaData() 313 { 314 return metaData; 315 } 316 317 319 public void addIDependOn(ServiceImplementation implementation) 320 { 321 Service service = implementation.getService(); 322 if (service instanceof Component) 323 { 324 component = (Component)service; 325 } 326 } 327 328 public void removeIDependOn(ServiceImplementation implementation) 329 { 330 Service service = implementation.getService(); 331 if (service instanceof Component) 332 { 333 component = null; 334 } 335 } 336 337 public SchemeStore getSchemeStore() 338 { 339 if (module == null) 340 { 341 try 342 { 343 module = (SecurityModule)new InitialContext ().lookup(ModuleConstants.SECURITYMODULE_JNDINAME); 344 } 345 catch (NamingException e) 346 { 347 log.error("Cannot get security module", e); 348 } 349 } 350 return module.getStore(component.getID()); 351 } 352 } 353 | Popular Tags |