1 /*
   2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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 /**
  25  *
  26  */
  27 
  28 import java.io.File;
  29 import java.rmi.Naming;
  30 import java.rmi.NoSuchObjectException;
  31 import java.rmi.NotBoundException;
  32 import java.rmi.Remote;
  33 import java.rmi.activation.Activatable;
  34 import java.rmi.activation.ActivationID;
  35 import java.rmi.activation.ActivationSystem;
  36 import java.rmi.registry.LocateRegistry;
  37 
  38 /**
  39  * Class of test utility/library methods related to Activatable
  40  * objects.
  41  */
  42 public class ActivationLibrary {
  43     private static void mesg(Object mesg) {
  44         System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());
  45     }
  46 
  47     /**
  48      * Deactivate an activated Activatable
  49      */
  50     public static void deactivate(Remote remote,
  51                                   ActivationID id) {
  52         final long POLLTIME_MS = 100L;
  53         final long DEACTIVATE_TIME_MS = 30_000L;
  54 
  55         long startTime = System.currentTimeMillis();
  56         long deadline = TestLibrary.computeDeadline(startTime, DEACTIVATE_TIME_MS);
  57 
  58         while (System.currentTimeMillis() < deadline) {
  59             try {
  60                 if (Activatable.inactive(id) == true) {
  61                     mesg("inactive successful");
  62                     return;
  63                 } else {
  64                     Thread.sleep(POLLTIME_MS);
  65                 }
  66             } catch (InterruptedException e) {
  67                 Thread.currentThread().interrupt();
  68                 mesg("Thread interrupted while trying to deactivate activatable. Exiting deactivation");
  69                 return;
  70             } catch (Exception e) {
  71                 try {
  72                     // forcibly unexport the object
  73                     mesg("Unexpected exception. Have to forcibly unexport the object." +
  74                          " Exception was :");
  75                     e.printStackTrace();
  76                     Activatable.unexportObject(remote, true);
  77                 } catch (NoSuchObjectException ex) {
  78                 }
  79                 return;
  80             }
  81         }
  82 
  83         mesg("unable to inactivate after " +
  84             (System.currentTimeMillis() - startTime) + "ms.");
  85         mesg("unexporting object forcibly instead");
  86 
  87         try {
  88             Activatable.unexportObject(remote, true);
  89         } catch (NoSuchObjectException e) {
  90         }
  91     }
  92 }