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.web.multi.server.app;
18  
19  import java.io.IOException;
20  import java.util.Date;
21  import java.util.Hashtable;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  import javax.servlet.annotation.WebServlet;
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.jboss.as.quickstarts.ejb.multi.server.app.AppOne;
34  import org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo;
35  
36  /**
37   * A simple servlet that is used to invoke all other application EJB's.
38   * 
39   * @author <a href="mailto:wfink@redhat.com">Wolf-Dieter Fink</a>
40   */
41  @WebServlet(urlPatterns = "/*")
42  public class Servlet extends HttpServlet {
43      private static final Logger LOGGER = Logger.getLogger(Servlet.class.getName());
44      private static final long serialVersionUID = 1L;
45  
46      protected void doGet(HttpServletRequest request, HttpServletResponse response) {
47          boolean fail = false;
48  
49          LOGGER.info("Servlet is called " + new Date());
50  
51          response.setContentType("html");
52          write(response, "<h1>Example Servlet to show how EJB's can be invoked</h1>");
53          write(response, "The node.names are read from the destination server via EJB invocation.<br/>");
54          write(response, "It shows the name of the host instance (host-controller) and the unique server name on this host.<br/>");
55  
56          try {
57              final InitialContext iCtx = getContext();
58  
59              write(response, "<h2>Invoke AppOne on different server</h2>");
60              try {
61                  AppOne proxy = (AppOne) lookup(response, iCtx,
62                      "ejb:jboss-ejb-multi-server-app-one/ejb//AppOneBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppOne");
63                  if (proxy != null) {
64                      write(response, "Invocation #1 return node.name => " + proxy.getJBossNodeName() + "<br/>");
65                      // second invocation shows whether the same or a different node is reached
66                      write(response, "Invocation #2 return node.name => " + proxy.getJBossNodeName() + "<br/>");
67                  } else {
68                      fail = true;
69                  }
70              } catch (Exception n) {
71                  LOGGER.log(Level.SEVERE, "Failed to invoke AppOne", n);
72                  write(response, "Failed to invoke AppOne<br/>");
73                  write(response, n.getMessage());
74                  fail = true;
75              }
76              write(response, "<h2>Invoke AppTwo on different server</h2>");
77              try {
78                  AppTwo proxy = (AppTwo) lookup(response, iCtx,
79                      "ejb:jboss-ejb-multi-server-app-two/ejb//AppTwoBean!org.jboss.as.quickstarts.ejb.multi.server.app.AppTwo");
80                  if (proxy != null) {
81                      write(response, "Invocation #1 return node.name => " + proxy.getJBossNodeName() + "<br/>");
82                      // second invocation shows whether the same or a different node is reached
83                      write(response, "Invocation #2 return node.name => " + proxy.getJBossNodeName() + "<br/>");
84                  } else {
85                      fail = true;
86                  }
87              } catch (Exception n) {
88                  LOGGER.log(Level.SEVERE, "Failed to invoke AppTwo", n);
89                  write(response, "Failed to invoke AppTwo<br/>");
90                  write(response, n.getMessage());
91                  fail = true;
92              }
93          } catch (NamingException e) {
94              write(response, "<h2>Failed to initialize InitialContext</h2>");
95              write(response, e.getMessage());
96          }
97  
98          if (fail) {
99              write(response,
100                 "<br/><br/><br/><p><b><i>Not all invocations are successful, see <i>EAP_HOME</i>/domain/servers/app-web/log/server.log</i></b></p>");
101         } else {
102             write(response, "<br/><br/><br/><p><i>All invocations are successful</i></p>");
103         }
104         write(response, "<p>Finish at " + new Date() + "</p>");
105     }
106 
107     private static void write(HttpServletResponse writer, String message) {
108 
109         try {
110             writer.getWriter().write(message + "\n");
111         } catch (IOException e) {
112             e.printStackTrace();
113         }
114     }
115 
116     private static InitialContext getContext() throws NamingException {
117         final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
118         jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
119         return new InitialContext(jndiProperties);
120     }
121 
122     private Object lookup(HttpServletResponse response, InitialContext ic, String name) {
123         try {
124             Object proxy = ic.lookup(name);
125             if (proxy == null) {
126                 write(response, "lookup(" + name + ") returns no proxy object");
127             }
128             return proxy;
129         } catch (NamingException e) {
130             write(response, "Failed to lookup(" + name + ")");
131             return null;
132         }
133     }
134 }