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 */
016
017package org.ametys.workspaces.repository.jcr;
018
019import javax.jcr.Session;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.ServiceableGenerator;
026
027import org.ametys.plugins.repositoryapp.RepositoryProvider;
028
029/**
030 * Abstract generator dealing with repository
031 */
032public abstract class AbstractRepositoryGenerator extends ServiceableGenerator
033{
034    
035    /** The JCR repository session */
036    protected Session _session;
037    
038    /** The repository provider. */
039    protected RepositoryProvider _repositoryProvider;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        super.service(serviceManager);
045        _repositoryProvider = (RepositoryProvider) serviceManager.lookup(RepositoryProvider.ROLE);
046    }
047    
048    /**
049     * Get the repository and session and set the _repository and _session protected fields
050     * @param request The request
051     * @param workspaceName The jcr workspace
052     * @throws ProcessingException if an error occured
053     */
054    protected void _getRepository(Request request, String workspaceName) throws ProcessingException
055    {
056        try
057        {
058            _session = _repositoryProvider.getSession(workspaceName);
059        }
060        catch (Exception e)
061        {
062            throw new ProcessingException(e);
063        }
064        
065    }
066}