01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.naming;
23:
24: import javax.naming.Context;
25: import javax.naming.InitialContext;
26: import javax.naming.NamingException;
27:
28: /**
29: * A service which allows arbitrary values to be bound into JNDI.
30: *
31: * @author Scott.Stark@jboss.org
32: * @version $Revision: 57209 $
33: */
34: public class JNDIBindingService {
35: /** The JNDI bindings managed by this service */
36: private JNDIBindings bindings;
37:
38: /** The root context name under which the values are bound */
39: private String rootName;
40:
41: public JNDIBindings getBindings() {
42: return bindings;
43: }
44:
45: public void setBindings(JNDIBindings bindings) {
46: this .bindings = bindings;
47: }
48:
49: public String getRootName() {
50: return rootName;
51: }
52:
53: public void setRootName(String rootName) {
54: this .rootName = rootName;
55: }
56:
57: public void addBindings() throws NamingException {
58: Context ctx = new InitialContext();
59: if (rootName != null)
60: ctx = (Context) ctx.lookup(rootName);
61:
62: JNDIBinding[] values = bindings.getBindings();
63: for (int n = 0; n < values.length; n++) {
64: String name = values[n].getName();
65: Object value;
66: try {
67: value = values[n].getValue();
68: } catch (Exception e) {
69: NamingException ne = new NamingException(
70: "Failed to obtain value from binding: " + name);
71: ne.setRootCause(e);
72: throw ne;
73: }
74: Util.bind(ctx, name, value);
75: }
76: ctx.close();
77: }
78:
79: public void removeBindings() throws NamingException {
80: Context ctx = new InitialContext();
81: if (rootName != null)
82: ctx = (Context) ctx.lookup(rootName);
83:
84: JNDIBinding[] values = bindings.getBindings();
85: for (int n = 0; n < values.length; n++) {
86: String name = values[n].getName();
87: Util.unbind(ctx, name);
88: }
89: ctx.close();
90: }
91:
92: public void start() throws Exception {
93: addBindings();
94: }
95:
96: public void stop() throws Exception {
97: removeBindings();
98: }
99: }
|