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;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
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        return null;
070    }
071
072    private UserWorkspaceService getUserWorkspaceService() {
073        if (userWorkspaceService == null) {
074            Class<?> klass = getConfiguration().getUserWorkspaceClass();
075            if (klass == null) {
076                throw new NuxeoException("No class specified for the userWorkspace");
077            }
078            try {
079                userWorkspaceService = (UserWorkspaceService) klass.newInstance();
080            } catch (ReflectiveOperationException e) {
081                throw new NuxeoException("Failed to instantiate class " + klass, e);
082            }
083        }
084        return userWorkspaceService;
085    }
086
087    @Override
088    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
089        descriptors.add((UserWorkspaceDescriptor) contribution);
090    }
091
092    @Override
093    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
094        descriptors.remove(contribution);
095    }
096
097    protected void recompute() {
098        userWorkspaceService = null;
099    }
100
101    public String getTargetDomainName() {
102        return getConfiguration().getTargetDomainName();
103    }
104
105    public UserWorkspaceDescriptor getConfiguration() {
106        return descriptors.getLast();
107    }
108
109    // for tests only
110    public static void reset() {
111        UserWorkspaceServiceImplComponent s = (UserWorkspaceServiceImplComponent) Framework.getRuntime().getComponent(
112                NAME);
113        s.userWorkspaceService = null;
114    }
115
116}