Skip to content

Commit

Permalink
Support JNDI component resources (EAR / EJB integration)
Browse files Browse the repository at this point in the history
  • Loading branch information
asoldano committed Feb 1, 2016
1 parent f767953 commit 3046ffc
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 11 deletions.
@@ -0,0 +1,105 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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.jboss.wsf.stack.cxf.deployment;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.message.Message;


/**
*
*
* @author alessio.soldano@jboss.com
* @since 01-Feb-2016
*
*/
public class JNDIComponentResourceProvider implements ResourceProvider
{
private String jndiName;
private InitialContext ctx;
private volatile Object reference;
private Class<?> scannable;
private boolean cache;

public JNDIComponentResourceProvider(String jndiName, Class<?> scannable, boolean cacheReference)
{
this.jndiName = jndiName;
this.scannable = scannable;
this.cache = cacheReference;
try
{
ctx = new InitialContext();
}
catch (NamingException e)
{
throw new RuntimeException(e);
}
}

@Override
public Object getInstance(Message m)
{
if (reference != null) return reference;
Object ref = reference;
if (ref == null)
{
try
{
ref = ctx.lookup(jndiName);
}
catch (NamingException e)
{
throw new RuntimeException(e);
}
if (cache)
{
synchronized (this)
{
reference = ref;
}
}
}
return ref;
}

@Override
public void releaseInstance(Message m, Object o)
{
//NOOP
}

@Override
public Class<?> getResourceClass()
{
return scannable;
}

@Override
public boolean isSingleton()
{
return false;
}

}
Expand Up @@ -47,6 +47,7 @@
import org.jboss.wsf.spi.deployment.Deployment;
import org.jboss.wsf.spi.metadata.JAXRSDeploymentMetadata;
import org.jboss.wsf.stack.cxf.client.configuration.JBossWSBusFactory;
import org.jboss.wsf.stack.cxf.deployment.JNDIComponentResourceProvider;


/**
Expand Down Expand Up @@ -120,13 +121,16 @@ private static void createFromApplication(JAXRSDeploymentMetadata md, Class<?> a
JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, md.isIgnoreApplicationPath(), false);
bean.setBus(bus);
bean.setApplicationInfo(providerApp);
List<Class<?>> additionalResources = new ArrayList<>();
if (app.getClasses().isEmpty() && app.getSingletons().isEmpty()) {
setResources(bean, md, bus, classLoader);
processResources(bean, md, bus, classLoader, additionalResources);
setProviders(bean, md, bus, classLoader);
}
processJNDIComponentResources(bean, md, bus, classLoader, additionalResources);
setJSONProviders(bean);
if (!bean.getResourceClasses().isEmpty()) {
bean.create();
if (!bean.getResourceClasses().isEmpty() || !additionalResources.isEmpty()) {
bean.setResourceClasses(additionalResources);
bean.create();
}
}

Expand All @@ -135,30 +139,31 @@ private static void create(JAXRSDeploymentMetadata md, Bus bus, ClassLoader clas
bean.setBus(bus);
bean.setAddress("/"); //TODO!!!
//resources...
setResources(bean, md, bus, classLoader);
List<Class<?>> resources = new ArrayList<>();
processResources(bean, md, bus, classLoader, resources);
//resource providers (CXF)... ?

//jndi resource... ?

//providers...
setProviders(bean, md, bus, classLoader);
//jndi resource...
processJNDIComponentResources(bean, md, bus, classLoader, resources);

setJSONProviders(bean);
if (!bean.getResourceClasses().isEmpty()) {
bean.create();
if (!bean.getResourceClasses().isEmpty() || !resources.isEmpty()) {
bean.setResourceClasses(resources);
bean.create();
}
}

private static void setResources(JAXRSServerFactoryBean bean, JAXRSDeploymentMetadata md, Bus bus, ClassLoader classLoader) {
private static void processResources(JAXRSServerFactoryBean bean, JAXRSDeploymentMetadata md, Bus bus, ClassLoader classLoader, List<Class<?>> resources) {
if (!md.getScannedResourceClasses().isEmpty()) {
List<Class<?>> resources = new ArrayList<>();
try {
for (String cl : md.getScannedResourceClasses()) {
resources.add(classLoader.loadClass(cl));
}
} catch (ClassNotFoundException cnfe) {
throw new WSFException(cnfe);
}
bean.setResourceClasses(resources);
}
}

Expand All @@ -177,6 +182,26 @@ private static void setProviders(JAXRSServerFactoryBean bean, JAXRSDeploymentMet
}
}

private static void processJNDIComponentResources(JAXRSServerFactoryBean bean, JAXRSDeploymentMetadata md, Bus bus, ClassLoader classLoader, List<Class<?>> resources) {
if (!md.getScannedJndiComponentResources().isEmpty()) {
try {
for (String cl : md.getScannedJndiComponentResources()) {
String[] config = cl.trim().split(";");
if (config.length < 3) {
throw new RuntimeException("Messages.MESSAGES.jndiComponentResourceNotSetCorrectly()");
}
String jndiName = config[0];
Class<?> clazz = classLoader.loadClass(config[1]);
boolean cacheRefrence = Boolean.valueOf(config[2].trim());
resources.add(clazz);
bean.setResourceProvider(clazz, new JNDIComponentResourceProvider(jndiName, clazz, cacheRefrence));
}
} catch (ClassNotFoundException cnfe) {
throw new WSFException(cnfe);
}
}
}

private static void setJSONProviders(JAXRSServerFactoryBean bean) {
//Add default Jettison provider
@SuppressWarnings("rawtypes")
Expand Down

0 comments on commit 3046ffc

Please sign in to comment.