1 17 18 19 20 package org.apache.lenya.cms.ac; 21 22 import java.io.File ; 23 24 import org.apache.avalon.framework.activity.Disposable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.logger.AbstractLogEnabled; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.avalon.framework.service.ServiceSelector; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceResolver; 35 import org.apache.excalibur.source.SourceUtil; 36 import org.apache.lenya.ac.AccessControlException; 37 import org.apache.lenya.ac.Accreditable; 38 import org.apache.lenya.ac.AccreditableManager; 39 import org.apache.lenya.ac.Policy; 40 import org.apache.lenya.ac.PolicyManager; 41 import org.apache.lenya.ac.impl.DefaultAccessController; 42 import org.apache.lenya.ac.impl.DefaultPolicy; 43 import org.apache.lenya.ac.impl.InheritingPolicyManager; 44 import org.apache.lenya.cms.publication.Document; 45 import org.apache.lenya.cms.publication.DocumentBuilder; 46 import org.apache.lenya.cms.publication.Publication; 47 import org.apache.lenya.cms.publication.PublicationFactory; 48 49 53 public class DocumentPolicyManagerWrapper 54 extends AbstractLogEnabled 55 implements InheritingPolicyManager, Serviceable, Configurable, Disposable { 56 57 60 public DocumentPolicyManagerWrapper() { 61 } 62 63 private InheritingPolicyManager policyManager; 64 private ServiceSelector policyManagerSelector; 65 66 73 protected String getPolicyURL(String webappUrl) throws AccessControlException { 74 75 if (getLogger().isDebugEnabled()) { 76 getLogger().debug("Resolving policy for webapp URL [" + webappUrl + "]"); 77 } 78 79 Publication publication = getPublication(webappUrl); 80 DocumentBuilder builder = publication.getDocumentBuilder(); 81 String url = null; 82 try { 83 if (builder.isDocument(publication, webappUrl)) { 84 Document document = builder.buildDocument(publication, webappUrl); 85 if (document.existsInAnyLanguage()) { 86 url = "/" + document.getArea() + document.getId(); 87 if (getLogger().isDebugEnabled()) { 88 getLogger().debug(" Document exists"); 89 getLogger().debug(" Document ID: [" + document.getId() + "]"); 90 } 91 } 92 } 93 } catch (Exception e) { 94 throw new AccessControlException(e); 95 } 96 97 if (url == null) { 98 if (getLogger().isDebugEnabled()) { 99 getLogger().debug(" Document does not exist."); 100 } 101 url = webappUrl.substring(("/" + publication.getId()).length()); 102 } 103 104 if (getLogger().isDebugEnabled()) { 105 getLogger().debug(" Using URL: [" + url + "]"); 106 } 107 return url; 108 } 109 110 117 protected Publication getPublication(String url) throws AccessControlException { 118 getLogger().debug("Building publication"); 119 120 Publication publication; 121 Source source = null; 122 SourceResolver resolver = null; 123 124 try { 125 resolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE); 126 source = resolver.resolveURI("context:///"); 127 File servletContext = SourceUtil.getFile(source); 128 getLogger().debug(" Webapp URL: [" + url + "]"); 129 getLogger().debug(" Serlvet context: [" + servletContext.getAbsolutePath() + "]"); 130 publication = PublicationFactory.getPublication(url, servletContext); 131 } catch (Exception e) { 132 throw new AccessControlException(e); 133 } finally { 134 if (resolver != null) { 135 if (source != null) { 136 resolver.release(source); 137 } 138 serviceManager.release(resolver); 139 } 140 } 141 return publication; 142 } 143 144 private ServiceManager serviceManager; 145 146 151 protected ServiceManager getServiceManager() { 152 return serviceManager; 153 } 154 155 158 public void service(ServiceManager manager) throws ServiceException { 159 this.serviceManager = manager; 160 } 161 162 165 public InheritingPolicyManager getPolicyManager() { 166 return policyManager; 167 } 168 169 172 public void setPolicyManager(InheritingPolicyManager policyManager) { 173 this.policyManager = policyManager; 174 } 175 176 180 public DefaultPolicy buildURLPolicy(AccreditableManager controller, String url) 181 throws AccessControlException { 182 return getPolicyManager().buildURLPolicy(controller, getPolicyURL(url)); 183 } 184 185 189 public DefaultPolicy buildSubtreePolicy(AccreditableManager controller, String url) 190 throws AccessControlException { 191 return getPolicyManager().buildSubtreePolicy(controller, getPolicyURL(url)); 192 } 193 194 198 public DefaultPolicy[] getPolicies(AccreditableManager controller, String url) 199 throws AccessControlException { 200 return getPolicyManager().getPolicies(controller, getPolicyURL(url)); 201 } 202 203 207 public void saveURLPolicy(String url, DefaultPolicy policy) throws AccessControlException { 208 getPolicyManager().saveURLPolicy(getPolicyURL(url), policy); 209 210 } 211 212 216 public void saveSubtreePolicy(String url, DefaultPolicy policy) throws AccessControlException { 217 getPolicyManager().saveSubtreePolicy(getPolicyURL(url), policy); 218 } 219 220 224 public Policy getPolicy(AccreditableManager controller, String url) 225 throws AccessControlException { 226 return getPolicyManager().getPolicy(controller, getPolicyURL(url)); 227 } 228 229 233 public void accreditableRemoved(AccreditableManager manager, Accreditable accreditable) 234 throws AccessControlException { 235 getPolicyManager().accreditableRemoved(manager, accreditable); 236 237 } 238 239 String ELEMENT_POLICY_MANAGER = "policy-manager"; 240 String ATTRIBUTE_TYPE = "type"; 241 242 245 public void configure(Configuration configuration) throws ConfigurationException { 246 Configuration policyManagerConfiguration = 247 configuration.getChild(ELEMENT_POLICY_MANAGER, false); 248 if (policyManagerConfiguration != null) { 249 String type = policyManagerConfiguration.getAttribute(ATTRIBUTE_TYPE); 250 try { 251 policyManagerSelector = 252 (ServiceSelector) getServiceManager().lookup(PolicyManager.ROLE + "Selector"); 253 254 PolicyManager policyManager = (PolicyManager) policyManagerSelector.select(type); 255 256 if (!(policyManager instanceof InheritingPolicyManager)) { 257 throw new AccessControlException( 258 "The " 259 + getClass().getName() 260 + " can only be used with an " 261 + InheritingPolicyManager.class.getName() 262 + "."); 263 } 264 265 DefaultAccessController.configureOrParameterize(policyManager, policyManagerConfiguration); 266 setPolicyManager((InheritingPolicyManager) policyManager); 267 } catch (Exception e) { 268 throw new ConfigurationException( 269 "Obtaining policy manager for type [" + type + "] failed: ", 270 e); 271 } 272 } 273 } 276 public void dispose() { 277 if (policyManagerSelector != null) { 278 if (getPolicyManager() != null) { 279 policyManagerSelector.release(getPolicyManager()); 280 } 281 getServiceManager().release(policyManagerSelector); 282 } 283 if (getLogger().isDebugEnabled()) { 284 getLogger().debug("Disposing [" + this +"]"); 285 } 286 287 } 288 289 292 public void accreditableAdded(AccreditableManager manager, Accreditable accreditable) throws AccessControlException { 293 getPolicyManager().accreditableAdded(manager, accreditable); 294 } 295 296 } 297 | Popular Tags |