SchedulerClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:8k
源码类别:
Java编程
开发平台:
Java
- package com.borland.training.meetings.clients;
- import com.borland.training.meetings.sessions.*;
- import javax.naming.*;
- import javax.rmi.PortableRemoteObject;
- import java.util.*;
- public class SchedulerClient {
- static SchedulerHome schedulerHome = null;
- static Scheduler scheduler = null;
- public SchedulerClient() {}
- 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/Scheduler");
- //cast to Home interface
- schedulerHome = (SchedulerHome) PortableRemoteObject.narrow(ref, SchedulerHome.class);
- scheduler = schedulerHome.create();
- String command = args[0];
- if(command.equals("create")) {
- create();
- }
- else if(command.equals("use")) {
- use();
- }
- else if(command.equals("remove")) {
- remove();
- }
- else {
- usage();
- }
- scheduler.remove();
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void create() {
- try {
- RoomValue roomValue = new RoomValue("100", (int) 10);
- System.out.println("Creating room: " + roomValue);
- String name = scheduler.createRoom(roomValue);
- System.out.println("Created room id: " + name);
- System.out.println();
- roomValue = new RoomValue("101", (int) 5);
- System.out.println("Creating room: " + roomValue);
- name = scheduler.createRoom(roomValue);
- System.out.println("Created room id: " + name);
- System.out.println();
- System.out.println("Changing capacity of room: " + roomValue.getName());
- roomValue.setCapacity(roomValue.getCapacity() + 10);
- scheduler.updateRoom(roomValue);
- System.out.println();
- Vector attendeeIds = new Vector(3);
- AttendeeValue attendeeValue =
- new AttendeeValue("John Smith", "Developer", "Mr.",
- "(123) 456 1001", "js@xyz.com");
- System.out.println("Creating attendee: " + attendeeValue);
- Long attendeeId = scheduler.createAttendee(attendeeValue);
- System.out.println("Created attendee id: " + attendeeId);
- attendeeIds.add(attendeeId);
- System.out.println();
- attendeeValue =
- new AttendeeValue("Joe Brown", "Manager", "Mr.",
- "(123) 456 1002", "jb@xyz.com");
- System.out.println("Creating attendee: " + attendeeValue);
- attendeeId = scheduler.createAttendee(attendeeValue);
- System.out.println("Created attendee id: " + attendeeId);
- attendeeIds.add(attendeeId);
- System.out.println();
- attendeeValue =
- new AttendeeValue("Steve Bates", "VP", "Dr.",
- "(123) 456 1003", "sb@xyz.com");
- System.out.println("Creating attendee: " + attendeeValue);
- attendeeId = scheduler.createAttendee(attendeeValue);
- System.out.println("Created attendee id: " + attendeeId);
- attendeeIds.add(attendeeId);
- System.out.println();
- Calendar cal = Calendar.getInstance();
- Date time1 = cal.getTime();
- cal.add(Calendar.HOUR, 1);
- Date time2 = cal.getTime();
- cal.add(Calendar.HOUR, 1);
- Date time3 = cal.getTime();
- MeetingValue meetingValue =
- new MeetingValue("Summer Vacation", time1, time2, "100", attendeeIds);
- System.out.println("Creating meeting: " + meetingValue);
- Long meetingId = scheduler.createMeeting(meetingValue);
- System.out.println("Created meeting id: " + meetingId);
- System.out.println();
- System.out.println("Looking for meeting :" + meetingId);
- meetingValue = scheduler.findMeeting(meetingId);
- System.out.println(meetingValue);
- System.out.println();
- attendeeIds.remove(1);
- meetingValue =
- new MeetingValue("Winter Vacation", time2, time3, "100", attendeeIds);
- System.out.println("Creating meeting: " + meetingValue);
- meetingId = scheduler.createMeeting(meetingValue);
- System.out.println("Created meeting id: " + meetingId);
- System.out.println();
- System.out.println("Looking for meeting: " + meetingId);
- meetingValue = scheduler.findMeeting(meetingId);
- System.out.println(meetingValue);
- System.out.println();
- System.out.println("Changing room for meeting: " + meetingId);
- meetingValue.setRoomName("101");
- scheduler.updateMeeting(meetingValue);
- System.out.println();
- System.out.println("Looking for meeting: " + meetingId);
- meetingValue = scheduler.findMeeting(meetingId);
- System.out.println(meetingValue);
- System.out.println();
- attendeeValue =
- new AttendeeValue("Joan Wilder", "Assistant", "Mrs.",
- "(123) 456 1004", "jw@xyz.com");
- System.out.println("Creating attendee: " + attendeeValue);
- attendeeId = scheduler.createAttendee(attendeeValue);
- System.out.println("Created attendee id: " + attendeeId);
- System.out.println("Signing up attendee: " + attendeeId
- + " to meeting: " + meetingId);
- meetingValue.getAttendeeIds().add(attendeeId);
- scheduler.updateMeeting(meetingValue);
- System.out.println("Looking for meeting:" + meetingId);
- meetingValue = scheduler.findMeeting(meetingId);
- System.out.println(meetingValue);
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void use() {
- try {
- String name = "100";
- System.out.println("Looking for room: " + name);
- try {
- RoomValue roomValue = scheduler.findRoom(name);
- System.out.println("Room found: " + roomValue);
- }
- catch (Exception e) {
- System.out.println("Room " + name + " cannot be found");
- }
- name = "101";
- System.out.println("Looking for room: " + name);
- try {
- RoomValue roomValue = scheduler.findRoom(name);
- System.out.println("Room found: " + roomValue);
- }
- catch (Exception e) {
- System.out.println("Room " + name + " cannot be found");
- }
- System.out.println("Getting all rooms");
- Collection roomValues = scheduler.findAllRooms();
- System.out.println(roomValues);
- System.out.println("Getting all attendees");
- Collection attendeeValues = scheduler.findAllAttendees();
- System.out.println(attendeeValues);
- System.out.println("Getting all meetings");
- Collection meetingValues = scheduler.findAllMeetings();
- System.out.println(meetingValues);
- name = "Joan Wilder";
- System.out.println("Looking for attendees: " + name);
- attendeeValues = scheduler.findAttendeesByName(name);
- System.out.println(attendeeValues);
- if(attendeeValues.isEmpty()) {
- return;
- }
- AttendeeValue attendeeValue = (AttendeeValue) attendeeValues.iterator().next();
- Long id = attendeeValue.getId();
- System.out.println("Looking for meetings of attendee: " + name + ", " + id);
- meetingValues = scheduler.findMeetingsOfAttendee(id);
- System.out.println(meetingValues);
- }
- catch(Exception e) {
- System.err.println("Exception caught");
- e.printStackTrace();
- }
- }
- private static void remove() {
- try {
- String name = "100";
- System.out.println("Removing room: " + name);
- try {
- scheduler.removeRoom(name);
- }
- catch (Exception e) {
- System.out.println("Room " + name + " cannot be removed");
- }
- System.out.println("Removing all rooms");
- scheduler.removeAllRooms();
- System.out.println("Removing all meetings");
- scheduler.removeAllMeetings();
- System.out.println("Removing all attendees");
- scheduler.removeAllAttendees();
- System.out.println("Done");
- }
- 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);
- }
- }