001: /*****************************************************************************
002: * Copyright (c) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: *****************************************************************************/package org.picocontainer.gems.jndi;
009:
010: import java.io.IOException;
011: import java.io.Serializable;
012:
013: import javax.naming.Context;
014: import javax.naming.InitialContext;
015: import javax.naming.Name;
016: import javax.naming.NameNotFoundException;
017: import javax.naming.NamingException;
018:
019: import org.picocontainer.ObjectReference;
020: import org.picocontainer.PicoCompositionException;
021:
022: /**
023: * object reference to store and retrieve objects from JNDI
024: *
025: * @author ko5tik
026: *
027: */
028: public class JNDIObjectReference<T> implements ObjectReference<T>,
029: Serializable {
030:
031: String name;
032:
033: transient Context context;
034:
035: public JNDIObjectReference(String name, Context ctx) {
036: super ();
037: this .name = name;
038: this .context = ctx;
039: }
040:
041: public JNDIObjectReference(String jndiName) throws NamingException {
042: this (jndiName, new InitialContext());
043: }
044:
045: /**
046: * retrieve object from JNDI if possible
047: */
048: public T get() {
049: try {
050: return (T) context.lookup(name);
051: } catch (NameNotFoundException e) {
052: // this is not error, but normal situation - nothing
053: // was stored yet
054: return null;
055: } catch (NamingException e) {
056: throw new PicoCompositionException(
057: "unable to resolve jndi name:" + name, e);
058: }
059: }
060:
061: /**
062: * store object in JNDI under specified name
063: */
064: public void set(T item) {
065: try {
066: if (item == null) {
067: context.unbind(name);
068: } else {
069:
070: Context ctx = context;
071: Name n = ctx.getNameParser("").parse(name);
072: while (n.size() > 1) {
073: String ctxName = n.get(0);
074: try {
075: ctx = (Context) ctx.lookup(ctxName);
076: } catch (NameNotFoundException e) {
077: ctx = ctx.createSubcontext(ctxName);
078: }
079: n = n.getSuffix(1);
080: }
081: // unbind name just in case
082: try {
083: if (ctx.lookup(n) != null) {
084: ctx.unbind(n);
085: }
086: } catch (NameNotFoundException e) {
087: // that's ok
088: }
089: ctx.bind(n, item);
090: }
091: } catch (NamingException e) {
092: throw new PicoCompositionException(
093: "unable to bind to jndi name:" + name, e);
094: }
095: }
096:
097: /**
098: * name of this reference
099: *
100: * @return
101: */
102: public String getName() {
103: return name;
104: }
105:
106: /**
107: * here we try to capture (eventual) deserealisation of this reference by
108: * some container (notably JBoss) and restore context as initial context
109: * I hope this will be sufficient for most puproses
110: *
111: * @param in
112: * @throws IOException
113: * @throws ClassNotFoundException
114: */
115: private void readObject(java.io.ObjectInputStream in)
116: throws IOException, ClassNotFoundException {
117: try {
118: context = new InitialContext();
119: } catch (NamingException e) {
120: throw new IOException("unable to create initial context");
121: }
122: in.defaultReadObject();
123: }
124:
125: public String toString() {
126: return "(" + getName() + ")";
127: }
128: }
|