use of javax.naming.Reference in project core by weld.
the class BeanManagerResourceBindingListener method contextInitialized.
public void contextInitialized(ServletContextEvent sce) {
try {
InitialContext ctx = new InitialContext();
boolean present = false;
try {
NamingEnumeration<NameClassPair> entries = ctx.list(RESOURCES_CONTEXT);
while (entries.hasMoreElements()) {
try {
NameClassPair e = entries.next();
if (e.getName().equals(BEAN_MANAGER_JNDI_NAME) && e.getClassName().equals(BeanManager.class)) {
present = true;
break;
}
} catch (Exception e) {
WeldServletLogger.LOG.problemWhenInterating(RESOURCES_CONTEXT, e);
}
}
} catch (NamingException e) {
WeldServletLogger.LOG.couldNotReadContext(RESOURCES_CONTEXT);
try {
Context compCtx = (Context) ctx.lookup("java:comp");
compCtx.createSubcontext("env");
} catch (Exception ex) {
WeldServletLogger.LOG.couldntCreateContext(RESOURCES_CONTEXT);
}
}
if (!present) {
try {
// we rebind just in case it really is there and we just couldn't read it
ctx.rebind(QUALIFIED_BEAN_MANAGER_JNDI_NAME, new Reference(BeanManager.class.getName(), BEAN_MANAGER_OBJECT_FACTORY, null));
bound = true;
WeldServletLogger.LOG.beanManagerReferenceBoundTo(QUALIFIED_BEAN_MANAGER_JNDI_NAME);
} catch (NamingException e) {
throw WeldServletLogger.LOG.couldNotBindBeanManagerReferenceToJNDI(e.getExplanation());
}
}
} catch (NamingException e) {
throw WeldServletLogger.LOG.couldNotCreateInitialContext(e.getExplanation());
}
}
use of javax.naming.Reference in project narayana by jbosstm.
the class JNDIManager method bindJTATransactionManagerImplementation.
/**
* Bind the currently configured transaction manager implementation to the JNDI
* context passed in.
* @param initialContext
* @throws javax.naming.NamingException
*/
public static void bindJTATransactionManagerImplementation(InitialContext initialContext) throws javax.naming.NamingException {
/**
* Look up and instantiate an instance of the configured transaction manager implementation *
*/
String tmImplementation = jtaPropertyManager.getJTAEnvironmentBean().getTransactionManagerClassName();
/**
* Bind the transaction manager to the appropriate JNDI context *
*/
Reference ref = new Reference(tmImplementation, tmImplementation, null);
initialContext.rebind(getTransactionManagerJNDIName(), ref);
}
use of javax.naming.Reference in project nimbus by nimbus-org.
the class WrappedDataSourceService method startService.
/**
* 開始処理を行う。<p>
*
* @exception Exception 開始処理に失敗した場合
*/
public void startService() throws Exception {
if (sourceJNDIName == null) {
throw new IllegalArgumentException("SourceJNDIName is null.");
}
if (wrappedJNDIName == null) {
throw new IllegalArgumentException("WrappedJNDIName is null.");
}
if (jndiRepositoryServiceName != null) {
jndiRepository = (Repository) ServiceManagerFactory.getServiceObject(jndiRepositoryServiceName);
}
if (jndiRepository == null) {
JNDIRepositoryService jndiRepositoryService = new JNDIRepositoryService();
jndiRepositoryService.create();
jndiRepositoryService.start();
jndiRepository = jndiRepositoryService;
}
if (getConnectionWrapperClassName() != null) {
connectionWrapperClass = Class.forName(getConnectionWrapperClassName(), true, NimbusClassLoader.getInstance());
}
if (connectionWrapperProperties != null && connectionWrapperProperties.size() != 0) {
properties = new LinkedHashMap();
final Iterator props = connectionWrapperProperties.keySet().iterator();
while (props.hasNext()) {
final String propName = (String) props.next();
final Object val = connectionWrapperProperties.get(propName);
final Property property = PropertyFactory.createProperty(propName);
properties.put(property, val);
}
}
if (getSourceDataSource() == null) {
throw new IllegalArgumentException("SourceJNDIName can not get from JNDIRepository.");
}
StringRefAddr addr = new StringRefAddr("nimbus/service", getServiceNameObject().toString());
reference = new Reference(getClass().getName(), addr, WrappedDataSourceObjectFactory.class.getName(), null);
if (!jndiRepository.register(wrappedJNDIName, reference)) {
throw new IllegalArgumentException("WrappedJNDIName can not register to JNDIRepository.");
}
}
use of javax.naming.Reference in project mule by mulesoft.
the class DefaultSpringJndiContext method lookup.
public Object lookup(String name) throws NamingException {
if (name.length() == 0) {
return this;
}
Object result = treeBindings.get(name);
if (result == null) {
result = bindings.get(name);
}
if (result == null) {
int pos = name.indexOf(':');
if (pos > 0) {
String scheme = name.substring(0, pos);
Context ctx = NamingManager.getURLContext(scheme, environment);
if (ctx == null) {
throw new NamingException("scheme " + scheme + " not recognized");
}
return ctx.lookup(name);
} else {
// Split out the first name of the path
// and look for it in the bindings map.
CompositeName path = new CompositeName(name);
if (path.size() == 0) {
return this;
} else {
String first = path.get(0);
Object obj = bindings.get(first);
if (obj == null) {
throw new NameNotFoundException(name);
} else if (obj instanceof Context && path.size() > 1) {
Context subContext = (Context) obj;
obj = subContext.lookup(path.getSuffix(1));
}
return obj;
}
}
}
if (result instanceof LinkRef) {
LinkRef ref = (LinkRef) result;
result = lookup(ref.getLinkName());
}
if (result instanceof Reference) {
try {
result = NamingManager.getObjectInstance(result, null, null, this.environment);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
}
}
if (result instanceof DefaultSpringJndiContext) {
String prefix = getNameInNamespace();
if (prefix.length() > 0) {
prefix = prefix + SEPARATOR;
}
result = new DefaultSpringJndiContext((DefaultSpringJndiContext) result, environment, prefix + name);
}
return result;
}
use of javax.naming.Reference in project derby by apache.
the class DataSourceReferenceTest method assertDataSourceReferencePopulated.
/**
* Make sure it is possible to recreate and serialize/deserialize a
* populated data source.
* <p>
* Populated means the various bean properties have non-default
* values set.
*
* @param dsDesc data source descriptor
* @param className data source class name
* @throws Exception on a wide variety of error conditions...
*/
private void assertDataSourceReferencePopulated(DataSourceDescriptor dsDesc, String className) throws Exception {
println("Testing recreated populated data source.");
Class<?> clazz = Class.forName(className);
Object ds = clazz.getConstructor().newInstance();
// Populate the data source.
Iterator propIter = dsDesc.getPropertyIterator();
while (propIter.hasNext()) {
String property = (String) propIter.next();
String value = dsDesc.getPropertyValue(property);
Method getMethod = getGet(property, ds);
Method setMethod = getSet(getMethod, ds);
Class paramType = getMethod.getReturnType();
if (paramType.equals(Integer.TYPE)) {
setMethod.invoke(ds, new Object[] { Integer.valueOf(value) });
} else if (paramType.equals(String.class)) {
setMethod.invoke(ds, new Object[] { value });
} else if (paramType.equals(Boolean.TYPE)) {
setMethod.invoke(ds, new Object[] { Boolean.valueOf(value) });
} else if (paramType.equals(Short.TYPE)) {
setMethod.invoke(ds, new Object[] { Short.valueOf(value) });
} else if (paramType.equals(Long.TYPE)) {
setMethod.invoke(ds, new Object[] { Long.valueOf(value) });
} else {
fail("'" + property + "' not settable - update test!!");
}
}
Referenceable refDs = (Referenceable) ds;
Reference dsAsReference = refDs.getReference();
String factoryClassName = dsAsReference.getFactoryClassName();
clazz = Class.forName(factoryClassName);
ObjectFactory factory = (ObjectFactory) clazz.getConstructor().newInstance();
Object recreatedDs = factory.getObjectInstance(dsAsReference, null, null, null);
// Recreated should not be same instance as original.
assertNotSame(recreatedDs, ds);
compareDataSources(dsDesc, ds, recreatedDs, false);
// Serialize and recreate.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ds);
oos.flush();
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
recreatedDs = ois.readObject();
compareDataSources(dsDesc, ds, recreatedDs, false);
}
Aggregations