Advertisement
Guest User

SecureInstance

a guest
Aug 9th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. /*
  2.  * WITH THIS CLASS IT'S POSSIBLE TO RUN METHODS AND CONSTRUCTOR IN THREADS
  3.  * + THERE IS A WATCHDOG TO RUN THE METHODS ONLY FOR SOME SECONDS
  4.  */
  5.  
  6. package game;
  7.  
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.lang.reflect.Method;
  10.  
  11. public class SecureInstance implements Runnable {
  12.    
  13.     /* Class for ainitialize a new Object via Thread */
  14.    
  15.     String classname;
  16.     Object returnValue = null;
  17.     Thread t = null;
  18.    
  19.     public SecureInstance(String a){
  20.         classname = a;
  21.         // Start the Thread for creating an instance of the class
  22.         t = new Thread(this);
  23.         t.start();
  24.     }
  25.    
  26.     public  Object getInstance(){
  27.         try {
  28.             // Wait until the Thread initialize the Object then return..
  29.             t.join();
  30.         } catch (InterruptedException e) {
  31.             // TODO Auto-generated catch block
  32.             e.printStackTrace();
  33.         }
  34.         return returnValue;    
  35.     }
  36.    
  37.  
  38.     public void run() {
  39.         // Create The WatchDog with maxtime = 2 seconds
  40.         WatchDog watchdog = new WatchDog(2);
  41.         Thread wdthread = new Thread(watchdog);
  42.        
  43.         // Add this thread to the WatchDog
  44.         watchdog.addThread(Thread.currentThread());
  45.         wdthread.start();
  46.        
  47.         try{       
  48.             this.returnValue  = (Object) Class.forName(classname).newInstance();
  49.         }catch(Exception e){}
  50.        
  51.         watchdog.close();  
  52.     }
  53.    
  54.    
  55.     public Object callMethod(String a){
  56.         // If you want to call any method from the Instance
  57.         RunMethod x = new RunMethod(a,this);
  58.         return x.getReturnValue();
  59.     }
  60.    
  61.    
  62.     private class RunMethod implements Runnable{
  63.         // Class to run the method
  64.        
  65.         private String method = null;
  66.         private SecureInstance instance = null;
  67.         private Object returnValue = null;
  68.         private Thread t;
  69.        
  70.         RunMethod(String a,SecureInstance b){
  71.             this.method = a;
  72.             this.instance = b;
  73.            
  74.             t = new Thread(this);
  75.            
  76.             WatchDog wd = new WatchDog(1);
  77.             Thread wdthread = new Thread(wd);
  78.             wd.addThread(t);
  79.            
  80.             wdthread.start();
  81.            
  82.             t.start();
  83.             try {
  84.                 t.join();
  85.             } catch (InterruptedException e) {
  86.                 // TODO Auto-generated catch block
  87.                 //e.printStackTrace();
  88.             }
  89.             wd.close();
  90.            
  91.            
  92.         }
  93.        
  94.         public Object getReturnValue(){
  95.             try {
  96.             // Wait until we have got a value to return
  97.             t.join();
  98.             } catch (InterruptedException e) {
  99.                 // TODO Auto-generated catch block
  100.                 //e.printStackTrace();
  101.             }
  102.             return returnValue;
  103.         }
  104.        
  105.         @Override
  106.         public void run() {
  107.             try {
  108.                 // Get the Method and then the return Value
  109.                 Method m = instance.getInstance().getClass().getMethod(this.method);
  110.                 this.returnValue = (m.invoke(instance.getInstance()));
  111.  
  112.             } catch (Exception e) {
  113.                 // TODO Auto-generated catch block
  114.                 //e.printStackTrace();
  115.             }
  116.         }
  117.        
  118.    
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement