001/* 002 * (C) Copyright 2006-2008 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 * troger 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.configuration.service; 023 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.platform.annotations.gwt.server.configuration.UserInfoMapper; 034import org.nuxeo.ecm.platform.annotations.gwt.server.configuration.WebPermission; 035import org.nuxeo.runtime.model.ComponentContext; 036import org.nuxeo.runtime.model.ComponentInstance; 037import org.nuxeo.runtime.model.DefaultComponent; 038 039/** 040 * @author <a href="mailto:[email protected]">Thomas Roger</a> 041 * @author <a href="mailto:[email protected]">Quentin Lamerand</a> 042 */ 043public class WebAnnotationConfigurationServiceImpl extends DefaultComponent implements 044 WebAnnotationConfigurationService { 045 046 private static final Log log = LogFactory.getLog(WebAnnotationConfigurationServiceImpl.class); 047 048 private static final String ANNOTATION_TYPES_EXTENSION_POINT = "types"; 049 050 private static final String USER_INFO_EXTENSION_POINT = "userInfo"; 051 052 private static final String WEB_PERMISSION_EXTENSION_POINT = "webPermission"; 053 054 private static final String FILTERS_EXTENSION_POINT = "filters"; 055 056 private static final String DISPLAYED_FIELDS_EXTENSION_POINT = "displayedFields"; 057 058 private Map<String, WebAnnotationDefinitionDescriptor> annotationDefinitionsDescriptors; 059 060 private UserInfoMapper userInfoMapper; 061 062 private WebPermission webPermission; 063 064 private Map<String, FilterDescriptor> filterDescriptors; 065 066 private Set<String> displayedFields; 067 068 private Map<String, String> fieldLabels; 069 070 @Override 071 public void activate(ComponentContext context) { 072 annotationDefinitionsDescriptors = new HashMap<String, WebAnnotationDefinitionDescriptor>(); 073 filterDescriptors = new HashMap<String, FilterDescriptor>(); 074 displayedFields = new HashSet<String>(); 075 fieldLabels = new HashMap<String, String>(); 076 } 077 078 @Override 079 public void deactivate(ComponentContext context) { 080 annotationDefinitionsDescriptors = null; 081 userInfoMapper = null; 082 filterDescriptors = null; 083 displayedFields = null; 084 fieldLabels = null; 085 } 086 087 @Override 088 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 089 if (ANNOTATION_TYPES_EXTENSION_POINT.equals(extensionPoint)) { 090 WebAnnotationDefinitionDescriptor descriptor = (WebAnnotationDefinitionDescriptor) contribution; 091 if (annotationDefinitionsDescriptors.put(descriptor.getName(), descriptor) != null) { 092 log.info("Already registered annotation type: " + descriptor.getName() + ", storing the new one."); 093 } 094 } else if (USER_INFO_EXTENSION_POINT.equals(extensionPoint)) { 095 UserInfoMapperDescriptor descriptor = (UserInfoMapperDescriptor) contribution; 096 userInfoMapper = newInstance(descriptor.getKlass()); 097 } else if (WEB_PERMISSION_EXTENSION_POINT.equals(extensionPoint)) { 098 WebPermissionDescriptor descriptor = (WebPermissionDescriptor) contribution; 099 webPermission = newInstance(descriptor.getKlass()); 100 } else if (FILTERS_EXTENSION_POINT.equals(extensionPoint)) { 101 FilterDescriptor descriptor = (FilterDescriptor) contribution; 102 if (filterDescriptors.put(descriptor.getName(), descriptor) != null) { 103 log.info("Already registered annotation filter: " + descriptor.getName() + ", storing the new one."); 104 } 105 } else if (DISPLAYED_FIELDS_EXTENSION_POINT.equals(extensionPoint)) { 106 DisplayedFieldsDescriptor descriptor = (DisplayedFieldsDescriptor) contribution; 107 String fieldName = descriptor.getName(); 108 if (descriptor.isDisplayed()) { 109 displayedFields.add(fieldName); 110 } else { 111 displayedFields.remove(fieldName); 112 } 113 114 String fieldLabel = descriptor.getLabel(); 115 if (fieldLabel != null) { 116 fieldLabels.put(fieldName, fieldLabel); 117 } 118 } 119 } 120 121 protected <T> T newInstance(Class<T> klass) { 122 try { 123 return klass.newInstance(); 124 } catch (ReflectiveOperationException e) { 125 throw new RuntimeException(e); 126 } 127 } 128 129 @Override 130 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 131 if (ANNOTATION_TYPES_EXTENSION_POINT.equals(extensionPoint)) { 132 WebAnnotationDefinitionDescriptor descriptor = (WebAnnotationDefinitionDescriptor) contribution; 133 annotationDefinitionsDescriptors.remove(descriptor.getName()); 134 } else if (FILTERS_EXTENSION_POINT.equals(extensionPoint)) { 135 FilterDescriptor descriptor = (FilterDescriptor) contribution; 136 filterDescriptors.remove(descriptor.getName()); 137 } else if (DISPLAYED_FIELDS_EXTENSION_POINT.equals(extensionPoint)) { 138 DisplayedFieldsDescriptor descriptor = (DisplayedFieldsDescriptor) contribution; 139 String fieldName = descriptor.getName(); 140 displayedFields.remove(fieldName); 141 fieldLabels.remove(fieldName); 142 } 143 } 144 145 public List<WebAnnotationDefinitionDescriptor> getAllWebAnnotationDefinitions() { 146 return new ArrayList<WebAnnotationDefinitionDescriptor>(annotationDefinitionsDescriptors.values()); 147 } 148 149 public List<WebAnnotationDefinitionDescriptor> getEnabledWebAnnotationDefinitions() { 150 List<WebAnnotationDefinitionDescriptor> definitions = new ArrayList<WebAnnotationDefinitionDescriptor>(); 151 for (WebAnnotationDefinitionDescriptor def : annotationDefinitionsDescriptors.values()) { 152 if (def.isEnabled()) { 153 definitions.add(def); 154 } 155 } 156 return definitions; 157 } 158 159 public UserInfoMapper getUserInfoMapper() { 160 return userInfoMapper; 161 } 162 163 public WebPermission getWebPermission() { 164 return webPermission; 165 } 166 167 public Map<String, FilterDescriptor> getFilterDefinitions() { 168 return filterDescriptors; 169 } 170 171 public Set<String> getDisplayedFields() { 172 return displayedFields; 173 } 174 175 public Map<String, String> getFieldLabels() { 176 return fieldLabels; 177 } 178 179}