Skip to content

Commit

Permalink
AS7-5737 If the factory class in the reference is null search for a U…
Browse files Browse the repository at this point in the history
…RL provider
  • Loading branch information
stuartwdouglas authored and bstansberry committed Mar 14, 2013
1 parent dc36255 commit eca3a00
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
<module name="org.jboss.remote-naming"/>
<module name="org.jboss.remoting3"/>
<module name="org.jboss.threads"/>
<module name="sun.jdk" />
</dependencies>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
<path name="com/sun/jndi/dns"/>
<path name="com/sun/jndi/ldap"/>
<path name="com/sun/jndi/url"/>
<path name="com/sun/jndi/url/corbaname"/>
<path name="com/sun/jndi/url/dns"/>
<path name="com/sun/jndi/url/iiop"/>
<path name="com/sun/jndi/url/iiopname"/>
<path name="com/sun/jndi/url/ldap"/>
<path name="com/sun/jndi/url/ldaps"/>
<path name="com/sun/crypto/provider"/>
<path name="com/sun/org/apache/xml/internal/security/transforms/implementations"/>
<path name="com/sun/security/auth"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Hashtable;
import java.util.StringTokenizer;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import javax.naming.directory.Attributes;
import javax.naming.spi.DirObjectFactory;
import javax.naming.spi.ObjectFactory;
Expand Down Expand Up @@ -59,7 +62,7 @@ private ObjectFactoryBuilder() {
* from the reference. If the parameter is not a reference, or the reference does not create an {@link javax.naming.spi.ObjectFactory}
* it will return {@code this} as the {@link javax.naming.spi.ObjectFactory} to use.
*
* @param obj The object bound in the naming context
* @param obj The object bound in the naming context
* @param environment The environment information
* @return The object factory the object resolves to
* @throws NamingException If any problems occur
Expand All @@ -69,38 +72,38 @@ public ObjectFactory createObjectFactory(final Object obj, Hashtable<?, ?> envir
if (obj instanceof Reference) {
return factoryFromReference((Reference) obj, environment);
}
} catch(Throwable ignored) {
} catch (Throwable ignored) {
}
return this;
}

/**
* Create an object instance.
*
* @param ref Object containing reference information
* @param name The name relative to nameCtx
* @param nameCtx The naming context
* @param ref Object containing reference information
* @param name The name relative to nameCtx
* @param nameCtx The naming context
* @param environment The environment information
* @return The object
* @throws Exception If any error occur
*/
public Object getObjectInstance(final Object ref, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
final ClassLoader classLoader = SecurityActions.getContextClassLoader();
if(classLoader == null) {
if (classLoader == null) {
return ref;
}
final String factoriesProp = (String)environment.get(Context.OBJECT_FACTORIES);
if(factoriesProp != null) {
final String factoriesProp = (String) environment.get(Context.OBJECT_FACTORIES);
if (factoriesProp != null) {
final String[] classes = factoriesProp.split(":");
for(String className : classes) {
for (String className : classes) {
try {
final Class<?> factoryClass = classLoader.loadClass(className);
final ObjectFactory objectFactory = ObjectFactory.class.cast(factoryClass.newInstance());
final Object result = objectFactory.getObjectInstance(ref, name, nameCtx, environment);
if(result != null) {
if (result != null) {
return result;
}
} catch(Throwable ignored) {
} catch (Throwable ignored) {
}
}
}
Expand All @@ -110,43 +113,46 @@ public Object getObjectInstance(final Object ref, final Name name, final Context
/**
* Create an object instance.
*
* @param ref Object containing reference information
* @param name The name relative to nameCtx
* @param nameCtx The naming context
* @param ref Object containing reference information
* @param name The name relative to nameCtx
* @param nameCtx The naming context
* @param environment The environment information
* @param attributes The directory attributes
* @param attributes The directory attributes
* @return The object
* @throws Exception If any error occur
*/
public Object getObjectInstance(final Object ref, final Name name, final Context nameCtx, final Hashtable<?, ?> environment, final Attributes attributes) throws Exception {
final ClassLoader classLoader = SecurityActions.getContextClassLoader();
if(classLoader == null) {
if (classLoader == null) {
return ref;
}
final String factoriesProp = (String)environment.get(Context.OBJECT_FACTORIES);
if(factoriesProp != null) {
final String factoriesProp = (String) environment.get(Context.OBJECT_FACTORIES);
if (factoriesProp != null) {
final String[] classes = factoriesProp.split(":");
for(String className : classes) {
for (String className : classes) {
try {
final Class<?> factoryClass = classLoader.loadClass(className);
final ObjectFactory objectFactory = ObjectFactory.class.cast(factoryClass.newInstance());
final Object result;
if(objectFactory instanceof DirObjectFactory) {
if (objectFactory instanceof DirObjectFactory) {
result = DirObjectFactory.class.cast(objectFactory).getObjectInstance(ref, name, nameCtx, environment, attributes);
} else {
result = objectFactory.getObjectInstance(ref, name, nameCtx, environment);
}
if(result != null) {
if (result != null) {
return result;
}
} catch(Throwable ignored) {
} catch (Throwable ignored) {
}
}
}
return ref;
}

private ObjectFactory factoryFromReference(final Reference reference, final Hashtable<?, ?> environment) throws Exception {
if (reference.getFactoryClassName() == null) {
return lookForURLs(reference, environment);
}
if (reference instanceof ModularReference) {
return factoryFromModularReference(ModularReference.class.cast(reference), environment);
}
Expand All @@ -172,6 +178,99 @@ private ObjectFactory factoryFromReference(final Reference reference, final Clas
}
}

static ObjectFactory lookForURLs(Reference ref, Hashtable environment)
throws NamingException {

for (int i = 0; i < ref.size(); i++) {
RefAddr addr = ref.get(i);
if (addr instanceof StringRefAddr &&
addr.getType().equalsIgnoreCase("URL")) {

String url = (String) addr.getContent();
ObjectFactory answer = processURL(url, environment);
if (answer != null) {
return answer;
}
}
}
return null;
}

private static ObjectFactory processURL(Object refInfo, Hashtable environment) throws NamingException {

if (refInfo instanceof String) {
String url = (String) refInfo;
String scheme = getURLScheme(url);
if (scheme != null) {
ObjectFactory answer = getURLObjectFactory(scheme, url, environment);
if (answer != null) {
return answer;
}
}
}

if (refInfo instanceof String[]) {
String[] urls = (String[]) refInfo;
for (int i = 0; i < urls.length; i++) {
String scheme = getURLScheme(urls[i]);
if (scheme != null) {
ObjectFactory answer = getURLObjectFactory(scheme, urls[i], environment);
if (answer != null) {
return answer;
}
}
}
}
return null;
}

private static ObjectFactory getURLObjectFactory(String scheme, String url, Hashtable environment) throws NamingException {

String facProp = (String) environment.get(Context.URL_PKG_PREFIXES);
if (facProp != null) {
facProp += ":" + "com.sun.jndi.url";
} else {
facProp = "com.sun.jndi.url";
}

ClassLoader loader = SecurityActions.getContextClassLoader();

String suffix = "." + scheme + "." + scheme + "URLContextFactory";

// Not cached; find first factory and cache
StringTokenizer parser = new StringTokenizer(facProp, ":");
String className;
ObjectFactory factory = null;
while (parser.hasMoreTokens()) {
className = parser.nextToken() + suffix;
try {
Class<?> clazz;
if (loader == null) {
clazz = Class.forName(className);
} else {
clazz = Class.forName(className, true, loader);
}
return new ReferenceUrlContextFactoryWrapper((ObjectFactory) clazz.newInstance(), url);
} catch (InstantiationException | IllegalAccessException e) {
NamingException ne = new NamingException(className);
ne.setRootCause(e);
throw ne;
} catch (Exception e) {
}
}

return factory;
}


private static String getURLScheme(String str) {
int colon = str.indexOf(':');
int slash = str.indexOf('/');

if (colon > 0 && (slash == -1 || colon + 1 == slash))
return str.substring(0, colon);
return null;
}

private static ServiceContainer currentServiceContainer() {
return AccessController.doPrivileged(new PrivilegedAction<ServiceContainer>() {
Expand All @@ -181,4 +280,20 @@ public ServiceContainer run() {
}
});
}

private static final class ReferenceUrlContextFactoryWrapper implements ObjectFactory {

private final ObjectFactory factory;
private final String url;

private ReferenceUrlContextFactoryWrapper(final ObjectFactory factory, final String url) {
this.factory = factory;
this.url = url;
}

@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
return factory.getObjectInstance(url, name, nameCtx, environment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.InitialContext;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ContainerResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand Down
Loading

0 comments on commit eca3a00

Please sign in to comment.