bind decorator factories

This commit is contained in:
Sebastian Sdorra
2012-11-29 08:54:18 +01:00
parent 3afd81c773
commit 3e443e5447
7 changed files with 615 additions and 10 deletions

View File

@@ -94,10 +94,27 @@ public class BindingExtensionProcessor implements ExtensionProcessor
{
this.moduleSet = Sets.newHashSet();
this.extensions = Sets.newHashSet();
this.decoratorFactories = Sets.newHashSet();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param binder
*/
public void bindDecorators(Binder binder)
{
DecoratorBinder decoratorBinder = new DecoratorBinder(binder);
for (Class<? extends DecoratorFactory<?>> c : decoratorFactories)
{
decoratorBinder.bindFactory(c);
}
}
/**
* Method description
*
@@ -373,6 +390,10 @@ public class BindingExtensionProcessor implements ExtensionProcessor
addModuleClass(extensionClass);
}
else if (DecoratorFactory.class.isAssignableFrom(extensionClass))
{
decoratorFactories.add(extensionClass);
}
else
{
extensions.add(extensionClass);
@@ -460,6 +481,9 @@ public class BindingExtensionProcessor implements ExtensionProcessor
//~--- fields ---------------------------------------------------------------
/** Field description */
private Set<Class<? extends DecoratorFactory<?>>> decoratorFactories;
/** Field description */
private Set<Class<?>> extensions;

View File

@@ -0,0 +1,127 @@
/**
* 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.multibindings.Multibinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.group.GroupManagerDecoratorFactory;
import sonia.scm.repository.RepositoryManagerDecoratorFactory;
import sonia.scm.user.UserManagerDecoratorFactory;
/**
*
* @author Sebastian Sdorra
*/
public class DecoratorBinder
{
/**
* the logger for DecoratorBinder
*/
private static final Logger logger =
LoggerFactory.getLogger(DecoratorBinder.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*
* @param binder
*/
public DecoratorBinder(Binder binder)
{
userManagerDecoratorFactories = Multibinder.newSetBinder(binder,
UserManagerDecoratorFactory.class);
groupManagerDecoratorFactories = Multibinder.newSetBinder(binder,
GroupManagerDecoratorFactory.class);
repositoryManagerDecoratorFactories = Multibinder.newSetBinder(binder,
RepositoryManagerDecoratorFactory.class);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param factoryClass
*/
public void bindFactory(Class factoryClass)
{
if (UserManagerDecoratorFactory.class.isAssignableFrom(factoryClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind user manager decorator {}");
}
userManagerDecoratorFactories.addBinding().to(factoryClass);
}
else if (GroupManagerDecoratorFactory.class.isAssignableFrom(factoryClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind group manager decorator {}");
}
groupManagerDecoratorFactories.addBinding().to(factoryClass);
}
else if (
RepositoryManagerDecoratorFactory.class.isAssignableFrom(factoryClass))
{
if (logger.isInfoEnabled())
{
logger.info("bind repository manager decorator {}");
}
repositoryManagerDecoratorFactories.addBinding().to(factoryClass);
}
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private Multibinder<GroupManagerDecoratorFactory> groupManagerDecoratorFactories;
/** Field description */
private Multibinder<RepositoryManagerDecoratorFactory> repositoryManagerDecoratorFactories;
/** Field description */
private Multibinder<UserManagerDecoratorFactory> userManagerDecoratorFactories;
}

View File

@@ -35,6 +35,7 @@ package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Provider;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.name.Names;
import com.google.inject.servlet.RequestScoped;
@@ -55,6 +56,7 @@ import sonia.scm.filter.SecurityFilter;
import sonia.scm.group.DefaultGroupManager;
import sonia.scm.group.GroupDAO;
import sonia.scm.group.GroupManager;
import sonia.scm.group.GroupManagerProvider;
import sonia.scm.group.xml.XmlGroupDAO;
import sonia.scm.io.DefaultFileSystem;
import sonia.scm.io.FileSystem;
@@ -71,6 +73,7 @@ import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryBrowserUtil;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryManagerProvider;
import sonia.scm.repository.RepositoryProvider;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.xml.XmlRepositoryDAO;
@@ -106,6 +109,7 @@ import sonia.scm.url.WebUIUrlProvider;
import sonia.scm.user.DefaultUserManager;
import sonia.scm.user.UserDAO;
import sonia.scm.user.UserManager;
import sonia.scm.user.UserManagerProvider;
import sonia.scm.user.xml.XmlUserDAO;
import sonia.scm.util.DebugServlet;
import sonia.scm.util.ScmConfigurationUtil;
@@ -231,6 +235,9 @@ public class ScmServletModule extends ServletModule
bind(SCMContextProvider.class).toInstance(context);
// bind decorators
bindExtProcessor.bindDecorators(binder());
ScmConfiguration config = getScmConfiguration(context);
CipherUtil cu = CipherUtil.getInstance();
@@ -277,11 +284,12 @@ public class ScmServletModule extends ServletModule
bind(UserDAO.class, XmlUserDAO.class);
bind(RepositoryDAO.class, XmlRepositoryDAO.class);
// bind(RepositoryManager.class).annotatedWith(Undecorated.class).to(
// BasicRepositoryManager.class);
bind(RepositoryManager.class, DefaultRepositoryManager.class);
bind(UserManager.class, DefaultUserManager.class);
bind(GroupManager.class, DefaultGroupManager.class);
bindDecorated(RepositoryManager.class, DefaultRepositoryManager.class,
RepositoryManagerProvider.class);
bindDecorated(UserManager.class, DefaultUserManager.class,
UserManagerProvider.class);
bindDecorated(GroupManager.class, DefaultGroupManager.class,
GroupManagerProvider.class);
bind(CGIExecutorFactory.class, DefaultCGIExecutorFactory.class);
bind(ChangesetViewerUtil.class);
bind(RepositoryBrowserUtil.class);
@@ -433,25 +441,79 @@ public class ScmServletModule extends ServletModule
*/
private <T> void bind(Class<T> clazz,
Class<? extends T> defaultImplementation)
{
Class<? extends T> implementation = find(clazz, defaultImplementation);
if (logger.isDebugEnabled())
{
logger.debug("bind {} to {}", clazz, implementation);
}
bind(clazz).to(implementation);
}
/**
* Method description
*
*
* @param clazz
* @param defaultImplementation
* @param providerClass
* @param <T>
*/
private <T> void bindDecorated(Class<T> clazz,
Class<? extends T> defaultImplementation,
Class<? extends Provider<T>> providerClass)
{
Class<? extends T> implementation = find(clazz, defaultImplementation);
if (logger.isDebugEnabled())
{
logger.debug("bind undecorated {} to {}", clazz, implementation);
}
bind(clazz).annotatedWith(Undecorated.class).to(implementation);
if (logger.isDebugEnabled())
{
logger.debug("bind {} to provider {}", clazz, providerClass);
}
bind(clazz).toProvider(providerClass);
}
/**
* Method description
*
*
* @param clazz
* @param defaultImplementation
* @param <T>
*
* @return
*/
private <T> Class<? extends T> find(Class<T> clazz,
Class<? extends T> defaultImplementation)
{
Class<? extends T> implementation = overrides.getOverride(clazz);
if (implementation != null)
{
logger.info("bind {} to override {}", clazz, implementation);
logger.info("found override {} for {}", implementation, clazz);
}
else
{
implementation = defaultImplementation;
if (logger.isDebugEnabled())
if (logger.isTraceEnabled())
{
logger.debug("bind {} to default implementation {}", clazz,
implementation);
logger.trace(
"no override available for {}, using default implementation {}",
clazz, implementation);
}
}
bind(clazz).to(implementation);
return implementation;
}
//~--- get methods ----------------------------------------------------------

View File

@@ -0,0 +1,101 @@
/**
* 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.group;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.Undecorated;
import sonia.scm.util.Decorators;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class GroupManagerProvider implements Provider<GroupManager>
{
/**
* Method description
*
*
* @return
*/
@Override
public GroupManager get()
{
return Decorators.decorate(groupManagerProvider.get(), decoratorFactories);
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param decoratorFactories
*/
public void setDecoratorFactories(
Set<GroupManagerDecoratorFactory> decoratorFactories)
{
this.decoratorFactories = decoratorFactories;
}
/**
* Method description
*
*
* @param groupManagerProvider
*/
public void setGroupManagerProvider(
Provider<GroupManager> groupManagerProvider)
{
this.groupManagerProvider = groupManagerProvider;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject(optional = true)
private Set<GroupManagerDecoratorFactory> decoratorFactories;
/** Field description */
@Inject
@Undecorated
private Provider<GroupManager> groupManagerProvider;
}

View File

@@ -0,0 +1,102 @@
/**
* 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.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.Undecorated;
import sonia.scm.util.Decorators;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class RepositoryManagerProvider implements Provider<RepositoryManager>
{
/**
* Method description
*
*
* @return
*/
@Override
public RepositoryManager get()
{
return Decorators.decorate(repositoryManagerProvider.get(),
decoratorFactories);
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param decoratorFactories
*/
public void setDecoratorFactories(
Set<RepositoryManagerDecoratorFactory> decoratorFactories)
{
this.decoratorFactories = decoratorFactories;
}
/**
* Method description
*
*
* @param repositoryManagerProvider
*/
public void setRepositoryManagerProvider(
Provider<RepositoryManager> repositoryManagerProvider)
{
this.repositoryManagerProvider = repositoryManagerProvider;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject(optional = true)
private Set<RepositoryManagerDecoratorFactory> decoratorFactories;
/** Field description */
@Inject
@Undecorated
private Provider<RepositoryManager> repositoryManagerProvider;
}

View File

@@ -0,0 +1,100 @@
/**
* 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.user;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Provider;
import sonia.scm.Undecorated;
import sonia.scm.util.Decorators;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
/**
*
* @author Sebastian Sdorra
*/
public class UserManagerProvider implements Provider<UserManager>
{
/**
* Method description
*
*
* @return
*/
@Override
public UserManager get()
{
return Decorators.decorate(userManagerProvider.get(), decoratorFactories);
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param decoratorFactories
*/
public void setDecoratorFactories(
Set<UserManagerDecoratorFactory> decoratorFactories)
{
this.decoratorFactories = decoratorFactories;
}
/**
* Method description
*
*
* @param userManagerProvider
*/
public void setUserManagerProvider(Provider<UserManager> userManagerProvider)
{
this.userManagerProvider = userManagerProvider;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Inject(optional = true)
private Set<UserManagerDecoratorFactory> decoratorFactories;
/** Field description */
@Inject
@Undecorated
private Provider<UserManager> userManagerProvider;
}

View File

@@ -0,0 +1,89 @@
/**
* 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.util;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.DecoratorFactory;
/**
*
* @author Sebastian Sdorra
*/
public class Decorators
{
/**
* the logger for Decorators
*/
private static final Logger logger =
LoggerFactory.getLogger(Decorators.class);
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param object
* @param decoratorFactories
* @param <T>
*
* @return
*/
public static <T> T decorate(T object,
Iterable<? extends DecoratorFactory<T>> decoratorFactories)
{
if (decoratorFactories != null)
{
for (DecoratorFactory<T> decoratorFactory : decoratorFactories)
{
if (logger.isDebugEnabled())
{
logger.debug("decorate {} with {}", object.getClass(),
decoratorFactory.getClass());
}
object = decoratorFactory.createDecorator(object);
}
}
else if (logger.isDebugEnabled())
{
logger.debug("no decorators found for {}", object.getClass());
}
return object;
}
}