Frames | No Frames |
1: /* GiopNamingServiceFactory.java -- handles corbaname: urls 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.javax.naming.giop; 40: 41: import gnu.CORBA.OrbFunctional; 42: 43: import gnu.java.lang.CPStringBuilder; 44: 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; 51: 52: import javax.naming.Context; 53: import javax.naming.Name; 54: 55: import org.omg.CORBA.ORB; 56: 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. 64: * 65: * @author Audrius Meskauskas (audriusa@Bioinformatics.org) 66: */ 67: public class GiopNamingServiceFactory 68: { 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 72: */ 73: public static final String DEFAULT_PROVIDER = 74: "corbaloc:iiop:1.2@127.0.0.1:900/NameService"; 75: 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. 80: */ 81: public static Hashtable orbs = new Hashtable(); 82: 83: 84: /** 85: * Create a new instance of the corbaname URL context. 86: */ 87: public Object getObjectInstance(Object refObj, Name name, Context nameCtx, 88: Hashtable environment) 89: { 90: String provider = (String) environment.get(Context.PROVIDER_URL); 91: if (provider == null) 92: provider = DEFAULT_PROVIDER; 93: 94: String orbSignature = getOrbSignature(environment); 95: 96: ORB orb; 97: synchronized (orbs) 98: { 99: orb = (ORB) orbs.get(orbSignature); 100: if (orb == null) 101: { 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() 108: { 109: public void run() 110: { 111: runIt.run(); 112: } 113: }.start(); 114: } 115: } 116: 117: return new GiopNamingServiceURLContext(environment, this, orb); 118: } 119: 120: /** 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. 125: * 126: * @param orb 127: * the ORB that maybe is no longer referenced. 128: */ 129: public void checkIfReferenced(ORB orb) 130: { 131: synchronized (orbs) 132: { 133: // We can only do this with the Classpath implementation. 134: if (orb instanceof OrbFunctional) 135: { 136: OrbFunctional cOrb = (OrbFunctional) orb; 137: // If there are no connected objects, we can destroy the orb. 138: if (cOrb.countConnectedObjects() == 0) 139: { 140: cOrb.shutdown(false); 141: cOrb.destroy(); 142: 143: Enumeration keys = orbs.keys(); 144: Object key; 145: Remove: while (keys.hasMoreElements()) 146: { 147: key = keys.nextElement(); 148: if (orbs.get(key) == orb) 149: { 150: orbs.remove(key); 151: break Remove; 152: } 153: } 154: } 155: } 156: } 157: } 158: 159: /** 160: * Get all properties. 161: */ 162: public String getOrbSignature(Map props) 163: { 164: TreeMap map = new TreeMap(); 165: map.putAll(props); 166: CPStringBuilder b = new CPStringBuilder(50*props.size()); 167: 168: Iterator iter = map.entrySet().iterator(); 169: Map.Entry m; 170: while (iter.hasNext()) 171: { 172: m = (Map.Entry) iter.next(); 173: b.append(m.getKey()); 174: b.append('='); 175: b.append(m.getValue()); 176: } 177: return b.toString(); 178: } 179: }