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 javax.annotation.PostConstruct;
20  import javax.annotation.Resource;
21  import javax.ejb.EJB;
22  import javax.enterprise.inject.Model;
23  import javax.enterprise.inject.Produces;
24  import javax.inject.Named;
25  
26  import org.jboss.logging.Logger;
27  
28  /**
29   * A simple JSF controller to show how the EJB invocation on different servers.
30   * 
31   * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
32   */
33  @Model
34  public class JsfController {
35      private static final Logger LOOGER = Logger.getLogger(JsfController.class);
36      private EjbInvocation invocation;
37  
38    /**
39     * Inject the 'standard' bean.<br/>
40     * The simple @EJB injection is valid only if the MainApp is unique within the same application EAR archive.
41     * Since there is a MainAppSContextBean using same interface, we can use this @EJB approach and specify
42     * the name and interface as shown here to specify which exact reference should be injected.<br/>
43     * The beanInterface and the mappedName can be used in this case, but it is not necessary if the beanName is unique and implement only one interface.
44     */
45      @EJB(beanName = "MainAppBean", beanInterface = MainApp.class)
46      MainApp mainApp;
47  
48    /**
49     * Inject a different bean implementation of the same interface.<br/>
50     * Or use the @Resource annotation with the lookup name only.
51     */
52      @Resource(mappedName = "ejb:jboss-ejb-multi-server-app-main/ejb/MainAppSContextBean!org.jboss.as.quickstarts.ejb.multi.server.app.MainApp")
53      MainApp mainAppScopedContext;
54  
55    /**
56     * Injection with @EJB is not possible for foreign application in a different server. For this we can use @Resource.<br/>
57     * Lookup is introduced in Java EE6, so there are compiler or runtime problems if a Java version is used which not contain
58     * the <code>javax.annotation.Resource</code> <code>lookup</code>.
59     * Therefore a fix/workaround is necessary to be able to compile.
60     * See <a href="http://jaitechwriteups.blogspot.co.uk/2011/02/resource-and-new-lookup-attribute-how.html">Jaikiran's technical blog<a> 
61     */
62      @Resource(lookup = "ejb:jboss-ejb-multi-server-app-one/ejb//AppOneBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppOne")
63      AppOne oneApp;
64  
65    /**
66     * Injection with @EJB is not possible for a foreign application in a different server. For this we can use @Resource.
67     * Here, we use <code>mappedName</code>, which was available prior to Java EE 6, to avoid compilation errors.
68     */
69      @Resource(mappedName = "ejb:jboss-ejb-multi-server-app-two/ejb//AppTwoBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo")
70      AppTwo twoApp;
71  
72    /**
73     * Initialize the controller.
74     */
75      @PostConstruct
76      public void initForm() {
77          this.invocation = new EjbInvocation();
78      }
79  
80      @Produces
81      @Named
82      public EjbInvocation getInvocation() {
83          return this.invocation;
84      }
85  
86      public void callEJBMainLocal() {
87          LOOGER.info("Try to invoke the local MainApp to log the given text and get the invocation results. Proxy=" + mainApp);
88          this.invocation.setResult(mainApp.invokeAll(this.invocation.getText()));
89      }
90  
91      public void callEJBMainScopedContextLocal() {
92          LOOGER.info("Try to invoke the local MainAppSContext to log the given text and get the invocation results. Proxy=" + mainAppScopedContext);
93          this.invocation.setResult(mainAppScopedContext.invokeAll(this.invocation.getText()));
94      }
95  
96      public void callEJBAppOneRemote() {
97          LOOGER.info("Try to invoke the remote AppOne to log the given text and get the invocation results. Proxy=" + oneApp);
98          this.invocation.setResult(oneApp.invoke(this.invocation.getText()));
99      }
100 
101     public void callEJBAppTwoRemote() {
102         LOOGER.info("Try to invoke the remote AppTwo to log the given text and get the invocation results. Proxy=" + twoApp);
103         this.invocation.setResult(twoApp.invoke(this.invocation.getText()));
104     }
105 }