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