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.plugins.repository.provider;
018
019import javax.jcr.Repository;
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.activity.Initializable;
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029/**
030 * Component holding a special admin JCR Session that must not be logged out
031 */
032public class AdminSessionProvider implements Serviceable, Initializable, Component
033{
034    /** Avalon Role */
035    public static final String ROLE = AdminSessionProvider.class.getName();
036    
037    private Repository _repository;
038    private Session _session;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _repository = (Repository) manager.lookup("javax.jcr.Repository");
044    }
045    
046    @Override
047    public void initialize() throws Exception
048    {
049        _session = _repository.login("default");    
050    }
051    
052    /**
053     * Returns the admin session
054     * @return the admin session
055     * @throws RepositoryException if an error occurs.
056     */
057    public Session getAdminSession() throws RepositoryException
058    {
059        _session.refresh(false);
060        return _session;
061    }
062}