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 == UserWorkspaceServiceImplComponent.class) { 067 return adapter.cast(this); 068 } 069 else if (adapter == UserWorkspaceService.class) { 070 return adapter.cast(getUserWorkspaceService()); 071 } 072 else if (adapter == CollectionLocationService.class) { 073 return adapter.cast(getUserWorkspaceService()); 074 } 075 return null; 076 } 077 078 private UserWorkspaceService getUserWorkspaceService() { 079 if (userWorkspaceService == null) { 080 Class<?> klass = getConfiguration().getUserWorkspaceClass(); 081 if (klass == null) { 082 throw new NuxeoException("No class specified for the userWorkspace"); 083 } 084 try { 085 userWorkspaceService = (UserWorkspaceService) klass.getDeclaredConstructor().newInstance(); 086 } catch (ReflectiveOperationException e) { 087 throw new NuxeoException("Failed to instantiate class " + klass, e); 088 } 089 } 090 return userWorkspaceService; 091 } 092 093 @Override 094 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 095 descriptors.add((UserWorkspaceDescriptor) contribution); 096 } 097 098 @Override 099 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 100 descriptors.remove(contribution); 101 } 102 103 protected void recompute() { 104 userWorkspaceService = null; 105 } 106 107 public String getTargetDomainName() { 108 return getConfiguration().getTargetDomainName(); 109 } 110 111 public UserWorkspaceDescriptor getConfiguration() { 112 return descriptors.getLast(); 113 } 114 115 // for tests only 116 public static void reset() { 117 UserWorkspaceServiceImplComponent s = Framework.getService(UserWorkspaceServiceImplComponent.class); 118 s.userWorkspaceService = null; 119 } 120 121}