RoomClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package com.borland.training.meetings.clients;
- import javax.naming.*;
- import javax.rmi.PortableRemoteObject;
- import java.util.Collection;
- import com.borland.training.meetings.entities.*;
- public class RoomClient {
- static RoomRemoteHome roomHome = null;
- static RoomRemote room = null;
- public RoomClient() {
- }
- public static void main(String[] args) {
- if(args.length != 1) {
- usage();
- }
- Object ref;
- try {
- //get naming context
- Context ctx = new InitialContext();
- //look up jndi name
- ref = ctx.lookup("meetings/RoomRemote");
- //cast to Home interface
- roomHome = (RoomRemoteHome) PortableRemoteObject.narrow(ref, RoomRemoteHome.class);
- String command = args[0];
- if(command.equals("create")) {
- create();
- }
- else if(command.equals("use")) {
- use();
- }
- else if(command.equals("remove")) {
- remove();
- }
- else {
- usage();
- }
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void create() {
- try {
- String roomName = "1001";
- System.out.println("Creating a new room with room name: " + roomName);
- room = roomHome.create(roomName, 10);
- roomName = "1011";
- System.out.println("Creating a new room with room id: " + roomName);
- room = roomHome.create(roomName, 5);
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void use() {
- try {
- String roomName = "1001";
- try {
- room = roomHome.findByPrimaryKey(roomName);
- System.out.println("Room found");
- System.out.println("Room name: " + room.getName());
- System.out.println("Room capacity: " + room.getCapacity());
- }
- catch(javax.ejb.ObjectNotFoundException e) {
- System.out.println("Room not found");
- }
- System.out.println("Listing all rooms");
- java.util.Iterator iterator = roomHome.findAll().iterator();
- if(!iterator.hasNext()) {
- System.out.println("No rooms found");
- }
- while(iterator.hasNext()) {
- RoomRemote room = (RoomRemote)
- javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
- System.out.println("Found room: " + room.getName() + ", " + room.getCapacity());
- }
- int minCapacity = 8;
- System.out.println("Listing all rooms with capacity greater than " + minCapacity);
- iterator = roomHome.findRoomsWithCapacityGreaterThan(minCapacity).iterator();
- if(!iterator.hasNext()) {
- System.out.println("No rooms found");
- }
- while(iterator.hasNext()) {
- RoomRemote room = (RoomRemote)
- javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
- System.out.println("Found room: " + room.getName() + ", " + room.getCapacity());
- }
- System.out.println("Total capacity of all rooms is " + roomHome.calculateTotalCapacity());
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void remove() {
- try {
- System.out.println("Removing all rooms");
- java.util.Iterator iterator = roomHome.findAll().iterator();
- if(!iterator.hasNext()) {
- System.out.println("No rooms");
- }
- while(iterator.hasNext()) {
- RoomRemote room = (RoomRemote)
- javax.rmi.PortableRemoteObject.narrow(iterator.next(), RoomRemote.class);
- System.out.println("Removing room: " + room.getName());
- room.remove();
- }
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void usage() {
- System.out.println("Usage: vbj RoomClient create | use | remove");
- System.exit(1);
- }
- }