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.ejb.multi.server.app;
18  
19  import java.security.Principal;
20  import java.util.Hashtable;
21  
22  import javax.annotation.PostConstruct;
23  import javax.annotation.Resource;
24  import javax.ejb.EJB;
25  import javax.ejb.SessionContext;
26  import javax.ejb.Stateless;
27  import javax.naming.Context;
28  import javax.naming.InitialContext;
29  import javax.naming.NamingException;
30  
31  import org.jboss.logging.Logger;
32  
33  /**
34   * <p>The main bean called by the standalone client.</p>
35   * <p>The sub applications, deployed in different servers are called direct or via indirect naming to hide the lookup name and use
36   * a configured name via comp/env environment.</p>
37   * 
38   * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
39   */
40  @Stateless
41  public class MainAppBean implements MainApp {
42      private static final Logger LOGGER = Logger.getLogger(MainAppBean.class);
43      @Resource
44      SessionContext context;
45  
46    /**
47     * The context to invoke foreign EJB's as the SessionContext can not be used for that.
48     */
49      private InitialContext iCtx;
50  
51      @EJB(lookup = "ejb:jboss-ejb-multi-server-app-one/ejb//AppOneBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppOne")
52      AppOne appOneProxy;
53      @EJB(lookup = "ejb:jboss-ejb-multi-server-app-two/ejb//AppTwoBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo")
54      AppTwo appTwoProxy;
55  
56    /**
57     * Initialize and store the context for the EJB invocations.
58     */
59      @PostConstruct
60      public void init() {
61          try {
62              final Hashtable<String, String> p = new Hashtable<String, String>();
63              p.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
64              this.iCtx = new InitialContext(p);
65          }catch (NamingException e) {
66              throw new RuntimeException("Could not initialize context", e);
67          }
68      }
69  
70      @Override
71      public String getJBossNodeName() {
72          return System.getProperty("jboss.node.name");
73      }
74  
75      @Override
76      public String invokeAll(String text) {
77          Principal caller = context.getCallerPrincipal();
78          LOGGER.info("[" + caller.getName() + "] " + text);
79          final StringBuilder result = new StringBuilder("MainApp[" + caller.getName() + "]@" + System.getProperty("jboss.node.name"));
80  
81      // Call AppOne with the direct ejb: naming
82          try {
83              result.append("  >  [ " + invokeAppOne(text));
84          }catch (Exception e) {
85              LOGGER.error("Could not invoke AppOne", e);
86          }
87  
88          String lookup = "";
89         // Call AppTwo with the direct ejb: naming
90          try {
91              lookup = "ejb:jboss-ejb-multi-server-app-two/ejb//AppTwoBean!" + AppTwo.class.getName();
92              result.append(" > " + invokeAppTwo(lookup, text));
93              LOGGER.info("Invoke '" + lookup + " OK");
94          }catch (Exception e) {
95              LOGGER.error("Could not invoke apptwo '" + lookup + "'", e);
96          }
97  
98          // Call AppTwo by using the local alias configured in
99          // META-INF/ejb-jar.xml
100         try {
101             lookup = "java:comp/env/AppTwoAlias";
102             result.append(" ; " + invokeAppTwo(lookup, text));
103             LOGGER.info("Invoke '" + lookup + " OK");
104         }catch (Exception e) {
105             LOGGER.error("Could not invoke apptwo '" + lookup + "'", e);
106         }
107 
108         result.append(" ]");
109 
110         return result.toString();
111     }
112 
113      /**
114      * The application one can only be called with the standard naming, there is
115      * no alias.
116      * 
117      * @param text
118      *            Simple text for logging in the target servers logfile
119      * @return A text with server details for demonstration
120      */
121     private String invokeAppOne(String text) {
122         try {
123             // invoke on the bean
124             final String appOneResult = this.appOneProxy.invoke(text);
125 
126             LOGGER.info("AppOne return : " + appOneResult);
127             return appOneResult;
128         }catch (Exception e) {
129             throw new RuntimeException("Could not invoke appOne", e);
130         }
131     }
132 
133     /**
134     * The application two can be called via lookup.
135     * <ul>
136     * <li>with the standard naming
137     * <i>ejb:jboss-ejb-multi-server-app-two/ejb//AppTwoBean!org.jboss.as.quickstarts
138     * .ejb.multi.server.app.AppTwo</i></li>
139     * <li><i>java:global/AliasAppTwo</i> the alias provided by the server
140     * configuration <b>this is not recommended</b></li>
141     * <li><i>java:comp/env/AppTwoAlias</i> the local alias provided by the
142     * ejb-jar.xml configuration</li>
143     * </ul>
144     * 
145     * @param text
146     *            Simple text for logging in the target servers logfile
147     * @return A text with server details for demonstration
148     */
149     private String invokeAppTwo(String lookup, String text) throws NamingException {
150         final AppTwo bean = (AppTwo) iCtx.lookup(lookup);
151 
152         // invoke on the bean
153         final String appTwoResult = bean.invoke(text);
154 
155         LOGGER.info("AppTwo return : " + appTwoResult);
156         return appTwoResult;
157     }
158 }