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    protected AnnotationsRepositoryServiceImpl annotationsRepositoryService;
039
040    protected AnnotationsRepositoryConfigurationServiceImpl confImpl;
041
042    @Override
043    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
044        ExtensionPoint point = Enum.valueOf(ExtensionPoint.class, extensionPoint);
045        switch (point) {
046        case documentAnnotability:
047            DocumentAnnotability annotability = newInstance(((DocumentAnnotabilityDescriptor) contribution).getKlass());
048            annotationsRepositoryService.setDocumentAnnotability(annotability);
049            break;
050        case documentEventListener:
051            AnnotatedDocumentEventListener listener = newInstance(((DocumentEventListenerDescriptor) contribution).getListener());
052            String listenerName = ((DocumentEventListenerDescriptor) contribution).getName();
053            confImpl.addEventListener(listenerName, listener);
054            break;
055        case jcrLifecycleEventId:
056            String eventId = ((EventIdDescriptor) contribution).getEventId();
057            confImpl.addEventId(eventId);
058            break;
059        case graphManagerEventListener:
060            GraphManagerEventListener graphListener = newInstance(((GraphManagerEventListenerDescriptor) contribution).getKlass());
061            confImpl.setGraphManagerEventListener(graphListener);
062            break;
063        }
064    }
065
066    protected <T> T newInstance(Class<T> klass) {
067        try {
068            return klass.newInstance();
069        } catch (ReflectiveOperationException e) {
070            throw new RuntimeException(e);
071        }
072    }
073
074    @Override
075    public void activate(ComponentContext context) {
076        annotationsRepositoryService = new AnnotationsRepositoryServiceImpl();
077        confImpl = new AnnotationsRepositoryConfigurationServiceImpl();
078    }
079
080    @Override
081    public void deactivate(ComponentContext context) {
082        annotationsRepositoryService.clear();
083        annotationsRepositoryService = null;
084    }
085
086    @SuppressWarnings("unchecked")
087    @Override
088    public <T> T getAdapter(Class<T> adapter) {
089        if (AnnotationsRepositoryService.class.isAssignableFrom(adapter)) {
090            return (T) annotationsRepositoryService;
091        } else if (AnnotationsRepositoryConfigurationService.class.isAssignableFrom(adapter)) {
092            return (T) confImpl;
093        }
094        return null;
095    }
096}