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