001 package biz.hammurapi.jms.adapter; 002 003 import java.util.Hashtable; 004 005 import javax.jms.Destination; 006 import javax.naming.InitialContext; 007 import javax.naming.NamingException; 008 009 import biz.hammurapi.config.ConfigurationException; 010 import biz.hammurapi.config.GenericContainer; 011 import biz.hammurapi.jms.adapter.definition.Context; 012 013 public class JndiContext extends GenericContainer implements DestinationResolver { 014 015 private Context definition; 016 017 public JndiContext(JmsAdapter jmsAdapter, biz.hammurapi.jms.adapter.definition.Context definition) throws ConfigurationException { 018 this.definition = definition; 019 biz.hammurapi.jms.adapter.definition.JndiConnection[] ca = definition.getConnectionArray(); 020 for (int i=0; i<ca.length; ++i) { 021 addComponent(ca[i].getName(), new JndiConnection(jmsAdapter, ca[i])); 022 } 023 } 024 025 private javax.naming.Context jndiContext; 026 027 public void start() throws ConfigurationException { 028 try { 029 Hashtable properties = JmsAdapter.instantiate(definition.getPropertyArray()); 030 // TODO Prompt for credentials. 031 jndiContext = new InitialContext(properties); 032 if (!JmsAdapter.isBlank(definition.getRoot())) { 033 jndiContext = (javax.naming.Context) jndiContext.lookup(definition.getRoot()); 034 } 035 } catch (NamingException e) { 036 throw new ConfigurationException(e); 037 } 038 super.start(); 039 } 040 041 public Destination lookupDestination(String name) throws ConfigurationException { 042 try { 043 return (Destination) jndiContext.lookup(name); 044 } catch (NamingException e) { 045 throw new ConfigurationException("Destination lookup failed: "+e, e); 046 } 047 } 048 049 public Object lookup(String name) throws ConfigurationException { 050 try { 051 return jndiContext.lookup(name); 052 } catch (NamingException e) { 053 throw new ConfigurationException("Lookup failed: "+e, e); 054 } 055 } 056 057 /** 058 * Bridges to underlying JNDI if super.get() returns null. 059 */ 060 public Object get(String name) { 061 Object ret = super.get(name); 062 063 if (ret!=null) { 064 return ret; 065 } 066 067 try { 068 return jndiContext.lookup(name); 069 } catch (NamingException e) { 070 return null; 071 } 072 } 073 074 }