001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.repository.service;
021
022import org.nuxeo.ecm.platform.annotations.repository.descriptor.DocumentAnnotabilityDescriptor;
023import org.nuxeo.ecm.platform.annotations.repository.descriptor.DocumentEventListenerDescriptor;
024import org.nuxeo.ecm.platform.annotations.repository.descriptor.EventIdDescriptor;
025import org.nuxeo.ecm.platform.annotations.repository.descriptor.GraphManagerEventListenerDescriptor;
026import org.nuxeo.ecm.platform.annotations.repository.service.AnnotationsRepositoryConstants.ExtensionPoint;
027import org.nuxeo.runtime.model.ComponentContext;
028import org.nuxeo.runtime.model.ComponentInstance;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * @author Alexandre Russel
033 */
034public class AnnotationsRepositoryComponent extends DefaultComponent {
035
036    public static AnnotationsRepositoryComponent instance;
037
038    protected AnnotationsRepositoryServiceImpl annotationsRepositoryService;
039
040    protected AnnotationsRepositoryConfigurationServiceImpl confImpl;
041
042    protected AnnotationsFulltextInjector injector;
043
044    @Override
045    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
046        ExtensionPoint point = Enum.valueOf(ExtensionPoint.class, extensionPoint);
047        switch (point) {
048        case documentAnnotability:
049            DocumentAnnotability annotability = newInstance(((DocumentAnnotabilityDescriptor) contribution).getKlass());
050            annotationsRepositoryService.setDocumentAnnotability(annotability);
051            break;
052        case documentEventListener:
053            AnnotatedDocumentEventListener listener = newInstance(((DocumentEventListenerDescriptor) contribution).getListener());
054            String listenerName = ((DocumentEventListenerDescriptor) contribution).getName();
055            confImpl.addEventListener(listenerName, listener);
056            break;
057        case jcrLifecycleEventId:
058            String eventId = ((EventIdDescriptor) contribution).getEventId();
059            confImpl.addEventId(eventId);
060            break;
061        case graphManagerEventListener:
062            GraphManagerEventListener graphListener = newInstance(((GraphManagerEventListenerDescriptor) contribution).getKlass());
063            confImpl.setGraphManagerEventListener(graphListener);
064            break;
065        }
066    }
067
068    protected <T> T newInstance(Class<T> klass) {
069        try {
070            return klass.newInstance();
071        } catch (ReflectiveOperationException e) {
072            throw new RuntimeException(e);
073        }
074    }
075
076    @Override
077    public void activate(ComponentContext context) {
078        instance = this;
079        annotationsRepositoryService = new AnnotationsRepositoryServiceImpl();
080        confImpl = new AnnotationsRepositoryConfigurationServiceImpl();
081        injector = new AnnotationsFulltextInjector();
082    }
083
084    @Override
085    public void deactivate(ComponentContext context) {
086        instance = null;
087        annotationsRepositoryService.clear();
088        annotationsRepositoryService = null;
089        injector = null;
090    }
091
092    public AnnotationsFulltextInjector getFulltextInjector() {
093        return injector;
094    }
095
096    @SuppressWarnings("unchecked")
097    @Override
098    public <T> T getAdapter(Class<T> adapter) {
099        if (AnnotationsRepositoryService.class.isAssignableFrom(adapter)) {
100            return (T) annotationsRepositoryService;
101        } else if (AnnotationsRepositoryConfigurationService.class.isAssignableFrom(adapter)) {
102            return (T) confImpl;
103        }
104        return null;
105    }
106}