KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.naming.Context JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.naming.Name JavaDoc;
21 import javax.naming.NameNotFoundException JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import java.io.Serializable JavaDoc;
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
31         implements ODMGJ2EEFactory, Serializable JavaDoc, ODMGFactoryMBean
32 {
33     private static final String JavaDoc JAVA_NAMESPACE = "java:/";
34
35     private String JavaDoc jndiName;
36
37     public ODMGFactory()
38     {
39         System.out.println("ODMGFactory called");
40     }
41
42     protected void startService() throws Exception JavaDoc
43     {
44         try
45         {
46             bind(new InitialContext JavaDoc(), JAVA_NAMESPACE + jndiName, this);
47         }
48         catch (Exception JavaDoc e)
49         {
50             System.out.println("Binding ODMG to JNDI failed");
51             e.printStackTrace();
52             throw e;
53         }
54         System.out.println("** OJB-ODMG MBean integration");
55         System.out.println("** ODMGFactory: " + this.getClass().getName());
56         System.out.println("** Use ODMGFactory via lookup:");
57         System.out.println("** ODMGFactory factory = (ODMGFactory) ctx.lookup(" + JAVA_NAMESPACE + jndiName + ")");
58         System.out.println("** Implementation odmg = factory.getInstance();");
59     }
60
61     public void stopService()
62     {
63         try
64         {
65             (new InitialContext JavaDoc()).unbind(JAVA_NAMESPACE + jndiName);
66         }
67         catch (NamingException JavaDoc namingexception)
68         {
69             namingexception.printStackTrace();
70         }
71     }
72
73     public Implementation getInstance()
74     {
75         return OJB.getInstance();
76     }
77
78     public void setJndiName(String JavaDoc jndiName)
79     {
80         this.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) throws NamingException JavaDoc
89     {
90         Name JavaDoc n;
91         for (n = ctx.getNameParser("").parse(name); n.size() > 1; n = n.getSuffix(1))
92         {
93             String JavaDoc ctxName = n.get(0);
94             try
95             {
96                 ctx = (Context JavaDoc) ctx.lookup(ctxName);
97             }
98             catch (NameNotFoundException JavaDoc namenotfoundexception)
99             {
100                 ctx = ctx.createSubcontext(ctxName);
101             }
102         }
103         ctx.bind(n.get(0), val);
104     }
105 }
106
Popular Tags