View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.client;
18  
19  import java.util.HashMap;
20  import java.util.Hashtable;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import javax.ejb.NoSuchEJBException;
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  
29  import org.jboss.as.quickstarts.sfsb.ShoppingCart;
30  import org.jboss.as.quickstarts.sfsb.ShoppingCartBean;
31  
32  public class Client {
33  
34      private static final String SOAP = "JBoss SOA Platform 6";
35      private static final String EAP = "Red Hat JBoss Enterprise Application Platform 6";
36  
37      public static void main(String[] args) throws NamingException {
38          // avoid INFO output for the client demo
39          Logger.getLogger("org.xnio").setLevel(Level.WARNING);
40          Logger.getLogger("org.jboss.remoting").setLevel(Level.WARNING);
41          Logger.getLogger("org.jboss.ejb.client").setLevel(Level.WARNING);
42  
43          // Create the JNDI InitialContext, configuring it for use with JBoss EJB
44          Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
45          jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
46          final Context context = new InitialContext(jndiProperties);
47  
48          /*
49           * The app name is the application name of the deployed EJBs. This is typically the ear name without the .ear suffix.
50           * However, the application name could be overridden in the application.xml of the EJB deployment on the server. Since
51           * we haven't deployed the application as a .ear, the app name for us will be an empty string
52           */
53          final String appName = "";
54  
55          /*
56           * This is the module name of the deployed EJBs on the server. This is typically the jar name of the EJB deployment,
57           * without the .jar suffix, but can be overridden via the ejb-jar.xml. In this example, we have deployed the EJBs in a
58           * jboss-shopping-cart-server.jar, so the module name is jboss-shopping-cart-server
59           */
60          final String moduleName = "jboss-shopping-cart-server";
61  
62          /*
63           * AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for our EJB
64           * deployment, so this is an empty string
65           */
66          final String distinctName = "";
67  
68          /*
69           * The EJB name which by default is the simple class name of the bean implementation class
70           */
71          final String beanName = ShoppingCartBean.class.getSimpleName();
72  
73          /* The remote view fully qualified class name */
74          final String viewClassName = ShoppingCart.class.getName();
75  
76          /* Lookup the remote interface of the shopping cart */
77          String lookupName = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName
78                  + "?stateful";
79          final ShoppingCart cart = (ShoppingCart) context.lookup(lookupName);
80  
81          System.out.println("\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
82          System.out.println("Obtained the remote interface to the shopping cart");
83  
84          /* invoke on the remote interface */
85          System.out.println("Buying a \"" + EAP + "\"");
86          cart.buy(EAP, 1);
87          System.out.println("Buying another \"" + EAP + "\"");
88          cart.buy(EAP, 1);
89  
90          System.out.println("Buying a \"" + SOAP + "\"");
91          cart.buy(SOAP, 1);
92  
93          System.out.println("\nPrint cart:");
94          HashMap<String, Integer> cartContents = cart.getCartContents();
95          for (String product : cartContents.keySet()) {
96              System.out.println(cartContents.get(product) + "     " + product);
97          }
98  
99          System.out.println("\nCheckout");
100         cart.checkout();
101 
102         /* Try to access the cart after checkout */
103         try {
104             cart.getCartContents();
105             System.err.println("ERROR: The cart should not be available after Checkout!");
106         } catch (NoSuchEJBException e) {
107             System.out.println("Cart was correctly removed, as expected, after Checkout and is no longer available!");
108         }
109         System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
110     }
111 }