001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     btatar
016 *     Damien METZLER (damien.metzler@leroymerlin.fr)
017 *
018 * $Id$
019 */
020
021package org.nuxeo.ecm.platform.userworkspace.core.service;
022
023import java.util.Deque;
024import java.util.LinkedList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.ComponentContext;
033import org.nuxeo.runtime.model.ComponentInstance;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036/**
037 * This component is used to register the service that provide the userworkspace service support.
038 *
039 * @author btatar
040 * @author Damien METZLER (damien.metzler@leroymerlin.fr)
041 */
042public class UserWorkspaceServiceImplComponent extends DefaultComponent {
043
044    public static final String NAME = "org.nuxeo.ecm.platform.userworkspace.UserWorkspaceService";
045
046    private static final Log log = LogFactory.getLog(UserWorkspaceService.class);
047
048    protected Deque<UserWorkspaceDescriptor> descriptors = new LinkedList<>();
049
050    protected UserWorkspaceService userWorkspaceService;
051
052    @Override
053    public void activate(ComponentContext context) {
054        log.info("UserWorkspaceService activated");
055    }
056
057    @Override
058    public void deactivate(ComponentContext context) {
059        log.info("UserWorkspaceService deactivated");
060    }
061
062    @Override
063    public <T> T getAdapter(Class<T> adapter) {
064        if (adapter == UserWorkspaceService.class) {
065            return adapter.cast(getUserWorkspaceService());
066        }
067        return null;
068    }
069
070    private UserWorkspaceService getUserWorkspaceService() {
071        if (userWorkspaceService == null) {
072            Class<?> klass = getConfiguration().getUserWorkspaceClass();
073            if (klass == null) {
074                throw new NuxeoException("No class specified for the userWorkspace");
075            }
076            try {
077                userWorkspaceService = (UserWorkspaceService) klass.newInstance();
078            } catch (ReflectiveOperationException e) {
079                throw new NuxeoException("Failed to instantiate class " + klass, e);
080            }
081        }
082        return userWorkspaceService;
083    }
084
085    @Override
086    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
087        descriptors.add((UserWorkspaceDescriptor) contribution);
088    }
089
090    @Override
091    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
092        descriptors.remove(contribution);
093    }
094
095    protected void recompute() {
096        userWorkspaceService = null;
097    }
098
099    public String getTargetDomainName() {
100        return getConfiguration().getTargetDomainName();
101    }
102
103    public UserWorkspaceDescriptor getConfiguration() {
104        return descriptors.getLast();
105    }
106
107    // for tests only
108    public static void reset() {
109        UserWorkspaceServiceImplComponent s = (UserWorkspaceServiceImplComponent) Framework.getRuntime().getComponent(
110                NAME);
111        s.userWorkspaceService = null;
112    }
113
114}