< prev index next >

test/java/rmi/testlibrary/RMID.java

Print this page




   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;

  25 import java.rmi.*;
  26 import java.rmi.activation.*;
  27 import java.rmi.registry.*;
  28 import java.util.concurrent.TimeoutException;
  29 
  30 /**
  31  * Utility class that creates an instance of rmid with a policy
  32  * file of name <code>TestParams.defaultPolicy</code>.
  33  *
  34  * Activation groups should run with the same security manager as the
  35  * test.
  36  */
  37 public class RMID extends JavaVM {
  38 
  39     // TODO: adjust these based on the timeout factor
  40     // such as jcov.sleep.multiplier; see start(long) method.
  41     // Also consider the test.timeout.factor property (a float).
  42     private static final long TIMEOUT_SHUTDOWN_MS = 60_000L;
  43     private static final long TIMEOUT_DESTROY_MS  = 10_000L;
  44     private static final long STARTTIME_MS        = 15_000L;
  45     private static final long POLLTIME_MS         = 100L;
  46 
  47     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
  48         // "java.rmi.activation.ActivationSystem"
  49 
  50     public static String MANAGER_OPTION="-Djava.security.manager=";
  51 
  52     /** Test port for rmid */
  53     private final int port;
  54 
  55     /** Initial log name */
  56     protected static String log = "log";
  57     /** rmid's logfile directory; currently must be "." */
  58     protected static String LOGDIR = ".";
  59 
  60     private static void mesg(Object mesg) {
  61         System.err.println("RMID: " + mesg.toString());
  62     }
  63 
  64     /** make test options and arguments */
  65     private static String makeOptions(boolean debugExec) {


 164         String args = makeArgs(includePortArg, port);
 165         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
 166                              out, err, port);
 167         rmid.setPolicyFile(TestParams.defaultRmidPolicy);
 168 
 169         return rmid;
 170     }
 171 
 172 
 173     /**
 174      * Private constructor. RMID instances should be created
 175      * using the static factory methods.
 176      */
 177     private RMID(String classname, String options, String args,
 178                    OutputStream out, OutputStream err, int port)
 179     {
 180         super(classname, options, args, out, err);
 181         this.port = port;
 182     }
 183 







 184     /**
 185      * Removes rmid's log file directory.
 186      */
 187     public static void removeLog() {
 188         File f = new File(LOGDIR, log);
 189 
 190         if (f.exists()) {
 191             mesg("Removing rmid's old log file.");
 192             String[] files = f.list();
 193 
 194             if (files != null) {
 195                 for (int i=0; i<files.length; i++) {
 196                     (new File(f, files[i])).delete();
 197                 }
 198             }
 199 
 200             if (! f.delete()) {
 201                 mesg("Warning: unable to delete old log file.");
 202             }
 203         }


 212     protected static String getCodeCoverageArgs() {
 213         return TestLibrary.getExtraProperty("rmid.jcov.args","");
 214     }
 215 
 216     /**
 217      * Looks up the activation system in the registry on the given port,
 218      * returning its stub, or null if it's not present. This method differs from
 219      * ActivationGroup.getSystem() because this method looks on a specific port
 220      * instead of using the java.rmi.activation.port property like
 221      * ActivationGroup.getSystem() does. This method also returns null instead
 222      * of throwing exceptions.
 223      */
 224     public static ActivationSystem lookupSystem(int port) {
 225         try {
 226             return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
 227         } catch (RemoteException | NotBoundException ex) {
 228             return null;
 229         }
 230     }
 231 

















 232     /**
 233      * Starts rmid and waits up to the default timeout period
 234      * to confirm that it's running.
 235      */
 236     public void start() throws IOException {
 237         start(STARTTIME_MS);
 238     }
 239 
 240     /**
 241      * Starts rmid and waits up to the given timeout period
 242      * to confirm that it's running.
 243      */
 244     public void start(long waitTime) throws IOException {
 245 
 246         // if rmid is already running, then the test will fail with
 247         // a well recognized exception (port already in use...).
 248 
 249         mesg("Starting rmid on port " + port + ".");
 250         super.start();
 251 


 253         // try {
 254         //     slopFactor = Integer.valueOf(
 255         //         TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 256         // } catch (NumberFormatException ignore) {}
 257         // waitTime = waitTime * slopFactor;
 258 
 259         long startTime = System.currentTimeMillis();
 260         long deadline = TestLibrary.computeDeadline(startTime, waitTime);
 261 
 262         while (true) {
 263             try {
 264                 Thread.sleep(POLLTIME_MS);
 265             } catch (InterruptedException ie) {
 266                 Thread.currentThread().interrupt();
 267                 mesg("Starting rmid interrupted, giving up at " +
 268                     (System.currentTimeMillis() - startTime) + "ms.");
 269                 return;
 270             }
 271 
 272             try {

 273                 int status = vm.exitValue();
 274                 TestLibrary.bomb("Rmid process exited with status " + status + " after " +
 275                     (System.currentTimeMillis() - startTime) + "ms.");
 276             } catch (IllegalThreadStateException ignore) { }
 277 
 278             // The rmid process is alive; check to see whether
 279             // it responds to a remote call.
 280 
 281             if (lookupSystem(port) != null) {
 282                 /*
 283                  * We need to set the java.rmi.activation.port value as the
 284                  * activation system will use the property to determine the
 285                  * port #.  The activation system will use this value if set.
 286                  * If it isn't set, the activation system will set it to an
 287                  * incorrect value.
 288                  */
 289                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 290                 mesg("Started successfully after " +
 291                     (System.currentTimeMillis() - startTime) + "ms.");
 292                 return;
 293             }
 294 
 295             if (System.currentTimeMillis() > deadline) {

 296                 TestLibrary.bomb("Failed to start rmid, giving up after " +
 297                     (System.currentTimeMillis() - startTime) + "ms.", null);
 298             }
 299         }
 300     }
 301 
 302     /**
 303      * Destroys rmid and restarts it. Note that this does NOT clean up
 304      * the log file, because it stores information about restartable
 305      * and activatable objects that must be carried over to the new
 306      * rmid instance.
 307      */
 308     public void restart() throws IOException {
 309         destroy();
 310         start();
 311     }
 312 
 313     /**
 314      * Ask rmid to shutdown gracefully using a remote method call.
 315      * catch any errors that might occur from rmid not being present




   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.net.BindException;
  26 import java.rmi.*;
  27 import java.rmi.activation.*;
  28 import java.rmi.registry.*;
  29 import java.util.concurrent.TimeoutException;
  30 
  31 /**
  32  * Utility class that creates an instance of rmid with a policy
  33  * file of name <code>TestParams.defaultPolicy</code>.
  34  *
  35  * Activation groups should run with the same security manager as the
  36  * test.
  37  */
  38 public class RMID extends JavaVM {
  39 
  40     // TODO: adjust these based on the timeout factor
  41     // such as jcov.sleep.multiplier; see start(long) method.
  42     // Also consider the test.timeout.factor property (a float).
  43     private static final long TIMEOUT_SHUTDOWN_MS = 60_000L;
  44     private static final long TIMEOUT_DESTROY_MS  = 10_000L;
  45     private static final long STARTTIME_MS        = 15_000L;
  46     private static final long POLLTIME_MS         = 2000L;
  47 
  48     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
  49         // "java.rmi.activation.ActivationSystem"
  50 
  51     public static String MANAGER_OPTION="-Djava.security.manager=";
  52 
  53     /** Test port for rmid */
  54     private final int port;
  55 
  56     /** Initial log name */
  57     protected static String log = "log";
  58     /** rmid's logfile directory; currently must be "." */
  59     protected static String LOGDIR = ".";
  60 
  61     private static void mesg(Object mesg) {
  62         System.err.println("RMID: " + mesg.toString());
  63     }
  64 
  65     /** make test options and arguments */
  66     private static String makeOptions(boolean debugExec) {


 165         String args = makeArgs(includePortArg, port);
 166         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
 167                              out, err, port);
 168         rmid.setPolicyFile(TestParams.defaultRmidPolicy);
 169 
 170         return rmid;
 171     }
 172 
 173 
 174     /**
 175      * Private constructor. RMID instances should be created
 176      * using the static factory methods.
 177      */
 178     private RMID(String classname, String options, String args,
 179                    OutputStream out, OutputStream err, int port)
 180     {
 181         super(classname, options, args, out, err);
 182         this.port = port;
 183     }
 184 
 185     private void checkAddressInUser() throws BindException {
 186         if (outputContains("Address already in use")
 187                 || outputContains("Port already in use")) {
 188             throw new BindException("Address already in use at port: " + port);
 189         }
 190     }
 191 
 192     /**
 193      * Removes rmid's log file directory.
 194      */
 195     public static void removeLog() {
 196         File f = new File(LOGDIR, log);
 197 
 198         if (f.exists()) {
 199             mesg("Removing rmid's old log file.");
 200             String[] files = f.list();
 201 
 202             if (files != null) {
 203                 for (int i=0; i<files.length; i++) {
 204                     (new File(f, files[i])).delete();
 205                 }
 206             }
 207 
 208             if (! f.delete()) {
 209                 mesg("Warning: unable to delete old log file.");
 210             }
 211         }


 220     protected static String getCodeCoverageArgs() {
 221         return TestLibrary.getExtraProperty("rmid.jcov.args","");
 222     }
 223 
 224     /**
 225      * Looks up the activation system in the registry on the given port,
 226      * returning its stub, or null if it's not present. This method differs from
 227      * ActivationGroup.getSystem() because this method looks on a specific port
 228      * instead of using the java.rmi.activation.port property like
 229      * ActivationGroup.getSystem() does. This method also returns null instead
 230      * of throwing exceptions.
 231      */
 232     public static ActivationSystem lookupSystem(int port) {
 233         try {
 234             return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
 235         } catch (RemoteException | NotBoundException ex) {
 236             return null;
 237         }
 238     }
 239 
 240     public static RMID launch() throws IOException {
 241         RMID rmid = null;
 242         for (int i = 0; i < 20; i++) {
 243             RMID.removeLog();
 244             rmid = RMID.createRMID();
 245             try {
 246                 rmid.start();
 247                 break;
 248             } catch (BindException ex) {
 249                 System.err.format("%ncatch BindException(%s), "
 250                         + "continue to launch rmid again...%n%n", ex.getMessage());
 251                 continue;
 252             }
 253         }
 254         return rmid;
 255     }
 256 
 257     /**
 258      * Starts rmid and waits up to the default timeout period
 259      * to confirm that it's running.
 260      */
 261     public void start() throws IOException {
 262         start(STARTTIME_MS);
 263     }
 264 
 265     /**
 266      * Starts rmid and waits up to the given timeout period
 267      * to confirm that it's running.
 268      */
 269     public void start(long waitTime) throws IOException {
 270 
 271         // if rmid is already running, then the test will fail with
 272         // a well recognized exception (port already in use...).
 273 
 274         mesg("Starting rmid on port " + port + ".");
 275         super.start();
 276 


 278         // try {
 279         //     slopFactor = Integer.valueOf(
 280         //         TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 281         // } catch (NumberFormatException ignore) {}
 282         // waitTime = waitTime * slopFactor;
 283 
 284         long startTime = System.currentTimeMillis();
 285         long deadline = TestLibrary.computeDeadline(startTime, waitTime);
 286 
 287         while (true) {
 288             try {
 289                 Thread.sleep(POLLTIME_MS);
 290             } catch (InterruptedException ie) {
 291                 Thread.currentThread().interrupt();
 292                 mesg("Starting rmid interrupted, giving up at " +
 293                     (System.currentTimeMillis() - startTime) + "ms.");
 294                 return;
 295             }
 296 
 297             try {
 298                 checkAddressInUser();
 299                 int status = vm.exitValue();
 300                 TestLibrary.bomb("Rmid process exited with status " + status + " after " +
 301                     (System.currentTimeMillis() - startTime) + "ms.");
 302             } catch (IllegalThreadStateException ignore) { }
 303 
 304             // The rmid process is alive; check to see whether
 305             // it responds to a remote call.
 306 
 307             if (lookupSystem(port) != null) {
 308                 /*
 309                  * We need to set the java.rmi.activation.port value as the
 310                  * activation system will use the property to determine the
 311                  * port #.  The activation system will use this value if set.
 312                  * If it isn't set, the activation system will set it to an
 313                  * incorrect value.
 314                  */
 315                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 316                 mesg("Started successfully after " +
 317                     (System.currentTimeMillis() - startTime) + "ms.");
 318                 return;
 319             }
 320 
 321             if (System.currentTimeMillis() > deadline) {
 322                 checkAddressInUser();
 323                 TestLibrary.bomb("Failed to start rmid, giving up after " +
 324                     (System.currentTimeMillis() - startTime) + "ms.", null);
 325             }
 326         }
 327     }
 328 
 329     /**
 330      * Destroys rmid and restarts it. Note that this does NOT clean up
 331      * the log file, because it stores information about restartable
 332      * and activatable objects that must be carried over to the new
 333      * rmid instance.
 334      */
 335     public void restart() throws IOException {
 336         destroy();
 337         start();
 338     }
 339 
 340     /**
 341      * Ask rmid to shutdown gracefully using a remote method call.
 342      * catch any errors that might occur from rmid not being present


< prev index next >