View Javadoc

1   /***
2    * Copyright 2009 Martin Vysny.
3    *
4    * This file is part of WebVM.
5    *
6    * WebVM is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * WebVM is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package sk.baka.webvm;
20  
21  import java.net.Inet4Address;
22  import java.net.InetAddress;
23  import java.net.NetworkInterface;
24  import java.util.Collections;
25  import java.util.Properties;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  import javax.naming.Context;
29  import org.apache.wicket.markup.html.basic.Label;
30  import sk.baka.tools.javaee.JeeServer;
31  
32  /***
33   * Shows an information about an application server: the connection strings, Hibernate configuration etc.
34   * @author Martin Vysny
35   */
36  public class AppServer extends WebVMPage {
37  
38      public AppServer(final JeeServer server) {
39          super();
40          border.add(new Label("appServerName", server.getServerName()));
41          border.add(new Label("sampleCodeRemoteEjb", getSampleCodeRemoteEjb(server)));
42          border.add(new Label("userTransactionJndi", nullable(server.getUserTransactionJndi())));
43          String tm = null;
44          try {
45              tm = nullable(server.getTransactionManagerJndi());
46          } catch (Exception e) {
47              tm = e.toString();
48          }
49          border.add(new Label("transactionManagerJndi", tm));
50          border.add(new Label("managerLookupClass", nullable(server.getHibernateTransactionManagerFactory())));
51      }
52  
53      private static String nullable(final String str) {
54          return str == null ? "unknown" : str;
55      }
56  
57      private String getSampleCodeRemoteEjb(final JeeServer server) {
58          final StringBuilder sb = new StringBuilder();
59          sb.append("final Properties p = new Properties();\n");
60          final Properties p = server.getJNDIProperties(false);
61          p.put(Context.PROVIDER_URL, server.getRemoteURL(getMyIp(), null));
62          for (Object key : p.keySet()) {
63              sb.append("p.put(\"");
64              sb.append(key);
65              sb.append("\", \"");
66              sb.append(p.get(key));
67              sb.append("\");\n");
68          }
69          sb.append("final Context c = new InitialContext(p);");
70          return sb.toString();
71      }
72  
73      /***
74       * Retrieves an IP address of this machine. Returns localhost if the address couldn't be retrieved.
75       * @return this machine IP address.
76       */
77      private static String getMyIp() {
78          try {
79              for (final NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
80                  for (final InetAddress ia : Collections.list(iface.getInetAddresses())) {
81                      // searching for IPV4 non-localhost address
82                      if (ia instanceof Inet4Address && ia.getAddress()[0] != 127) {
83                          return HomePage.getIP(ia);
84                      }
85                  }
86              }
87          } catch (Exception ex) {
88              log.log(Level.WARNING, "Failed to detect IP address of this machine", ex);
89          }
90          return "127.0.0.1";
91      }
92      private final static Logger log = Logger.getLogger(AppServer.class.getName());
93  }