Commit 635777c8 authored by Ate Douma's avatar Ate Douma

TRIVIAL cleanup master

parent 2fed27ed
* text=auto !eol
/LICENSE -text
/NOTICE -text
/*.iml
/.classpath
/.idea
/.project
/.settings
/target
This diff is collapsed.
Hippo CMS7 Services - eventbus
Copyright 2012-2018 Hippo B.V. (http://www.onehippo.com)
This product includes software developed by:
Hippo B.V., Amsterdam, The Netherlands (http://www.onehippo.com/);
The Apache Software Foundation (http://www.apache.org/).
NOTICE: Only our own original work is licensed under the terms of the
Apache License Version 2.0. The licenses of some libraries might impose
different redistribution or general licensing terms than those stated in the
Apache License. Users and redistributors are hereby requested to verify these
conditions and agree upon them.
# This is not the branch you're looking for...
BloomReach only provides the git trees for the release tags of Hippo CMS, as explained on https://www.onehippo.org/about/open-source-release-policy.html
To checkout the code for a specific release tag, after cloning this repository, use the following:
## to show the available tags
git tag
## to checkout a specific tag
git checkout <tag name>
## to modify a project
If you want to make modifications to a project, for example to create a patch, create a new fork branch from the specific tag like this:
git checkout -b forked-<tag name> <tag name>
For the latter, also see the **Build from Source** documentation at https://www.onehippo.org/library/development/build-hippo-cms-from-scratch.html
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2018 Hippo B.V. (http://www.onehippo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms7-project</artifactId>
<version>29.4-SNAPSHOT</version>
</parent>
<name>Hippo CMS7 Services - eventbus</name>
<artifactId>hippo-services-eventbus</artifactId>
<version>4.3.0-SNAPSHOT</version>
<inceptionYear>2012</inceptionYear>
<properties>
<!-- use root project name for all project modules NOTICE files, should be the same as in the root NOTICE file -->
<notice.project.name>Hippo CMS7 Services - eventbus</notice.project.name>
<hippo.services.version>4.3.0-SNAPSHOT</hippo.services.version>
<hippo.commons.version>4.3.0-SNAPSHOT</hippo.commons.version>
<asm.version>5.0.3</asm.version>
<junit.version>4.10</junit.version>
</properties>
<scm>
<connection>scm:git:https://code.onehippo.org/cms-community/hippo-services-eventbus.git</connection>
<developerConnection>scm:git:git@code.onehippo.org:cms-community/hippo-services-eventbus.git</developerConnection>
<url>https://code.onehippo.org/cms-community/hippo-services-eventbus</url>
</scm>
<repositories>
<repository>
<id>hippo</id>
<name>Hippo Maven 2</name>
<url>https://maven.onehippo.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms7-commons</artifactId>
<version>${hippo.commons.version}</version>
</dependency>
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-services</artifactId>
<version>${hippo.services.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>pedantic</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<excludes combine.children="append">
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.eventbus;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Base class for dynamically generated proxy classes which are used as wrapper for Guava EventBus registration through
* the {@link GuavaHippoEventBus}.
* <p>
* The creation and caching of both generated proxy classes and instances is managed through the
* {@link org.onehippo.cms7.services.eventbus.GuavaEventBusListenerProxyFactory}.
* </p>
* @see GuavaHippoEventBus
* @see GuavaEventBusListenerProxyFactory
*/
public abstract class GuavaEventBusListenerProxy implements Cloneable {
private volatile Object listener;
private ClassLoader cl;
private Method[] methods;
protected GuavaEventBusListenerProxy(Object listener, ClassLoader cl, Method[] methods) {
this.listener = listener;
this.cl = cl;
this.methods = methods;
}
protected void handleEvent(int methodIndex, Object event) throws InvocationTargetException {
ClassLoader lcl = cl;
Method m = methods[methodIndex];
Object ll = listener;
// protect against concurrent called destroy
if (listener != null) {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(lcl);
m.invoke(ll, event);
} catch (IllegalArgumentException e) {
throw new Error("Method rejected target/argument: " + event, e);
} catch (IllegalAccessException e) {
throw new Error("Method became inaccessible: " + event, e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Error) {
throw (Error) e.getCause();
}
throw e;
}
finally {
Thread.currentThread().setContextClassLoader(ccl);
}
}
}
GuavaEventBusListenerProxy clone(Object listener) {
try {
GuavaEventBusListenerProxy clone = (GuavaEventBusListenerProxy)super.clone();
clone.listener = listener;
return clone;
} catch (CloneNotSupportedException e) {
// will never happen
}
return null;
}
/**
* Clears the proxy wrapped listener, its classloader and its wrapped methods.
* The proxy can no longer be used after this, other than to unregister from the Guava EventBus!
*/
void destroy() {
listener = null;
cl = null;
for (int i = 0; i < methods.length; i++) {
methods[i] = null;
}
}
}
/*
* Copyright 2012-2013 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.eventbus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.eventbus.AsyncEventBus;
import org.onehippo.cms7.event.HippoEvent;
import org.onehippo.cms7.services.HippoServiceRegistration;
import org.onehippo.cms7.services.HippoServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GuavaHippoEventBus implements HippoEventBus {
private static final Logger log = LoggerFactory.getLogger(GuavaHippoEventBus.class);
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final AsyncEventBus eventBus = new AsyncEventBus(executor);
private final List<HippoServiceRegistration> listeners = Collections.synchronizedList(new ArrayList<HippoServiceRegistration>());
private final GuavaEventBusListenerProxyFactory proxyFactory = new GuavaEventBusListenerProxyFactory();
private volatile int version = -1;
public void destroy() {
for (GuavaEventBusListenerProxy proxy : proxyFactory.clear()) {
eventBus.unregister(proxy);
}
listeners.clear();
executor.shutdown();
}
@Override
public void register(final Object listener) {
registerProxy(listener);
log.warn("HippoEventBus method #register is deprecated, use whiteboard pattern instead");
}
@Override
public void unregister(final Object listener) {
unregisterProxy(listener);
}
public void post(final Object event) {
if (version != HippoServiceRegistry.getVersion()) {
updateListeners();
}
if (event instanceof HippoEvent) {
((HippoEvent) event).sealEvent();
}
eventBus.post(event);
}
protected void unregisterProxy(final Object listener) {
GuavaEventBusListenerProxy proxy = proxyFactory.removeProxy(listener);
if (proxy != null) {
eventBus.unregister(proxy);
}
}
protected void registerProxy(final Object listener) {
GuavaEventBusListenerProxy proxy = proxyFactory.createProxy(listener);
if (proxy != null) {
eventBus.register(proxy);
}
}
private void updateListeners() {
List<HippoServiceRegistration> registered = HippoServiceRegistry.getRegistrations(HippoEventBus.class);
for (HippoServiceRegistration registration : registered) {
if (!listeners.contains(registration)) {
listeners.add(registration);
registerProxy(registration);
}
}
Iterator<HippoServiceRegistration> iterator = listeners.iterator();
while (iterator.hasNext()) {
HippoServiceRegistration registration = iterator.next();
if (!registered.contains(registration)) {
iterator.remove();
unregisterProxy(registration);
}
}
version = HippoServiceRegistry.getVersion();
}
}
/*
* Copyright 2012-2013 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.eventbus;
import org.junit.Test;
import org.onehippo.cms7.services.HippoServiceRegistry;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class GuavaHippoEventBusTest {
public class Listener {
boolean fired = false;
@Subscribe
public void eventFired(Object payload) {
fired = true;
synchronized (GuavaHippoEventBusTest.this) {
GuavaHippoEventBusTest.this.notify();
}
}
}
@Test
public void testEventBusWithDirectListener() throws InterruptedException {
GuavaHippoEventBus eventBus = new GuavaHippoEventBus();
Listener listener = new Listener();
eventBus.register(listener);
eventBus.post(new Object());
synchronized (this) {
wait(500);
}
assertTrue(listener.fired);
}
@Test
public void testEventBusWithWhiteboardListener() throws InterruptedException {
HippoEventBus eventBus = new GuavaHippoEventBus();
Listener listener = new Listener();
HippoServiceRegistry.registerService(listener, HippoEventBus.class);
eventBus.post(new Object());
synchronized (this) {
wait(500);
}
assertTrue(listener.fired);
listener.fired = false;
HippoServiceRegistry.unregisterService(listener, HippoEventBus.class);
eventBus.post(new Object());
synchronized (this) {
wait(500);
}
assertFalse(listener.fired);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment