001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository.provider; 017 018import javax.jcr.LoginException; 019import javax.jcr.NoSuchWorkspaceException; 020import javax.jcr.Repository; 021import javax.jcr.RepositoryException; 022import javax.jcr.Session; 023import javax.jcr.SimpleCredentials; 024import javax.naming.InitialContext; 025import javax.naming.NamingException; 026 027import org.apache.avalon.framework.activity.Initializable; 028import org.apache.avalon.framework.component.Component; 029import org.apache.avalon.framework.configuration.ConfigurationException; 030import org.apache.avalon.framework.context.ContextException; 031import org.apache.avalon.framework.context.Contextualizable; 032import org.apache.cocoon.Constants; 033import org.apache.cocoon.environment.Context; 034 035import org.ametys.runtime.config.Config; 036 037/** 038 * JCR Repository obtained through JNDI 039 */ 040public class JNDIRepository extends AbstractRepository implements Initializable, Contextualizable, Component 041{ 042 private Context _context; 043 044 public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException 045 { 046 _context = (Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 047 } 048 049 public void initialize() throws Exception 050 { 051 // FIXME JNDI Repository should be compatible with safe mode to be able to run maintenance tasks 052 // But currently it relies on the config. 053 String name = Config.getInstance().getValueAsString("org.ametys.plugins.repository.jndi"); 054 055 InitialContext ctx = null; 056 try 057 { 058 ctx = new InitialContext(); 059 _delegate = (Repository) ctx.lookup(name); 060 061 // Register the delegate repository in the context for the repository workspace. 062 _context.setAttribute(CONTEXT_REPOSITORY_KEY, _delegate); 063 _context.setAttribute(CONTEXT_CREDENTIALS_KEY, new SimpleCredentials("ametys", new char[]{})); 064 _context.setAttribute(CONTEXT_IS_JNDI_KEY, true); 065 } 066 catch (NamingException e) 067 { 068 throw new ConfigurationException("Cannot lookup JNDI entry '" + name + "'", e); 069 } 070 finally 071 { 072 if (ctx != null) 073 { 074 try 075 { 076 ctx.close(); 077 } 078 catch (NamingException ignored) 079 { 080 // Ignored 081 } 082 } 083 } 084 } 085 086 public Session login(String workspace) throws LoginException, NoSuchWorkspaceException, RepositoryException 087 { 088 return _delegate.login(new SimpleCredentials("ametys", new char[]{}), workspace); 089 } 090}