001 /*
002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.11/src/java/org/apache/commons/ssl/rmi/Test.java $
003 * $Revision: 121 $
004 * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 2007) $
005 *
006 * ====================================================================
007 * Licensed to the Apache Software Foundation (ASF) under one
008 * or more contributor license agreements. See the NOTICE file
009 * distributed with this work for additional information
010 * regarding copyright ownership. The ASF licenses this file
011 * to you under the Apache License, Version 2.0 (the
012 * "License"); you may not use this file except in compliance
013 * with the License. You may obtain a copy of the License at
014 *
015 * http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing,
018 * software distributed under the License is distributed on an
019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020 * KIND, either express or implied. See the License for the
021 * specific language governing permissions and limitations
022 * under the License.
023 * ====================================================================
024 *
025 * This software consists of voluntary contributions made by many
026 * individuals on behalf of the Apache Software Foundation. For more
027 * information on the Apache Software Foundation, please see
028 * <http://www.apache.org/>.
029 *
030 */
031
032 package org.apache.commons.ssl.rmi;
033
034 import org.apache.commons.ssl.LogWrapper;
035 import org.apache.commons.ssl.RMISocketFactoryImpl;
036
037 import java.net.MalformedURLException;
038 import java.rmi.Naming;
039 import java.rmi.NotBoundException;
040 import java.rmi.Remote;
041 import java.rmi.RemoteException;
042 import java.rmi.registry.LocateRegistry;
043 import java.rmi.server.RMISocketFactory;
044
045 /**
046 * @author Credit Union Central of British Columbia
047 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
048 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
049 * @since 22-Feb-2007
050 */
051 public class Test {
052 private final static LogWrapper log = LogWrapper.getLogger(Test.class);
053 private final static String TEST_DATE_NAME = "/org.apache.commons.ssl.rmi.testdate";
054 private final static String TEST_INT_NAME = "/org.apache.commons.ssl.rmi.testint";
055 protected final static int PORT;
056 protected final static String URL;
057
058 private static boolean rmiRunning = false;
059
060 static {
061 int port = 1099;
062 String host = "127.0.0.1";
063 PORT = port;
064 // e.g. "rmi://localhost:1099/"
065 URL = "rmi://" + host + ":" + port;
066 }
067
068 /**
069 * <p/>
070 * JNDI/RMI lookup wrapper. Appends "java:" if we expect
071 * binding/lookup to occur in the same JVM. Otherwise, appends "rmi:".
072 * </p>
073 *
074 * @param ref String reference.
075 * @return Object Object previously bound with String reference.
076 * @throws java.rmi.RemoteException rmi problem
077 * @throws java.rmi.NotBoundException rmi problem
078 * @throws java.net.MalformedURLException rmi problem
079 */
080 public static Object lookup(String ref)
081 throws RemoteException, NotBoundException, MalformedURLException {
082 return Naming.lookup(URL + ref);
083 }
084
085 /**
086 * <p/>
087 * JNDI/RMI rebind wrapper for the UCS. Appends "java:" if we expect
088 * binding/lookup to occur in the same JVM. Otherwise, append "rmi:".
089 * </p><p>
090 * Also attempts to start a naming server on the localhost if one is
091 * not already running. Currently we use RMI.
092 * </p>
093 *
094 * @param ref String reference to bind with.
095 * @param obj Object to bind.
096 * @throws java.rmi.RemoteException rmi problem
097 * @throws java.net.MalformedURLException rmi problem
098 */
099 public static void rebind(String ref, Remote obj)
100 throws RemoteException, MalformedURLException {
101 requireNameServer();
102 String realRef = URL + ref;
103 Naming.rebind(realRef, obj);
104 try {
105 Object o = lookup(ref);
106 log.debug("Bound " + o.getClass().getName() + " to [" + realRef + "]");
107 }
108 catch (NotBoundException nbe) {
109 log.debug("Error binding " + obj.getClass().getName() + " to [" + realRef + "]");
110 }
111 }
112
113 private static void rebindTest() throws Exception {
114 Remote remoteTest = new DateRMI();
115 Naming.rebind(URL + TEST_DATE_NAME, remoteTest);
116 Object o = Naming.lookup(URL + TEST_DATE_NAME);
117 if (!remoteTest.equals(o)) {
118 throw new RuntimeException("rmi: Test failed. Lookup != Rebind");
119 }
120 }
121
122 /**
123 * <p/>
124 * Attempts to start a naming server on the localhost if one is not
125 * already running.
126 * </p>
127 */
128 private synchronized static void requireNameServer() {
129 if (rmiRunning) {
130 // We've already established that the name server is running.
131 return;
132 }
133 try {
134 // If this rebind works, then the naming server is running.
135 rebindTest();
136 rmiRunning = true;
137 }
138 catch (Exception e) {
139 Test.tryToStartNameServer();
140 try {
141 // Okay, we've started our naming server. Now we must perform a
142 // quick test to see that it's actually doing something.
143 rebindTest();
144 log.debug(Test.class.getName() + " successfully started.");
145 rmiRunning = true;
146 return;
147 }
148 catch (Exception e2) {
149 e2.printStackTrace();
150 log.error(e2.getMessage(), e2);
151 }
152
153 String msg = Test.class.getName() + " cannot start.";
154 log.error(msg);
155 throw new RuntimeException(msg);
156 }
157 }
158
159 public static void tryToStartNameServer() {
160 String className = Test.class.getName();
161 log.debug(className + " probably not running. Trying to start one.");
162 try {
163 LocateRegistry.createRegistry(PORT);
164 log.debug("registry on " + PORT + " started!");
165 }
166 catch (Exception problem) {
167 // bah - no luck
168 problem.printStackTrace();
169 log.warn(problem, problem);
170 }
171 }
172
173
174 public static void main(String[] args) throws Exception {
175 System.setProperty(RMISocketFactoryImpl.RMI_HOSTNAME_KEY, "localhost");
176 RMISocketFactoryImpl impl = new RMISocketFactoryImpl();
177 RMISocketFactory.setSocketFactory(impl);
178
179 if (args.length > 0) {
180
181 } else {
182 Test.requireNameServer();
183 Test.rebindTest();
184
185 IntegerRMI remoteInt = new IntegerRMI();
186 Test.rebind(TEST_INT_NAME, remoteInt);
187 }
188
189 Object o = Test.lookup(TEST_DATE_NAME);
190 RemoteDate rd = (RemoteDate) o;
191 System.out.println("The remote-date is: " + rd.getDate());
192
193 o = Test.lookup(TEST_INT_NAME);
194 RemoteInteger ri = (RemoteInteger) o;
195 System.out.println("The remote-int is: " + ri.getInt());
196
197 }
198
199
200 }