Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #28 from metacosm/master
Browse files Browse the repository at this point in the history
GTNWSRP-289
  • Loading branch information
metacosm committed Sep 29, 2012
2 parents 5b31cf3 + 6f57bc7 commit 7ffdc17
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 109 deletions.
13 changes: 0 additions & 13 deletions admin-gui/src/main/java/org/gatein/wsrp/admin/ui/ProducerBean.java
Expand Up @@ -442,19 +442,6 @@ public boolean isDefaultRegistrationPolicy()
}

public String getValidatorClassName()
{
final String validatorClassName = getValidatorClassNameOrNullIfNotDefault();
if (validatorClassName != null)
{
return validatorClassName;
}
else
{
throw new IllegalStateException("getValidatorClassName shouldn't be called if we're not using the default registration");
}
}

private String getValidatorClassNameOrNullIfNotDefault()
{
if (isDefaultRegistrationPolicy())
{
Expand Down
Expand Up @@ -24,6 +24,8 @@

import org.gatein.pc.api.invocation.PortletInvocation;
import org.gatein.pc.api.invocation.response.PortletInvocationResponse;
import org.gatein.wsrp.api.plugins.Plugins;
import org.gatein.wsrp.api.plugins.PluginsAccess;

/**
* A delegate that can be used to intercept PortletInvocations and PortletInvocationReponses before they are processed
Expand Down Expand Up @@ -55,37 +57,39 @@ public abstract class InvocationHandlerDelegate

static
{
consumerDelegate = createDelegate(System.getProperty(CONSUMER_DELEGATE_CLASSNAME));
producerDelegate = createDelegate(System.getProperty(PRODUCER_DELEGATE_CLASSNAME));
final Plugins plugins = PluginsAccess.getPlugins();
consumerDelegate = createInstance(CONSUMER_DELEGATE_CLASSNAME, plugins);
producerDelegate = createInstance(PRODUCER_DELEGATE_CLASSNAME, plugins);
}

private static InvocationHandlerDelegate createDelegate(String delegateClassName)
private static InvocationHandlerDelegate createInstance(String propertyName, Plugins plugins)
{
String delegateClassName = System.getProperty(propertyName);
if (delegateClassName != null && !delegateClassName.isEmpty())
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try
{
Class delegateClass = loader.loadClass(delegateClassName);
if (!InvocationHandlerDelegate.class.isAssignableFrom(delegateClass))
{
throw new IllegalArgumentException("Invocation handler delegate class " + delegateClassName + "does not extends " + InvocationHandlerDelegate.class.getName());
}
return (InvocationHandlerDelegate)delegateClass.newInstance();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return plugins.createPluginInstance(delegateClassName, InvocationHandlerDelegate.class);
}
else
{
return null;
}
return null;
}

/**
* Only public for testing purposes
*
* @param delegate
*/
public synchronized static void registerConsumerDelegate(InvocationHandlerDelegate delegate)
{
consumerDelegate = delegate;
}

/**
* Only public for testing purposes
*
* @param delegate
*/
public synchronized static void registerProducerDelegate(InvocationHandlerDelegate delegate)
{
producerDelegate = delegate;
Expand Down
92 changes: 92 additions & 0 deletions api/src/main/java/org/gatein/wsrp/api/plugins/AbstractPlugins.java
@@ -0,0 +1,92 @@
/*
* JBoss, a division of Red Hat
* Copyright 2012, Red Hat Middleware, LLC, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.gatein.wsrp.api.plugins;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a> */
public abstract class AbstractPlugins implements Plugins
{
private static final List<String> KNOWN_PLUGIN_INTERFACES = new ArrayList<String>(3);
static
{
KNOWN_PLUGIN_INTERFACES.add("org.gatein.registration.RegistrationPolicy");
KNOWN_PLUGIN_INTERFACES.add("org.gatein.registration.policies.RegistrationPropertyValidator");
KNOWN_PLUGIN_INTERFACES.add("org.gatein.wsrp.api.extensions.InvocationHandlerDelegate");
}

@Override
public List<String> getKnownPluginInterfaceNames()
{
return Collections.unmodifiableList(KNOWN_PLUGIN_INTERFACES);
}

@Override
public List<String> getPluginImplementationNames(Class pluginClass, String defaultImplementationClassName)
{
try
{
// find all available implementations, including at least the default one
final List<String> implementations = getImplementationNamesFor(pluginClass.getCanonicalName(), defaultImplementationClassName);
// sort alphabetically
Collections.sort(implementations);

return implementations;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

protected abstract List<String> getImplementationNamesFor(String pluginClassName, String defaultImplementationClassName);

@Override
public <T> T createPluginInstance(String className, Class<T> pluginClass)
{
try
{
final Class<? extends T> clazz = getImplementationNamed(className, pluginClass);
if (!pluginClass.isAssignableFrom(clazz))
{
throw new IllegalArgumentException("Class does not implement" + pluginClass.getCanonicalName());
}
else
{
return pluginClass.cast(clazz.newInstance());
}
}
catch (ClassNotFoundException e)
{
throw new IllegalArgumentException("Couldn't find class " + className, e);
}
catch (Exception e)
{
throw new IllegalArgumentException("Couldn't instantiate class " + className, e);
}
}

protected abstract <T> Class<? extends T> getImplementationNamed(String className, Class<T> pluginClass) throws ClassNotFoundException;
}
37 changes: 37 additions & 0 deletions api/src/main/java/org/gatein/wsrp/api/plugins/Plugins.java
@@ -0,0 +1,37 @@
/*
* JBoss, a division of Red Hat
* Copyright 2012, Red Hat Middleware, LLC, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.gatein.wsrp.api.plugins;

import java.util.List;

/** @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a> */
public interface Plugins
{
String WSRP_PLUGIN_EXTENSION_SUFFIX = ".wsrp.jar";

List<String> getKnownPluginInterfaceNames();

List<String> getPluginImplementationNames(Class pluginClass, String defaultImplementationClassName);

<T> T createPluginInstance(String className, Class<T> pluginClass);
}
50 changes: 50 additions & 0 deletions api/src/main/java/org/gatein/wsrp/api/plugins/PluginsAccess.java
@@ -0,0 +1,50 @@
/*
* JBoss, a division of Red Hat
* Copyright 2012, Red Hat Middleware, LLC, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.gatein.wsrp.api.plugins;

/** @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a> */
public class PluginsAccess
{
private static Plugins plugins;

private PluginsAccess()
{
}

public synchronized static Plugins getPlugins()
{
return plugins;
}

public synchronized static void register(Plugins plugins)
{
if (PluginsAccess.plugins == null)
{
PluginsAccess.plugins = plugins;
}
else
{
throw new IllegalStateException("A Plugins implementation (" + plugins.getClass().getCanonicalName() + ") has already been provided");
}
}
}
25 changes: 21 additions & 4 deletions common/src/main/java/org/gatein/wsrp/ResourceFinder.java
Expand Up @@ -212,7 +212,7 @@ public List<String> findAllStrings(String uri) throws IOException
{
URL url = resources.nextElement();
String string = readContents(url);
strings.add(string);
Collections.addAll(strings, string.split("\\s"));
}
return strings;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public List<String> findAvailableStrings(String uri) throws IOException
try
{
String string = readContents(url);
strings.add(string);
Collections.addAll(strings, string.split("\\s"));
}
catch (IOException notAvailable)
{
Expand Down Expand Up @@ -327,7 +327,11 @@ public Map<String, String> mapAvailableStrings(String uri) throws IOException
try
{
String value = readContents(url);
strings.put(name, value);
final String[] split = value.split("\\s");
for (String s : split)
{
strings.put(name, s);
}
}
catch (IOException notAvailable)
{
Expand Down Expand Up @@ -358,6 +362,20 @@ public Class<?> findClass(String uri) throws IOException, ClassNotFoundException
return classLoader.loadClass(className);
}

public <T> Class<? extends T> findClass(String className, Class<T> pluginClass) throws IOException, ClassNotFoundException
{
List<String> strings = findAllStrings(pluginClass.getCanonicalName());
for (String name : strings)
{
if(className.equals(name))
{
return classLoader.loadClass(className).asSubclass(pluginClass);
}
}

throw new ClassNotFoundException("Couldn't find an implementation of " + pluginClass.getCanonicalName() + " named " + className);
}

/**
* Executes findAllStrings assuming the strings are
* the names of a classes that should be loaded and returned.
Expand Down Expand Up @@ -1297,5 +1315,4 @@ public static String decode(String fileName)
}
return result.toString();
}

}
5 changes: 3 additions & 2 deletions common/src/main/java/org/gatein/wsrp/WSRPConstants.java
Expand Up @@ -68,10 +68,11 @@ public final class WSRPConstants
}
WSRP_SERVICE_VERSION = props.getProperty("wsrp.service.version");

final String serverPath = System.getProperty("jboss.server.base.dir");
// we should really get the path from a system property instead of hardcoding it
final String serverPath = System.getProperty("jboss.home.dir");
if (serverPath != null)
{
SERVICES_DIRECTORY_URL = serverPath + "/deployments";
SERVICES_DIRECTORY_URL = serverPath + "/gatein/deployments";
}
else
{
Expand Down
22 changes: 22 additions & 0 deletions examples/invocation-handler-delegate/pom.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>wsrp-examples</artifactId>
<groupId>org.gatein.wsrp</groupId>
<version>2.2.0.Beta06-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<name>GateIn WSRP Examples: InvocationHandlerDelegate Implementation</name>
<artifactId>wsrp-invocation-handler-delegate-example</artifactId>
<dependencies>
<dependency>
<groupId>org.gatein.wsrp</groupId>
<artifactId>wsrp-integration-api</artifactId>
</dependency>
</dependencies>


</project>

0 comments on commit 7ffdc17

Please sign in to comment.