libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / javax / naming / giop / GiopNamingServiceFactory.java
blob6c221548f475abfc52b499d404b307c85d42281a
1 /* GiopNamingServiceFactory.java -- handles corbaname: urls
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.javax.naming.giop;
41 import gnu.CORBA.OrbFunctional;
43 import gnu.java.lang.CPStringBuilder;
45 import java.util.Enumeration;
46 import java.util.Hashtable;
47 import java.util.Iterator;
48 import java.util.Map;
49 import java.util.Properties;
50 import java.util.TreeMap;
52 import javax.naming.Context;
53 import javax.naming.Name;
55 import org.omg.CORBA.ORB;
57 /**
58 * The context factory to represent the corbaname: style urls. Such URL states
59 * that the CORBA naming service exists on the given host. This service can
60 * return the required object, finding it by the given name. The names are
61 * parsed using the specification of the corbaname urls. Being the naming
62 * service, the returned context supports creating the subcontexts, forwarding
63 * this task to the existing naming service.
65 * @author Audrius Meskauskas (audriusa@Bioinformatics.org)
67 public class GiopNamingServiceFactory
69 /**
70 * The default naming service provider. It is assumed, that the naming service
71 * is running on the port 900 of the local host, using the GIOP version 1.2
73 public static final String DEFAULT_PROVIDER =
74 "corbaloc:iiop:1.2@127.0.0.1:900/NameService";
76 /**
77 * The table of all instantiated ORB's that are found by they ORB
78 * properties signatures. If all ORB related properties are the same,
79 * the ORB's are shared.
81 public static Hashtable orbs = new Hashtable();
84 /**
85 * Create a new instance of the corbaname URL context.
87 public Object getObjectInstance(Object refObj, Name name, Context nameCtx,
88 Hashtable environment)
90 String provider = (String) environment.get(Context.PROVIDER_URL);
91 if (provider == null)
92 provider = DEFAULT_PROVIDER;
94 String orbSignature = getOrbSignature(environment);
96 ORB orb;
97 synchronized (orbs)
99 orb = (ORB) orbs.get(orbSignature);
100 if (orb == null)
102 Properties props = new Properties();
103 props.putAll(environment);
104 orb = ORB.init(new String[0], props);
105 orbs.put(orbSignature, orb);
106 final ORB runIt = orb;
107 new Thread()
109 public void run()
111 runIt.run();
113 }.start();
117 return new GiopNamingServiceURLContext(environment, this, orb);
121 * Check if this ORB is still in use (maybe it is time to shutdown it). This
122 * method only works when the Classpath CORBA implementation is used
123 * (otherwise it return without action). The method is called from the close()
124 * method of the created context.
126 * @param orb
127 * the ORB that maybe is no longer referenced.
129 public void checkIfReferenced(ORB orb)
131 synchronized (orbs)
133 // We can only do this with the Classpath implementation.
134 if (orb instanceof OrbFunctional)
136 OrbFunctional cOrb = (OrbFunctional) orb;
137 // If there are no connected objects, we can destroy the orb.
138 if (cOrb.countConnectedObjects() == 0)
140 cOrb.shutdown(false);
141 cOrb.destroy();
143 Enumeration keys = orbs.keys();
144 Object key;
145 Remove: while (keys.hasMoreElements())
147 key = keys.nextElement();
148 if (orbs.get(key) == orb)
150 orbs.remove(key);
151 break Remove;
160 * Get all properties.
162 public String getOrbSignature(Map props)
164 TreeMap map = new TreeMap();
165 map.putAll(props);
166 CPStringBuilder b = new CPStringBuilder(50*props.size());
168 Iterator iter = map.entrySet().iterator();
169 Map.Entry m;
170 while (iter.hasNext())
172 m = (Map.Entry) iter.next();
173 b.append(m.getKey());
174 b.append('=');
175 b.append(m.getValue());
177 return b.toString();