RoomClient.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

  1. package com.borland.training.meetings.clients;
  2. import javax.naming.*;
  3. import javax.rmi.PortableRemoteObject;
  4. import java.util.Collection;
  5. import com.borland.training.meetings.entities.*;
  6. public class RoomClient {
  7.   static RoomRemoteHome roomHome = null;
  8.   static RoomRemote room = null;
  9.   public RoomClient() {
  10.   }
  11.   public static void main(String[] args) {
  12.     if(args.length != 1) {
  13.       usage();
  14.     }
  15.     Object ref;
  16.     try {
  17.       //get naming context
  18.       Context ctx = new InitialContext();
  19.       //look up jndi name
  20.       ref = ctx.lookup("meetings/RoomRemote");
  21.       //cast to Home interface
  22.       roomHome = (RoomRemoteHome) PortableRemoteObject.narrow(ref, RoomRemoteHome.class);
  23.       String command = args[0];
  24.       if(command.equals("create")) {
  25. create();
  26.       }
  27.       else if(command.equals("use")) {
  28. use();
  29.       }
  30.       else if(command.equals("remove")) {
  31. remove();
  32.       }
  33.       else {
  34. usage();
  35.       }
  36.     }
  37.     catch(Exception e) {
  38.       System.err.println("Exception caught");
  39.       e.printStackTrace();
  40.     }
  41.   }
  42.   private static void create() {
  43.     try {
  44.       String roomName = "1001";
  45.       System.out.println("Creating a new room with room name: " + roomName);
  46.       room = roomHome.create(roomName, 10);
  47.       roomName = "1011";
  48.       System.out.println("Creating a new room with room id: " + roomName);
  49.       room = roomHome.create(roomName, 5);
  50.     }
  51.     catch(Exception e) {
  52.       System.err.println("Exception caught");
  53.       e.printStackTrace();
  54.     }
  55.   }
  56.   private static void use() {
  57.     try {
  58.       String roomName = "1001";
  59.       try {
  60.         room = roomHome.findByPrimaryKey(roomName);
  61.         System.out.println("Room found");
  62.         System.out.println("Room name: " + room.getName());
  63.         System.out.println("Room capacity: " + room.getCapacity());
  64.       }
  65.       catch(javax.ejb.ObjectNotFoundException e) {
  66.         System.out.println("Room not found");
  67.       }
  68.       System.out.println("Listing all rooms");
  69.       java.util.Iterator iterator = roomHome.findAll().iterator();
  70.       if(!iterator.hasNext()) {
  71.         System.out.println("No rooms found");
  72.       }
  73.       while(iterator.hasNext()) {
  74.         RoomRemote room = (RoomRemote)
  75.           javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
  76.         System.out.println("Found room: " + room.getName() + ", " + room.getCapacity());
  77.       }
  78.       int minCapacity = 8;
  79.       System.out.println("Listing all rooms with capacity greater than " + minCapacity);
  80.       iterator = roomHome.findRoomsWithCapacityGreaterThan(minCapacity).iterator();
  81.       if(!iterator.hasNext()) {
  82.         System.out.println("No rooms found");
  83.       }
  84.       while(iterator.hasNext()) {
  85.         RoomRemote room = (RoomRemote)
  86.           javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
  87.         System.out.println("Found room: " + room.getName() + ", " + room.getCapacity());
  88.       }
  89.       System.out.println("Total capacity of all rooms is " + roomHome.calculateTotalCapacity());
  90.     }
  91.     catch(Exception e) {
  92.       System.err.println("Exception caught");
  93.       e.printStackTrace();
  94.     }
  95.   }
  96.   private static void remove() {
  97.     try {
  98.       System.out.println("Removing all rooms");
  99.       java.util.Iterator iterator = roomHome.findAll().iterator();
  100.       if(!iterator.hasNext()) {
  101.         System.out.println("No rooms");
  102.       }
  103.       while(iterator.hasNext()) {
  104.         RoomRemote room = (RoomRemote)
  105.           javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
  106.         System.out.println("Removing room: " + room.getName());
  107. room.remove();
  108.       }
  109.     }
  110.     catch(Exception e) {
  111.       System.err.println("Exception caught");
  112.       e.printStackTrace();
  113.     }
  114.   }
  115.   private static void usage() {
  116.     System.out.println("Usage: vbj RoomClient create | use | remove");
  117.     System.exit(1);
  118.   }
  119. }