001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI.service;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.runtime.model.ComponentContext;
032import org.nuxeo.runtime.model.ComponentInstance;
033import org.nuxeo.runtime.model.DefaultComponent;
034import org.restlet.Restlet;
035
036/**
037 * Runtime service used to register restlets
038 *
039 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
040 */
041public class PluggableRestletService extends DefaultComponent {
042
043    public static final String NAME = "org.nuxeo.ecm.platform.ui.web.restAPI.service.PluggableRestletService";
044
045    private static final Log log = LogFactory.getLog(PluggableRestletService.class);
046
047    private Map<String, RestletPluginDescriptor> restletsDescriptors;
048
049    @Override
050    public void activate(ComponentContext context) {
051        restletsDescriptors = new HashMap<String, RestletPluginDescriptor>();
052    }
053
054    @Override
055    public void deactivate(ComponentContext context) {
056        restletsDescriptors = null;
057    }
058
059    private void mergeDescriptors(RestletPluginDescriptor newContrib) {
060        RestletPluginDescriptor oldDescriptor = restletsDescriptors.get(newContrib.getName());
061
062        // Enable/Disable
063        if (newContrib.getEnabled() != null) {
064            oldDescriptor.setEnabled(newContrib.getEnabled());
065        }
066
067        // override URL
068        if (newContrib.getUrlPatterns() != null && !newContrib.getUrlPatterns().isEmpty()) {
069            oldDescriptor.getUrlPatterns().addAll(newContrib.getUrlPatterns());
070        }
071
072        // override class (NXP-170)
073        if (newContrib.getClassName() != null) {
074            oldDescriptor.setClassName(newContrib.getClassName());
075        }
076    }
077
078    @Override
079    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
080
081        RestletPluginDescriptor descriptor = (RestletPluginDescriptor) contribution;
082
083        if (restletsDescriptors.containsKey(descriptor.getName())) {
084            mergeDescriptors(descriptor);
085            log.debug("merged RestletDescriptor: " + descriptor.getName());
086        } else {
087            restletsDescriptors.put(descriptor.getName(), descriptor);
088            log.debug("registered RestletDescriptor: " + descriptor.getName());
089        }
090    }
091
092    public List<String> getContributedRestletNames() {
093        List<String> res = new ArrayList<String>();
094
095        res.addAll(restletsDescriptors.keySet());
096        return res;
097    }
098
099    public RestletPluginDescriptor getContributedRestletDescriptor(String name) {
100        if (restletsDescriptors.containsKey(name)) {
101            return restletsDescriptors.get(name);
102        } else {
103            return null;
104        }
105    }
106
107    public Restlet getContributedRestletByName(String name) {
108        if (restletsDescriptors.containsKey(name)) {
109
110            RestletPluginDescriptor rpd = restletsDescriptors.get(name);
111            if (rpd == null) {
112                log.error("Error while creating Restlet instance. Cannot get RestletPluginDescriptor for name: " + name);
113                return null;
114            }
115            Class<Restlet> theClass = rpd.getClassName();
116            if (theClass == null) {
117                log.error("Error while creating Restlet instance. Class not available for restlet descriptor: " + name);
118                return null;
119            }
120            Restlet restlet;
121            try {
122                restlet = theClass.newInstance();
123            } catch (ReflectiveOperationException e) {
124                log.error("Error while creating Restlet instance for name " + name, e);
125                return null;
126            }
127            return restlet;
128        } else {
129            log.error("Restlet " + name + " is not registred");
130            return null;
131        }
132    }
133
134}