KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > remote > DataSourceFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jdbc.remote;
23
24 import java.rmi.server.UID JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import javax.naming.spi.ObjectFactory JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Reference JavaDoc;
32 import javax.naming.BinaryRefAddr JavaDoc;
33 import javax.naming.StringRefAddr JavaDoc;
34 import org.jboss.util.naming.NonSerializableFactory;
35
36 /** A JNDI ObjectFactory for handling either the local or remote access to
37  * a JCA javax.sql.DataSource binding.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37459 $
41  */

42 public class DataSourceFactory implements ObjectFactory JavaDoc
43 {
44    /** The class VM ID */
45    public static final UID JavaDoc vmID = new UID JavaDoc();
46
47    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc ctx,
48       Hashtable JavaDoc env) throws Exception JavaDoc
49    {
50       Object JavaDoc instance = null;
51       if (obj instanceof Reference JavaDoc)
52       {
53          Reference JavaDoc ref = (Reference JavaDoc) obj;
54          // Check the local id
55
BinaryRefAddr JavaDoc localID = (BinaryRefAddr JavaDoc) ref.get("VMID");
56          byte[] idBytes = (byte[]) localID.getContent();
57          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(idBytes);
58          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
59          UID JavaDoc id = (UID JavaDoc) ois.readObject();
60          if( id.equals(vmID) == true )
61          {
62             // Use the local datasource
63
StringRefAddr JavaDoc jndiAddr = (StringRefAddr JavaDoc) ref.get("JndiName");
64             String JavaDoc jndiName = (String JavaDoc) jndiAddr.getContent();
65             instance = NonSerializableFactory.lookup(jndiName);
66          }
67          else
68          {
69             // Use the embedded proxy
70
BinaryRefAddr JavaDoc proxyAddr = (BinaryRefAddr JavaDoc) ref.get("ProxyData");
71             byte[] proxyBytes = (byte[]) proxyAddr.getContent();
72             ByteArrayInputStream JavaDoc bais2 = new ByteArrayInputStream JavaDoc(proxyBytes);
73             ObjectInputStream JavaDoc ois2 = new ObjectInputStream JavaDoc(bais2);
74             instance = ois2.readObject();
75          }
76       }
77       return instance;
78    }
79
80 }
81
Popular Tags