001/* 002 * Copyright 2015 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.util.Collection; 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configuration; 024 025import org.ametys.runtime.config.ConfigParameterInfo; 026 027/** 028 * Object representation of a plugin.xml 029 */ 030public class Plugin 031{ 032 private String _pluginName; 033 private String _version; 034 private Configuration _configuration; 035 036 private Map<String, ConfigParameterInfo> _configParameters = new HashMap<>(); 037 private Map<String, ConfigParameterInfo> _paramCheckers = new HashMap<>(); 038 private Map<String, Feature> _features = new HashMap<>(); 039 private Map<String, ExtensionPointDefinition> _extensionPoints = new HashMap<>(); 040 041 Plugin(String pluginName) 042 { 043 _pluginName = pluginName; 044 } 045 046 /** 047 * Returns this plugin's name. 048 * @return this plugin's name. 049 */ 050 public String getName() 051 { 052 return _pluginName; 053 } 054 055 /** 056 * Returns this plugin's version, or null if not specified. 057 * @return this plugin's version. 058 */ 059 public String getVersion() 060 { 061 return _version; 062 } 063 064 /** 065 * Returns the {@link Feature}s defined by this plugin. 066 * @return the {@link Feature}s defined by this plugin. 067 */ 068 public Map<String, Feature> getFeatures() 069 { 070 return _features; 071 } 072 073 /** 074 * Returns the ids of the extension points defined by this plugin. 075 * @return the ids of the extension points defined by this plugin. 076 */ 077 public Collection<String> getExtensionPoints() 078 { 079 return Collections.unmodifiableSet(_extensionPoints.keySet()); 080 } 081 082 Configuration getConfiguration() 083 { 084 return _configuration; 085 } 086 087 Map<String, ExtensionPointDefinition> getExtensionPointDefinitions() 088 { 089 return _extensionPoints; 090 } 091 092 Map<String, ConfigParameterInfo> getConfigParameters() 093 { 094 return _configParameters; 095 } 096 097 Map<String, ConfigParameterInfo> getParameterCheckers() 098 { 099 return _paramCheckers; 100 } 101 102 void configure(Configuration configuration) 103 { 104 _configuration = configuration; 105 106 _version = configuration.getAttribute("version", null); 107 108 Configuration configConfiguration = configuration.getChild("config"); 109 110 _configureConfigParameters(configConfiguration); 111 _configureParametersCheckers(configConfiguration); 112 113 _configureFeatures(); 114 _configureExtensionPoints(); 115 } 116 117 private void _configureConfigParameters(Configuration configConfiguration) 118 { 119 Configuration[] parameterConfigurations = configConfiguration.getChildren("param"); 120 for (Configuration parameterConfiguration : parameterConfigurations) 121 { 122 // XML schema requires attributes id and enforces id uniqueness 123 String id = parameterConfiguration.getAttribute("id", null); 124 125 // Add the new parameter to the list of declared parameters 126 _configParameters.put(id, new ConfigParameterInfo(id, _pluginName, parameterConfiguration)); 127 } 128 } 129 130 private void _configureParametersCheckers(Configuration configConfiguration) 131 { 132 Configuration[] parameterConfigurations = configConfiguration.getChildren("param-checker"); 133 for (Configuration parameterConfiguration : parameterConfigurations) 134 { 135 String id = parameterConfiguration.getAttribute("id", null); 136 137 // Add the new parameter to the list of declared parameters 138 _paramCheckers.put(id, new ConfigParameterInfo(id, _pluginName, parameterConfiguration)); 139 } 140 } 141 142 private void _configureFeatures() 143 { 144 Configuration[] featuresConf = _configuration.getChildren("feature"); 145 146 for (Configuration conf : featuresConf) 147 { 148 // XML schema requires attributes name and enforces name uniqueness 149 String featureName = conf.getAttribute("name", null); 150 151 Feature feature = new Feature(_pluginName, featureName); 152 feature.configure(conf); 153 _features.put(feature.getFeatureId(), feature); 154 } 155 } 156 157 private void _configureExtensionPoints() 158 { 159 Configuration[] extPointConfs = _configuration.getChild("extension-points").getChildren("extension-point"); 160 161 for (Configuration conf : extPointConfs) 162 { 163 // XML schema requires attributes id and class and enforces id uniqueness 164 String id = conf.getAttribute("id", null); 165 boolean safe = conf.getAttributeAsBoolean("safe", false); 166 _extensionPoints.put(id, new ExtensionPointDefinition(id, conf, _pluginName, safe)); 167 } 168 } 169}