001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.8.4.3 $
022: */package java.rmi.registry;
023:
024: import java.net.InetAddress;
025: import java.net.UnknownHostException;
026: import java.rmi.RemoteException;
027: import java.rmi.server.RMIClientSocketFactory;
028: import java.rmi.server.RMIServerSocketFactory;
029: import java.rmi.server.ObjID;
030: import java.rmi.server.RemoteRef;
031:
032: import org.apache.harmony.rmi.internal.nls.Messages;
033: import org.apache.harmony.rmi.registry.RegistryImpl;
034: import org.apache.harmony.rmi.remoteref.UnicastRef;
035: import org.apache.harmony.rmi.remoteref.UnicastRef2;
036:
037: /**
038: * @com.intel.drl.spec_ref
039: * This class could not be instantiated.
040: *
041: * @author Mikhail A. Markov
042: * @version $Revision: 1.8.4.3 $
043: */
044: public final class LocateRegistry {
045:
046: // This class could not be instantiated.
047: private LocateRegistry() {
048: }
049:
050: /**
051: * @com.intel.drl.spec_ref
052: */
053: public static Registry createRegistry(int port,
054: RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
055: throws RemoteException {
056: if (port < 0) {
057: // rmi.15=Port value out of range: {0}
058: throw new IllegalArgumentException(Messages.getString(
059: "rmi.15", port)); //$NON-NLS-1$
060: }
061: return new RegistryImpl(port, csf, ssf);
062: }
063:
064: /**
065: * @com.intel.drl.spec_ref
066: */
067: public static Registry getRegistry(String host, int port,
068: RMIClientSocketFactory csf) throws RemoteException {
069: if (host == null) {
070: try {
071: host = InetAddress.getLocalHost().getHostName();
072: } catch (UnknownHostException uhe) {
073: host = "localhost"; //$NON-NLS-1$
074: }
075: }
076:
077: if (port <= 0) {
078: port = Registry.REGISTRY_PORT;
079: }
080:
081: try {
082: Class regClass = Class
083: .forName("org.apache.harmony.rmi.registry.RegistryImpl_Stub"); //$NON-NLS-1$
084: RemoteRef ref;
085:
086: if (csf == null) {
087: ref = new UnicastRef(host, port, new ObjID(
088: ObjID.REGISTRY_ID));
089: } else {
090: ref = new UnicastRef2(host, port, csf, new ObjID(
091: ObjID.REGISTRY_ID));
092: }
093: return (Registry) regClass.getConstructor(
094: new Class[] { RemoteRef.class }).newInstance(
095: new Object[] { ref });
096: } catch (Exception ex) {
097: // rmi.16=Unable to get registry.
098: throw new RemoteException(Messages.getString("rmi.16"), ex); //$NON-NLS-1$
099: }
100: }
101:
102: /**
103: * @com.intel.drl.spec_ref
104: */
105: public static Registry getRegistry(String host, int port)
106: throws RemoteException {
107: return getRegistry(host, port, null);
108: }
109:
110: /**
111: * @com.intel.drl.spec_ref
112: */
113: public static Registry getRegistry(String host)
114: throws RemoteException {
115: return getRegistry(host, Registry.REGISTRY_PORT);
116: }
117:
118: /**
119: * @com.intel.drl.spec_ref
120: */
121: public static Registry getRegistry(int port) throws RemoteException {
122: return getRegistry(null, port);
123: }
124:
125: /**
126: * @com.intel.drl.spec_ref
127: */
128: public static Registry createRegistry(int port)
129: throws RemoteException {
130: return createRegistry(port, null, null);
131: }
132:
133: /**
134: * @com.intel.drl.spec_ref
135: */
136: public static Registry getRegistry() throws RemoteException {
137: return getRegistry(Registry.REGISTRY_PORT);
138: }
139: }
|