Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/BindingExtensionProcessor.java

385 lines
11 KiB
Java
Raw Normal View History

2010-12-01 14:26:29 +01:00
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.multibindings.Multibinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2011-01-02 14:33:26 +01:00
import sonia.scm.group.GroupListener;
2011-01-30 13:48:07 +01:00
import sonia.scm.io.FileSystem;
2010-12-01 14:26:29 +01:00
import sonia.scm.plugin.ext.Extension;
import sonia.scm.plugin.ext.ExtensionProcessor;
2011-04-18 21:51:54 +02:00
import sonia.scm.repository.ChangesetPreProcessor;
2011-08-15 15:07:35 +02:00
import sonia.scm.repository.ChangesetPreProcessorFactory;
2010-12-01 14:26:29 +01:00
import sonia.scm.repository.RepositoryHandler;
2011-07-19 15:32:53 +02:00
import sonia.scm.repository.RepositoryHook;
2011-01-02 14:33:26 +01:00
import sonia.scm.repository.RepositoryListener;
2011-11-27 21:02:51 +01:00
import sonia.scm.repository.RepositoryRequestListener;
2011-01-18 19:51:37 +01:00
import sonia.scm.resources.ResourceHandler;
2010-12-01 14:26:29 +01:00
import sonia.scm.security.EncryptionHandler;
2011-01-02 14:33:26 +01:00
import sonia.scm.user.UserListener;
2010-12-04 15:58:13 +01:00
import sonia.scm.web.security.AuthenticationHandler;
2011-01-07 11:57:15 +01:00
import sonia.scm.web.security.AuthenticationListener;
2010-12-04 15:58:13 +01:00
import sonia.scm.web.security.XmlAuthenticationHandler;
2010-12-01 14:26:29 +01:00
//~--- JDK imports ------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class BindingExtensionProcessor implements ExtensionProcessor
{
/** the logger for BindingExtensionProcessor */
private static final Logger logger =
LoggerFactory.getLogger(BindingExtensionProcessor.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
*/
public BindingExtensionProcessor()
{
this.moduleSet = new HashSet<Module>();
this.extensions = new HashSet<Class<?>>();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param binder
*/
@SuppressWarnings("unchecked")
public void bindExtensions(Binder binder)
{
Multibinder<RepositoryHandler> repositoryHandlers =
Multibinder.newSetBinder(binder, RepositoryHandler.class);
2010-12-04 15:58:13 +01:00
Multibinder<AuthenticationHandler> authenticators =
Multibinder.newSetBinder(binder, AuthenticationHandler.class);
2011-01-18 19:51:37 +01:00
Multibinder<ResourceHandler> resourceHandler =
Multibinder.newSetBinder(binder, ResourceHandler.class);
2011-04-18 21:51:54 +02:00
Multibinder<ChangesetPreProcessor> changesetPreProcessorBinder =
Multibinder.newSetBinder(binder, ChangesetPreProcessor.class);
2011-08-15 15:07:35 +02:00
Multibinder<ChangesetPreProcessorFactory> changesetPreProcessorFactoryBinder =
Multibinder.newSetBinder(binder, ChangesetPreProcessorFactory.class);
2011-09-04 13:36:37 +02:00
Multibinder<RepositoryHook> repositoryHookBinder =
Multibinder.newSetBinder(binder, RepositoryHook.class);
2010-12-02 20:44:13 +01:00
// listeners
Multibinder<RepositoryListener> repositoryListenerBinder =
Multibinder.newSetBinder(binder, RepositoryListener.class);
Multibinder<UserListener> userListenerBinder =
Multibinder.newSetBinder(binder, UserListener.class);
Multibinder<GroupListener> groupListenerBinder =
Multibinder.newSetBinder(binder, GroupListener.class);
Multibinder<AuthenticationListener> authenticationListenerBinder =
Multibinder.newSetBinder(binder, AuthenticationListener.class);
2011-11-27 21:02:51 +01:00
Multibinder<RepositoryRequestListener> repositoryRequestListenerBinder =
Multibinder.newSetBinder(binder, RepositoryRequestListener.class);
2010-12-04 15:58:13 +01:00
authenticators.addBinding().to(XmlAuthenticationHandler.class);
2010-12-01 14:26:29 +01:00
for (Class extensionClass : extensions)
{
2011-09-04 13:36:37 +02:00
if (RepositoryHandler.class.isAssignableFrom(extensionClass))
2010-12-01 14:26:29 +01:00
{
2011-09-04 13:36:37 +02:00
if (logger.isInfoEnabled())
2010-12-01 14:26:29 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind RepositoryHandler {}", extensionClass.getName());
2010-12-01 14:26:29 +01:00
}
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
repositoryHandlers.addBinding().to(extensionClass);
}
else if (EncryptionHandler.class.isAssignableFrom(extensionClass))
{
bind(binder, EncryptionHandler.class, extensionClass);
}
else if (AuthenticationHandler.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2010-12-01 14:26:29 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind AuthenticationHandler {}",
extensionClass.getName());
2010-12-01 14:26:29 +01:00
}
2010-12-02 20:44:13 +01:00
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
authenticators.addBinding().to(extensionClass);
}
else if (GroupListener.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2011-01-02 14:33:26 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind GroupListener {}", extensionClass.getName());
2011-01-02 14:33:26 +01:00
}
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
groupListenerBinder.addBinding().to(extensionClass);
}
else if (UserListener.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2011-01-02 14:33:26 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind UserListener {}", extensionClass.getName());
2011-01-02 14:33:26 +01:00
}
2011-01-07 11:57:15 +01:00
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
userListenerBinder.addBinding().to(extensionClass);
}
else if (RepositoryListener.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2011-01-18 19:51:37 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind RepositoryListener {}", extensionClass.getName());
2011-01-18 19:51:37 +01:00
}
2011-01-30 13:48:07 +01:00
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
repositoryListenerBinder.addBinding().to(extensionClass);
}
else if (AuthenticationListener.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2011-04-18 21:51:54 +02:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind AuthenticaitonListener {}",
extensionClass.getName());
2011-04-18 21:51:54 +02:00
}
2011-08-15 14:53:35 +02:00
2011-09-04 13:36:37 +02:00
binder.bind(extensionClass);
authenticationListenerBinder.addBinding().to(extensionClass);
}
else if (ResourceHandler.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
2011-09-04 13:36:37 +02:00
logger.info("bind ResourceHandler {}", extensionClass.getName());
}
2011-09-04 13:36:37 +02:00
resourceHandler.addBinding().to(extensionClass);
}
else if (FileSystem.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind FileSystem {}", extensionClass.getName());
}
2011-09-04 13:36:37 +02:00
fileSystemClass = extensionClass;
}
else if (ChangesetPreProcessor.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
2010-12-01 14:26:29 +01:00
{
2011-09-04 13:36:37 +02:00
logger.info("bind ChangesetPreProcessor {}",
extensionClass.getName());
}
2010-12-01 14:26:29 +01:00
2011-09-04 13:36:37 +02:00
changesetPreProcessorBinder.addBinding().to(extensionClass);
}
else if (ChangesetPreProcessorFactory.class.isAssignableFrom(
extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind ChangesetPreProcessorFactory {}",
extensionClass.getName());
2010-12-01 14:26:29 +01:00
}
2011-09-04 13:36:37 +02:00
changesetPreProcessorFactoryBinder.addBinding().to(extensionClass);
2010-12-01 14:26:29 +01:00
}
2011-09-04 13:36:37 +02:00
else if (RepositoryHook.class.isAssignableFrom(extensionClass))
2011-01-30 15:06:58 +01:00
{
2011-09-04 13:36:37 +02:00
if (logger.isInfoEnabled())
{
logger.info("bind RepositoryHook {}", extensionClass.getName());
}
repositoryHookBinder.addBinding().to(extensionClass);
2011-01-30 15:06:58 +01:00
}
2011-11-27 21:02:51 +01:00
else if (RepositoryRequestListener.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind RepositoryRequestListener {}",
extensionClass.getName());
}
repositoryRequestListenerBinder.addBinding().to(extensionClass);
}
2011-09-04 13:36:37 +02:00
else
2010-12-01 14:26:29 +01:00
{
2011-09-04 13:36:37 +02:00
if (logger.isInfoEnabled())
{
logger.info("bind {}", extensionClass.getName());
}
binder.bind(extensionClass);
2010-12-01 14:26:29 +01:00
}
}
}
/**
* Method description
*
*
* @param extension
* @param extensionClass
*/
@Override
public void processExtension(Extension extension, Class extensionClass)
{
if (Module.class.isAssignableFrom(extensionClass))
{
if (logger.isInfoEnabled())
{
logger.info("add GuiceModule {}", extensionClass.getName());
}
addModuleClass(extensionClass);
}
else
{
extensions.add(extensionClass);
}
}
//~--- get methods ----------------------------------------------------------
2011-01-30 13:48:07 +01:00
/**
* Method description
*
*
* @return
*/
public Class<? extends FileSystem> getFileSystemClass()
{
return fileSystemClass;
}
2010-12-01 14:26:29 +01:00
/**
* Method description
*
*
* @return
*/
2011-07-19 15:32:53 +02:00
public Set<RepositoryHook> getHooks()
2010-12-01 14:26:29 +01:00
{
2011-07-19 15:32:53 +02:00
return hooks;
2010-12-01 14:26:29 +01:00
}
/**
* Method description
*
*
* @return
*/
2011-07-19 15:32:53 +02:00
public Set<Module> getModuleSet()
{
2011-07-19 15:32:53 +02:00
return moduleSet;
}
2010-12-01 14:26:29 +01:00
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param extensionClass
*/
private void addModuleClass(Class<? extends Module> extensionClass)
{
try
{
Module module = extensionClass.newInstance();
moduleSet.add(module);
}
catch (Exception ex)
{
logger.error(ex.getMessage(), ex);
}
}
/**
* Method description
*
*
*
* @param binder
* @param type
* @param bindingType
* @param <T>
*
*/
private <T> void bind(Binder binder, Class<T> type,
Class<? extends T> bindingType)
{
if (logger.isDebugEnabled())
{
2011-01-02 14:33:26 +01:00
logger.debug("bind {} of type {}", type.getName(), bindingType.getName());
2010-12-01 14:26:29 +01:00
}
binder.bind(type).to(bindingType);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Set<Class<?>> extensions;
2011-01-30 13:48:07 +01:00
/** Field description */
private Class<? extends FileSystem> fileSystemClass;
2010-12-01 14:26:29 +01:00
/** Field description */
2011-07-19 15:32:53 +02:00
private Set<RepositoryHook> hooks = new HashSet<RepositoryHook>();
2011-01-02 14:33:26 +01:00
/** Field description */
2011-07-19 15:32:53 +02:00
private Set<Module> moduleSet;
2010-12-01 14:26:29 +01:00
}