001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.plugins;
021
022 import org.picocontainer.Characteristics;
023 import org.picocontainer.MutablePicoContainer;
024 import org.slf4j.Logger;
025 import org.slf4j.LoggerFactory;
026 import org.sonar.api.BatchExtension;
027 import org.sonar.api.Extension;
028 import org.sonar.api.Plugin;
029 import org.sonar.api.ServerExtension;
030
031 import java.util.IdentityHashMap;
032 import java.util.Map;
033
034 /**
035 * EXPERIMENTAL - DO NOT USE - IT WILL BE REMOVED IN VERSION 2.2
036 * @since 2.1
037 */
038 public class ExtensionProvider implements BatchExtension, ServerExtension {
039
040 private static final Logger LOG = LoggerFactory.getLogger(ExtensionProvider.class);
041
042 private PluginProvider pluginProvider;
043 private Map<Object, Plugin> pluginsByExtension = new IdentityHashMap<Object, Plugin>();
044
045 public ExtensionProvider(PluginProvider pluginProvider) {
046 this.pluginProvider = pluginProvider;
047 }
048
049 public void registerBatchExtensions(MutablePicoContainer container) {
050 for (Plugin plugin : pluginProvider.getPlugins()) {
051 registerExtensions(container, plugin, BatchExtension.class);
052 }
053 }
054
055 public void registerServerExtensions(MutablePicoContainer container) {
056 for (Plugin plugin : pluginProvider.getPlugins()) {
057 registerExtensions(container, plugin, ServerExtension.class);
058 }
059 }
060
061 private void registerExtensions(MutablePicoContainer container, Plugin plugin, Class<? extends Extension> extensionClass) {
062 for (Object extension : plugin.getExtensions()) {
063 if (isExtension(extension, extensionClass)) {
064 LOG.debug("Registering the extension: " + extension);
065 registerExtension(container, plugin, extension);
066 }
067 }
068 }
069
070 public void registerExtension(MutablePicoContainer container, Plugin plugin, Object extension) {
071 container.as(Characteristics.CACHE).addComponent(getExtensionKey(extension), extension);
072 pluginsByExtension.put(extension, plugin);
073 }
074
075 public Plugin getPluginForExtension(Object extension) {
076 Plugin plugin = pluginsByExtension.get(extension);
077 if (plugin == null && !(extension instanceof Class)) {
078 plugin = pluginsByExtension.get(extension.getClass());
079 }
080 return plugin;
081 }
082
083 public String getPluginKeyForExtension(Object extension) {
084 Plugin plugin = getPluginForExtension(extension);
085 if (plugin != null) {
086 return plugin.getKey();
087 }
088 return null;
089 }
090
091 private boolean isExtension(Object extension, Class<? extends Extension> extensionClass) {
092 Class clazz = (extension instanceof Class ? (Class)extension : extension.getClass());
093 return extensionClass.isAssignableFrom(clazz);
094 }
095
096 private Object getExtensionKey(Object component) {
097 if (component instanceof Class) {
098 return component;
099 }
100 return component.getClass().getCanonicalName() + "-" + component.toString();
101 }
102
103
104 }