jdk/test/java/rmi/server/UnicastRemoteObject/useDynamicProxies/UseDynamicProxies.java
changeset 309 bda219d843f6
parent 2 90ce3da70b43
child 715 f16baef3a20e
equal deleted inserted replaced
308:33a1639d64a5 309:bda219d843f6
     1 /* 
     1 /*
     2  * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
    41 
    41 
    42 public class UseDynamicProxies implements RemoteInterface {
    42 public class UseDynamicProxies implements RemoteInterface {
    43 
    43 
    44 
    44 
    45     public Object passObject(Object obj) {
    45     public Object passObject(Object obj) {
    46 	return obj;
    46         return obj;
    47     }
    47     }
    48 
    48 
    49     public int passInt(int x) {
    49     public int passInt(int x) {
    50 	return x;
    50         return x;
    51     }
    51     }
    52 
    52 
    53     public String passString(String string) {
    53     public String passString(String string) {
    54 	return string;
    54         return string;
    55     }
    55     }
    56 
    56 
    57     public static void main(String[] args) throws Exception {
    57     public static void main(String[] args) throws Exception {
    58 	
       
    59 	RemoteInterface server = null;
       
    60 	RemoteInterface proxy = null;
       
    61 	
       
    62 	try {
       
    63 	    System.setProperty("java.rmi.server.ignoreStubClasses", args[0]);
       
    64 	    boolean ignoreStubClasses = Boolean.parseBoolean(args[0]);
       
    65 
    58 
    66 	    if (System.getSecurityManager() == null) {
    59         RemoteInterface server = null;
    67 		System.setSecurityManager(new SecurityManager());
    60         RemoteInterface proxy = null;
    68 	    }
       
    69 
    61 
    70 	    System.err.println("export object");
    62         try {
    71 	    server = new UseDynamicProxies();
    63             System.setProperty("java.rmi.server.ignoreStubClasses", args[0]);
    72 	    proxy =
    64             boolean ignoreStubClasses = Boolean.parseBoolean(args[0]);
    73 		(RemoteInterface) UnicastRemoteObject.exportObject(server, 0);
       
    74 
    65 
    75 	    System.err.println("proxy = " + proxy);
    66             if (System.getSecurityManager() == null) {
    76 	    if (ignoreStubClasses) {
    67                 System.setSecurityManager(new SecurityManager());
    77 		if (!Proxy.isProxyClass(proxy.getClass())) {
    68             }
    78 		    throw new RuntimeException(
       
    79 		        "server proxy is not a dynamic proxy");
       
    80 		}
       
    81 		if (!(Proxy.getInvocationHandler(proxy) instanceof
       
    82 		      RemoteObjectInvocationHandler))
       
    83 		{
       
    84 		    throw new RuntimeException("invalid invocation handler");
       
    85 		}
       
    86 		
       
    87 	    } else if (!(proxy instanceof RemoteStub)) {
       
    88 		throw new RuntimeException(
       
    89  		    "server proxy is not a RemoteStub");
       
    90 	    }
       
    91 
    69 
    92 	    System.err.println("invoke methods");
    70             System.err.println("export object");
    93 	    Object obj = proxy.passObject(proxy);
    71             server = new UseDynamicProxies();
    94 	    if (!proxy.equals(obj)) {
    72             proxy =
    95 		throw new RuntimeException("returned proxy not equal");
    73                 (RemoteInterface) UnicastRemoteObject.exportObject(server, 0);
    96 	    }
       
    97 
    74 
    98 	    int x = proxy.passInt(53);
    75             System.err.println("proxy = " + proxy);
    99 	    if (x != 53) {
    76             if (ignoreStubClasses) {
   100 		throw new RuntimeException("returned int not equal");
    77                 if (!Proxy.isProxyClass(proxy.getClass())) {
   101 	    }
    78                     throw new RuntimeException(
       
    79                         "server proxy is not a dynamic proxy");
       
    80                 }
       
    81                 if (!(Proxy.getInvocationHandler(proxy) instanceof
       
    82                       RemoteObjectInvocationHandler))
       
    83                 {
       
    84                     throw new RuntimeException("invalid invocation handler");
       
    85                 }
   102 
    86 
   103 	    String string = proxy.passString("test");
    87             } else if (!(proxy instanceof RemoteStub)) {
   104 	    if (!string.equals("test")) {
    88                 throw new RuntimeException(
   105 		throw new RuntimeException("returned string not equal");
    89                     "server proxy is not a RemoteStub");
   106 	    }
    90             }
   107 	    
    91 
   108 	    System.err.println("TEST PASSED");
    92             System.err.println("invoke methods");
   109 	    
    93             Object obj = proxy.passObject(proxy);
   110 	} finally {
    94             if (!proxy.equals(obj)) {
   111 	    if (proxy != null) {
    95                 throw new RuntimeException("returned proxy not equal");
   112 		UnicastRemoteObject.unexportObject(server, true);
    96             }
   113 	    }
    97 
   114 	}
    98             int x = proxy.passInt(53);
       
    99             if (x != 53) {
       
   100                 throw new RuntimeException("returned int not equal");
       
   101             }
       
   102 
       
   103             String string = proxy.passString("test");
       
   104             if (!string.equals("test")) {
       
   105                 throw new RuntimeException("returned string not equal");
       
   106             }
       
   107 
       
   108             System.err.println("TEST PASSED");
       
   109 
       
   110         } finally {
       
   111             if (proxy != null) {
       
   112                 UnicastRemoteObject.unexportObject(server, true);
       
   113             }
       
   114         }
   115     }
   115     }
   116 }
   116 }
   117 
   117 
   118 
   118 
   119 interface RemoteInterface extends Remote {
   119 interface RemoteInterface extends Remote {