OSDN Git Service

libjava/
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / CORBA / CollocatedOrbs.java
1 /* CollocatedOrbs.java -- Handles collocations
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package gnu.CORBA;
40
41 import gnu.CORBA.Poa.gnuServantObject;
42
43 import java.net.InetAddress;
44 import java.net.UnknownHostException;
45 import java.util.ArrayList;
46
47 /**
48  * This class provides support for the direct method invocations without
49  * involving the network in the case when both ORBs run on the same java
50  * virtual machine. Special attention is only needed when call is made
51  * between two independent ORBs, instantiated via ORB.init. The call to the
52  * object, obtained via IOR reference from the ORB where it was locally
53  * connected is always local anyway.
54  * 
55  * For security reasons it may be sensible to keep this class and all support
56  * package private.
57  * 
58  * @author Audrius Meskauskas
59  */
60 class CollocatedOrbs
61 {
62   /**
63    * This field is used in automated Classpath specific testing to disable
64    * the direct calls. 
65    */
66   static boolean DIRECT_CALLS_ALLOWED = true;
67   
68    /**
69     * Containts the references of the all running GNU Classpath ORBs in the
70     * local virtual machine. GNU Classpath ORBs register themselves here when
71     * created and unregister when either ORB.destroy is called or in the 
72     * finalizer.
73     */
74    private static ArrayList orbs = new ArrayList();
75    
76    /**
77     * The address of the local host.
78     */
79    static String localHost;
80    
81    static
82     {
83       try
84         {
85           localHost = InetAddress.getLocalHost().getHostAddress();
86         }
87       catch (UnknownHostException ex)
88         {
89           throw new InternalError("Local host is not accessible:" + ex);
90         }
91     }
92    
93    /**
94      * Register the new ORB
95      * 
96      * @param orb the orb to register
97      */
98   static void registerOrb(OrbFunctional orb)
99   {
100     if (DIRECT_CALLS_ALLOWED)
101       synchronized (orbs)
102         {
103           assert ! orbs.contains(orb);
104           orbs.add(orb);
105         }
106   }
107   
108    /**
109      * Unregister the ORB. The ORB will no longer be reacheable locally but may
110      * be reacheable via network as if it would be remote.
111      * 
112      * @param orb the orb to unregister
113      */
114   static void unregisterOrb(OrbFunctional orb)
115   {
116     if (DIRECT_CALLS_ALLOWED)
117       synchronized (orbs)
118         {
119           assert orbs.contains(orb);
120           orbs.remove(orb);
121         }
122   }
123   
124   /**
125    * Search the possibly local object. If the IOR is not local or none of the
126    * found ORBs of this virtual machine knows about it, null is returned.
127    * 
128    * @param ior the IOR to search
129    * @return the found local CORBA object or null in not found.
130    */
131   static org.omg.CORBA.Object searchLocalObject(IOR ior)
132   {
133     if (! DIRECT_CALLS_ALLOWED && ! ior.Internet.host.equals(localHost))
134       return null;
135
136     synchronized (orbs)
137       {
138         OrbFunctional orb;
139         org.omg.CORBA.Object object;
140         for (int i = 0; i < orbs.size(); i++)
141           {
142             orb = (OrbFunctional) orbs.get(i);
143             object = orb.find_connected_object(ior.key, ior.Internet.port);
144             if (object != null)
145               {
146                 if (object instanceof SafeForDirectCalls)
147                   {
148                     return object;
149                   }
150                 else if (object instanceof gnuServantObject)
151                   {
152                     return object;
153                   }
154               }
155           }
156       }
157     return null;
158   }
159   
160 }