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 * @since 2.1
036 */
037 public class ExtensionProvider implements BatchExtension, ServerExtension {
038
039 private static final Logger LOG = LoggerFactory.getLogger(ExtensionProvider.class);
040
041 private PluginProvider pluginProvider;
042 private Map<Object, Plugin> pluginsByExtension = new IdentityHashMap<Object, Plugin>();
043
044 public ExtensionProvider(PluginProvider pluginProvider) {
045 this.pluginProvider = pluginProvider;
046 }
047
048 public void registerBatchExtensions(MutablePicoContainer container) {
049 for (Plugin plugin : pluginProvider.getPlugins()) {
050 registerExtensions(container, plugin, BatchExtension.class);
051 }
052 }
053
054 public void registerServerExtensions(MutablePicoContainer container) {
055 for (Plugin plugin : pluginProvider.getPlugins()) {
056 registerExtensions(container, plugin, ServerExtension.class);
057 }
058 }
059
060 private void registerExtensions(MutablePicoContainer container, Plugin plugin, Class<? extends Extension> extensionClass) {
061 for (Object extension : plugin.getExtensions()) {
062 if (isExtension(extension, extensionClass)) {
063 LOG.debug("Registering the extension: " + extension);
064 container.as(Characteristics.CACHE).addComponent(getExtensionKey(extension), extension);
065 pluginsByExtension.put(extension, plugin);
066 }
067 }
068 }
069
070 public Plugin getPluginForExtension(Object extension) {
071 Plugin plugin = pluginsByExtension.get(extension);
072 if (plugin == null && !(extension instanceof Class)) {
073 plugin = pluginsByExtension.get(extension.getClass());
074 }
075 return plugin;
076 }
077
078 public String getPluginKeyForExtension(Object extension) {
079 Plugin plugin = getPluginForExtension(extension);
080 if (plugin != null) {
081 return plugin.getKey();
082 }
083 return null;
084 }
085
086 private boolean isExtension(Object extension, Class<? extends Extension> extensionClass) {
087 Class clazz = (extension instanceof Class ? (Class)extension : extension.getClass());
088 return extensionClass.isAssignableFrom(clazz);
089 }
090
091 private Object getExtensionKey(Object component) {
092 if (component instanceof Class) {
093 return component;
094 }
095 return component.getClass().getCanonicalName() + "-" + component.toString();
096 }
097
098
099 }