Guest User

RMI Fail

a guest
Jan 9th, 2012
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.rmi.registry.LocateRegistry;
  2. import java.rmi.registry.Registry;
  3. import java.math.BigDecimal;
  4. import compute.Compute;
  5. import java.util.Properties;
  6. import java.io.FileInputStream;
  7.  
  8. public class ComputePi {
  9.     private static String sysPropsFile = "uk/org/client/Properties.txt";
  10.     private static String clientPolicyFileDefault = "uk/org/client/client.policy";
  11.  
  12.     public static void run( String regHost, String precision ) {
  13.         setSystemProps();
  14.         if(System.getSecurityManager() ==  null) {
  15.             System.setSecurityManager(new SecurityManager());
  16.         }
  17.         try {
  18.             String name = "Compute";
  19.             Registry registry = LocateRegistry.getRegistry(regHost);
  20.             Compute comp = (Compute) registry.lookup(name);
  21.             Pi task = new Pi(Integer.parseInt(precision));
  22.             BigDecimal pi = comp.executeTask(task);
  23.             System.out.println(pi);
  24.         } catch(Exception e) {
  25.             System.err.println("ComputePi exception.");
  26.             e.printStackTrace();
  27.         }
  28.     }
  29.  
  30.     /**
  31.         Normally set at runtime, these properties
  32.         tell the app where things are and what to use for RMI
  33.     **/
  34.     public static void setSystemProps() {
  35.             Properties p = new Properties(System.getProperties());
  36.         try {
  37.             // Find the sys props file
  38.             String f = Thread.currentThread().getContextClassLoader().getResource(sysPropsFile).getFile();
  39.    
  40.             // Loads the sys props from a file
  41.                 FileInputStream propFile = new FileInputStream( f );
  42.                 p.load(propFile);
  43.    
  44.         } catch(Exception e) {
  45.             System.err.println("Error: Failed to load client properties.");
  46.             e.printStackTrace();
  47.             System.err.println("Setting default properties...");
  48.  
  49.             // Set defaults
  50.             p.setProperty("java.rmi.server.codebase","http://10.0.0.243:9876/smartmeters/classes/");
  51.         }
  52.  
  53.         // Find the client policy
  54.         String clientPolicyFile = p.getProperty("java.security.policy");
  55.  
  56.         try {
  57.             // Find the sys props file
  58.             String cf = Thread.currentThread().getContextClassLoader().getResource(clientPolicyFileDefault).getFile();
  59.             if(cf==null)
  60.                 System.err.println("Warning: Failed to find the client policy file.");
  61.             else
  62.                 clientPolicyFile = cf;
  63.         } catch(Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.  
  67.         p.setProperty("java.security.policy",clientPolicyFile);
  68.  
  69.         // set the system properties
  70.             System.setProperties(p);
  71.  
  72.             // display new properties
  73.             //System.getProperties().list(System.out);
  74.     }
  75.  
  76.     public static void testThisThing() {
  77.         System.out.println("COMPUTEPI.TEST SUCCESS!");
  78.     }
  79.  
  80. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×