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.repository.service;
023
024import org.nuxeo.ecm.platform.annotations.repository.descriptor.DocumentAnnotabilityDescriptor;
025import org.nuxeo.ecm.platform.annotations.repository.descriptor.DocumentEventListenerDescriptor;
026import org.nuxeo.ecm.platform.annotations.repository.descriptor.EventIdDescriptor;
027import org.nuxeo.ecm.platform.annotations.repository.descriptor.GraphManagerEventListenerDescriptor;
028import org.nuxeo.ecm.platform.annotations.repository.service.AnnotationsRepositoryConstants.ExtensionPoint;
029import org.nuxeo.runtime.model.ComponentContext;
030import org.nuxeo.runtime.model.ComponentInstance;
031import org.nuxeo.runtime.model.DefaultComponent;
032
033/**
034 * @author Alexandre Russel
035 */
036public class AnnotationsRepositoryComponent extends DefaultComponent {
037
038    public static AnnotationsRepositoryComponent instance;
039
040    protected AnnotationsRepositoryServiceImpl annotationsRepositoryService;
041
042    protected AnnotationsRepositoryConfigurationServiceImpl confImpl;
043
044    protected AnnotationsFulltextInjector injector;
045
046    @Override
047    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
048        ExtensionPoint point = Enum.valueOf(ExtensionPoint.class, extensionPoint);
049        switch (point) {
050        case documentAnnotability:
051            DocumentAnnotability annotability = newInstance(((DocumentAnnotabilityDescriptor) contribution).getKlass());
052            annotationsRepositoryService.setDocumentAnnotability(annotability);
053            break;
054        case documentEventListener:
055            AnnotatedDocumentEventListener listener = newInstance(((DocumentEventListenerDescriptor) contribution).getListener());
056            String listenerName = ((DocumentEventListenerDescriptor) contribution).getName();
057            confImpl.addEventListener(listenerName, listener);
058            break;
059        case jcrLifecycleEventId:
060            String eventId = ((EventIdDescriptor) contribution).getEventId();
061            confImpl.addEventId(eventId);
062            break;
063        case graphManagerEventListener:
064            GraphManagerEventListener graphListener = newInstance(((GraphManagerEventListenerDescriptor) contribution).getKlass());
065            confImpl.setGraphManagerEventListener(graphListener);
066            break;
067        }
068    }
069
070    protected <T> T newInstance(Class<T> klass) {
071        try {
072            return klass.newInstance();
073        } catch (ReflectiveOperationException e) {
074            throw new RuntimeException(e);
075        }
076    }
077
078    @Override
079    public void activate(ComponentContext context) {
080        instance = this;
081        annotationsRepositoryService = new AnnotationsRepositoryServiceImpl();
082        confImpl = new AnnotationsRepositoryConfigurationServiceImpl();
083        injector = new AnnotationsFulltextInjector();
084    }
085
086    @Override
087    public void deactivate(ComponentContext context) {
088        instance = null;
089        annotationsRepositoryService.clear();
090        annotationsRepositoryService = null;
091        injector = null;
092    }
093
094    public AnnotationsFulltextInjector getFulltextInjector() {
095        return injector;
096    }
097
098    @SuppressWarnings("unchecked")
099    @Override
100    public <T> T getAdapter(Class<T> adapter) {
101        if (AnnotationsRepositoryService.class.isAssignableFrom(adapter)) {
102            return (T) annotationsRepositoryService;
103        } else if (AnnotationsRepositoryConfigurationService.class.isAssignableFrom(adapter)) {
104            return (T) confImpl;
105        }
106        return null;
107    }
108}