Coverage Report - org.openpermis.examples.ejb.client.HelloWorldClient
 
Classes in this File Line Coverage Branch Coverage Complexity
HelloWorldClient
0%
0/39
0%
0/8
2.143
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.examples.ejb.client;
 11  
 
 12  
 import static javax.swing.JOptionPane.ERROR_MESSAGE;
 13  
 import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
 14  
 import static javax.swing.JOptionPane.NO_OPTION;
 15  
 import static javax.swing.JOptionPane.QUESTION_MESSAGE;
 16  
 import static javax.swing.JOptionPane.showMessageDialog;
 17  
 import static javax.swing.JOptionPane.showOptionDialog;
 18  
 
 19  
 import java.awt.BorderLayout;
 20  
 
 21  
 import javax.naming.InitialContext;
 22  
 import javax.naming.NamingException;
 23  
 import javax.swing.JComboBox;
 24  
 import javax.swing.JLabel;
 25  
 import javax.swing.JPanel;
 26  
 import javax.swing.SwingUtilities;
 27  
 
 28  
 import org.openpermis.examples.ejb.server.HelloWorldException;
 29  
 import org.openpermis.examples.ejb.server.HelloWorldServiceRemote;
 30  
 
 31  
 /**
 32  
  * Java client that prompts the user for a name and executes the hello world service.
 33  
  * <p>The client uses the configured naming context and tries to access the hello world service
 34  
  * from the EJB name passed on the command line. (The name at which the EJB is registered at
 35  
  * the naming context usually corresponds to the EAR name when deploying to a EJB3 container.)</p>
 36  
  * <p>The intresting part of this example, namely the call to the EJB is perfomed in method 
 37  
  * {@link #execute(String)}. The rest of this client deals with a simple user interface.</p>
 38  
  * @see #execute(String) 
 39  
  * @since 0.3.0
 40  
  */
 41  
 public class HelloWorldClient
 42  
         implements Runnable
 43  
 {
 44  
         
 45  
         //---- Static
 46  
         
 47  
         /**
 48  
          * The service context location.
 49  
          * @see #execute(String)
 50  
          * @since 0.3.0
 51  
          */
 52  
         private static final String SERVICE = "/HelloWorldService/remote";
 53  
 
 54  
         /**
 55  
          * The users offered at the chooser.
 56  
          * @since 0.3.0
 57  
          */
 58  0
         private static final String[] USER_CHOICES = new String[] {
 59  
                 "cn=john,o=post,c=ch",
 60  
                 "cn=sara,o=post,c=ch"
 61  
         };
 62  
         
 63  
         /**
 64  
          * Starts the hello world EJB client.
 65  
          * <p>Pass the name of the EJB as the first argument to the client.</p>
 66  
          * @param args the application arguments containing the EJB name as the first argument.
 67  
          * @see HelloWorldClient#run()
 68  
          * @since 0.3.0
 69  
          */
 70  
         public static void main (String[] args) {
 71  0
                 if (args.length == 0) {
 72  0
                         System.out.println("Usage: HelloWorldClient ejb-name");
 73  0
                         System.exit(0);
 74  
                 }
 75  0
                 SwingUtilities.invokeLater(new HelloWorldClient(args[0] + SERVICE));
 76  0
         }
 77  
 
 78  
         //---- State
 79  
         
 80  
         /**
 81  
          * The context location of the {@link HelloWorldServiceRemote}.
 82  
          * @since 0.3.0
 83  
          */
 84  
         private final String serviceName;
 85  
 
 86  
         //---- Constructors
 87  
         
 88  
         /**
 89  
          * Creates a new hello world client that connects to the specified service.
 90  
          * @param serviceName the context location of the {@link HelloWorldServiceRemote}.
 91  
          * @since 0.3.0
 92  
          */
 93  0
         public HelloWorldClient (String serviceName) {
 94  0
                 this.serviceName = serviceName;
 95  0
         }
 96  
         
 97  
         //---- Methods
 98  
         
 99  
         /**
 100  
          * Displays a message box.
 101  
          * @param message the message to display.
 102  
          * @param error {@code true} if the message to display is an error message.
 103  
          * @since 0.3.0
 104  
          */
 105  
         private void showMessage (String message, boolean error) {
 106  0
                 showMessageDialog(
 107  
                         null, message, "EJB Example", error ? ERROR_MESSAGE : INFORMATION_MESSAGE
 108  
                 );
 109  0
         }
 110  
         
 111  
         /**
 112  
          * Displays a prompt to choose a user for the hello world service.
 113  
          * @return the user chosen or {@code null} if aborted.
 114  
          * @since 0.3.0
 115  
          */
 116  
         private String chooseUser () {
 117  0
                 final JComboBox users = new JComboBox(USER_CHOICES);
 118  0
                 final JPanel userPanel = new JPanel(new BorderLayout());
 119  0
                 userPanel.add(new JLabel("Username: "), BorderLayout.WEST);
 120  0
                 userPanel.add(users, BorderLayout.CENTER);
 121  0
                 final String[] options = new String[] { "Execute", "Quit" };
 122  0
                 final int result = showOptionDialog(
 123  
                         null, new Object[] { userPanel }, "EJB Example", NO_OPTION,
 124  
                         QUESTION_MESSAGE, null, options, options[0]
 125  
                 );
 126  0
                 if (result != 0) {
 127  0
                         return null;
 128  
                 }
 129  0
                 return (String) users.getSelectedItem();
 130  
         }
 131  
 
 132  
         /**
 133  
          * Performs a lookup of the hello world service.
 134  
          * @return the hello world service.
 135  
          * @throws NamingException if the service lookup fails.
 136  
          * @since 0.3.0
 137  
          */
 138  
         private HelloWorldServiceRemote getHelloWorldService () throws NamingException {
 139  0
                 return (HelloWorldServiceRemote) new InitialContext().lookup(this.serviceName);
 140  
         }
 141  
         
 142  
         /**
 143  
          * Retrieves the hello message for the specified user.
 144  
          * @param user the user name to pass to the hello world service.
 145  
          * @since 0.3.0
 146  
          */
 147  
         private void execute (String user) {
 148  
                 try {
 149  0
                         final HelloWorldServiceRemote service = getHelloWorldService();
 150  0
                         final String message = "Service result for user '" + user + "'.\n";
 151  
                         try {
 152  0
                                 final String result = service.getHelloMessage(user);
 153  0
                                 showMessage(message + "Success: " + result, false);
 154  0
                         } catch (HelloWorldException e) {
 155  0
                                 showMessage(message + "Failure: " + e.getMessage(), true);
 156  0
                         }
 157  0
                 } catch (Exception e) {
 158  0
                         e.printStackTrace();
 159  0
                         showMessage("Internal error, see console for details.\n" + e.getMessage(), true);
 160  0
                         System.exit(0);
 161  0
                 }
 162  0
         }
 163  
 
 164  
         //---- Runnable
 165  
         
 166  
         /**
 167  
          * Starts the hello world EJB client.
 168  
          * <p>Prompts for a user name and executes the hello world service.</p>
 169  
          * @since 0.3.0
 170  
          */
 171  
         public void run () {
 172  
                 while (true) {
 173  0
                         final String user = chooseUser();
 174  0
                         if (user == null) {
 175  0
                                 System.exit(0);
 176  
                         }
 177  0
                         execute(user);
 178  0
                 }
 179  
         }
 180  
         
 181  
 }
 182