Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, November 21, 2012

Java RMI

1) Compute.java

package compute;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Compute extends Remote {
   <T> T executeTask(Task<T> t) throws RemoteException;
}

2) Task.java

package compute;

public interface Task<T> {
   T execute();
}

cd c:\home\waldo\src
javac compute\Compute.java compute\Task.java
jar cvf compute.jar compute\*.class

3) ComputeEngine.java

package engine;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;

public class ComputeEngine implements Compute{
   public ComputeEngine(){
      super();
   }
  
   public<T> T executeTask(Task<T> t){
      return t.execute();
   }

   public static void main(String[] args){
      if(System.getSecurityManager() == null){
         System.setSecurityManager(new SecurityManager());
      }
      try{
         String name = "Compute";
         Compute engine = new ComputeEngine();
         Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
         Registry registry = LocateRegistry.getRegistry();
         registry.rebind(name, stub);
      } catch(Exception e){
         System.err.println("ComputeEngine exception: ");
         e.printStackTrace();
      }
   }
}

cd c:\home\ann\src
javac -cp c:\home\ann\public_html\classes\compute.jar
    engine\ComputeEngine.java

Pi.java

package client;
import compute.Task;
import java.io.Serializable;
import java.math.BigDecimal;

public class Pi implements Task<BigDecimal>, Serializable{
   private final int digits;
   public Pi(int digits){
      this.digits = digits;
   }
   public BigDecimal execute(){
      return computePi(digits);
   }
   public static BigDecimal computePi(int digits){
      return result;
   }
}

ComputPi.java

package client;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.math.BigDecimal;
import compute.Compute;

public class ComputePi{
   public static void main(String args[]){
      if(System.getSecurityManager() == null){
         System.setSecurityManager(new SecurityManager());
      }
      try{
         String name = "Compute";
         Registry registry = LocateRegistry.getRegistry(args[0]);
         Compute comp = (Compute) registry.lookup(name);
         Pi task = new Pi(Integer.parseInt(args[1]);
         BigDecimal pi = comp.executeTask(task);
         System.out.println(pi);
      } catch(Exception e){
         System.err.println("ComputerPi exception:");
         e.printStackTrace();
      }
   }
}

cd c:\home\jones\src
javac -cp c:\home\jones\public_html\classes\compute.jar
   client\ComputePi.java client\Pi.java
mkdir c:\home\jones\public_html\classes\client
cp client\Pi.class
   c:\home\jones\public_html\classes\client

server.policy

grant codeBase "file:/home/ann/src/" {
   permission java.security.AllPermission;
};

client.policy

grant codeBase "file:/home/jones/src/" {
   permission java.security.AllPermission;
};

start rmiregistry (or javaw if start is not available)

java -cp c:\home\ann\src;c:\home\ann\public_html\classes\compute.jar
       -Djava.rmi.server.codebase=file:/c:/home/ann/public_html/classes/compute.jar
       -Djava.rmi.server.hostname=mycomputer.example.com
       -Djava.security.policy=server.policy
          engine.ComputeEngine

java -cp c:\home\jones\src;c:\home\jones\public_html\classes\compute.jar
       -Djava.rmi.server.codebase=file:/c:/home/jones/public_html/classes/
       -Djava.security.policy=client.policy
          client.ComputePi mycomputer.example.com 45

Thursday, November 8, 2012

Java vs. Objective-C

Java
Rectangle rect = Rectangle(someWidth, someHeight);
println(rect.getHeight());
boolean inside = rect.contains(x,y);

Objective-C
Rectangle *rect = [[Rectangle alloc] initWithWidth:someWidth andHeight:someHeight];
NSLog(@"%f", [rect height]);
BOOL inside = [rect containsPointWithX:x andY:y]

Saturday, May 6, 2006

Java Collections in j2sdk-1_4_2

 







































  Implementations
Hash Table Resizable Array Balanced Tree Linked List Hash Table + Linked List
Interfaces Set HashSet   TreeSet   LinkedHashSet
List   ArrayList   LinkedList  
Map HashMap   TreeMap   LinkedHashMap

 

Wednesday, February 15, 2006

Bloch’s Standard Exceptions

IllegalArgumentException: Parameter value is inappropriate

NullPointerException: Parameter null where prohibited

IndexOutOfBoundsException: Index param out of range

ConcurrentModificationException: Concurrent modification detected when not allowed

IllegalStateException: Object state is inappropriate for method invocation. Ojbect may not be initialized before accessing its state. ClassCastException (Illegal state of object)

UnsupportedOperationException: Object does not support the method. Substitutional principal

Thursday, January 26, 2006

Installed J2SE Development Kit and NetBeans IDE

The Install Wizard has successfully installed J2SE Development Kit 1.4.2_10 and NetBeans IDE 4.1 on your computer. Choose Finish to exit the Wizard.  J2SE Development Kit 1.4.2_10 installation location: C:\j2sdk1.4.2_10  NetBeans IDE 4.1 installation location: C:\Program Files\netbeans-4.1
To run the IDE, launch:
C:\Program Files\netbeans-4.1\bin\netbeans.exe
To uninstall the IDE, launch:
C:\Program Files\netbeans-4.1\_uninst\uninstaller.exe
To uninstall J2SE Development Kit 1.4.2_10:
Use Add or Remove Programs in Control Panel