[jboss-cvs] JBossAS SVN: r66729 - in projects/microcontainer/trunk/docs/examples/User_Guide: commandLineClient/src/main/java/org/jboss/example/client and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 5 07:49:06 EST 2007


Author: newtonm
Date: 2007-11-05 07:49:05 -0500 (Mon, 05 Nov 2007)
New Revision: 66729

Added:
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/UserInterface.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/MockUserInterface.java
Removed:
   projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java
   projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java
Log:
Renamed cmdLineClient to commandLineClient.

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient (from rev 66724, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient)

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/Client.java	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,184 +0,0 @@
-package org.jboss.example.client;
-
-import java.io.IOException;
-import java.net.URL;
-import java.text.ParseException;
-import java.util.Date;
-import java.util.Set;
-
-import org.jboss.example.service.Address;
-import org.jboss.example.service.Employee;
-import org.jboss.example.service.HRManager;
-import org.jboss.example.service.util.AgeBasedSalaryStrategy;
-import org.jboss.example.service.util.LocationBasedSalaryStrategy;
-import org.jboss.example.service.util.SalaryStrategy;
-import org.jboss.kernel.Kernel;
-import org.jboss.kernel.spi.dependency.KernelControllerContext;
-import org.jboss.kernel.spi.registry.KernelBus;
-import org.jboss.kernel.spi.registry.KernelRegistry;
-
-public class Client {
-    
-	protected boolean useBus = false;
-		
-	private EmbeddedBootstrap bootstrap;
-	private Kernel kernel;
-	private KernelRegistry registry;
-	private KernelBus bus;
-
-	private URL url;
-	private HRManager manager;
-
-	private final static String HRSERVICE = "HRService";
-	private final static String EMPLOYEE = "org.jboss.example.service.Employee";
-
-	public static void main(String[] args) throws Exception {
-		if ((args.length == 1 && !args[0].equals("bus")) || args.length > 1) {
-			System.out.println("Usage: java -jar cmdLineClient-1.0.0.jar [bus]");
-			System.exit(1);
-		}
-
-		new Client(args.length == 1);
-    }
-
-	public Client(final boolean useBus) throws Exception {
-		
-		this.useBus = useBus;
-		
-		ClassLoader cl = Thread.currentThread().getContextClassLoader();
-		url = cl.getResource("META-INF/jboss-beans.xml");
-	
-		// Start JBoss Microcontainer
-		bootstrap = new EmbeddedBootstrap();
-		bootstrap.run();
-		kernel = bootstrap.getKernel();
-		registry = kernel.getRegistry();
-		bus = kernel.getBus();
-		
-		new ConsoleInput(this, bootstrap, useBus, url);		
- 	}
-	
-	void cacheServiceRef() {
-		if (manager == null) {
-			KernelControllerContext context = (KernelControllerContext) registry.getEntry(HRSERVICE);
-			if (context != null) { manager = (HRManager) context.getTarget(); }
-		}
-	}
-
-	private Object invoke(String serviceName, String methodName, Object[] args, String[] types) {
-		Object result = null;
-		try {
-			result = bus.invoke(serviceName, methodName, args, types);
-		} catch (Throwable t) {
-			t.printStackTrace();
-		}	
-		return result;
-	}
-	
-	void addEmployee() throws ParseException, NumberFormatException, IllegalArgumentException, IOException {
-		Employee newEmployee = ConsoleInput.getEmployee();		
-		Address address = ConsoleInput.getAddress();
-		Date dateOfBirth = ConsoleInput.getDateOfBirth();
-		
-		newEmployee.setAddress(address);
-		newEmployee.setDateOfBirth(dateOfBirth);
-		
-		boolean added;	
-		if (useBus)
-			added = (Boolean) invoke(HRSERVICE, "addEmployee", new Object[] {newEmployee}, new String[] {EMPLOYEE});
-		else
-			added = manager.addEmployee(newEmployee);			
-		System.out.println("Added employee: " + added);
-	}
-	
-	@SuppressWarnings("unchecked")
-	void listEmployees() {			
-		Set<Employee> employees;
-		if (useBus)
-			employees = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
-		else
-			employees = manager.getEmployees();
-		System.out.println("Employees: " + employees);					
-	}
-	
-	void removeEmployee() throws IllegalArgumentException, IOException {			
-		Employee employee = ConsoleInput.getEmployee();
-		
-		if (useBus)
-			invoke(HRSERVICE, "removeEmployee", new Object[] {employee}, new String[] {EMPLOYEE});
-		else
-			manager.removeEmployee(employee);
-	}
-	
-	void getSalary() throws IllegalArgumentException, IOException {
-		Employee employee = ConsoleInput.getEmployee();
-
-		Integer salary = null;
-		if (useBus)
-			salary = (Integer) invoke(HRSERVICE, "getSalary", new Object[] {employee}, new String[] {EMPLOYEE});
-		else
-			salary = manager.getSalary(employee);
-		System.out.println("Salary: " + salary);			
-	}
-	
-	void setSalary() throws NumberFormatException, IllegalArgumentException, IOException {
-		Employee employee = ConsoleInput.getEmployee();	
-		Integer salary = ConsoleInput.getSalary();		
-		
-		Employee actualEmployee;
-		if (useBus) {
-			actualEmployee = (Employee) invoke(HRSERVICE, "getEmployee", new Object[] {employee.getFirstName(), employee.getLastName()}, new String[] {"java.lang.String","java.lang.String"});	
-			invoke(HRSERVICE, "setSalary", new Object[] {actualEmployee, salary}, new String[] {EMPLOYEE, "java.lang.Integer"});	
-		} else {
-			actualEmployee = manager.getEmployee(employee.getFirstName(), employee.getLastName());
-			manager.setSalary(actualEmployee, salary);			
-		}			
-	}
-	
-	void toggleHiringFreeze() {
-		boolean hiringFreeze;
-		if (useBus) {
-			hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[] {}, new String[] {});	
-			invoke(HRSERVICE, "setHiringFreeze", new Object[] {!hiringFreeze}, new String[] {"boolean"});	
-		} else {
-			hiringFreeze = manager.isHiringFreeze();
-			manager.setHiringFreeze(!hiringFreeze);
-		}		
-	}
-	
-	@SuppressWarnings("unchecked")
-	void printStatus() {
-		boolean hiringFreeze;
-		int totalEmployees;
-		SalaryStrategy salaryStrategy;
-		
-		if (useBus) {
-			try {
-				hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[] {}, new String[] {});
-				Set<Employee> employees = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
-				totalEmployees = employees.size();				
-				salaryStrategy = (SalaryStrategy) invoke(HRSERVICE, "getSalaryStrategy", new Object[] {}, new String[] {});
-			} catch (Exception e) {
-				System.out.println("HRService is not deployed.");
-				return;
-			}
-		} else {
-			hiringFreeze = manager.isHiringFreeze();
-			totalEmployees = manager.getEmployees().size();
-			salaryStrategy = manager.getSalaryStrategy();		
-		}	
-		
-		System.out.println("Total number of employees: " + totalEmployees);
-		System.out.println("Hiring Freeze: " + hiringFreeze);
-		String strategy = "";
-		if (salaryStrategy == null) { strategy = "None"; }
-		else if (salaryStrategy instanceof AgeBasedSalaryStrategy ) { strategy = "AgeBased"; }
-		else if (salaryStrategy instanceof LocationBasedSalaryStrategy ) { strategy = "LocationBased"; }
-		
-		System.out.print("Salary Strategy: " + strategy);
-		if (salaryStrategy != null) {
-			System.out.print(" - MinSalary: " + salaryStrategy.getMinSalary() + " MaxSalary: " + salaryStrategy.getMaxSalary());
-		}
-		System.out.println();
-	}
-}
\ No newline at end of file

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/Client.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/Client.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,182 @@
+package org.jboss.example.client;
+
+import java.io.IOException;
+import java.net.URL;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Set;
+
+import org.jboss.example.service.Address;
+import org.jboss.example.service.Employee;
+import org.jboss.example.service.HRManager;
+import org.jboss.example.service.util.AgeBasedSalaryStrategy;
+import org.jboss.example.service.util.LocationBasedSalaryStrategy;
+import org.jboss.example.service.util.SalaryStrategy;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.registry.KernelBus;
+import org.jboss.kernel.spi.registry.KernelRegistry;
+
+public class Client {
+    
+	private boolean useBus = false;
+	private URL url;
+	private UserInterface userInterface;
+	private HRManager manager;
+	
+	private EmbeddedBootstrap bootstrap;
+	private Kernel kernel;
+	private KernelRegistry registry;
+	private KernelBus bus;
+
+	private final static String HRSERVICE = "HRService";
+	private final static String EMPLOYEE = "org.jboss.example.service.Employee";
+
+	public static void main(String[] args) throws Exception {
+		if ((args.length == 1 && !args[0].equals("bus")) || args.length > 1) {
+			System.out.println("Usage: java -jar client-1.0.0.jar [bus]");
+			System.exit(1);
+		}
+
+		Client client = new Client(args.length == 1);
+		client.setUserInterface(new ConsoleInput(client));
+    }
+
+	public Client(final boolean useBus) throws Exception {
+		this.useBus = useBus;
+		
+		ClassLoader cl = Thread.currentThread().getContextClassLoader();
+		url = cl.getResource("META-INF/jboss-beans.xml");
+	
+		// Start JBoss Microcontainer
+		bootstrap = new EmbeddedBootstrap();
+		bootstrap.run();
+		
+		kernel = bootstrap.getKernel();
+		registry = kernel.getRegistry();
+		bus = kernel.getBus();		
+ 	}
+	
+	public void setUserInterface(UserInterface userInterface) {
+		this.userInterface = userInterface;
+	}
+	
+	void deploy() {
+		bootstrap.deploy(url);
+		if (manager == null) {
+			KernelControllerContext context = (KernelControllerContext) registry.getEntry(HRSERVICE);
+			if (context != null) { manager = (HRManager) context.getTarget(); }
+		}
+	}
+	
+	void undeploy() {
+		bootstrap.undeploy(url);
+	}
+	
+	private Object invoke(String serviceName, String methodName, Object[] args, String[] types) {
+		Object result = null;
+		try {
+			result = bus.invoke(serviceName, methodName, args, types);
+		} catch (Throwable t) {
+			t.printStackTrace();
+		}	
+		return result;
+	}
+	
+	boolean addEmployee() throws ParseException, NumberFormatException, IllegalArgumentException, IOException {
+		Employee newEmployee = userInterface.getEmployee();		
+		Address address = userInterface.getAddress();
+		Date dateOfBirth = userInterface.getDateOfBirth();		
+		newEmployee.setAddress(address);
+		newEmployee.setDateOfBirth(dateOfBirth);
+		
+		if (useBus)
+			return (Boolean) invoke(HRSERVICE, "addEmployee", new Object[] {newEmployee}, new String[] {EMPLOYEE});
+		else
+			return manager.addEmployee(newEmployee);			
+	}
+	
+	@SuppressWarnings("unchecked")
+	Set<Employee> listEmployees() {			
+		if (useBus)
+			return (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
+		else
+			return manager.getEmployees();
+	}
+	
+	void removeEmployee() throws IllegalArgumentException, IOException {			
+		Employee employee = userInterface.getEmployee();
+		
+		if (useBus)
+			invoke(HRSERVICE, "removeEmployee", new Object[] {employee}, new String[] {EMPLOYEE});
+		else
+			manager.removeEmployee(employee);
+	}
+	
+	Integer getSalary() throws IllegalArgumentException, IOException {
+		Employee employee = userInterface.getEmployee();
+
+		if (useBus)
+			return(Integer) invoke(HRSERVICE, "getSalary", new Object[] {employee}, new String[] {EMPLOYEE});
+		else
+			return manager.getSalary(employee);
+	}
+	
+	void setSalary() throws NumberFormatException, IllegalArgumentException, IOException {
+		Employee employee = userInterface.getEmployee();	
+		Integer salary = userInterface.getSalary();		
+		
+		Employee actualEmployee;
+		if (useBus) {
+			actualEmployee = (Employee) invoke(HRSERVICE, "getEmployee", new Object[] {employee.getFirstName(), employee.getLastName()}, new String[] {"java.lang.String","java.lang.String"});	
+			invoke(HRSERVICE, "setSalary", new Object[] {actualEmployee, salary}, new String[] {EMPLOYEE, "java.lang.Integer"});	
+		} else {
+			actualEmployee = manager.getEmployee(employee.getFirstName(), employee.getLastName());
+			manager.setSalary(actualEmployee, salary);			
+		}			
+	}
+	
+	boolean toggleHiringFreeze() {
+		boolean hiringFreeze;
+		if (useBus) {
+			hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[] {}, new String[] {});	
+			invoke(HRSERVICE, "setHiringFreeze", new Object[] {!hiringFreeze}, new String[] {"boolean"});	
+		} else {
+			hiringFreeze = manager.isHiringFreeze();
+			manager.setHiringFreeze(!hiringFreeze);
+		}
+		return !hiringFreeze;
+	}
+	
+	@SuppressWarnings("unchecked")
+	String printStatus() {
+		boolean hiringFreeze;
+		int totalEmployees;
+		SalaryStrategy salaryStrategy;
+		
+		if (useBus) {
+			hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[] {}, new String[] {});
+			Set<Employee> employees = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
+			totalEmployees = employees.size();				
+			salaryStrategy = (SalaryStrategy) invoke(HRSERVICE, "getSalaryStrategy", new Object[] {}, new String[] {});
+		} else {
+			hiringFreeze = manager.isHiringFreeze();
+			totalEmployees = manager.getEmployees().size();
+			salaryStrategy = manager.getSalaryStrategy();		
+		}	
+		
+		String strategy = "Unknown";
+		if (salaryStrategy == null) { strategy = "None"; }
+		else if (salaryStrategy instanceof AgeBasedSalaryStrategy ) { strategy = "AgeBased"; }
+		else if (salaryStrategy instanceof LocationBasedSalaryStrategy ) { strategy = "LocationBased"; }
+		
+		StringBuffer buffer = new StringBuffer();
+		buffer.append("Total number of employees: " + totalEmployees);
+		buffer.append("\nHiring Freeze: " + hiringFreeze);	
+		buffer.append("\nSalary Strategy: " + strategy);
+		if (salaryStrategy != null) {
+			buffer.append(" - MinSalary: " + salaryStrategy.getMinSalary() + " MaxSalary: " + salaryStrategy.getMaxSalary());
+		}
+		return buffer.toString();
+	}
+}

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,127 +0,0 @@
-package org.jboss.example.client;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-import org.jboss.example.service.Address;
-import org.jboss.example.service.Employee;
-
-public class ConsoleInput {
-	
-	public ConsoleInput(final Client client, final EmbeddedBootstrap bootstrap, final boolean useBus, final URL url) {
-		printMenu();
-		
-		Thread eventThread = new Thread(new Runnable() {
-			private boolean initialDeployment = false;
-			private boolean quit = false;
-			
-			public void run() {
-				
-				while (!quit) {
-
-					System.out.print(">");
-					
-					try {
-						String input = in.readLine();
-						if (input.length() != 1) {
-							continue;
-						}
-	
-						char option = input.charAt(0);
-						if ((option == '2' || option == '3' || option == 'a' || option == 'l' || option == 'r'
-							    || option == 'g' || option == 's' || option == 't' || option == 'p')
-								&& !useBus && initialDeployment == false) {
-							System.out.println("Service has not been deployed yet.");
-							continue;
-						}
-						
-						switch (option) {
-							case '1': bootstrap.deploy(url); client.cacheServiceRef(); initialDeployment = true; break;
-							case '2': bootstrap.undeploy(url); bootstrap.deploy(url); client.cacheServiceRef(); break;
-							case '3': bootstrap.undeploy(url); break;
-							case 'a': client.addEmployee(); break;
-							case 'l': client.listEmployees(); break;
-							case 'r': client.removeEmployee(); break;
-							case 'g': client.getSalary(); break;
-							case 's': client.setSalary(); break;
-							case 't': client.toggleHiringFreeze(); break;
-							case 'm': printMenu(); break;
-							case 'p': client.printStatus(); break;
-							case 'q': quit = true; break;
-							default: break;
-						}
-					} catch (ParseException e) {
-						System.out.println(e.getMessage());
-					} catch (NumberFormatException e) {
-						System.out.println("Invalid integer " + e.getMessage());
-					} catch (IllegalArgumentException e) {
-						System.out.println(e.getMessage());
-					} catch (IOException e) {
-						e.printStackTrace();
-					}					
-				}
-			}
-		});
-		
-		eventThread.start();			
-	}
-	
-	private void printMenu() {
-		System.out.println("-----------------------------------");
-		System.out.println("Menu:");
-		System.out.println();
-		System.out.println("1) Deploy Human Resources service");
-		System.out.println("2) Redeploy Human Resources service");
-		System.out.println("3) Undeploy Human Resources service");
-		System.out.println();
-		System.out.println("a) Add employee");
-		System.out.println("l) List employees");
-		System.out.println("r) Remove employee");
-		System.out.println("g) Get a salary");
-		System.out.println("s) Set a salary");
-		System.out.println("t) Toggle hiring freeze");
-		System.out.println();
-		System.out.println("m) Display menu");
-		System.out.println("p) Print service status");
-		System.out.println("q) Quit");
-	}
-
-	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-
-	public static Employee getEmployee() throws IllegalArgumentException, IOException {	
-
-		System.out.println("Please enter the employee's name [firstName lastName]:");
-		String name = in.readLine();
-		String[] names = name.split("\\s");
-		if (names.length != 2) { throw new IllegalArgumentException("Employees must have a first and last name."); }
-		return new Employee(names[0], names[1]);
-	}
-	
-	public static Address getAddress() throws NumberFormatException, IllegalArgumentException, IOException {
-	
-		System.out.println("Please enter the employee's address [number,street,city]:");
-		String address = in.readLine();
-		String[] lines = address.split(",");
-		if (lines.length != 3) { throw new IllegalArgumentException("Addresses must contain a number, street and city."); }
-		return new Address(Integer.parseInt(lines[0]), lines[1], lines[2]);
-	}
-	
-	public static Date getDateOfBirth() throws ParseException, IOException {
-	
-		System.out.println("Please enter the employee's date of birth [dd/MM/yyyy]:");
-		String date = in.readLine();
-		return new SimpleDateFormat("dd/MM/yyyy").parse(date);
-	}
-	
-	public static Integer getSalary()  throws NumberFormatException, IOException {	
-	
-		System.out.println("Please enter the employee's new salary [integer]: ");
-		String salary = in.readLine();
-		return Integer.valueOf(salary);
-	}
-}

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,127 @@
+package org.jboss.example.client;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.jboss.example.service.Address;
+import org.jboss.example.service.Employee;
+
+public class ConsoleInput implements UserInterface {
+	
+	private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+
+	public ConsoleInput(final Client client) {
+		System.out.println(getMenu());
+		
+		Thread eventThread = new Thread(new Runnable() {
+			private boolean initialDeployment = false;
+			private boolean quit = false;
+			
+			public void run() {
+				
+				while (!quit) {
+
+					System.out.print(">");
+					
+					try {
+						String input = in.readLine();
+						if (input.length() != 1) {
+							System.out.println("Please enter a valid option.");
+							continue;
+						}
+	
+						char option = input.charAt(0);
+						if (initialDeployment == false &&
+						    (option == 'u' || option == 'a' || option == 'l' || option == 'r' ||
+							 option == 'g' || option == 's' || option == 't' || option == 'p')) {
+							System.out.println("Service has not been deployed yet.");
+							continue;
+						}
+						
+						switch (option) {
+							case 'd': client.deploy(); initialDeployment = true; break;
+							case 'u': client.undeploy(); break;
+							case 'a': System.out.println("Added employee: " + client.addEmployee()); break;
+							case 'l': System.out.println("Employees: " + client.listEmployees()); break;
+							case 'r': client.removeEmployee(); break;
+							case 'g': System.out.println("Salary: " + client.getSalary()); break;
+							case 's': client.setSalary(); break;
+							case 't': System.out.println("Hiring Freeze: " + client.toggleHiringFreeze()); break;
+							case 'm': System.out.println(getMenu()); break;
+							case 'p': System.out.println(client.printStatus()); break;
+							case 'q': quit = true; break;
+							default: System.out.println("Invalid option."); break;
+						}
+					} catch (ParseException e) {
+						System.out.println(e.getMessage());
+					} catch (NumberFormatException e) {
+						System.out.println("Invalid integer " + e.getMessage());
+					} catch (IllegalArgumentException e) {
+						System.out.println(e.getMessage());
+					} catch (IOException e) {
+						e.printStackTrace();
+					}					
+				}
+			}
+		});
+		
+		eventThread.start();			
+	}
+	
+	private String getMenu() {
+		StringBuffer buffer = new StringBuffer();
+		buffer.append("-----------------------------------\n");
+		buffer.append("Menu:\n");
+		buffer.append("\n");
+		buffer.append("d) Deploy Human Resources service\n");
+		buffer.append("u) Undeploy Human Resources service\n");
+		buffer.append("\n");
+		buffer.append("a) Add employee\n");
+		buffer.append("l) List employees\n");
+		buffer.append("r) Remove employee\n");
+		buffer.append("g) Get a salary\n");
+		buffer.append("s) Set a salary\n");
+		buffer.append("t) Toggle hiring freeze\n");
+		buffer.append("\n");
+		buffer.append("m) Display menu\n");
+		buffer.append("p) Print service status\n");
+		buffer.append("q) Quit");
+		return buffer.toString();
+	}
+
+	public Employee getEmployee() throws IllegalArgumentException, IOException {	
+
+		System.out.println("Please enter the employee's name [firstName lastName]:");
+		String name = in.readLine();
+		String[] names = name.split("\\s");
+		if (names.length != 2) { throw new IllegalArgumentException("Employees must have a first and last name."); }
+		return new Employee(names[0], names[1]);
+	}
+	
+	public Address getAddress() throws NumberFormatException, IllegalArgumentException, IOException {
+	
+		System.out.println("Please enter the employee's address [number,street,city]:");
+		String address = in.readLine();
+		String[] lines = address.split(",");
+		if (lines.length != 3) { throw new IllegalArgumentException("Addresses must contain a number, street and city."); }
+		return new Address(Integer.parseInt(lines[0]), lines[1], lines[2]);
+	}
+	
+	public Date getDateOfBirth() throws ParseException, IOException {
+	
+		System.out.println("Please enter the employee's date of birth [dd/MM/yyyy]:");
+		String date = in.readLine();
+		return new SimpleDateFormat("dd/MM/yyyy").parse(date);
+	}
+	
+	public Integer getSalary()  throws NumberFormatException, IOException {	
+	
+		System.out.println("Please enter the employee's new salary [integer]: ");
+		String salary = in.readLine();
+		return Integer.valueOf(salary);
+	}
+}

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,48 +0,0 @@
-package org.jboss.example.client;
-
-import java.net.URL;
-
-import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
-import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
-
-public class EmbeddedBootstrap extends BasicBootstrap {
-	
-	protected BasicXMLDeployer deployer;
-
-	public EmbeddedBootstrap() throws Exception {
-		super();
-	}
-
-	public void bootstrap() throws Throwable {
-		super.bootstrap();
-		deployer = new BasicXMLDeployer(getKernel());
-		Runtime.getRuntime().addShutdownHook(new Shutdown());
-	}
-
-	public void deploy(URL url) {
-		try {
-			// Workaround the fact that the BasicXMLDeployer does not handle redeployment correctly
-			if (deployer.getDeploymentNames().contains(url.toString())) {
-				throw new IllegalArgumentException("Already installed " + url.toString());
-			}
-			deployer.deploy(url);
-		} catch (Throwable t) {
-			log.warn("Error during deployment: " + url, t);
-		}
-	}
-
-	public void undeploy(URL url) {
-		try {
-			deployer.undeploy(url);
-		} catch (Throwable t) {
-			log.warn("Error during undeployment: " + url, t);
-		}
-	}
-
-	protected class Shutdown extends Thread {
-		public void run() {
-			log.info("Shutting down");		
-			deployer.shutdown();
-		}
-	}	
-}
\ No newline at end of file

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,53 @@
+package org.jboss.example.client;
+
+import java.net.URL;
+
+import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
+import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
+
+public class EmbeddedBootstrap extends BasicBootstrap {
+	
+	protected BasicXMLDeployer deployer;
+
+	public EmbeddedBootstrap() throws Exception {
+		super();
+	}
+
+	public void bootstrap() throws Throwable {
+		super.bootstrap();
+		deployer = new BasicXMLDeployer(getKernel());
+		Runtime.getRuntime().addShutdownHook(new Shutdown());
+	}
+
+	public void deploy(URL url) {
+		try {
+			// Workaround the fact that the BasicXMLDeployer does not handle redeployment correctly
+			if (deployer.getDeploymentNames().contains(url.toString())) {
+				System.out.println("Service is already deployed.");
+				return;
+			}
+			deployer.deploy(url);
+		} catch (Throwable t) {
+			log.warn("Error during deployment: " + url, t);
+		}
+	}
+
+	public void undeploy(URL url) {
+		if (!deployer.getDeploymentNames().contains(url.toString())) {
+			System.out.println("Service is already undeployed.");
+			return;
+		}
+		try {
+			deployer.undeploy(url);
+		} catch (Throwable t) {
+			log.warn("Error during undeployment: " + url, t);
+		}
+	}
+
+	protected class Shutdown extends Thread {
+		public void run() {
+			log.info("Shutting down");		
+			deployer.shutdown();
+		}
+	}	
+}
\ No newline at end of file

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/UserInterface.java (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/UserInterface.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/UserInterface.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/java/org/jboss/example/client/UserInterface.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,15 @@
+package org.jboss.example.client;
+
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.Date;
+
+import org.jboss.example.service.Address;
+import org.jboss.example.service.Employee;
+
+public interface UserInterface {
+	Employee getEmployee() throws IOException;
+	Address getAddress() throws IOException;
+	Date getDateOfBirth() throws ParseException, IOException;
+	Integer getSalary() throws IOException;
+}

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,8 +0,0 @@
-log4j.rootLogger=DEBUG, stdout
-
-log4j.appender.stdout=org.apache.log4j.ConsoleAppender
-log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
-
-# Pattern to output the caller's file name and line number.
-log4j.appender.stdout.layout.ConversionPattern=Client %5p [%d{dd-MM-yyyy HH:mm:ss}] %c{1} - %m%n
-

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/main/resources/log4j.properties	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,7 @@
+log4j.rootLogger=DEBUG, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+
+# Pattern to output the caller's file name and line number.
+log4j.appender.stdout.layout.ConversionPattern=Client %5p [%d{dd-MM-yyyy HH:mm:ss}] %c{1} - %m%n

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,32 +0,0 @@
-package org.jboss.example.client;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-public class ClientTestCase extends TestCase
-{
-   public ClientTestCase(String name)
-   {
-      super(name);
-   }
-
-   /**
-    * Setup the test
-    *
-    * @return the test
-    */
-   public static Test suite()
-   {
-      TestSuite suite = new TestSuite();
-      suite.addTest(new TestSuite(ClientTestCase.class));
-      return suite;
-   }
-
-   public void testConfigure() throws Exception
-   {
-      Client client = new Client(false);
-      assertNotNull(client);
-   }
-
-}

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java (from rev 66727, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,45 @@
+package org.jboss.example.client;
+
+import junit.framework.TestCase;
+
+public class ClientTestCase extends TestCase
+{	
+    public ClientTestCase(String name) {
+		super(name);
+	}
+
+	public void testClientWithoutBus() throws Exception {
+		Client client = new Client(false);
+		assertNotNull(client);
+		runTests(client);
+	}
+	
+	public void testClientWithBus() throws Exception {
+		Client client = new Client(true);
+		assertNotNull(client);
+		runTests(client);
+	}
+	
+	private void runTests(Client client) throws Exception {
+		client.setUserInterface(new MockUserInterface());
+		
+		client.deploy();
+
+		assertEquals(true, client.toggleHiringFreeze());
+		assertEquals(false, client.addEmployee());
+		assertEquals(0, client.listEmployees().size());
+
+		assertEquals(false, client.toggleHiringFreeze());
+		assertEquals(true, client.addEmployee());
+		assertEquals(1, client.listEmployees().size());
+		assertEquals((Integer) 10000, client.getSalary());
+		
+		client.setSalary();
+		assertEquals((Integer) 50000, client.getSalary());
+		
+		client.removeEmployee();
+		assertEquals(0, client.listEmployees().size());
+		
+		client.undeploy();
+	}
+}

Deleted: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java	2007-11-05 08:42:57 UTC (rev 66724)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -1,24 +0,0 @@
-package org.jboss.example.client;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
-public class ClientTestSuite extends TestSuite
-{
-
-   public static void main(String[] args)
-   {
-      TestRunner.run(suite());
-   }
-
-   public static Test suite()
-   {
-      TestSuite suite = new TestSuite("CmdLineClient Tests");
-
-      suite.addTest(ClientTestCase.suite());
-
-      return suite;
-   }
-
-}

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java (from rev 66726, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,20 @@
+package org.jboss.example.client;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+public class ClientTestSuite extends TestSuite
+{
+   public static void main(String[] args) {
+      TestRunner.run(suite());
+   }
+
+   public static Test suite() {
+      TestSuite suite = new TestSuite("Client Tests");
+
+      suite.addTestSuite(ClientTestCase.class);
+
+      return suite;
+   }
+}

Copied: projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/MockUserInterface.java (from rev 66727, projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/MockUserInterface.java)
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/MockUserInterface.java	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/commandLineClient/src/test/java/org/jboss/example/client/MockUserInterface.java	2007-11-05 12:49:05 UTC (rev 66729)
@@ -0,0 +1,34 @@
+package org.jboss.example.client;
+
+import java.io.IOException;
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.jboss.example.service.Address;
+import org.jboss.example.service.Employee;
+
+public class MockUserInterface implements UserInterface {
+
+	public Address getAddress() throws IOException {
+		return new Address(5, "Oxford St", "London");
+	}
+
+	public Date getDateOfBirth() throws ParseException, IOException {
+		Calendar age = Calendar.getInstance();
+		int year = age.get(Calendar.YEAR);
+		int month = age.get(Calendar.MONTH);
+		int day = age.get(Calendar.DAY_OF_MONTH);
+		
+		age.set(year - 43, month, day);
+		return age.getTime();
+	}
+
+	public Employee getEmployee() throws IOException {
+		return new Employee("David", "Hasselhof");
+	}
+
+	public Integer getSalary() throws IOException {
+		return new Integer("50000");
+	}
+}




More information about the jboss-cvs-commits mailing list