/* * Copyright 2006 TKLSoft.de All rights reserved. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.ConnectException; import java.rmi.NotBoundException; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.UnknownHostException; import java.rmi.UnmarshalException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class RmiLookupTest { public static void main(String[] args) { boolean verbose = false; for(int i = 0; i < args.length; i++) { if(args[i].toLowerCase().equals("-v")) { verbose = true; break; } } boolean specialBinding = false; String bindName = "GIBBET_BESTIMMT_NICHT"; try { Registry registry = LocateRegistry.getRegistry(args[0], Integer.parseInt(args[1])); if(args.length > 2) { specialBinding = true; bindName = args[2]; } Remote remote = registry.lookup(bindName); Class remoteInterface = remote.getClass().getInterfaces()[0]; Method[] methods = remoteInterface.getDeclaredMethods(); Method fittingMethod = findFittingMethod(methods); try { fittingMethod.invoke(remote, null); } catch(RuntimeException re) { // ok, ggf falsche Parameter, aber Aufruf funktioniert an sich } System.out.println("OK: RmiRegistry found at " + args[0] + ":" + args[1] + " and lookup and invocation for \"" + bindName + "\" succeeded"); System.exit(0); } catch(NotBoundException nbE) { if(verbose) { nbE.printStackTrace(); } if(specialBinding) { System.out.println("Critical: RmiRegistry found at " + args[0] + ":" + args[1] + ", but lookup for \"" + bindName + "\" failed"); System.exit(2); } else { System.out.println("OK: Rmi-Server found at " + args[0] + ":" + args[1]); System.exit(0); } } catch(UnmarshalException uE) { if(verbose) { uE.printStackTrace(); } System.out.println("OK: Rmi-Server found at " + args[0] + ":" + args[1]); System.exit(0); } catch(ConnectException cE) { System.out.println("Critical: Host " + args[0] + " refused connection to port " + args[1]); if(verbose) { cE.printStackTrace(); } System.exit(2); } catch(UnknownHostException uhE) { System.out.println("Critical: Host " + args[0] + " not reachable"); if(verbose) { uhE.printStackTrace(); } System.exit(2); } catch(RemoteException e) { System.out.println("Critical: " + e.getMessage()); if(verbose) { e.printStackTrace(); } System.exit(2); } catch(RuntimeException th) { System.out.println(th.getClass().getName() + ": " + th.getMessage()); if(verbose) { th.printStackTrace(); } System.exit(3); } catch(IllegalAccessException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); if(verbose) { e.printStackTrace(); } System.exit(3); } catch(InvocationTargetException e) { if(verbose) { e.printStackTrace(); } if(specialBinding && null != e.getCause() && e.getCause().getClass() == ConnectException.class) { System.out.println("Critical: RmiRegistry found at " + args[0] + ":" + args[1] + ", lookup for \"" + bindName + "\" succeeded but underlying process is lost"); System.exit(2); } else { System.out.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(3); } } } /** * Moeglichst eine Methode ohne Parameter zurueck geben * @param methods * @return Method */ private static Method findFittingMethod(Method[] methods) { for(int i = 0; i < methods.length; i++) { if(methods[i].getParameterTypes().length == 0) { return methods[i]; } } return methods[0]; } }