001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 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.server.plugins; 021 022 import com.google.common.collect.Sets; 023 import org.slf4j.LoggerFactory; 024 import org.sonar.api.Plugin; 025 import org.sonar.api.Properties; 026 import org.sonar.api.Property; 027 import org.sonar.api.platform.PluginMetadata; 028 import org.sonar.api.platform.ServerPluginRepository; 029 import org.sonar.core.plugins.PluginClassloaders; 030 031 import java.util.Collection; 032 import java.util.Map; 033 import java.util.Set; 034 035 /** 036 * @since 2.2 037 */ 038 public class DefaultServerPluginRepository implements ServerPluginRepository { 039 040 private PluginClassloaders classloaders; 041 private PluginDeployer deployer; 042 private Map<String, Plugin> pluginsByKey; 043 private Set<String> disabledPlugins = Sets.newHashSet(); 044 045 public DefaultServerPluginRepository(PluginDeployer deployer) { 046 this.classloaders = new PluginClassloaders(getClass().getClassLoader()); 047 this.deployer = deployer; 048 } 049 050 public void start() { 051 Collection<PluginMetadata> metadata = deployer.getMetadata(); 052 pluginsByKey = classloaders.init(metadata); 053 } 054 055 public void stop() { 056 if (classloaders != null) { 057 classloaders.clean(); 058 classloaders = null; 059 } 060 } 061 062 public void disable(String pluginKey) { 063 disabledPlugins.add(pluginKey); 064 for (PluginMetadata metadata : getMetadata()) { 065 if (pluginKey.equals(metadata.getBasePlugin())) { 066 disable(metadata.getKey()); 067 } 068 } 069 } 070 071 public boolean isDisabled(String pluginKey) { 072 return disabledPlugins.contains(pluginKey); 073 } 074 075 public Collection<Plugin> getPlugins() { 076 return pluginsByKey.values(); 077 } 078 079 public Plugin getPlugin(String key) { 080 return pluginsByKey.get(key); 081 } 082 083 public ClassLoader getClassloader(String pluginKey) { 084 return classloaders.get(pluginKey); 085 } 086 087 public Class getClass(String pluginKey, String classname) { 088 Class clazz = null; 089 ClassLoader classloader = getClassloader(pluginKey); 090 if (classloader != null) { 091 try { 092 clazz = classloader.loadClass(classname); 093 094 } catch (ClassNotFoundException e) { 095 LoggerFactory.getLogger(getClass()).warn("Class not found in plugin " + pluginKey + ": " + classname, e); 096 } 097 } 098 return clazz; 099 } 100 101 102 public Property[] getProperties(Plugin plugin) { 103 if (plugin != null) { 104 Class<? extends Plugin> classInstance = plugin.getClass(); 105 if (classInstance.isAnnotationPresent(Properties.class)) { 106 return classInstance.getAnnotation(Properties.class).value(); 107 } 108 } 109 return new Property[0]; 110 } 111 112 public Collection<PluginMetadata> getMetadata() { 113 return deployer.getMetadata(); 114 } 115 116 public PluginMetadata getMetadata(String pluginKey) { 117 return deployer.getMetadata(pluginKey); 118 } 119 120 }