1 28 29 package com.caucho.config.types; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.naming.Jndi; 33 import com.caucho.util.L10N; 34 35 import javax.annotation.PostConstruct; 36 import javax.naming.Reference ; 37 import javax.naming.StringRefAddr ; 38 import javax.naming.spi.ObjectFactory ; 39 import java.util.ArrayList ; 40 import java.util.Collections ; 41 import java.util.HashMap ; 42 43 46 public class ReferenceConfig { 47 private static L10N L = new L10N(ReferenceConfig.class); 48 49 private String _name; 50 private Class _factory; 51 private String _description; 52 53 private InitProgram _init; 54 private HashMap <String ,String > _params; 55 56 private ObjectFactory _objectFactory; 57 58 61 public void setJndiName(String name) 62 { 63 _name = name; 64 } 65 66 69 public String getJndiName() 70 { 71 return _name; 72 } 73 74 77 public Class getFactory() 78 { 79 return _factory; 80 } 81 82 85 public void setFactory(Class factory) 86 { 87 _factory = factory; 88 } 89 90 93 public void setInit(InitProgram init) 94 { 95 _init = init; 96 } 97 98 101 public InitProgram getInit() 102 { 103 return _init; 104 } 105 106 109 public void addInitParam(InitParam initParam) 110 { 111 if (_params == null) 112 _params = new HashMap <String ,String >(); 113 114 _params.putAll(initParam.getParameters()); 115 } 116 117 120 @PostConstruct 121 public void init() 122 throws Exception 123 { 124 Object obj = null; 125 126 if (_factory == null) { 127 throw new ConfigException(L.l("<reference> configuration need a <factory>. The <factory> is the class name of the resource bean.")); 128 } 129 else if (ObjectFactory .class.isAssignableFrom(_factory)) { 130 Reference ref; 131 132 if (_init != null) { 133 throw new ConfigException(L.l("<init> is not allowed for object factories. A <resource> with a <factory> must only have <init-param> configuration.")); 134 } 135 136 String factoryName = _factory.getName(); 137 ref = new Reference (factoryName, factoryName, null); 138 139 if (_params != null) { 140 ArrayList <String > names = new ArrayList <String >(_params.keySet()); 141 Collections.sort(names); 142 143 for (int i = 0; i < names.size(); i++) { 144 String name = names.get(i); 145 String value = _params.get(name); 146 147 ref.add(new StringRefAddr (name, value)); 148 } 149 } 150 151 obj = ref; 152 } 153 else { 154 throw new ConfigException(L.l("`{0}' must implement ObjectFactory. <factory> classes in <resource> must implement ObjectFactory.", _factory.getName())); 155 } 156 157 if (_name.startsWith("java:comp")) 158 Jndi.bindDeep(_name, obj); 159 else 160 Jndi.bindDeep("java:comp/env/" + _name, obj); 161 } 162 163 protected void configure(Object obj) 164 throws Throwable 165 { 166 if (_init != null) 167 _init.init(obj); 168 } 169 170 public String toString() 171 { 172 return "Resource[" + _name + "]"; 173 } 174 } 175 176 | Popular Tags |