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 org.jboss.system.ServiceMBeanSupport;
19: import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
20: import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
21:
22: import javax.naming.InitialContext;
23: import javax.naming.NamingException;
24: import javax.naming.Context;
25: import javax.naming.Name;
26: import javax.naming.NameNotFoundException;
27: import java.io.Serializable;
28:
29: /**
30: * mbean for the PersistenceBrokerFactory
31: *
32: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
33: */
34:
35: public class PBFactory extends ServiceMBeanSupport implements
36: PBFactoryMBean, Serializable {
37: private String _jndiName;
38: private static final String JAVA_NAMESPACE = "java:/";
39:
40: public PersistenceBrokerFactoryIF getInstance() {
41: return PersistenceBrokerFactoryFactory.instance();
42: }
43:
44: public String getName() {
45: return "PBAPI-Implementation";
46: }
47:
48: protected void startService() throws Exception {
49: try {
50: bind(new InitialContext(), JAVA_NAMESPACE + _jndiName, this );
51: } catch (Throwable e) {
52: e.printStackTrace();
53: }
54: System.out.println("** OJB-PB MBean integration");
55: System.out.println("** PBFactory: " + this .getClass().getName()
56: + " / " + this .getServiceName().toString());
57: System.out.println("** Lookup PersistenceBrokerFactory via '"
58: + JAVA_NAMESPACE + _jndiName + "'");
59: }
60:
61: public void stopService() {
62: try {
63: (new InitialContext()).unbind(JAVA_NAMESPACE + _jndiName);
64: } catch (NamingException namingexception) {
65: namingexception.printStackTrace();
66: }
67: }
68:
69: public void setJndiName(String jndiName) {
70: _jndiName = jndiName;
71: }
72:
73: public String getJndiName() {
74: return _jndiName;
75: }
76:
77: private void bind(Context ctx, String name, Object val)
78: throws NamingException {
79: Name n;
80: for (n = ctx.getNameParser("").parse(name); n.size() > 1; n = n
81: .getSuffix(1)) {
82: String ctxName = n.get(0);
83: try {
84: ctx = (Context) ctx.lookup(ctxName);
85: } catch (NameNotFoundException namenotfoundexception) {
86: ctx = ctx.createSubcontext(ctxName);
87: }
88: }
89: ctx.bind(n.get(0), val);
90: }
91: }
|