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.explorer.resources.jcr;
017
018import javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021
022import org.ametys.plugins.repository.AmetysObjectFactory;
023import org.ametys.plugins.repository.AmetysObjectResolver;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory;
027import org.ametys.plugins.repository.jcr.LockComponent;
028
029/**
030 * {@link AmetysObjectFactory} for handling {@link JCRResource}s.
031 */
032public class JCRResourceFactory extends DefaultAmetysObjectFactory
033{
034    /** JCR nodetype for resources collection */
035    public static final String RESOURCES_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":resource";
036    
037    private LockComponent _lockComponent;
038    
039    @SuppressWarnings("unchecked")
040    @Override
041    public JCRResource getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
042    {
043        return new JCRResource(node, parentPath, this);
044    }
045    
046    AmetysObjectResolver getResolver()
047    {
048        return _resolver;
049    }
050    
051    LockComponent getLockComponent()
052    {
053        // lazy initialization to prevent chicken/egg scenario on startup
054        if (_lockComponent == null)
055        {
056            try
057            {
058                _lockComponent = (LockComponent) _manager.lookup(LockComponent.ROLE);
059            }
060            catch (ServiceException e)
061            {
062                throw new AmetysRepositoryException("Caught an exception while retrieving LockComponent", e);
063            }
064        }
065        
066        return _lockComponent;
067    }
068}