mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
improve plugin structure
This commit is contained in:
@@ -9,12 +9,6 @@ package sonia.scm;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.repository.BasicRepositoryManager;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.security.EncryptionHandler;
|
||||
import sonia.scm.security.MessageDigestEncryptionHandler;
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -22,11 +16,6 @@ import sonia.scm.util.Util;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -63,33 +52,14 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
for (SCMPlugin plugin : plugins)
|
||||
{
|
||||
plugin.contextDestroyed(this);
|
||||
}
|
||||
|
||||
for (GroupManager manager : groupManagerMap.values())
|
||||
{
|
||||
manager.close();
|
||||
}
|
||||
|
||||
repositoryManager.close();
|
||||
}
|
||||
public void close() throws IOException {}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void init()
|
||||
{
|
||||
loadGroupManagers();
|
||||
loadRepositoryManager();
|
||||
loadEncryptionHandler();
|
||||
loadPlugins();
|
||||
}
|
||||
public void init() {}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
@@ -105,45 +75,6 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
return baseDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public EncryptionHandler getEncryptionHandler()
|
||||
{
|
||||
return encryptionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public GroupManager getGroupManager(String type)
|
||||
{
|
||||
return groupManagerMap.get(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public RepositoryManager getRepositoryManager()
|
||||
{
|
||||
return repositoryManager;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -177,82 +108,8 @@ public class BasicContextProvider implements SCMContextProvider
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadEncryptionHandler()
|
||||
{
|
||||
encryptionHandler = ServiceUtil.getService(EncryptionHandler.class);
|
||||
|
||||
if (encryptionHandler == null)
|
||||
{
|
||||
encryptionHandler = new MessageDigestEncryptionHandler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadGroupManagers()
|
||||
{
|
||||
groupManagerMap = new HashMap<String, GroupManager>();
|
||||
|
||||
List<GroupManager> groupManagers =
|
||||
ServiceUtil.getServices(GroupManager.class);
|
||||
|
||||
for (GroupManager manager : groupManagers)
|
||||
{
|
||||
manager.init(this);
|
||||
groupManagerMap.put(manager.getType(), manager);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadPlugins()
|
||||
{
|
||||
plugins = ServiceUtil.getServices(SCMPlugin.class);
|
||||
|
||||
for (SCMPlugin plugin : plugins)
|
||||
{
|
||||
plugin.contextInitialized(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
private void loadRepositoryManager()
|
||||
{
|
||||
repositoryManager = ServiceUtil.getService(RepositoryManager.class);
|
||||
|
||||
if (repositoryManager == null)
|
||||
{
|
||||
repositoryManager = new BasicRepositoryManager();
|
||||
}
|
||||
|
||||
repositoryManager.init(this);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private File baseDirectory;
|
||||
|
||||
/** Field description */
|
||||
private EncryptionHandler encryptionHandler;
|
||||
|
||||
/** Field description */
|
||||
private Map<String, GroupManager> groupManagerMap;
|
||||
|
||||
/** Field description */
|
||||
private List<SCMPlugin> plugins;
|
||||
|
||||
/** Field description */
|
||||
private RepositoryManager repositoryManager;
|
||||
}
|
||||
|
||||
@@ -7,12 +7,6 @@
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.security.EncryptionHandler;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.Closeable;
|
||||
@@ -40,31 +34,4 @@ public interface SCMContextProvider extends Closeable
|
||||
* @return
|
||||
*/
|
||||
public File getBaseDirectory();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public EncryptionHandler getEncryptionHandler();
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
public GroupManager getGroupManager(String type);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public RepositoryManager getRepositoryManager();
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface SCMPlugin
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void contextDestroyed(SCMContextProvider context);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void contextInitialized(SCMContextProvider context);
|
||||
}
|
||||
91
scm-core/src/main/java/sonia/scm/plugin/SCMPlugin.java
Normal file
91
scm-core/src/main/java/sonia/scm/plugin/SCMPlugin.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.repository.RepositoryHandler;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@XmlRootElement(name = "plugin-config")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class SCMPlugin
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public SCMPlugin()
|
||||
{
|
||||
handlers = new HashSet<Class<? extends RepositoryHandler>>();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param handlerClass
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean addHandler(Class<? extends RepositoryHandler> handlerClass)
|
||||
{
|
||||
return handlers.add(handlerClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param handlerClass
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean removeHandler(Class<? extends RepositoryHandler> handlerClass)
|
||||
{
|
||||
return handlers.remove(handlerClass);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<Class<? extends RepositoryHandler>> getHandlers()
|
||||
{
|
||||
return handlers;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@XmlElementWrapper(name = "repository-handlers")
|
||||
@XmlElement(name = "handler")
|
||||
private Set<Class<? extends RepositoryHandler>> handlers;
|
||||
}
|
||||
142
scm-core/src/main/java/sonia/scm/plugin/SCMPluginManager.java
Normal file
142
scm-core/src/main/java/sonia/scm/plugin/SCMPluginManager.java
Normal file
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.repository.RepositoryHandler;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.bind.JAXB;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SCMPluginManager
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String PATH_PLUGINCONFIG = "META-INF/scm/plugin.xml";
|
||||
|
||||
/** Field description */
|
||||
private static final Logger logger =
|
||||
Logger.getLogger(SCMPluginManager.class.getName());
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public SCMPluginManager()
|
||||
{
|
||||
repositoryHandlers = new HashSet<Class<? extends RepositoryHandler>>();
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load() throws IOException
|
||||
{
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
if (classLoader == null)
|
||||
{
|
||||
classLoader = SCMPluginManager.class.getClassLoader();
|
||||
}
|
||||
|
||||
load(classLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param classLoader
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void load(ClassLoader classLoader) throws IOException
|
||||
{
|
||||
Enumeration<URL> urlEnum = classLoader.getResources(PATH_PLUGINCONFIG);
|
||||
|
||||
if (urlEnum != null)
|
||||
{
|
||||
while (urlEnum.hasMoreElements())
|
||||
{
|
||||
URL url = urlEnum.nextElement();
|
||||
|
||||
loadPlugin(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<Class<? extends RepositoryHandler>> getRepositoryHandlers()
|
||||
{
|
||||
return repositoryHandlers;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
private void loadPlugin(URL url)
|
||||
{
|
||||
try
|
||||
{
|
||||
SCMPlugin plugin = JAXB.unmarshal(url, SCMPlugin.class);
|
||||
Collection<Class<? extends RepositoryHandler>> handlers =
|
||||
plugin.getHandlers();
|
||||
|
||||
if (Util.isNotEmpty(handlers))
|
||||
{
|
||||
repositoryHandlers.addAll(handlers);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Set<Class<? extends RepositoryHandler>> repositoryHandlers;
|
||||
}
|
||||
@@ -9,12 +9,14 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import sonia.scm.ConfigurationException;
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.SCMContextProvider;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -32,17 +34,26 @@ import java.util.Set;
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class BasicRepositoryManager extends AbstractRepositoryManager
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param handlerSet
|
||||
*/
|
||||
public BasicRepositoryManager()
|
||||
@Inject
|
||||
public BasicRepositoryManager(Set<RepositoryHandler> handlerSet)
|
||||
{
|
||||
handlerMap = new HashMap<String, RepositoryHandler>();
|
||||
types = new ArrayList<RepositoryType>();
|
||||
|
||||
for (RepositoryHandler handler : handlerSet)
|
||||
{
|
||||
addHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -70,6 +81,7 @@ public class BasicRepositoryManager extends AbstractRepositoryManager
|
||||
|
||||
handlerMap.put(type.getName(), handler);
|
||||
handler.init(SCMContext.getContext());
|
||||
types.add(type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,9 +93,9 @@ public class BasicRepositoryManager extends AbstractRepositoryManager
|
||||
@Override
|
||||
public void close() throws IOException
|
||||
{
|
||||
for (RepositoryHandler manager : handlerMap.values())
|
||||
for (RepositoryHandler handler : handlerMap.values())
|
||||
{
|
||||
manager.close();
|
||||
IOUtil.close(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,23 +140,7 @@ public class BasicRepositoryManager extends AbstractRepositoryManager
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void init(SCMContextProvider context)
|
||||
{
|
||||
List<RepositoryHandler> handlerList =
|
||||
ServiceUtil.getServices(RepositoryHandler.class);
|
||||
|
||||
if (Util.isNotEmpty(handlerList))
|
||||
{
|
||||
for (RepositoryHandler handler : handlerList)
|
||||
{
|
||||
RepositoryType type = handler.getType();
|
||||
|
||||
types.add(type);
|
||||
handlerMap.put(type.getName(), handler);
|
||||
handler.init(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void init(SCMContextProvider context) {}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
|
||||
Reference in New Issue
Block a user