001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.runtime.services.resource;
013
014import java.net.URL;
015import java.util.Map;
016import java.util.concurrent.ConcurrentHashMap;
017
018import org.nuxeo.runtime.model.ComponentContext;
019import org.nuxeo.runtime.model.ComponentInstance;
020import org.nuxeo.runtime.model.Extension;
021import org.nuxeo.runtime.model.ReloadableComponent;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026// FIXME: make it handle hot reload correctly
027public class ResourceService extends ReloadableComponent {
028
029    public final static String XP_RESOURCES = "resources";
030
031    protected Map<String, URL> registry;
032
033    public ResourceService() {
034    }
035
036    public URL getResource(String name) {
037        return registry.get(name);
038    }
039
040    public void addResource(ResourceDescriptor resource) {
041        addResource(resource.getName(), resource.getResource().toURL());
042    }
043
044    public void addResource(String name, URL url) {
045        registry.put(name, url);
046    }
047
048    public URL removeResource(String name) {
049        return registry.remove(name);
050    }
051
052    @Override
053    public void activate(ComponentContext context) {
054        registry = new ConcurrentHashMap<String, URL>();
055    }
056
057    @Override
058    public void deactivate(ComponentContext context) {
059        registry = null;
060    }
061
062    @Override
063    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
064        if (XP_RESOURCES.equals(extensionPoint)) {
065            addResource((ResourceDescriptor) contribution);
066        }
067    }
068
069    @Override
070    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
071        if (XP_RESOURCES.equals(extensionPoint)) {
072            ResourceDescriptor rd = (ResourceDescriptor) contribution;
073            ResourceDescriptor last = findLastContributedResource(rd.getName());
074            if (last != null) {
075                addResource(last);
076            } else {
077                removeResource(rd.getName());
078            }
079        }
080    }
081
082    protected ResourceDescriptor findLastContributedResource(String name) {
083        for (int i = extensions.size() - 1; i >= 0; i--) {
084            Extension xt = extensions.get(i);
085            Object[] contribs = xt.getContributions();
086            for (int k = contribs.length - 1; k >= 0; k--) {
087                ResourceDescriptor r = (ResourceDescriptor) contribs[k];
088                if (name.equals(r.getName())) {
089                    return r;
090                }
091            }
092        }
093        return null;
094    }
095}