use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class NamingBindingAdd method doRebind.
void doRebind(OperationContext context, ModelNode model, BinderService service) throws OperationFailedException {
ManagedReferenceFactory ref = service.getManagedObjectInjector().getValue();
if (ref instanceof MutableManagedReferenceFactory) {
MutableManagedReferenceFactory factory = (MutableManagedReferenceFactory) ref;
final BindingType type = BindingType.forName(NamingBindingResourceDefinition.BINDING_TYPE.resolveModelAttribute(context, model).asString());
if (type == BindingType.SIMPLE) {
Object bindValue = createSimpleBinding(context, model);
factory.setFactory(new ValueManagedReferenceFactory(new ImmediateValue<Object>(bindValue)));
service.setSource(bindValue);
} else if (type == BindingType.OBJECT_FACTORY) {
final ObjectFactory objectFactoryClassInstance = createObjectFactory(context, model);
final Hashtable<String, String> environment = getObjectFactoryEnvironment(context, model);
factory.setFactory(new ObjectFactoryManagedReference(objectFactoryClassInstance, service.getName(), environment));
service.setSource(objectFactoryClassInstance);
} else if (type == BindingType.LOOKUP) {
final String lookup = NamingBindingResourceDefinition.LOOKUP.resolveModelAttribute(context, model).asString();
factory.setFactory(new LookupManagedReferenceFactory(lookup));
service.setSource(null);
} else if (type == BindingType.EXTERNAL_CONTEXT) {
throw NamingLogger.ROOT_LOGGER.cannotRebindExternalContext();
} else {
throw NamingLogger.ROOT_LOGGER.unknownBindingType(type.toString());
}
} else {
throw NamingLogger.ROOT_LOGGER.cannotRebindExternalContext();
}
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class NamingBindingAdd method installExternalContext.
void installExternalContext(final OperationContext context, final String name, final ModelNode model) throws OperationFailedException {
final String moduleID = NamingBindingResourceDefinition.MODULE.resolveModelAttribute(context, model).asString();
final String className = NamingBindingResourceDefinition.CLASS.resolveModelAttribute(context, model).asString();
final ModelNode cacheNode = NamingBindingResourceDefinition.CACHE.resolveModelAttribute(context, model);
boolean cache = cacheNode.isDefined() ? cacheNode.asBoolean() : false;
final ObjectFactory objectFactoryClassInstance = new ExternalContextObjectFactory();
final ServiceTarget serviceTarget = context.getServiceTarget();
final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(name);
final Hashtable<String, String> environment = getObjectFactoryEnvironment(context, model);
environment.put(ExternalContextObjectFactory.CACHE_CONTEXT, Boolean.toString(cache));
environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_CLASS, className);
environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_MODULE, moduleID);
final ExternalContextBinderService binderService = new ExternalContextBinderService(name, objectFactoryClassInstance);
binderService.getManagedObjectInjector().inject(new ContextListAndJndiViewManagedReferenceFactory() {
@Override
public ManagedReference getReference() {
try {
final Object value = objectFactoryClassInstance.getObjectInstance(name, null, null, environment);
return new ImmediateManagedReference(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public String getInstanceClassName() {
return className;
}
@Override
public String getJndiViewInstanceValue() {
final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
try {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(objectFactoryClassInstance.getClass().getClassLoader());
return String.valueOf(getReference().getInstance());
} finally {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(cl);
}
}
});
serviceTarget.addService(bindInfo.getBinderServiceName(), binderService).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).addDependency(ExternalContextsService.SERVICE_NAME, ExternalContexts.class, binderService.getExternalContextsInjector()).install();
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class ObjectFactoryBuilder method processURL.
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;
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class NamingContext method getObjectInstance.
private Object getObjectInstance(final Object object, final Name name, final Hashtable<?, ?> environment) throws NamingException {
try {
final ObjectFactoryBuilder factoryBuilder = ObjectFactoryBuilder.INSTANCE;
final ObjectFactory objectFactory = factoryBuilder.createObjectFactory(object, environment);
return objectFactory == null ? null : objectFactory.getObjectInstance(object, name, this, environment);
} catch (NamingException e) {
throw e;
} catch (Throwable t) {
throw NamingLogger.ROOT_LOGGER.cannotDeferenceObject(t);
}
}
use of javax.naming.spi.ObjectFactory in project wildfly by wildfly.
the class ObjectFactoryBuilder method getURLObjectFactory.
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 = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
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;
}
Aggregations