01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.jems.as;
23:
24: import org.jboss.util.naming.NonSerializableFactory;
25:
26: import javax.naming.CompositeName;
27: import javax.naming.NameNotFoundException;
28: import javax.naming.NamingException;
29:
30: /**
31: * Various JNDI stuff.
32: *
33: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
34: * @version $Revision: 8784 $
35: */
36: public class JNDI {
37:
38: /**
39: * Encapsulate JNDI binding operation into one single class, hidding non usefull JNDI complexity. It has been
40: * designed to fit with service life cycle.
41: * <p/>
42: * <code>
43: * <p/>
44: * private String jndiName; private JNDI.Binding jndiBinding;
45: * <p/>
46: * public void startService() throws Exception { ... if (this.jndiName != null) { jndiBinding = new
47: * JNDI.Binding(jndiName, this); jndiBinding.bind(); } ... }
48: * <p/>
49: * public void stopService() throws Exception { ... if (jndiBinding != null) { jndiBinding.unbind(); jndiBinding =
50: * null; } ... } </code>
51: */
52: public static class Binding {
53:
54: /** . */
55: private final String jndiName;
56:
57: /** . */
58: private String unbindJNDIName;
59:
60: /** . */
61: private Object o;
62:
63: public Binding(String jndiName, Object o) {
64: if (jndiName == null) {
65: throw new IllegalArgumentException(
66: "No JNDI name provided");
67: }
68: if (o == null) {
69: throw new IllegalArgumentException(
70: "No object to bind for JNDI name " + jndiName);
71: }
72: this .jndiName = jndiName;
73: this .o = o;
74: }
75:
76: /**
77: * Attempt to perform binding.
78: *
79: * @throws NamingException on failure
80: */
81: public void bind() throws NamingException {
82: NonSerializableFactory.rebind(new CompositeName(jndiName),
83: o, true);
84: unbindJNDIName = jndiName;
85: }
86:
87: /** Unbinds in a safe manner. */
88: public void unbind() {
89: if (unbindJNDIName != null) {
90: try {
91: NonSerializableFactory.unbind(unbindJNDIName);
92: } catch (NameNotFoundException ignore) {
93: }
94: }
95: }
96: }
97: }
|