1 16 package org.apache.cocoon.jcr; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.configuration.Configurable; 21 import org.apache.avalon.framework.configuration.Configuration; 22 import org.apache.avalon.framework.configuration.ConfigurationException; 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.avalon.framework.logger.AbstractLogEnabled; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.avalon.framework.thread.ThreadSafe; 31 32 import org.apache.cocoon.components.ContextHelper; 33 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 34 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 35 import org.apache.cocoon.environment.ObjectModelHelper; 36 import org.apache.cocoon.environment.Request; 37 import org.apache.cocoon.sitemap.PatternException; 38 39 import org.apache.excalibur.source.SourceResolver; 40 import org.apache.excalibur.source.impl.FileSource; 41 42 import javax.jcr.Credentials; 43 import javax.jcr.LoginException; 44 import javax.jcr.NoSuchWorkspaceException; 45 import javax.jcr.Repository; 46 import javax.jcr.RepositoryException; 47 import javax.jcr.Session; 48 import javax.jcr.SimpleCredentials; 49 import java.util.Map ; 50 51 84 public abstract class AbstractRepository extends AbstractLogEnabled 85 implements Repository, ThreadSafe, Contextualizable, Serviceable, 86 Configurable, Disposable, Component { 87 88 91 public static final String ROLE = "javax.jcr.Repository"; 92 93 96 public static final String JCR_SESSION_REQUEST_ATTRIBUTE = "jcr-session"; 97 98 protected ServiceManager manager; 99 100 protected Context context; 101 102 protected Repository delegate; 103 104 107 protected VariableResolver loginResolver; 108 109 protected VariableResolver passwordResolver; 110 111 112 116 public void contextualize(Context context) throws ContextException { 117 this.context = context; 118 } 119 120 public void service(ServiceManager manager) throws ServiceException { 121 this.manager = manager; 122 } 123 124 public void configure(Configuration config) throws ConfigurationException { 125 Configuration jaas = config.getChild("jaas", false); 127 if (jaas != null) { 128 String jaasURI = jaas.getAttribute("src"); 129 SourceResolver resolver = null; 130 FileSource jaasSrc = null; 131 try { 132 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 133 jaasSrc = (FileSource) resolver.resolveURI(jaasURI); 134 if (System.getProperty("java.security.auth.login.config") == null) { 135 System.setProperty("java.security.auth.login.config", jaasSrc.getFile().getAbsolutePath()); 136 } else { 137 } 139 } catch (Exception e) { 140 throw new ConfigurationException("Cannot resolve jaas URI: " + jaasURI + " at " + config.getLocation()); 141 } finally { 142 if (jaasSrc != null) { 143 resolver.release(jaasSrc); 144 } 145 this.manager.release(resolver); 146 } 147 } 148 149 Configuration credentials = config.getChild("credentials", false); 150 if (credentials != null) { 151 String login = credentials.getAttribute("login"); 152 String password = credentials.getAttribute("password"); 153 154 try { 155 this.loginResolver = VariableResolverFactory.getResolver(login, this.manager); 156 } catch (PatternException e) { 157 throw new ConfigurationException("Invalid expression for 'login' at " + credentials.getLocation(), e); 158 } 159 try { 160 this.passwordResolver = VariableResolverFactory.getResolver(password, this.manager); 161 } catch (PatternException e) { 162 if (this.loginResolver instanceof Disposable) { 163 ((Disposable) this.loginResolver).dispose(); 164 } 165 this.loginResolver = null; 166 throw new ConfigurationException("Invalid expression for 'password' at " + credentials.getLocation(), e); 167 } 168 } 169 } 170 171 public void dispose() { 172 this.context = null; 173 this.delegate = null; 174 175 if (this.loginResolver instanceof Disposable) { 176 ((Disposable) this.loginResolver).dispose(); 177 } 178 this.loginResolver = null; 179 if (this.passwordResolver instanceof Disposable) { 180 ((Disposable) this.passwordResolver).dispose(); 181 } 182 this.passwordResolver = null; 183 this.manager = null; 184 } 185 186 190 public String getDescriptor(String key) { 191 return delegate.getDescriptor(key); 192 } 193 194 public String [] getDescriptorKeys() { 195 return delegate.getDescriptorKeys(); 196 } 197 198 public Session login() 199 throws LoginException, NoSuchWorkspaceException, RepositoryException { 200 Session session = getCachedSession(null); 201 202 if (session == null) { 203 Credentials creds = getCredentials(); 204 session = creds == null ? delegate.login() : delegate.login(creds); 205 cacheSession(session, null); 206 } 207 208 return session; 209 } 210 211 public Session login(Credentials creds) 212 throws LoginException, NoSuchWorkspaceException, RepositoryException { 213 Session session = getCachedSession(null); 214 215 if (session == null) { 216 session = delegate.login(creds); 217 cacheSession(session, null); 218 } 219 220 return session; 221 } 222 223 public Session login(Credentials creds, String workspace) 224 throws LoginException, NoSuchWorkspaceException, RepositoryException { 225 Session session = getCachedSession(workspace); 226 227 if (session == null) { 228 session = delegate.login(creds, workspace); 229 cacheSession(session, workspace); 230 } 231 232 return session; 233 } 234 235 public Session login(String workspace) 236 throws LoginException, NoSuchWorkspaceException, RepositoryException { 237 Session session = getCachedSession(workspace); 238 239 if (session == null) { 240 Credentials creds = getCredentials(); 241 session = creds == null ? delegate.login(workspace) : delegate.login(creds, workspace); 242 cacheSession(session, workspace); 243 } 244 245 return session; 246 } 247 248 250 254 private Session getCachedSession(String workspace) { 255 Map objectModel; 256 try { 257 objectModel = ContextHelper.getObjectModel(context); 258 } catch(Exception e) { 259 return null; 261 } 262 263 String attributeName = workspace == null ? 264 JCR_SESSION_REQUEST_ATTRIBUTE : JCR_SESSION_REQUEST_ATTRIBUTE + "/" + workspace; 265 266 Request request = ObjectModelHelper.getRequest(objectModel); 267 if (request == null) return null; 269 Session session = (Session) request.getAttribute(attributeName); 270 271 return (session != null && session.isLive()) ? session : null; 272 } 273 274 private void cacheSession(Session session, String workspace) { 275 Map objectModel; 276 try { 277 objectModel = ContextHelper.getObjectModel(context); 278 } catch(Exception e) { 279 return; 281 } 282 283 String attributeName = workspace == null ? 284 JCR_SESSION_REQUEST_ATTRIBUTE : JCR_SESSION_REQUEST_ATTRIBUTE + "/" + workspace; 285 286 Request request = ObjectModelHelper.getRequest(objectModel); 287 if (request == null) return; 289 request.setAttribute(attributeName, session); 290 } 291 292 private Credentials getCredentials() throws LoginException { 293 if (this.loginResolver != null) { 294 295 Map objectModel; 296 try { 297 objectModel = ContextHelper.getObjectModel(context); 298 } catch(Exception e) { 299 throw new LoginException("No objectModel to evaluate credentials", e); 301 } 302 303 try { 304 String login = this.loginResolver.resolve(objectModel); 305 String password = this.loginResolver.resolve(objectModel); 306 return new SimpleCredentials(login, password.toCharArray()); 307 } catch (PatternException e) { 308 throw new LoginException("Failed to evaluate credentials", e); 309 } 310 } else { 311 return null; 312 } 313 } 314 } 315 | Popular Tags |