001: /* JndiUtils.java
002: *
003: * Created Aug 11, 2005
004: *
005: * Copyright (C) 2005 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.util;
024:
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027: import java.util.Properties;
028:
029: import javax.management.MalformedObjectNameException;
030: import javax.management.ObjectName;
031: import javax.naming.CompoundName;
032: import javax.naming.Context;
033: import javax.naming.InitialContext;
034: import javax.naming.InvalidNameException;
035: import javax.naming.NameNotFoundException;
036: import javax.naming.NamingException;
037: import javax.naming.Reference;
038: import javax.naming.StringRefAddr;
039:
040: /**
041: * JNDI utilities.
042: * @author stack
043: * @version $Date: 2005-10-27 22:20:20 +0000 (Thu, 27 Oct 2005) $ $Version$
044: */
045: public class JndiUtils {
046: /**
047: * Syntax that will work with jmx ObjectNames (i.e. will escape '.' and
048: * will add treat ',' and '=' specially.
049: */
050: private static final Properties COMPOUND_NAME_SYNTAX = new Properties();
051: static {
052: COMPOUND_NAME_SYNTAX.put("jndi.syntax.direction",
053: "left_to_right");
054: COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator", "+");
055: COMPOUND_NAME_SYNTAX.put("jndi.syntax.ignorecase", "false");
056: COMPOUND_NAME_SYNTAX.put("jndi.syntax.escape", "\\");
057: COMPOUND_NAME_SYNTAX.put("jndi.syntax.beginquote", "'");
058: COMPOUND_NAME_SYNTAX.put("jndi.syntax.trimblanks", "true");
059: COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator.ava", ",");
060: COMPOUND_NAME_SYNTAX.put("jndi.syntax.separator.typeval", "=");
061: }
062:
063: public static CompoundName getCompoundName(final String name)
064: throws InvalidNameException {
065: return new CompoundName(name, COMPOUND_NAME_SYNTAX);
066: }
067:
068: /**
069: * Return name to use as jndi name.
070: * Used to do a subset of the ObjectName fields but not just
071: * let all through so its easy to just use the jndi name to
072: * find mbean.
073: * @param on ObjectName instance to work with.
074: * @return Returns a compound name to use as jndi key.
075: * @throws NullPointerException
076: * @throws InvalidNameException
077: */
078: public static CompoundName getCompoundName(final ObjectName on)
079: throws NullPointerException, InvalidNameException {
080: return getCompoundName(on.getCanonicalKeyPropertyListString());
081: }
082:
083: /**
084: * @param on ObjectName instance to work with.
085: * @return A simple reference based on passed <code>on</code>
086: */
087: public static Reference getReference(final ObjectName on) {
088: Reference r = new Reference(String.class.getName());
089: Hashtable ht = on.getKeyPropertyList();
090: r.add(new StringRefAddr(JmxUtils.HOST, (String) ht
091: .get(JmxUtils.HOST)));
092: r.add(new StringRefAddr(JmxUtils.NAME, (String) ht
093: .get(JmxUtils.NAME)));
094: // Put in a value to serve as a unique 'key'.
095: r.add(new StringRefAddr(JmxUtils.KEY, on
096: .getCanonicalKeyPropertyListString()));
097: return r;
098: }
099:
100: /**
101: * Get subcontext. Only looks down one level.
102: * @param subContext Name of subcontext to return.
103: * @return Sub context.
104: * @throws NamingException
105: */
106: public static Context getSubContext(final String subContext)
107: throws NamingException {
108: return getSubContext(getCompoundName(subContext));
109: }
110:
111: /**
112: * Get subcontext. Only looks down one level.
113: * @param subContext Name of subcontext to return.
114: * @return Sub context.
115: * @throws NamingException
116: */
117: public static Context getSubContext(final CompoundName subContext)
118: throws NamingException {
119: Context context = new InitialContext();
120: try {
121: context = (Context) context.lookup(subContext);
122: } catch (NameNotFoundException e) {
123: context = context.createSubcontext(subContext);
124: }
125: return context;
126: }
127:
128: /**
129: *
130: * @param context A subcontext named for the <code>on.getDomain()</code>
131: * (Assumption is that caller already setup this subcontext).
132: * @param on The ObjectName we're to base our bind name on.
133: * @return Returns key we used binding this ObjectName.
134: * @throws NamingException
135: * @throws NullPointerException
136: */
137: public static CompoundName bindObjectName(Context context,
138: final ObjectName on) throws NamingException,
139: NullPointerException {
140: // I can't call getNameInNamespace in tomcat. Complains about
141: // unsupported operation -- that I can't get absolute name.
142: // Therefore just skip this test below -- at least for now.
143: // Check that passed context has the passed ObjectNames' name.
144: //
145: // String name = getCompoundName(context.getNameInNamespace()).toString();
146: // if (!name.equals(on.getDomain())) {
147: // throw new NamingException("The current context is " + name +
148: // " but domain is " + on.getDomain() + " (Was expecting " +
149: // "them to be the same).");
150: // }
151: CompoundName key = getCompoundName(on);
152: context.rebind(key, getReference(on));
153: return key;
154: }
155:
156: public static CompoundName unbindObjectName(final Context context,
157: final ObjectName on) throws NullPointerException,
158: NamingException {
159: CompoundName key = getCompoundName(on);
160: context.unbind(key);
161: return key;
162: }
163:
164: /**
165: * Testing code.
166: * @param args Command line arguments.
167: * @throws NullPointerException
168: * @throws MalformedObjectNameException
169: * @throws NamingException
170: * @throws InvalidNameException
171: */
172: public static void main(String[] args)
173: throws MalformedObjectNameException, NullPointerException,
174: InvalidNameException, NamingException {
175: final ObjectName on = new ObjectName(
176: "org.archive.crawler:"
177: + "type=Service,name=Heritrix00,host=debord.archive.org");
178: Context c = getSubContext(getCompoundName(on.getDomain()));
179: CompoundName key = bindObjectName(c, on);
180: Reference r = (Reference) c.lookup(key);
181: for (Enumeration e = r.getAll(); e.hasMoreElements();) {
182: System.out.println(e.nextElement());
183: }
184: unbindObjectName(c, on);
185: }
186: }
|