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 java.util.Enumeration; 44: import java.util.Hashtable; 45: import java.util.Iterator; 46: import java.util.Map; 47: import java.util.Properties; 48: import java.util.TreeMap; 49: 50: import javax.naming.Context; 51: import javax.naming.Name; 52: 53: import org.omg.CORBA.ORB; 54: 55: /** 56: * The context factory to represent the corbaname: style urls. Such URL states 57: * that the CORBA naming service exists on the given host. This service can 58: * return the required object, finding it by the given name. The names are 59: * parsed using the specification of the corbaname urls. Being the naming 60: * service, the returned context supports creating the subcontexts, forwarding 61: * this task to the existing naming service. 62: * 63: * @author Audrius Meskauskas (audriusa@Bioinformatics.org) 64: */ 65: public class GiopNamingServiceFactory 66: { 67: /** 68: * The default naming service provider. It is assumed, that the naming service 69: * is running on the port 900 of the local host, using the GIOP version 1.2 70: */ 71: public static final String DEFAULT_PROVIDER = 72: "corbaloc:iiop:1.2@127.0.0.1:900/NameService"; 73: 74: /** 75: * The table of all instantiated ORB's that are found by they ORB 76: * properties signatures. If all ORB related properties are the same, 77: * the ORB's are shared. 78: */ 79: public static Hashtable orbs = new Hashtable(); 80: 81: 82: /** 83: * Create a new instance of the corbaname URL context. 84: */ 85: public Object getObjectInstance(Object refObj, Name name, Context nameCtx, 86: Hashtable environment) 87: { 88: String provider = (String) environment.get(Context.PROVIDER_URL); 89: if (provider == null) 90: provider = DEFAULT_PROVIDER; 91: 92: String orbSignature = getOrbSignature(environment); 93: 94: ORB orb; 95: synchronized (orbs) 96: { 97: orb = (ORB) orbs.get(orbSignature); 98: if (orb == null) 99: { 100: Properties props = new Properties(); 101: props.putAll(environment); 102: orb = ORB.init(new String[0], props); 103: orbs.put(orbSignature, orb); 104: final ORB runIt = orb; 105: new Thread() 106: { 107: public void run() 108: { 109: runIt.run(); 110: } 111: }.start(); 112: } 113: } 114: 115: return new GiopNamingServiceURLContext(environment, this, orb); 116: } 117: 118: /** 119: * Check if this ORB is still in use (maybe it is time to shutdown it). This 120: * method only works when the Classpath CORBA implementation is used 121: * (otherwise it return without action). The method is called from the close() 122: * method of the created context. 123: * 124: * @param orb 125: * the ORB that maybe is no longer referenced. 126: */ 127: public void checkIfReferenced(ORB orb) 128: { 129: synchronized (orbs) 130: { 131: // We can only do this with the Classpath implementation. 132: if (orb instanceof OrbFunctional) 133: { 134: OrbFunctional cOrb = (OrbFunctional) orb; 135: // If there are no connected objects, we can destroy the orb. 136: if (cOrb.countConnectedObjects() == 0) 137: { 138: cOrb.shutdown(false); 139: cOrb.destroy(); 140: 141: Enumeration keys = orbs.keys(); 142: Object key; 143: Remove: while (keys.hasMoreElements()) 144: { 145: key = keys.nextElement(); 146: if (orbs.get(key) == orb) 147: { 148: orbs.remove(key); 149: break Remove; 150: } 151: } 152: } 153: } 154: } 155: } 156: 157: /** 158: * Get all properties. 159: */ 160: public String getOrbSignature(Map props) 161: { 162: TreeMap map = new TreeMap(); 163: map.putAll(props); 164: StringBuffer b = new StringBuffer(50*props.size()); 165: 166: Iterator iter = map.entrySet().iterator(); 167: Map.Entry m; 168: while (iter.hasNext()) 169: { 170: m = (Map.Entry) iter.next(); 171: b.append(m.getKey()); 172: b.append('='); 173: b.append(m.getValue()); 174: } 175: return b.toString(); 176: } 177: }