1 28 29 package com.caucho.naming; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.Name ; 37 import javax.naming.NameNotFoundException ; 38 import javax.naming.NameParser ; 39 import javax.naming.NamingException ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 46 public class Jndi { 47 private static Logger log = Log.open(Jndi.class); 48 private static L10N L = new L10N(Jndi.class); 49 50 53 public static String getFullName(String shortName) 54 { 55 if (shortName.startsWith("java:comp")) 56 return shortName; 57 else 58 return "java:comp/env/" + shortName; 59 } 60 61 public static void bindDeepShort(String name, Object obj) 62 throws NamingException 63 { 64 bindImpl(new InitialContext (), getFullName(name), obj, name); 65 } 66 67 public static void bindDeepShort(Context context, String name, Object obj) 68 throws NamingException 69 { 70 bindImpl(context, getFullName(name), obj, name); 71 } 72 73 public static void bindDeep(String name, Object obj) 74 throws NamingException 75 { 76 bindImpl(new InitialContext (), name, obj, name); 77 } 78 79 public static void bindDeep(Context context, String name, Object obj) 80 throws NamingException 81 { 82 bindImpl(context, name, obj, name); 83 } 84 85 private static void bindImpl(Context context, String name, 86 Object obj, String fullName) 87 throws NamingException 88 { 89 NameParser parser = context.getNameParser(""); 90 Name parsedName = parser.parse(name); 91 92 if (parsedName.size() == 1) { 93 Object value = null; 94 95 try { 96 value = context.lookup(name); 97 } catch (NameNotFoundException e) { 98 } 99 100 context.rebind(name, obj); 101 102 if (value != null && value != obj) 104 log.warning(L.l("'{0}' overrides a previous JNDI resource. The old resource is '{1}' and the new one is '{2}'", 105 fullName, value, obj)); 106 107 return; 108 } 109 110 Object sub = null; 111 112 try { 113 sub = context.lookup(parsedName.get(0)); 114 } catch (NameNotFoundException e) { 115 } 116 117 if (sub == null) 118 sub = context.createSubcontext(parsedName.get(0)); 119 120 if (sub instanceof Context ) 121 bindImpl((Context ) sub, parsedName.getSuffix(1).toString(), obj, fullName); 122 123 else 124 throw new NamingException (L.l("`{0}' is an invalid JNDI name because `{1} is not a Context. One of the subcontexts is not a Context as expected.", 125 fullName, sub)); 126 } 127 128 133 public static void rebindDeepShort(String name, Object obj) 134 throws NamingException 135 { 136 rebindImpl(new InitialContext (), getFullName(name), obj, name); 137 } 138 139 144 public static void rebindDeepShort(Context context, String name, Object obj) 145 throws NamingException 146 { 147 rebindImpl(context, getFullName(name), obj, name); 148 } 149 150 154 public static void rebindDeep(String name, Object obj) 155 throws NamingException 156 { 157 rebindImpl(new InitialContext (), name, obj, name); 158 } 159 160 164 public static void rebindDeep(Context context, String name, Object obj) 165 throws NamingException 166 { 167 rebindImpl(context, name, obj, name); 168 } 169 170 174 private static void rebindImpl(Context context, String name, 175 Object obj, String fullName) 176 throws NamingException 177 { 178 NameParser parser = context.getNameParser(""); 179 Name parsedName = parser.parse(name); 180 181 if (parsedName.size() == 1) { 182 context.rebind(name, obj); 183 return; 184 } 185 186 Object sub = null; 187 188 try { 189 sub = context.lookup(parsedName.get(0)); 190 } catch (NameNotFoundException e) { 191 log.log(Level.FINEST, e.toString(), e); 192 } 193 194 if (sub == null) 195 sub = context.createSubcontext(parsedName.get(0)); 196 197 if (sub instanceof Context ) 198 rebindImpl((Context ) sub, parsedName.getSuffix(1).toString(), obj, 199 fullName); 200 201 else 202 throw new NamingException (L.l("`{0}' is an invalid JNDI name because `{1} is not a Context. One of the subcontexts is not a Context as expected.", 203 fullName, sub)); 204 } 205 206 public static Object lookup(String name) 208 { 209 try { 210 Object value = new InitialContext ().lookup(name); 211 212 if (value != null) 213 return value; 214 } catch (NamingException e) { 215 if (log.isLoggable(Level.FINEST)) 216 log.log(Level.FINEST, e.toString(), e); 217 } 218 219 if (! name.startsWith("java:comp/env")) { 220 try { 221 Object value = new InitialContext ().lookup("java:comp/env/" + name); 222 223 if (value != null) 224 return value; 225 } catch (NamingException e) { 226 if (log.isLoggable(Level.FINEST)) 227 log.log(Level.FINEST, e.toString(), e); 228 } 229 } 230 231 return null; 232 } 233 234 private Jndi() {} 235 } 236 237 | Popular Tags |