Path: blob/master/test/jdk/java/rmi/transport/dgcDeadLock/TestImpl.java
41153 views
/*1* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*/2526import java.rmi.*;27import sun.rmi.transport.*;28import java.io.*;29import java.lang.reflect.*;30import java.rmi.dgc.*;31import java.util.*;32import java.rmi.registry.*;33import java.rmi.server.*;3435public class TestImpl extends RegistryRunner36implements Test {37static Thread locker = null;38static TestImpl foo = null;39static TestImpl bar = null;4041TestImpl() throws RemoteException {42}4344public String echo(String msg) throws RemoteException {4546if (locker == null) {47// hold the target if not already held48locker = lockTargetExpireLeases(foo, DGCDeadLock.HOLD_TARGET_TIME);49}50return "Message received: " + msg;51}5253static public void main(String[] args) {54try {55int registryPort = RegistryRunner.init(args);5657//export "Foo"58foo = new TestImpl();59Naming.rebind("rmi://:" +60registryPort61+ "/Foo", foo);6263try {64//export "Bar" after leases have been expired.65bar = new TestImpl();66Naming.rebind("rmi://localhost:" +67registryPort68+ "/Bar", bar);69} catch (Exception e) {70throw new RemoteException(e.getMessage());71}7273RegistryRunner.notify(registryPort);74} catch (Exception e) {75System.err.println(e.getMessage());76e.printStackTrace();77}78}7980static Thread lockTargetExpireLeases(Remote toLock, int timeOut) {81Thread t = new Thread((Runnable) new TargetLocker(toLock, timeOut));82t.start();83return t;84}8586static class TargetLocker implements Runnable {8788Remote toLock = null;89int timeOut = 0;9091TargetLocker(Remote toLock, int timeOut) {92this.toLock = toLock;93this.timeOut = timeOut;94}9596public void run() {97try {98// give dgc dirty calls time to finish.99Thread.currentThread().sleep(4000);100101java.security.AccessController.102doPrivileged(new LockTargetCheckLeases(toLock,103timeOut));104105} catch (Exception e) {106System.err.println(e.getMessage());107e.printStackTrace();108System.exit(1);109}110}111}112113static class LockTargetCheckLeases114implements java.security.PrivilegedAction {115116Remote toLock = null;117int timeOut = 0;118119LockTargetCheckLeases(Remote toLock, int timeOut) {120this.toLock = toLock;121this.timeOut = timeOut;122}123124public Object run() {125try {126127Class args[] = new Class[1];128129Class objTableClass = Class.forName130("sun.rmi.transport.ObjectTable");131132/* get the Target that corresponds to toLock from the133* ObjectTable134*/135args[0] = Class.forName("java.rmi.Remote");136Method objTableGetTarget =137objTableClass.getDeclaredMethod("getTarget", args );138objTableGetTarget.setAccessible(true);139140Target lockTarget =141((Target) objTableGetTarget.invoke142(null , new Object [] {toLock} ));143144// make sure the lease on this object has expired.145expireLeases(lockTarget);146147// stop other threads from using the target for toLock.148synchronized (lockTarget) {149System.err.println("Locked the relevant target, sleeping " +150timeOut/1000 + " seconds");151Thread.currentThread().sleep(timeOut);152System.err.println("Target unlocked");153}154155} catch (Exception e) {156System.err.println(e.getMessage());157e.printStackTrace();158System.exit(1);159}160return null;161}162}163164/* leases have long values, so no dirty calls which would lock out165* a clean call, but the leases need to expire anyway, so we do it166* explicitly.167*/168static void expireLeases(Target t) throws Exception {169170final Target target = t;171172java.security.AccessController.doPrivileged(173174// put this into another class?175new java.security.PrivilegedAction() {176public Object run() {177try {178179Class DGCClass = Class.forName("sun.rmi.transport.DGCImpl");180Method getDGCImpl =181DGCClass.getDeclaredMethod("getDGCImpl", null );182getDGCImpl.setAccessible(true);183184// make sure the lease on this object has expired.185DGC dgcImpl = ((DGC) getDGCImpl.invoke(null, null));186187/* Get the lease table from the DGCImpl. */188Field reflectedLeaseTable =189dgcImpl.getClass().getDeclaredField("leaseTable");190reflectedLeaseTable.setAccessible(true);191192Map leaseTable = (Map) reflectedLeaseTable.get(dgcImpl);193194// dont really need this synchronization...195synchronized (leaseTable) {196Iterator en = leaseTable.values().iterator();197while (en.hasNext()) {198Object info = en.next();199200/* Get the notifySet in the leaseInfo object. */201Field notifySetField =202info.getClass().getDeclaredField("notifySet");203notifySetField.setAccessible(true);204HashSet notifySet = (HashSet) notifySetField.get(info);205206Iterator iter = notifySet.iterator();207while (iter.hasNext()) {208Target notified = (Target) iter.next();209210if (notified == target) {211212/* Get and set the expiration field from the info object. */213Field expirationField = info.getClass().214getDeclaredField("expiration");215expirationField.setAccessible(true);216expirationField.setLong(info, 0);217}218}219}220}221} catch (Exception e) {222System.err.println(e.getMessage());223e.printStackTrace();224System.exit(1);225}226// no interesting return value for this privileged action227return null;228}229});230}231}232233234