001/* 002 * Copyright 2012 Anyware Services 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 */ 016package org.ametys.runtime.plugin; 017 018import java.io.File; 019import java.io.IOException; 020import java.net.MalformedURLException; 021import java.util.Map; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025import org.apache.avalon.framework.logger.AbstractLogEnabled; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029import org.apache.excalibur.source.Source; 030import org.apache.excalibur.source.SourceFactory; 031import org.apache.excalibur.source.SourceNotFoundException; 032import org.apache.excalibur.source.SourceResolver; 033import org.apache.excalibur.source.impl.FileSource; 034 035import org.ametys.core.source.OptionalSourceFallbackExtensionPoint; 036 037 038/** 039 * SourceFactory handling resources URIs for plugins. 040 * Plugin resources can be found of File System, in a JAR, ... 041 */ 042public class PluginSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable 043{ 044 private static final Pattern __SOURCE_PATTERN = Pattern.compile("^[\\w]+:(" + PluginsManager.PLUGIN_NAME_REGEXP + ")(\\?)?://(.*)$"); 045 046 private SourceResolver _resolver; 047 048 private OptionalSourceFallbackExtensionPoint _optionalSourceFallbackExtensionPoint; 049 050 private ServiceManager _manager; 051 052 public Source getSource(String location, Map parameters) throws IOException 053 { 054 Matcher m = __SOURCE_PATTERN.matcher(location); 055 if (!m.matches()) 056 { 057 throw new MalformedURLException("URI must be like protocol:<plugin name>://path/to/resource. Location was '" + location + "'"); 058 } 059 060 String pluginName = m.group(1); 061 String optional = m.group(2); 062 String path = m.group(3); 063 064 String resourceURI = PluginsManager.getInstance().getResourceURI(pluginName); 065 066 if (resourceURI == null) 067 { 068 File pluginLocation = PluginsManager.getInstance().getPluginLocation(pluginName); 069 if (pluginLocation == null) 070 { 071 if (optional == null) 072 { 073 String errorMessage = "The plugin '" + pluginName + "' does not exist."; 074 getLogger().warn(errorMessage); 075 076 throw new SourceNotFoundException(errorMessage); 077 } 078 else 079 { 080 String fallback = _getOptionalSourceFallbackExtensionPoint().fallback(location); 081 if (fallback == null) 082 { 083 String errorMessage = "While resolving the url '" + location + "', the plugin '" + pluginName + "' does not exist and no fallback is matching it."; 084 getLogger().warn(errorMessage); 085 throw new SourceNotFoundException(errorMessage); 086 } 087 088 try 089 { 090 Source source = _resolver.resolveURI(fallback); 091 if (!source.exists()) 092 { 093 String errorMessage = "While resolving the url '" + location + "', the plugin '" + pluginName + "' does not exist and the fallback '" + fallback + "' does not exist."; 094 getLogger().warn(errorMessage); 095 throw new SourceNotFoundException(errorMessage); 096 } 097 return source; 098 } 099 catch (SourceNotFoundException e) 100 { 101 throw new SourceNotFoundException("While resolving the url '" + location + "', the plugin '" + pluginName + "' does not exist and the fallback '" + fallback + "' does not exist.", e); 102 } 103 } 104 } 105 106 return new FileSource("file", new File(pluginLocation, path)); 107 } 108 else 109 { 110 return _resolver.resolveURI(resourceURI + '/' + path); 111 } 112 } 113 114 public void release(Source source) 115 { 116 // empty method 117 } 118 119 private synchronized OptionalSourceFallbackExtensionPoint _getOptionalSourceFallbackExtensionPoint() 120 { 121 if (_optionalSourceFallbackExtensionPoint == null) 122 { 123 try 124 { 125 _optionalSourceFallbackExtensionPoint = (OptionalSourceFallbackExtensionPoint) _manager.lookup(OptionalSourceFallbackExtensionPoint.ROLE); 126 } 127 catch (ServiceException e) 128 { 129 throw new RuntimeException("Unable to get the OptionalSourceFallbacksExtensionPoint extension point.", e); 130 } 131 } 132 return _optionalSourceFallbackExtensionPoint; 133 } 134 135 public void service(ServiceManager manager) throws ServiceException 136 { 137 _manager = manager; 138 _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 139 } 140}