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.cocoon; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.component.Component; 025import org.apache.avalon.framework.configuration.Configuration; 026import org.apache.avalon.framework.configuration.ConfigurationException; 027import org.apache.avalon.framework.logger.AbstractLogEnabled; 028 029import org.ametys.runtime.plugin.ExtensionPoint; 030 031/** 032 * Allows to dynamically declare sitemap components to the main sitemap, subsequently available to all plugins and workspaces 033 */ 034public class SitemapConfigurationExtensionPoint extends AbstractLogEnabled implements ExtensionPoint<Configuration[]>, Component 035{ 036 /** Avalon Role */ 037 public static final String ROLE = SitemapConfigurationExtensionPoint.class.getName(); 038 039 private static final Collection<String> __COMPONENTS = new ArrayList<>(); 040 041 static 042 { 043 __COMPONENTS.add("actions"); 044 __COMPONENTS.add("generators"); 045 __COMPONENTS.add("transformers"); 046 __COMPONENTS.add("serializers"); 047 __COMPONENTS.add("readers"); 048 __COMPONENTS.add("matchers"); 049 __COMPONENTS.add("selectors"); 050 __COMPONENTS.add("pipes"); 051 } 052 053 private Map<String, Collection<Configuration>> _sitemapConfigurations = new HashMap<>(); 054 private Map<String, Configuration[]> _extensions = new HashMap<>(); 055 056 public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException 057 { 058 Configuration[] configurations = configuration.getChildren(); 059 060 _extensions.put(id, configurations); 061 062 for (Configuration config : configurations) 063 { 064 String name = config.getName(); 065 String componentName = name + 's'; 066 067 if (!__COMPONENTS.contains(componentName)) 068 { 069 String errorMessage = "The feature '" + pluginName + "." + featureName + "' declares an invalid sitemap component : " + name; 070 getLogger().error(errorMessage); 071 throw new IllegalArgumentException(errorMessage); 072 } 073 074 Collection<Configuration> configs = _sitemapConfigurations.get(componentName); 075 if (configs == null) 076 { 077 configs = new ArrayList<>(); 078 _sitemapConfigurations.put(componentName, configs); 079 } 080 081 configs.add(config); 082 } 083 } 084 085 public Configuration[] getExtension(String id) 086 { 087 return _extensions.get(id); 088 } 089 090 public Set<String> getExtensionsIds() 091 { 092 return _extensions.keySet(); 093 } 094 095 public boolean hasExtension(String id) 096 { 097 return _extensions.containsKey(id); 098 } 099 100 public void initializeExtensions() throws Exception 101 { 102 // nothing to do 103 } 104 105 /** 106 * Returns all the Configurations for a given sitemap component type 107 * @param component a sitemap component type (eg. "action", "generator", ...) 108 * @return all the Configurations for a given sitemap component type. May be null if the given component type does not exist or have not been extended. 109 */ 110 public Collection<Configuration> getConfigurations(String component) 111 { 112 if (!_sitemapConfigurations.containsKey(component)) 113 { 114 return null; 115 } 116 117 return _sitemapConfigurations.get(component); 118 } 119}