001/* 002 * (C) Copyright 2006-2011 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 * bstefanescu 018 */ 019package org.nuxeo.runtime.services.resource; 020 021import java.net.URL; 022import java.util.Map; 023import java.util.concurrent.ConcurrentHashMap; 024 025import org.nuxeo.runtime.model.ComponentContext; 026import org.nuxeo.runtime.model.ComponentInstance; 027import org.nuxeo.runtime.model.Extension; 028import org.nuxeo.runtime.model.ReloadableComponent; 029 030/** 031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 032 */ 033// FIXME: make it handle hot reload correctly 034public class ResourceService extends ReloadableComponent { 035 036 public final static String XP_RESOURCES = "resources"; 037 038 protected Map<String, URL> registry; 039 040 public ResourceService() { 041 } 042 043 public URL getResource(String name) { 044 return registry.get(name); 045 } 046 047 public void addResource(ResourceDescriptor resource) { 048 addResource(resource.getName(), resource.getResource().toURL()); 049 } 050 051 public void addResource(String name, URL url) { 052 registry.put(name, url); 053 } 054 055 public URL removeResource(String name) { 056 return registry.remove(name); 057 } 058 059 @Override 060 public void activate(ComponentContext context) { 061 registry = new ConcurrentHashMap<String, URL>(); 062 } 063 064 @Override 065 public void deactivate(ComponentContext context) { 066 registry = null; 067 } 068 069 @Override 070 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 071 if (XP_RESOURCES.equals(extensionPoint)) { 072 addResource((ResourceDescriptor) contribution); 073 } 074 } 075 076 @Override 077 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 078 if (XP_RESOURCES.equals(extensionPoint)) { 079 ResourceDescriptor rd = (ResourceDescriptor) contribution; 080 ResourceDescriptor last = findLastContributedResource(rd.getName()); 081 if (last != null) { 082 addResource(last); 083 } else { 084 removeResource(rd.getName()); 085 } 086 } 087 } 088 089 protected ResourceDescriptor findLastContributedResource(String name) { 090 for (int i = extensions.size() - 1; i >= 0; i--) { 091 Extension xt = extensions.get(i); 092 Object[] contribs = xt.getContributions(); 093 for (int k = contribs.length - 1; k >= 0; k--) { 094 ResourceDescriptor r = (ResourceDescriptor) contribs[k]; 095 if (name.equals(r.getName())) { 096 return r; 097 } 098 } 099 } 100 return null; 101 } 102}