1 28 29 package com.caucho.config.types; 30 31 import com.caucho.config.BeanBuilderException; 32 import com.caucho.loader.ClassLoaderListener; 33 import com.caucho.loader.Environment; 34 import com.caucho.loader.EnvironmentListener; 35 import com.caucho.naming.Jndi; 36 import com.caucho.naming.LinkProxy; 37 import com.caucho.util.L10N; 38 39 import javax.annotation.PostConstruct; 40 import javax.naming.spi.InitialContextFactory ; 41 import java.util.Hashtable ; 42 43 46 public class JndiLink { 47 private static L10N L = new L10N(JndiLink.class); 48 49 private String _name; 50 private Class _factoryClass; 51 private String _description; 52 53 private Hashtable _properties = new Hashtable (); 54 55 private InitProgram _init; 56 57 60 public void setName(String name) 61 { 62 _name = name; 63 } 64 65 68 public String getName() 69 { 70 return _name; 71 } 72 73 76 public void setFactory(Class factory) 77 { 78 _factoryClass = factory; 79 } 80 81 84 public Class getFactory() 85 { 86 return _factoryClass; 87 } 88 89 92 public void setInit(InitProgram init) 93 { 94 _init = init; 95 } 96 97 100 public InitProgram getInit() 101 { 102 return _init; 103 } 104 105 108 @PostConstruct 109 public void init() 110 throws Exception 111 { 112 if (_name == null) 113 throw new BeanBuilderException(L.l("<jndi-link> configuration needs a <name>. The <name> is the JNDI name where the context will be linked.")); 114 115 Class factory = _factoryClass; 116 117 if (factory == null) 118 throw new BeanBuilderException(L.l("<jndi-link> configuration need a <factory>. The <factory> is the class name of the InitialContextFactory bean.")); 119 120 Object obj = factory.newInstance(); 121 122 127 128 if (obj instanceof ClassLoaderListener) { 129 ClassLoaderListener listener = (ClassLoaderListener) obj; 130 131 Environment.addClassLoaderListener(listener); 132 } 133 134 if (obj instanceof EnvironmentListener) { 135 EnvironmentListener listener = (EnvironmentListener) obj; 136 137 Environment.addEnvironmentListener(listener); 138 } 139 140 Object proxy = new LinkProxy((InitialContextFactory ) obj, 141 _properties, 142 null); 143 144 if (_name.startsWith("java:comp")) 145 Jndi.bindDeep(_name, proxy); 146 else { 147 Jndi.bindDeep("java:comp/env/" + _name, proxy); 148 } 149 } 150 151 protected void configure(Object obj) 152 throws Throwable 153 { 154 if (_init != null) 155 _init.init(obj); 156 } 157 158 public String toString() 159 { 160 return "JndiLink[" + _name + "]"; 161 } 162 } 163 164 | Popular Tags |