[jboss-cvs] JBossAS SVN: r66682 - in projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient: src/main/assembly and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Nov 2 12:31:18 EDT 2007


Author: newtonm
Date: 2007-11-02 12:31:18 -0400 (Fri, 02 Nov 2007)
New Revision: 66682

Added:
   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
Modified:
   projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/pom.xml
   projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/assembly/dist.xml
   projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/CmdLineClient.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/CmdLineClientTestCase.java
Log:
Completed command line client.

Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/pom.xml
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/pom.xml	2007-11-02 15:44:58 UTC (rev 66681)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/pom.xml	2007-11-02 16:31:18 UTC (rev 66682)
@@ -26,7 +26,7 @@
       <groupId>org.jboss.microcontainer.examples</groupId>
       <artifactId>humanResourcesService</artifactId>
       <version>1.0.0</version>
-      <scope>runtime</scope>
+      <scope>compile</scope>
     </dependency>
   </dependencies>
     
@@ -44,7 +44,7 @@
               <classpathPrefix>lib</classpathPrefix>  
             </manifest>
             <manifestEntries>
-              <Class-Path>. ./deploy/humanResourcesService-1.0.0.jar lib/jboss-common-logging-log4j-2.0.4.GA.jar lib/log4j-1.2.14.jar</Class-Path>
+              <Class-Path>. ./deploy/humanResourcesService-1.0.0.jar ./deploy/humanResourcesService-1.0.0.jar/ lib/jboss-common-logging-log4j-2.0.4.GA.jar lib/log4j-1.2.14.jar</Class-Path>
             </manifestEntries>
           </archive>
         </configuration>
@@ -56,7 +56,18 @@
             <descriptor>src/main/assembly/dist.xml</descriptor>
           </descriptors>
         </configuration>
-      </plugin>	   
+      </plugin>	 
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+          <debug>true</debug>
+          <showDeprecation>true</showDeprecation>
+          <showWarnings>true</showWarnings>
+          <optimize>true</optimize>
+        </configuration>
+      </plugin>  
     </plugins>
   </build>
 

Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/assembly/dist.xml
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/assembly/dist.xml	2007-11-02 15:44:58 UTC (rev 66681)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/assembly/dist.xml	2007-11-02 16:31:18 UTC (rev 66682)
@@ -19,10 +19,13 @@
       <outputDirectory>/lib</outputDirectory>
       <unpack>false</unpack>
       <scope>compile</scope>
+       <excludes>
+        <exclude>org.jboss.microcontainer.examples:humanResourcesService</exclude>
+      </excludes>
     </dependencySet>
     <dependencySet>
       <outputDirectory>/deploy</outputDirectory>
-      <unpack>false</unpack>
+      <unpack>true</unpack>
       <scope>runtime</scope>
       <includes>
         <include>org.jboss.microcontainer.examples:humanResourcesService</include>

Modified: projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/CmdLineClient.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/CmdLineClient.java	2007-11-02 15:44:58 UTC (rev 66681)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/CmdLineClient.java	2007-11-02 16:31:18 UTC (rev 66682)
@@ -1,20 +1,184 @@
 package org.jboss.example.client;
 
-import org.jboss.kernel.plugins.bootstrap.standalone.StandaloneBootstrap;
+import java.io.IOException;
+import java.net.URL;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Set;
 
-/**
- * @author <a href="mailto:mark.newton at jboss.org">Mark Newton</a>
- */
+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 CmdLineClient {
     
-    public static void main( String[] args ) throws Exception
-    {
-        System.out.println("Hello from the CmdLineClient main method");
-        StandaloneBootstrap.main(args);
-        System.out.println("Bye from the CmdLineClient main method");
+	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 CmdLineClient(args.length == 1);
     }
 
-	public CmdLineClient() {
-		System.out.println("Hello from the CmdLineClient constructor");
+	public CmdLineClient(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

Added: 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	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/ConsoleInput.java	2007-11-02 16:31:18 UTC (rev 66682)
@@ -0,0 +1,127 @@
+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 CmdLineClient 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);
+	}
+}

Added: 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	                        (rev 0)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/java/org/jboss/example/client/EmbeddedBootstrap.java	2007-11-02 16:31:18 UTC (rev 66682)
@@ -0,0 +1,48 @@
+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

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-02 15:44:58 UTC (rev 66681)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/main/resources/log4j.properties	2007-11-02 16:31:18 UTC (rev 66682)
@@ -2,6 +2,7 @@
 
 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=MC Examples %5p [%d{dd-MM-yyyy HH:mm:ss}] %c{1} - %m%n
+log4j.appender.stdout.layout.ConversionPattern=CmdLineClient %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/CmdLineClientTestCase.java
===================================================================
--- projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/CmdLineClientTestCase.java	2007-11-02 15:44:58 UTC (rev 66681)
+++ projects/microcontainer/trunk/docs/examples/User_Guide/cmdLineClient/src/test/java/org/jboss/example/client/CmdLineClientTestCase.java	2007-11-02 16:31:18 UTC (rev 66682)
@@ -28,7 +28,7 @@
 
    public void testConfigure() throws Exception
    {
-      CmdLineClient client = new CmdLineClient();
+      CmdLineClient client = new CmdLineClient(false);
       assertNotNull(client);
    }
 




More information about the jboss-cvs-commits mailing list