001/*
002 *  Copyright 2012 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.cms.repository;
017
018import javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.ContentExtractor;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.jcr.LockComponent;
027
028/**
029 * {@link AmetysObjectFactory} handling {@link Content}.
030 */
031public class ModifiableContentFactory extends ContentFactory
032{
033    private LockComponent _lockComponent;
034    private ServiceManager _serviceManager;
035    private ContentExtractor _contentExtractor;
036    
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        super.service(manager);
041        _serviceManager = manager;
042    }
043    
044    @Override
045    public ModifiableDefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
046    {
047        return new ModifiableDefaultContent<>(node, parentPath, this);
048    }
049    
050    LockComponent getLockComponent()
051    {
052        // lazy initialization to prevent chicken/egg scenario on startup
053        if (_lockComponent == null)
054        {
055            try
056            {
057                _lockComponent = (LockComponent) _serviceManager.lookup(LockComponent.ROLE);
058            }
059            catch (ServiceException e)
060            {
061                throw new AmetysRepositoryException("Caught an exception while retrieving LockComponent", e);
062            }
063        }
064        
065        return _lockComponent;
066    }
067    
068    ContentExtractor getContentExtractor()
069    {
070        if (_contentExtractor == null)
071        {
072            try
073            {
074                _contentExtractor = (ContentExtractor) _manager.lookup(ContentExtractor.CMS_CONTENT_EXTACTOR_ROLE);
075            }
076            catch (ServiceException e)
077            {
078                throw new IllegalStateException("Unable to get ContentExtractor", e);
079            }
080        }
081        
082        return _contentExtractor;
083    }
084}