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.Credentials;
019import javax.jcr.LoginException;
020import javax.jcr.NoSuchWorkspaceException;
021import javax.jcr.Repository;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024import javax.jcr.SimpleCredentials;
025import javax.jcr.Value;
026
027import org.apache.avalon.framework.logger.AbstractLogEnabled;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031
032/**
033 * Base class for JCR repository as a component.
034 */
035public abstract class AbstractRepository extends AbstractLogEnabled implements Repository, Serviceable 
036{
037    /**
038     * Role which shall be used for JCR repository implementations.
039     */
040    public static final String ROLE = "javax.jcr.Repository";
041    
042    /** The context repository key. */
043    public static final String CONTEXT_REPOSITORY_KEY = "org.ametys.repository.Repository";
044    
045    /** The credentials object key. */
046    public static final String CONTEXT_CREDENTIALS_KEY = "org.ametys.repository.Credentials";
047    
048    /** The JNDI variable key. */
049    public static final String CONTEXT_IS_JNDI_KEY = "org.ametys.repository.JNDI";
050    
051    /** Actual JCR Repository */
052    protected Repository _delegate;
053
054    /** Avalon ServiceManager */
055    protected ServiceManager _manager;
056    
057    private WorkspaceSelector _workspaceSelector;
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        _manager = manager;
063        _workspaceSelector = (WorkspaceSelector) manager.lookup(WorkspaceSelector.ROLE);
064    }
065    
066    public String getDescriptor(String key)
067    {
068        return _delegate.getDescriptor(key);
069    }
070
071    public String[] getDescriptorKeys()
072    {
073        return _delegate.getDescriptorKeys();
074    }
075    
076    @Override
077    public Value getDescriptorValue(String key)
078    {
079        return _delegate.getDescriptorValue(key);
080    }
081    
082    @Override
083    public Value[] getDescriptorValues(String key)
084    {
085        return _delegate.getDescriptorValues(key);
086    }
087    
088    @Override
089    public boolean isSingleValueDescriptor(String key)
090    {
091        return _delegate.isSingleValueDescriptor(key);
092    }
093    
094    @Override
095    public boolean isStandardDescriptor(String key)
096    {
097        return _delegate.isStandardDescriptor(key);
098    }
099
100    public Session login() throws LoginException, NoSuchWorkspaceException, RepositoryException
101    {
102        String workspace = _workspaceSelector.getWorkspace();
103        return login(workspace);
104    }
105
106    public Session login(Credentials creds) throws LoginException, NoSuchWorkspaceException, RepositoryException
107    {
108        String workspace = _workspaceSelector.getWorkspace();
109        return login(creds, workspace);
110    }
111
112    public Session login(Credentials creds, String workspace) throws LoginException, NoSuchWorkspaceException, RepositoryException
113    {
114        if (creds != null && !(creds instanceof SimpleCredentials))
115        {
116            throw new RepositoryException("Credentials must be instance of SimpleCredentials");
117        }
118        
119        if (creds != null)
120        {
121            String login = ((SimpleCredentials) creds).getUserID();
122        
123            if (!login.equals("ametys"))
124            {
125                throw new LoginException("The authentication is only allowed with the ametys login.");
126            }
127        }
128        
129        return login(workspace);
130    }
131}