01: package org.apache.ojb.jboss;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import javax.naming.Context;
19: import javax.naming.InitialContext;
20: import javax.naming.Name;
21: import javax.naming.NameNotFoundException;
22: import javax.naming.NamingException;
23: import java.io.Serializable;
24:
25: import org.apache.ojb.odmg.ODMGJ2EEFactory;
26: import org.apache.ojb.odmg.OJB;
27: import org.jboss.system.ServiceMBeanSupport;
28: import org.odmg.Implementation;
29:
30: public class ODMGFactory extends ServiceMBeanSupport implements
31: ODMGJ2EEFactory, Serializable, ODMGFactoryMBean {
32: private static final String JAVA_NAMESPACE = "java:/";
33:
34: private String jndiName;
35:
36: public ODMGFactory() {
37: System.out.println("ODMGFactory called");
38: }
39:
40: protected void startService() throws Exception {
41: try {
42: bind(new InitialContext(), JAVA_NAMESPACE + jndiName, this );
43: } catch (Exception e) {
44: System.out.println("Binding ODMG to JNDI failed");
45: e.printStackTrace();
46: throw e;
47: }
48: System.out.println("** OJB-ODMG MBean integration");
49: System.out.println("** ODMGFactory: "
50: + this .getClass().getName());
51: System.out.println("** Use ODMGFactory via lookup:");
52: System.out
53: .println("** ODMGFactory factory = (ODMGFactory) ctx.lookup("
54: + JAVA_NAMESPACE + jndiName + ")");
55: System.out
56: .println("** Implementation odmg = factory.getInstance();");
57: }
58:
59: public void stopService() {
60: try {
61: (new InitialContext()).unbind(JAVA_NAMESPACE + jndiName);
62: } catch (NamingException namingexception) {
63: namingexception.printStackTrace();
64: }
65: }
66:
67: public Implementation getInstance() {
68: return OJB.getInstance();
69: }
70:
71: public void setJndiName(String jndiName) {
72: this .jndiName = jndiName;
73: }
74:
75: public String getJndiName() {
76: return jndiName;
77: }
78:
79: private void bind(Context ctx, String name, Object val)
80: throws NamingException {
81: Name n;
82: for (n = ctx.getNameParser("").parse(name); n.size() > 1; n = n
83: .getSuffix(1)) {
84: String ctxName = n.get(0);
85: try {
86: ctx = (Context) ctx.lookup(ctxName);
87: } catch (NameNotFoundException namenotfoundexception) {
88: ctx = ctx.createSubcontext(ctxName);
89: }
90: }
91: ctx.bind(n.get(0), val);
92: }
93: }
|