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 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.service; 023 024import org.nuxeo.ecm.platform.annotations.api.AnnotationsService; 025import org.nuxeo.ecm.platform.annotations.api.UriResolver; 026import org.nuxeo.ecm.platform.annotations.api.AnnotationsConstants.ExtensionPoint; 027import org.nuxeo.ecm.platform.annotations.descriptors.AnnotabilityManagerDescriptor; 028import org.nuxeo.ecm.platform.annotations.descriptors.AnnotationIDGeneratorDescriptor; 029import org.nuxeo.ecm.platform.annotations.descriptors.EventListenerDescriptor; 030import org.nuxeo.ecm.platform.annotations.descriptors.MetadataMapperDescriptor; 031import org.nuxeo.ecm.platform.annotations.descriptors.PermissionManagerDescriptor; 032import org.nuxeo.ecm.platform.annotations.descriptors.PermissionMapperDescriptor; 033import org.nuxeo.ecm.platform.annotations.descriptors.URLPatternFilterDescriptor; 034import org.nuxeo.ecm.platform.annotations.descriptors.UriResolverDescriptor; 035import org.nuxeo.ecm.platform.annotations.proxy.AnnotationServiceProxy; 036import org.nuxeo.runtime.model.ComponentContext; 037import org.nuxeo.runtime.model.ComponentInstance; 038import org.nuxeo.runtime.model.ComponentName; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041/** 042 * @author Alexandre Russel 043 */ 044public class AnnotationsComponent extends DefaultComponent { 045 046 public static final ComponentName NAME = new ComponentName( 047 "org.nuxeo.ecm.platform.annotations.services.annotationServiceFactory"); 048 049 private AnnotationServiceProxy annotationServiceProxy; 050 051 private final AnnotationConfigurationService configuration = new AnnotationConfigurationServiceImpl(); 052 053 @Override 054 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 055 ExtensionPoint point = Enum.valueOf(ExtensionPoint.class, extensionPoint); 056 switch (point) { 057 case uriResolver: 058 UriResolver resolver = newInstance(((UriResolverDescriptor) contribution).getKlass()); 059 configuration.setUriResolver(resolver); 060 break; 061 case urlPatternFilter: 062 URLPatternFilterDescriptor descriptor = (URLPatternFilterDescriptor) contribution; 063 boolean order = descriptor.getOrder().equalsIgnoreCase("Allow,Deny"); 064 URLPatternFilter filter = new URLPatternFilter(order, descriptor.getDenies(), descriptor.getAllows()); 065 configuration.setFilter(filter); 066 break; 067 case metadataMapper: 068 MetadataMapper mapper = newInstance(((MetadataMapperDescriptor) contribution).getKlass()); 069 configuration.setMetadataMapper(mapper); 070 break; 071 case permissionManager: 072 PermissionManager manager = newInstance(((PermissionManagerDescriptor) contribution).getKlass()); 073 configuration.setPermissionManager(manager); 074 break; 075 case annotabilityManager: 076 AnnotabilityManager annotabilityManager = newInstance(((AnnotabilityManagerDescriptor) contribution).getKlass()); 077 configuration.setAnnotabilityManager(annotabilityManager); 078 break; 079 case eventListener: 080 Class<? extends EventListener> listener = ((EventListenerDescriptor) contribution).getListener(); 081 configuration.addListener(newInstance(listener)); 082 break; 083 case annotationIDGenerator: 084 AnnotationIDGenerator generator = newInstance(((AnnotationIDGeneratorDescriptor) contribution).getKlass()); 085 configuration.setIDGenerator(generator); 086 break; 087 case permissionMapper: 088 configuration.setPermissionMapper((PermissionMapperDescriptor) contribution); 089 break; 090 } 091 } 092 093 protected <T> T newInstance(Class<T> klass) { 094 try { 095 return klass.newInstance(); 096 } catch (ReflectiveOperationException e) { 097 throw new RuntimeException(e); 098 } 099 } 100 101 @Override 102 public void activate(ComponentContext context) { 103 } 104 105 @Override 106 public void deactivate(ComponentContext context) { 107 } 108 109 @SuppressWarnings("unchecked") 110 @Override 111 public <T> T getAdapter(Class<T> adapter) { 112 if (AnnotationsService.class.isAssignableFrom(adapter)) { 113 return (T) getAnnotationServiceProxy(); 114 } else if (AnnotationConfigurationService.class.isAssignableFrom(adapter)) { 115 return (T) configuration; 116 } 117 return null; 118 } 119 120 private AnnotationsService getAnnotationServiceProxy() { 121 if (annotationServiceProxy == null) { 122 annotationServiceProxy = new AnnotationServiceProxy(); 123 annotationServiceProxy.initialise(); 124 } 125 return annotationServiceProxy; 126 } 127 128}