1 22 package org.jboss.naming; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 28 34 public class JNDIBindingService 35 { 36 37 private JNDIBindings bindings; 38 39 40 private String rootName; 41 42 public JNDIBindings getBindings() 43 { 44 return bindings; 45 } 46 public void setBindings(JNDIBindings bindings) 47 { 48 this.bindings = bindings; 49 } 50 51 public String getRootName() 52 { 53 return rootName; 54 } 55 public void setRootName(String rootName) 56 { 57 this.rootName = rootName; 58 } 59 60 public void addBindings() throws NamingException 61 { 62 Context ctx = new InitialContext (); 63 if( rootName != null ) 64 ctx = (Context ) ctx.lookup(rootName); 65 66 JNDIBinding[] values = bindings.getBindings(); 67 for(int n = 0; n < values.length; n ++) 68 { 69 String name = values[n].getName(); 70 Object value; 71 try 72 { 73 value = values[n].getValue(); 74 } 75 catch(Exception e) 76 { 77 NamingException ne = new NamingException ("Failed to obtain value from binding: "+name); 78 ne.setRootCause(e); 79 throw ne; 80 } 81 Util.bind(ctx, name, value); 82 } 83 ctx.close(); 84 } 85 public void removeBindings() throws NamingException 86 { 87 Context ctx = new InitialContext (); 88 if( rootName != null ) 89 ctx = (Context ) ctx.lookup(rootName); 90 91 JNDIBinding[] values = bindings.getBindings(); 92 for(int n = 0; n < values.length; n ++) 93 { 94 String name = values[n].getName(); 95 Util.unbind(ctx, name); 96 } 97 ctx.close(); 98 } 99 100 public void start() throws Exception 101 { 102 addBindings(); 103 } 104 public void stop() throws Exception 105 { 106 removeBindings(); 107 } 108 } 109 | Popular Tags |