KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jboss > PBFactory


1 package org.apache.ojb.jboss;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.Name JavaDoc;
26 import javax.naming.NameNotFoundException JavaDoc;
27 import java.io.Serializable JavaDoc;
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 PBFactoryMBean, Serializable JavaDoc
36 {
37     private String JavaDoc _jndiName;
38     private static final String JavaDoc JAVA_NAMESPACE = "java:/";
39
40     public PersistenceBrokerFactoryIF getInstance()
41     {
42         return PersistenceBrokerFactoryFactory.instance();
43     }
44
45     public String JavaDoc getName()
46     {
47         return "PBAPI-Implementation";
48     }
49
50     protected void startService()
51             throws Exception JavaDoc
52     {
53         try
54         {
55             bind(new InitialContext JavaDoc(), JAVA_NAMESPACE + _jndiName, this);
56         }
57         catch (Throwable JavaDoc e)
58         {
59             e.printStackTrace();
60         }
61         System.out.println("** OJB-PB MBean integration");
62         System.out.println("** PBFactory: "+this.getClass().getName()+" / "+this.getServiceName().toString());
63         System.out.println("** Lookup PersistenceBrokerFactory via '"+JAVA_NAMESPACE+_jndiName+"'");
64     }
65
66     public void stopService()
67     {
68         try
69         {
70             (new InitialContext JavaDoc()).unbind(JAVA_NAMESPACE + _jndiName);
71         }
72         catch (NamingException JavaDoc namingexception)
73         {
74             namingexception.printStackTrace();
75         }
76     }
77
78     public void setJndiName(String JavaDoc jndiName)
79     {
80         _jndiName = jndiName;
81     }
82
83     public String JavaDoc getJndiName()
84     {
85         return _jndiName;
86     }
87
88     private void bind(Context JavaDoc ctx, String JavaDoc name, Object JavaDoc val)
89             throws NamingException JavaDoc
90     {
91         Name JavaDoc n;
92         for (n = ctx.getNameParser("").parse(name); n.size() > 1; n = n.getSuffix(1))
93         {
94             String JavaDoc ctxName = n.get(0);
95             try
96             {
97                 ctx = (Context JavaDoc) ctx.lookup(ctxName);
98             }
99             catch (NameNotFoundException JavaDoc namenotfoundexception)
100             {
101                 ctx = ctx.createSubcontext(ctxName);
102             }
103         }
104         ctx.bind(n.get(0), val);
105     }
106 }
107
Popular Tags