replace guava eventbus with legman

This commit is contained in:
Sebastian Sdorra
2014-01-03 12:56:18 +01:00
parent 5e6259f3b7
commit 0c7d6fa62f
17 changed files with 114 additions and 454 deletions

View File

@@ -46,7 +46,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.event.ScmEventBus;
import sonia.scm.event.Subscriber;
/**
*
@@ -85,34 +84,11 @@ public class ScmEventBusModule extends AbstractModule
logger.trace("register subscriber {}", clazz);
ScmEventBus.getInstance().register(object, isAsync(clazz));
ScmEventBus.getInstance().register(object);
}
});
}
});
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param clazz
*
* @return
*/
private boolean isAsync(Class<?> clazz)
{
boolean async = true;
Subscriber subscriber = clazz.getAnnotation(Subscriber.class);
if (subscriber != null)
{
async = subscriber.async();
}
return async;
}
}

View File

@@ -35,8 +35,8 @@ package sonia.scm.api.rest.resources;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe;
import com.google.common.base.Function;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;

View File

@@ -1,178 +0,0 @@
/**
* 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.event;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.ThrowingEventBus;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.shiro.concurrent.SubjectAwareExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
*
* @author Sebastian Sdorra
*/
public class GuavaScmEventBus extends ScmEventBus
{
/** Field description */
private static final String THREAD_NAME = "EventBus-%s";
/**
* the logger for GuavaScmEventBus
*/
private static final Logger logger =
LoggerFactory.getLogger(GuavaScmEventBus.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs a new ScmEventBus
*
*/
public GuavaScmEventBus()
{
eventBus = new ThrowingEventBus();
//J-
ThreadFactory factory = new ThreadFactoryBuilder()
.setNameFormat(THREAD_NAME).build();
asyncEventBus = new AsyncEventBus(
new SubjectAwareExecutorService(Executors.newCachedThreadPool(factory))
);
//J+
}
//~--- methods --------------------------------------------------------------
/**
* {@inheritDoc}
*
*
* @param event
*/
@Override
public void post(Object event)
{
logger.debug("post {} to event bus", event);
asyncEventBus.post(event);
eventBus.post(event);
}
/**
* {@inheritDoc}
*
*
* @param object
*/
@Override
public void register(Object object)
{
register(object, true);
}
/**
* Registers a object to the eventbus.
*
* @param object object to register
* @param async handle event asynchronously
*
* @see {@link #register(java.lang.Object)}
*/
@Override
public void register(Object object, boolean async)
{
logger.debug("register {} to event bus, async = {}", object, async);
if (async)
{
asyncEventBus.register(object);
}
else
{
eventBus.register(object);
}
}
/**
* {@inheritDoc}
*
*
* @param object
*/
@Override
public void unregister(Object object)
{
logger.debug("unregister {} from event bus", object);
unregister(asyncEventBus, object);
unregister(eventBus, object);
}
/**
* Unregisters the object from eventbus.
*
*
* @param bus event bus
* @param object object to unregister
*/
private void unregister(EventBus bus, Object object)
{
try
{
bus.unregister(object);
}
catch (IllegalArgumentException ex)
{
logger.trace("object {} was not registered at {}", object, bus);
}
}
//~--- fields ---------------------------------------------------------------
/** asynchronous event bus */
private AsyncEventBus asyncEventBus;
/** synchronous event bus */
private EventBus eventBus;
}

View File

@@ -31,34 +31,41 @@
package com.google.common.eventbus;
package sonia.scm.event;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Throwables;
import com.github.legman.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.event.EventBusException;
//~--- JDK imports ------------------------------------------------------------
import java.lang.reflect.InvocationTargetException;
/**
* {@link EventBus} which is able to throw {@link EventBusException}.
*
* @author Sebastian Sdorra
*/
public class ThrowingEventBus extends EventBus
public class LegmanScmEventBus extends ScmEventBus
{
/** Field description */
private static final String NAME = "ScmEventBus";
/**
* the logger for ThrowingEventBus
* the logger for LegmanScmEventBus
*/
private static final Logger logger =
LoggerFactory.getLogger(ThrowingEventBus.class);
LoggerFactory.getLogger(LegmanScmEventBus.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public LegmanScmEventBus()
{
eventBus = new EventBus(NAME);
}
//~--- methods --------------------------------------------------------------
@@ -67,24 +74,51 @@ public class ThrowingEventBus extends EventBus
*
*
* @param event
* @param wrapper
*/
@Override
void dispatch(Object event, EventHandler wrapper)
public void post(Object event)
{
logger.debug("post {} to event bus", event);
eventBus.post(event);
}
/**
* Registers a object to the eventbus.
*
* @param object object to register
*
* @see {@link #register(java.lang.Object)}
*/
@Override
public void register(Object object)
{
logger.debug("register {} to event bus", object);
eventBus.register(object);
}
/**
* {@inheritDoc}
*
*
* @param object
*/
@Override
public void unregister(Object object)
{
logger.debug("unregister {} from event bus", object);
try
{
wrapper.handleEvent(event);
eventBus.unregister(object);
}
catch (InvocationTargetException ex)
catch (IllegalArgumentException ex)
{
Throwable cause = ex.getCause();
Throwables.propagateIfPossible(cause);
logger.trace("could not propagate exception, throw as EventBusException");
throw new EventBusException(
"could not handle event ".concat(event.toString()), cause);
logger.trace("object {} was not registered", object);
}
}
//~--- fields ---------------------------------------------------------------
/** event bus */
private final EventBus eventBus;
}

View File

@@ -35,8 +35,8 @@ package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe;
import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe;
import com.google.common.io.Files;
import com.google.inject.Inject;
import com.google.inject.Provider;

View File

@@ -35,12 +35,12 @@ package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;

View File

@@ -30,14 +30,16 @@
*/
package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -48,7 +50,6 @@ import org.slf4j.LoggerFactory;
import sonia.scm.HandlerEvent;
import sonia.scm.event.ScmEventBus;
import sonia.scm.event.Subscriber;
import sonia.scm.group.GroupEvent;
import sonia.scm.store.ConfigurationEntryStore;
import sonia.scm.store.ConfigurationEntryStoreFactory;
@@ -79,7 +80,6 @@ import javax.xml.bind.annotation.XmlRootElement;
* @since 1.31
*/
@Singleton
@Subscriber(async = true)
public class DefaultSecuritySystem implements SecuritySystem
{
@@ -445,7 +445,7 @@ public class DefaultSecuritySystem implements SecuritySystem
return classLoader;
}
//~--- inner classes --------------------------------------------------------
/**