KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > ResourceObjectJNDIHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  * --------------------------------------------------------------------------
21  * $Id: ResourceObjectJNDIHandler.java,v 1.5 2004/10/29 23:33:37 ehardesty Exp $
22  * --------------------------------------------------------------------------
23  */

24 package org.objectweb.jonas.resource;
25
26 import java.util.Hashtable JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.RefAddr JavaDoc;
31 import javax.naming.Reference JavaDoc;
32 import javax.naming.spi.ObjectFactory JavaDoc;
33
34 import org.objectweb.jonas.common.JNDIUtils;
35 import org.objectweb.jonas_rar.deployment.api.ConnectorDesc;
36 import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc;
37
38 /**
39  *
40  * @author Eric Hardesty
41  * Contributor(s):
42  *
43  */

44 public class ResourceObjectJNDIHandler implements ObjectFactory JavaDoc {
45
46     /**
47      * Create an object using the information provided
48      * @param refObj the possibly null object containing reference information to create the object.
49      * @param name the name of this object relative to nameCtx, or null if no name is specified.
50      * @param nameCtx the context relative to which the name parameter is specified, or null if name
51      * is relative to the default initial context.
52      * @param env the possibly null environment that is used in creating the object.
53      * @return Object created or null if unable to create
54      * @throws Exception if an exception happened while trying to create the object
55      */

56     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc name,
57                                     Context JavaDoc nameCtx, Hashtable JavaDoc env) throws Exception JavaDoc {
58
59         Reference JavaDoc ref = (Reference JavaDoc) refObj;
60
61         if (ref == null) {
62             System.out.println("No reference found");
63             return null;
64         }
65         String JavaDoc clname = ref.getClassName();
66         String JavaDoc raname = (String JavaDoc) ref.get(ResourceServiceImpl.JNDI_NAME).getContent();
67
68         Object JavaDoc obj = null;
69         try {
70             obj = Rar.getResourceObject(raname);
71         } catch (Throwable JavaDoc ex) {
72             ex.printStackTrace();
73         }
74         if (obj != null) {
75             return obj;
76         }
77
78         // Need to load the factory in the client environment
79
String JavaDoc rarObjectName = (String JavaDoc) ref.get(ResourceServiceImpl.RAR_OBJNAME).getContent();
80         String JavaDoc factoryType = (String JavaDoc) ref.get(ResourceServiceImpl.FACTORY_TYPE).getContent();
81         String JavaDoc factoryOff = (String JavaDoc) ref.get(ResourceServiceImpl.FACTORY_OFFSET).getContent();
82         int factoryOffset = Integer.parseInt(factoryOff);
83
84         RefAddr JavaDoc refAddr = null;
85         ConnectorDesc conn = null;
86         refAddr = ref.get(ResourceServiceImpl.RA_XML);
87         if (refAddr != null) {
88             conn = (ConnectorDesc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
89         }
90
91         JonasConnectorDesc jConn = null;
92         refAddr = ref.get(ResourceServiceImpl.JONAS_RA_XML);
93         if (refAddr != null) {
94             jConn = (JonasConnectorDesc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
95         }
96
97         // This will either get the other server's implemenation or a client version
98
Rar ra = new Rar();
99         obj = ra.createFactory(raname, rarObjectName, factoryOffset, factoryType,
100                                conn, jConn);
101
102         return obj;
103     }
104 }
105
Popular Tags