1 23 package org.archive.util; 24 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Properties ; 28 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 import javax.naming.CompoundName ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.InvalidNameException ; 35 import javax.naming.NameNotFoundException ; 36 import javax.naming.NamingException ; 37 import javax.naming.Reference ; 38 import javax.naming.StringRefAddr ; 39 40 45 public class JndiUtils { 46 50 private static final Properties COMPOUND_NAME_SYNTAX = new Properties (); 51 static { 52 COMPOUND_NAME_SYNTAX.put("jndi.syntax.direction", "left_to_right"); 53 COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator", "+"); 54 COMPOUND_NAME_SYNTAX.put("jndi.syntax.ignorecase", "false"); 55 COMPOUND_NAME_SYNTAX.put("jndi.syntax.escape", "\\"); 56 COMPOUND_NAME_SYNTAX.put("jndi.syntax.beginquote", "'"); 57 COMPOUND_NAME_SYNTAX.put("jndi.syntax.trimblanks", "true"); 58 COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator.ava", ","); 59 COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator.typeval", "="); 60 } 61 62 public static CompoundName getCompoundName(final String name) 63 throws InvalidNameException { 64 return new CompoundName (name, COMPOUND_NAME_SYNTAX); 65 } 66 67 77 public static CompoundName getCompoundName(final ObjectName on) 78 throws NullPointerException , 79 InvalidNameException { 80 return getCompoundName(on.getCanonicalKeyPropertyListString()); 81 } 82 83 87 public static Reference getReference(final ObjectName on) { 88 Reference r = new Reference (String .class.getName()); 89 Hashtable ht = on.getKeyPropertyList(); 90 r.add(new StringRefAddr (JmxUtils.HOST, (String )ht.get(JmxUtils.HOST))); 91 r.add(new StringRefAddr (JmxUtils.NAME, (String )ht.get(JmxUtils.NAME))); 92 r.add(new StringRefAddr (JmxUtils.KEY, 94 on.getCanonicalKeyPropertyListString())); 95 return r; 96 } 97 98 104 public static Context getSubContext(final String subContext) 105 throws NamingException { 106 return getSubContext(getCompoundName(subContext)); 107 } 108 109 115 public static Context getSubContext(final CompoundName subContext) 116 throws NamingException { 117 Context context = new InitialContext (); 118 try { 119 context = (Context )context.lookup(subContext); 120 } catch (NameNotFoundException e) { 121 context = context.createSubcontext(subContext); 122 } 123 return context; 124 } 125 126 135 public static CompoundName bindObjectName(Context context, 136 final ObjectName on) 137 throws NamingException , NullPointerException { 138 CompoundName key = getCompoundName(on); 150 context.rebind(key, getReference(on)); 151 return key; 152 } 153 154 public static CompoundName unbindObjectName(final Context context, 155 final ObjectName on) 156 throws NullPointerException , NamingException { 157 CompoundName key = getCompoundName(on); 158 context.unbind(key); 159 return key; 160 } 161 162 163 171 public static void main(String [] args) 172 throws MalformedObjectNameException , NullPointerException , 173 InvalidNameException , NamingException { 174 final ObjectName on = new ObjectName ("org.archive.crawler:" + 175 "type=Service,name=Heritrix00,host=debord.archive.org"); 176 Context c = getSubContext(getCompoundName(on.getDomain())); 177 CompoundName key = bindObjectName(c, on); 178 Reference r = (Reference )c.lookup(key); 179 for (Enumeration e = r.getAll(); e.hasMoreElements();) { 180 System.out.println(e.nextElement()); 181 } 182 unbindObjectName(c, on); 183 } 184 } 185 | Popular Tags |