001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     btatar
018 *     Damien METZLER (damien.metzler@leroymerlin.fr)
019 *
020 * $Id$
021 */
022
023package org.nuxeo.ecm.platform.userworkspace.core.service;
024
025import java.util.Deque;
026import java.util.LinkedList;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.collections.api.CollectionLocationService;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
033import org.nuxeo.runtime.api.Framework;
034import org.nuxeo.runtime.model.ComponentContext;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038/**
039 * This component is used to register the service that provide the userworkspace service support.
040 *
041 * @author btatar
042 * @author Damien METZLER (damien.metzler@leroymerlin.fr)
043 */
044public class UserWorkspaceServiceImplComponent extends DefaultComponent {
045
046    public static final String NAME = "org.nuxeo.ecm.platform.userworkspace.UserWorkspaceService";
047
048    private static final Log log = LogFactory.getLog(UserWorkspaceService.class);
049
050    protected Deque<UserWorkspaceDescriptor> descriptors = new LinkedList<>();
051
052    protected UserWorkspaceService userWorkspaceService;
053
054    @Override
055    public void activate(ComponentContext context) {
056        log.info("UserWorkspaceService activated");
057    }
058
059    @Override
060    public void deactivate(ComponentContext context) {
061        log.info("UserWorkspaceService deactivated");
062    }
063
064    @Override
065    public <T> T getAdapter(Class<T> adapter) {
066        if (adapter == UserWorkspaceService.class) {
067            return adapter.cast(getUserWorkspaceService());
068        }
069        if (adapter == CollectionLocationService.class) {
070            return adapter.cast(getUserWorkspaceService());
071        }
072        return null;
073    }
074
075    private UserWorkspaceService getUserWorkspaceService() {
076        if (userWorkspaceService == null) {
077            Class<?> klass = getConfiguration().getUserWorkspaceClass();
078            if (klass == null) {
079                throw new NuxeoException("No class specified for the userWorkspace");
080            }
081            try {
082                userWorkspaceService = (UserWorkspaceService) klass.newInstance();
083            } catch (ReflectiveOperationException e) {
084                throw new NuxeoException("Failed to instantiate class " + klass, e);
085            }
086        }
087        return userWorkspaceService;
088    }
089
090    @Override
091    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
092        descriptors.add((UserWorkspaceDescriptor) contribution);
093    }
094
095    @Override
096    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
097        descriptors.remove(contribution);
098    }
099
100    protected void recompute() {
101        userWorkspaceService = null;
102    }
103
104    public String getTargetDomainName() {
105        return getConfiguration().getTargetDomainName();
106    }
107
108    public UserWorkspaceDescriptor getConfiguration() {
109        return descriptors.getLast();
110    }
111
112    // for tests only
113    public static void reset() {
114        UserWorkspaceServiceImplComponent s = (UserWorkspaceServiceImplComponent) Framework.getRuntime().getComponent(
115                NAME);
116        s.userWorkspaceService = null;
117    }
118
119}