test/java/rmi/activation/Activatable/checkAnnotations/CheckAnnotations.java

Print this page




  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 /* @test
  25  * @bug 4109103
  26  * @summary rmid should annotate child process output
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID MyRMI CheckAnnotations_Stub
  32  * @run main/othervm/policy=security.policy/timeout=480 CheckAnnotations
  33  */
  34 
  35 import java.io.*;
  36 import java.rmi.*;
  37 import java.rmi.server.*;
  38 import java.rmi.activation.*;
  39 import java.security.CodeSource;
  40 import java.util.Properties;
  41 import java.util.StringTokenizer;




  42 
  43 
  44 public class CheckAnnotations
  45     extends Activatable implements MyRMI, Runnable
  46 {
  47 
  48     private static Object dummy = new Object();
  49     private static MyRMI myRMI = null;
































  50 
  51     // buffers to store rmid output.
  52     private static ByteArrayOutputStream rmidOut = new ByteArrayOutputStream();
  53     private static ByteArrayOutputStream rmidErr = new ByteArrayOutputStream();



  54 
  55     public static void main(String args[]) {
  56         /*
  57          * The following line is required with the JDK 1.2 VM so that the
  58          * VM can exit gracefully when this test completes.  Otherwise, the
  59          * conservative garbage collector will find a handle to the server
  60          * object on the native stack and not clear the weak reference to
  61          * it in the RMI runtime's object table.
  62          */
  63         Object dummy1 = new Object();
  64         RMID rmid = null;
  65 
  66         System.err.println("\nRegression test for bug/rfe 4109103\n");
  67 
  68         try {
  69 
  70             // Set security manager according to the
  71             // testlibrary.
  72             TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
  73 
  74             // start an rmid.
  75             RMID.removeLog();
  76             rmid = RMID.createRMID(rmidOut, rmidErr, false);
  77             rmid.start();
  78 
  79             /* Cause activation groups to have a security policy that will
  80              * allow security managers to be downloaded and installed
  81              */
  82             Properties p = new Properties();
  83             // this test must always set policies/managers in its
  84             // activation groups
  85             p.put("java.security.policy",
  86                   TestParams.defaultGroupPolicy);
  87             p.put("java.security.manager",
  88                   TestParams.defaultSecurityManager);
  89 
  90             /* new desc - we will reuse in order to get multiple vms.*/
  91             System.err.println("Create activation group in this VM");
  92             ActivationGroupDesc groupDesc =
  93                 new ActivationGroupDesc(p, null);
  94             ActivationSystem system = ActivationGroup.getSystem();
  95             ActivationGroupID groupID = system.registerGroup(groupDesc);
  96             ActivationGroup.createGroup(groupID, groupDesc, 0);
  97 
  98             ActivationDesc desc = new ActivationDesc
  99                 ("CheckAnnotations", null, null);
 100             myRMI = (MyRMI) Activatable.register(desc);
 101 
 102             /* The test-
 103              * Loop a bunch of times to force activator to
 104              * spawn VMs (groups)
 105              */
 106             for (int i = 0; i < 3; i++) {
 107 
 108                 // object activated in annotation check via method call
 109                 if(!checkAnnotations(i-1)) {
 110                     TestLibrary.bomb("Test failed: output improperly annotated.");
 111                 }
 112 
 113                 /*
 114                  * Clean up object too.
 115                  */
 116                 System.err.println
 117                     ("Deactivate object via method call");
 118                 myRMI.shutdown();
 119             }
 120             System.err.println
 121                 ("\nsuccess: CheckAnnotations test passed ");
 122 
 123         } catch (Exception e) {
 124             TestLibrary.bomb("\nfailure: unexpected exception ", e);
 125         } finally {
 126             try {
 127                 Thread.sleep(4000);
 128             } catch (InterruptedException e) {
 129             }
 130 
 131             myRMI = null;
 132             System.err.println("rmid shut down");
 133             ActivationLibrary.rmidCleanup(rmid);
 134         }
 135     }
 136 
 137     /**
 138      * check to make sure that the output from a spawned vm is
 139      * formatted/annotated properly.




 140      */
 141     public static boolean checkAnnotations(int iteration)
 142         throws IOException
 143     {
 144         try {
 145             Thread.sleep(5000);
 146         } catch(Exception e) {
 147             System.err.println(e.getMessage());
 148         }
 149 
 150         /**
 151          * cause the spawned vm to generate output that will
 152          * be checked for proper annotation.  printOut is
 153          * actually being called on an activated implementation.
 154          */
 155         myRMI.printOut("out" + iteration);
 156         myRMI.printErr("err" + iteration);
 157         myRMI.printOut("out" + iteration);
 158         myRMI.printErr("err" + iteration);
 159 
 160         /* we have to wait for output to filter down
 161          * from children so we can read it before we
 162          * kill rmid.
 163          */
 164 
 165         String outString = null;
 166         String errString = null;
 167 
 168         for (int i = 0 ; i < 5 ; i ++ ) {
 169             // have to give output from rmid time to trickle down to
 170             // this process














 171             try {
 172                 Thread.sleep(4000);
 173             } catch(InterruptedException e) {










 174             }
 175 
 176             outString = rmidOut.toString();
 177             errString = rmidErr.toString();
 178 
 179             if ((!outString.equals("")) &&
 180                 (!errString.equals("")))
 181             {








 182                 System.err.println("obtained annotations");
 183                 break;
 184             }
 185             System.err.println("rmid output not yet received, retrying...");
 186         }
 187 
 188         rmidOut.reset();
 189         rmidErr.reset();
 190 
 191         // only test when we are annotating..., first run does not annotate
 192         if (iteration >= 0) {
 193             System.err.println("Checking annotations...");
 194             System.err.println(outString);
 195             System.err.println(errString);
 196 
 197             StringTokenizer stOut = new StringTokenizer(outString, ":");
 198             StringTokenizer stErr = new StringTokenizer(errString, ":");
 199 
 200             String execErr = null;
 201             String execOut = null;
 202             String destOut = null;
 203             String destErr = null;
 204             String outTmp  = null;
 205             String errTmp  = null;
 206 
 207             while (stOut.hasMoreTokens()) {
 208                 execOut = outTmp;
 209                 outTmp  = destOut;
 210                 destOut = stOut.nextToken();
 211             }
 212             while (stErr.hasMoreTokens()) {
 213                 execErr = errTmp;
 214                 errTmp  = destErr;
 215                 destErr = stErr.nextToken();
 216             }
 217 
 218             if ((execErr == null)||(errTmp == null)||
 219                 (destErr == null)) {
 220                 return false;
 221             }
 222             if ((execOut == null)||(outTmp == null)||
 223                 (destOut == null)) {
 224                 return false;
 225             }
 226 
 227             // just make sure that last two strings are what we expect.
 228             if (execOut.equals("ExecGroup-" + iteration)
 229                 && (new String(destOut.substring(0,4)).equals("out" +
 230                                                               iteration))
 231                 && (execErr.equals("ExecGroup-"+iteration))
 232                 && (new String(destErr.substring(0,4)).equals("err" +
 233                                                               iteration)) ) {
 234                 return true;
 235             } else {
 236                 return false;
 237             }
 238         }
 239         return true;
 240     }
 241 
 242     // implementation of MyRMI, make this object activatable.
 243     public CheckAnnotations
 244         (ActivationID id, MarshalledObject mo)
 245      throws RemoteException {
 246 
 247         // register/export anonymously
 248         super(id,0);
 249     }
 250 

 251     public void printOut(String toPrint) {
 252         System.out.println(toPrint);
 253     }
 254 

 255     public void printErr(String toPrint) {
 256         System.err.println(toPrint);
 257     }
 258 
 259     /**
 260      * Spawns a thread to deactivate the object.

 261      */

 262     public void shutdown() throws Exception {
 263         (new Thread(this,"CheckAnnotations")).start();
 264     }
 265 
 266     /**
 267      * Thread to deactivate object. First attempts to make object
 268      * inactive (via the inactive method).  If that fails (the
 269      * object may still have pending/executing calls), then
 270      * unexport the object forcibly.
 271      */

 272     public void run() {
 273         ActivationLibrary.deactivate(this, getID());
 274     }
 275 }


  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 /* @test
  25  * @bug 4109103
  26  * @summary rmid should annotate child process output
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID MyRMI CheckAnnotations_Stub
  32  * @run main/othervm/policy=security.policy/timeout=480 CheckAnnotations
  33  */
  34 
  35 import java.io.*;
  36 import java.rmi.*;

  37 import java.rmi.activation.*;

  38 import java.util.Properties;
  39 import java.util.StringTokenizer;
  40 import java.util.concurrent.TimeUnit;
  41 import java.util.concurrent.locks.Condition;
  42 import java.util.concurrent.locks.Lock;
  43 import java.util.concurrent.locks.ReentrantLock;
  44 
  45 
  46 public class CheckAnnotations
  47     extends Activatable implements MyRMI, Runnable
  48 {


  49     private static MyRMI myRMI = null;
  50     static class NotifyOutputStream extends ByteArrayOutputStream {
  51         final Lock lock;
  52         final Condition signal;
  53         
  54         NotifyOutputStream(Lock lock, Condition signal) {
  55             this.lock = lock;
  56             this.signal = signal;
  57         }
  58 
  59         @Override
  60         public synchronized void write(int b) {
  61             lock.lock();
  62             try{
  63                 super.write(b);
  64                 signal.signal();
  65             } finally {
  66                 lock.unlock();
  67             }
  68         }
  69         
  70         @Override
  71         public synchronized void write(byte b[], int off, int len) {
  72             lock.lock();
  73             try{
  74                 super.write(b, off, len);
  75                 if (len != 0)
  76                     signal.signal();
  77             } finally {
  78                 lock.unlock();
  79             }
  80         }
  81     }
  82 
  83     // buffers to store rmid output.
  84     private static final Lock lock = new ReentrantLock();
  85     private static final Condition writeOutSig = lock.newCondition();
  86     private static final Condition writeErrSig = lock.newCondition();
  87     private static final NotifyOutputStream rmidOut = new NotifyOutputStream(lock, writeOutSig);
  88     private static final NotifyOutputStream rmidErr = new NotifyOutputStream(lock, writeErrSig);
  89 
  90     public static void main(String args[]) {








  91         RMID rmid = null;
  92 
  93         System.err.println("\nRegression test for bug/rfe 4109103\n");
  94 
  95         try {
  96 
  97             // Set security manager according to the
  98             // testlibrary.
  99             TestLibrary.suggestSecurityManager(TestParams.defaultSecurityManager);
 100 
 101             // start an rmid.
 102             RMID.removeLog();
 103             rmid = RMID.createRMID(rmidOut, rmidErr, false);
 104             rmid.start();
 105 
 106             /* Cause activation groups to have a security policy that will
 107              * allow security managers to be downloaded and installed
 108              */
 109             Properties p = new Properties();
 110             // this test must always set policies/managers in its
 111             // activation groups
 112             p.put("java.security.policy",
 113                   TestParams.defaultGroupPolicy);
 114             p.put("java.security.manager",
 115                   TestParams.defaultSecurityManager);
 116 
 117             /* new desc - we will reuse in order to get multiple vms.*/
 118             System.err.println("Create activation group in this VM");
 119             ActivationGroupDesc groupDesc =
 120                 new ActivationGroupDesc(p, null);
 121             ActivationSystem system = ActivationGroup.getSystem();
 122             ActivationGroupID groupID = system.registerGroup(groupDesc);
 123             ActivationGroup.createGroup(groupID, groupDesc, 0);
 124 
 125             ActivationDesc desc = new ActivationDesc
 126                 ("CheckAnnotations", null, null);
 127             myRMI = (MyRMI) Activatable.register(desc);

 128             /* The test-
 129              * Loop a bunch of times to force activator to
 130              * spawn VMs (groups)
 131              */
 132             for (int i = 0; i < 3; i++) {
 133 
 134                 // object activated in annotation check via method call
 135                 if(!checkAnnotations(i-1)) {
 136                     TestLibrary.bomb("Test failed: output improperly annotated.");
 137                 }

 138                 /*
 139                  * Clean up object too.
 140                  */
 141                 System.err.println
 142                     ("Deactivate object via method call");
 143                 myRMI.shutdown();
 144             }
 145             System.err.println
 146                 ("\nsuccess: CheckAnnotations test passed ");
 147 
 148         } catch (Exception e) {
 149             TestLibrary.bomb("\nfailure: unexpected exception ", e);
 150         } finally {
 151             try {
 152                 Thread.sleep(4000);
 153             } catch (InterruptedException ignore) {
 154             }

 155             myRMI = null;
 156             System.err.println("rmid shut down");
 157             ActivationLibrary.rmidCleanup(rmid);
 158         }
 159     }
 160 
 161     /**
 162      * check to make sure that the output from a spawned vm is
 163      * formatted/annotated properly.
 164      * @param iteration
 165      * @return 
 166      * @throws java.io.IOException 
 167      * @throws java.lang.InterruptedException 
 168      */
 169     public static boolean checkAnnotations(int iteration)
 170         throws IOException, InterruptedException
 171     {
 172         try {
 173             Thread.sleep(1000);
 174         } catch(InterruptedException e) {
 175             System.err.println(e.getMessage());
 176         }

 177         /**
 178          * cause the spawned vm to generate output that will
 179          * be checked for proper annotation.  printOut is
 180          * actually being called on an activated implementation.
 181          */
 182         myRMI.printOut("out" + iteration);
 183         myRMI.printErr("err" + iteration);
 184         myRMI.printOut("out" + iteration);
 185         myRMI.printErr("err" + iteration);
 186 
 187         /* we have to wait for output to filter down
 188          * from children so we can read it before we
 189          * kill rmid.
 190          */
 191 
 192         String outString, errString;

 193         
 194         class WaitThread extends Thread{
 195             final Lock lock;
 196             final Condition signal;
 197             final long timeout;
 198             WaitThread(Lock lock, Condition signal, long timeout) {
 199                 this.lock = lock;
 200                 this.signal = signal;
 201                 this.timeout = timeout;
 202             }
 203 
 204             @Override
 205             public void run(){
 206                 long goTime = timeout;
 207                 lock.lock();
 208                 try{
 209                     while(goTime > 0L) {
 210                         long start = System.currentTimeMillis();
 211                         try {
 212                             if(signal.await(goTime, TimeUnit.MILLISECONDS)) {
 213                                 break;
 214                             }
 215                         } catch (InterruptedException e) {
 216                             throw new RuntimeException("Signal interrupt unexpected:" + e);
 217                         }
 218                         goTime =  timeout - System.currentTimeMillis() + start;
 219                     }
 220                 } finally {
 221                     lock.unlock();
 222                 }
 223             }
 224         }

 225         outString = rmidOut.toString();
 226         errString = rmidErr.toString();
 227         if ((!"".equals(outString)) && (!"".equals(errString))) {
 228             System.err.println("obtained annotations");
 229         } else { 
 230             WaitThread waitOutT = new WaitThread(lock, writeOutSig, 10000);
 231             WaitThread waitErrT = new WaitThread(lock, writeErrSig, 10000);
 232             waitOutT.start();
 233             waitErrT.start();
 234             waitOutT.join();
 235             waitErrT.join();
 236             outString = rmidOut.toString();
 237             errString = rmidErr.toString();
 238             if ((!"".equals(outString)) && (!"".equals(errString))) {
 239                 System.err.println("obtained annotations");

 240             }

 241         }
 242 
 243         rmidOut.reset();
 244         rmidErr.reset();

 245         // only test when we are annotating..., first run does not annotate
 246         if (iteration >= 0) {
 247             System.err.println("Checking annotations...");
 248             System.err.println(outString);
 249             System.err.println(errString);
 250 
 251             StringTokenizer stOut = new StringTokenizer(outString, ":");
 252             StringTokenizer stErr = new StringTokenizer(errString, ":");
 253 
 254             String execErr = null;
 255             String execOut = null;
 256             String destOut = null;
 257             String destErr = null;
 258             String outTmp  = null;
 259             String errTmp  = null;
 260 
 261             while (stOut.hasMoreTokens()) {
 262                 execOut = outTmp;
 263                 outTmp  = destOut;
 264                 destOut = stOut.nextToken();
 265             }
 266             while (stErr.hasMoreTokens()) {
 267                 execErr = errTmp;
 268                 errTmp  = destErr;
 269                 destErr = stErr.nextToken();
 270             }
 271 
 272             if ((execErr == null)||(errTmp == null)||
 273                 (destErr == null)) {
 274                 return false;
 275             }
 276             if ((execOut == null)||(outTmp == null)||
 277                 (destOut == null)) {
 278                 return false;
 279             }

 280             // just make sure that last two strings are what we expect.
 281             if (execOut.equals("ExecGroup-" + iteration)
 282                 && (new String(destOut.substring(0,4)).equals("out" +
 283                                                               iteration))
 284                 && (execErr.equals("ExecGroup-"+iteration))
 285                 && (new String(destErr.substring(0,4)).equals("err" +
 286                                                               iteration)) ) {
 287                 return true;
 288             } else {
 289                 return false;
 290             }
 291         }
 292         return true;
 293     }
 294 
 295     // implementation of MyRMI, make this object activatable.
 296     public CheckAnnotations
 297         (ActivationID id, MarshalledObject mo)
 298      throws RemoteException {
 299 
 300         // register/export anonymously
 301         super(id,0);
 302     }
 303 
 304     @Override
 305     public void printOut(String toPrint) {
 306         System.out.println(toPrint);
 307     }
 308 
 309     @Override
 310     public void printErr(String toPrint) {
 311         System.err.println(toPrint);
 312     }
 313 
 314     /**
 315      * Spawns a thread to deactivate the object.
 316      * @throws java.lang.Exception
 317      */
 318     @Override
 319     public void shutdown() throws Exception {
 320         (new Thread(this,"CheckAnnotations")).start();
 321     }
 322 
 323     /**
 324      * Thread to deactivate object. First attempts to make object
 325      * inactive (via the inactive method).  If that fails (the
 326      * object may still have pending/executing calls), then
 327      * unexport the object forcibly.
 328      */
 329     @Override
 330     public void run() {
 331         ActivationLibrary.deactivate(this, getID());
 332     }
 333 }