KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > binding > remote > RemoteConnectionFactoryHelper


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.binding.remote;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.rmi.server.UID JavaDoc;
27 import java.util.Hashtable JavaDoc;
28
29 import javax.naming.BinaryRefAddr JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.StringRefAddr JavaDoc;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35
36 import org.jboss.util.naming.NonSerializableFactory;
37
38 /**
39  * A RemoteConnectionFactoryHelper.
40  *
41  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
42  * @version $Revision: 44967 $
43  */

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