[jboss-cvs] JBossAS SVN: r66726 - in projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src: main/resources and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Nov 5 06:53:49 EST 2007
Author: newtonm
Date: 2007-11-05 06:53:49 -0500 (Mon, 05 Nov 2007)
New Revision: 66726
Added:
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/UserInterface.java
Modified:
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/Client.java
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java
projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java
Log:
Refactored client to use MVC pattern.
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/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 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/Client.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -19,30 +19,30 @@
public class Client {
- protected boolean useBus = false;
-
+ 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 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.out.println("Usage: java -jar client-1.0.0.jar [bus]");
System.exit(1);
}
- new Client(args.length == 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();
@@ -51,20 +51,28 @@
// Start JBoss Microcontainer
bootstrap = new EmbeddedBootstrap();
bootstrap.run();
+
kernel = bootstrap.getKernel();
registry = kernel.getRegistry();
- bus = kernel.getBus();
-
- new ConsoleInput(this, bootstrap, useBus, url);
+ bus = kernel.getBus();
}
- void cacheServiceRef() {
+ 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 {
@@ -75,34 +83,29 @@
return result;
}
- void addEmployee() throws ParseException, NumberFormatException, IllegalArgumentException, IOException {
- Employee newEmployee = ConsoleInput.getEmployee();
- Address address = ConsoleInput.getAddress();
- Date dateOfBirth = ConsoleInput.getDateOfBirth();
-
+ 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);
- boolean added;
if (useBus)
- added = (Boolean) invoke(HRSERVICE, "addEmployee", new Object[] {newEmployee}, new String[] {EMPLOYEE});
+ return (Boolean) invoke(HRSERVICE, "addEmployee", new Object[] {newEmployee}, new String[] {EMPLOYEE});
else
- added = manager.addEmployee(newEmployee);
- System.out.println("Added employee: " + added);
+ return manager.addEmployee(newEmployee);
}
@SuppressWarnings("unchecked")
- void listEmployees() {
- Set<Employee> employees;
+ Set<Employee> listEmployees() {
if (useBus)
- employees = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
+ return (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
else
- employees = manager.getEmployees();
- System.out.println("Employees: " + employees);
+ return manager.getEmployees();
}
void removeEmployee() throws IllegalArgumentException, IOException {
- Employee employee = ConsoleInput.getEmployee();
+ Employee employee = userInterface.getEmployee();
if (useBus)
invoke(HRSERVICE, "removeEmployee", new Object[] {employee}, new String[] {EMPLOYEE});
@@ -110,20 +113,18 @@
manager.removeEmployee(employee);
}
- void getSalary() throws IllegalArgumentException, IOException {
- Employee employee = ConsoleInput.getEmployee();
+ Integer getSalary() throws IllegalArgumentException, IOException {
+ Employee employee = userInterface.getEmployee();
- Integer salary = null;
if (useBus)
- salary = (Integer) invoke(HRSERVICE, "getSalary", new Object[] {employee}, new String[] {EMPLOYEE});
+ return(Integer) invoke(HRSERVICE, "getSalary", new Object[] {employee}, new String[] {EMPLOYEE});
else
- salary = manager.getSalary(employee);
- System.out.println("Salary: " + salary);
+ return manager.getSalary(employee);
}
void setSalary() throws NumberFormatException, IllegalArgumentException, IOException {
- Employee employee = ConsoleInput.getEmployee();
- Integer salary = ConsoleInput.getSalary();
+ Employee employee = userInterface.getEmployee();
+ Integer salary = userInterface.getSalary();
Employee actualEmployee;
if (useBus) {
@@ -135,7 +136,7 @@
}
}
- void toggleHiringFreeze() {
+ boolean toggleHiringFreeze() {
boolean hiringFreeze;
if (useBus) {
hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[] {}, new String[] {});
@@ -143,42 +144,39 @@
} else {
hiringFreeze = manager.isHiringFreeze();
manager.setHiringFreeze(!hiringFreeze);
- }
+ }
+ return !hiringFreeze;
}
@SuppressWarnings("unchecked")
- void printStatus() {
+ String 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;
- }
+ 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();
}
- System.out.println("Total number of employees: " + totalEmployees);
- System.out.println("Hiring Freeze: " + hiringFreeze);
- String strategy = "";
+ String strategy = "Unknown";
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);
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("Total number of employees: " + totalEmployees);
+ buffer.append("\nHiring Freeze: " + hiringFreeze);
+ buffer.append("\nSalary Strategy: " + strategy);
if (salaryStrategy != null) {
- System.out.print(" - MinSalary: " + salaryStrategy.getMinSalary() + " MaxSalary: " + salaryStrategy.getMaxSalary());
+ buffer.append(" - MinSalary: " + salaryStrategy.getMinSalary() + " MaxSalary: " + salaryStrategy.getMaxSalary());
}
- System.out.println();
+ return buffer.toString();
}
-}
\ No newline at end of file
+}
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/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 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -3,7 +3,6 @@
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;
@@ -11,10 +10,12 @@
import org.jboss.example.service.Address;
import org.jboss.example.service.Employee;
-public class ConsoleInput {
+public class ConsoleInput implements UserInterface {
- public ConsoleInput(final Client client, final EmbeddedBootstrap bootstrap, final boolean useBus, final URL url) {
- printMenu();
+ 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;
@@ -29,31 +30,31 @@
try {
String input = in.readLine();
if (input.length() != 1) {
+ System.out.println("Please enter a valid option.");
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) {
+ 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 '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 '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': client.getSalary(); break;
+ case 'g': System.out.println("Salary: " + client.getSalary()); break;
case 's': client.setSalary(); break;
- case 't': client.toggleHiringFreeze(); break;
- case 'm': printMenu(); break;
- case 'p': client.printStatus(); 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: break;
+ default: System.out.println("Invalid option."); break;
}
} catch (ParseException e) {
System.out.println(e.getMessage());
@@ -71,30 +72,29 @@
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 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();
}
- private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ public Employee getEmployee() throws IllegalArgumentException, IOException {
- 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");
@@ -102,7 +102,7 @@
return new Employee(names[0], names[1]);
}
- public static Address getAddress() throws NumberFormatException, IllegalArgumentException, IOException {
+ public Address getAddress() throws NumberFormatException, IllegalArgumentException, IOException {
System.out.println("Please enter the employee's address [number,street,city]:");
String address = in.readLine();
@@ -111,14 +111,14 @@
return new Address(Integer.parseInt(lines[0]), lines[1], lines[2]);
}
- public static Date getDateOfBirth() throws ParseException, IOException {
+ 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 static Integer getSalary() throws NumberFormatException, IOException {
+ public Integer getSalary() throws NumberFormatException, IOException {
System.out.println("Please enter the employee's new salary [integer]: ");
String salary = in.readLine();
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/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 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -23,7 +23,8 @@
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());
+ System.out.println("Service is already deployed.");
+ return;
}
deployer.deploy(url);
} catch (Throwable t) {
@@ -32,6 +33,10 @@
}
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) {
Added: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/UserInterface.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/UserInterface.java (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/UserInterface.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -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;
+}
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties 2007-11-05 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties 2007-11-05 11:53:49 UTC (rev 66726)
@@ -5,4 +5,3 @@
# 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
-
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/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 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestCase.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -1,32 +1,15 @@
package org.jboss.example.client;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import junit.framework.TestCase;
public class ClientTestCase extends TestCase
{
- public ClientTestCase(String name)
- {
+ 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
- {
+ public void testClient() throws Exception {
Client client = new Client(false);
assertNotNull(client);
}
-
}
Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/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 09:45:32 UTC (rev 66725)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/ClientTestSuite.java 2007-11-05 11:53:49 UTC (rev 66726)
@@ -6,19 +6,15 @@
public class ClientTestSuite extends TestSuite
{
-
- public static void main(String[] args)
- {
+ public static void main(String[] args) {
TestRunner.run(suite());
}
- public static Test suite()
- {
- TestSuite suite = new TestSuite("CmdLineClient Tests");
+ public static Test suite() {
+ TestSuite suite = new TestSuite("Client Tests");
- suite.addTest(ClientTestCase.suite());
+ suite.addTestSuite(ClientTestCase.class);
return suite;
}
-
}
More information about the jboss-cvs-commits
mailing list