Coverage Report - sk.baka.webvm.AppServer
 
Classes in this File Line Coverage Branch Coverage Complexity
AppServer
0%
0/34
0%
0/12
3.25
 
 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  0
         super();
 40  0
         border.add(new Label("appServerName", server.getServerName()));
 41  0
         border.add(new Label("sampleCodeRemoteEjb", getSampleCodeRemoteEjb(server)));
 42  0
         border.add(new Label("userTransactionJndi", nullable(server.getUserTransactionJndi())));
 43  0
         String tm = null;
 44  
         try {
 45  0
             tm = nullable(server.getTransactionManagerJndi());
 46  0
         } catch (Exception e) {
 47  0
             tm = e.toString();
 48  0
         }
 49  0
         border.add(new Label("transactionManagerJndi", tm));
 50  0
         border.add(new Label("managerLookupClass", nullable(server.getHibernateTransactionManagerFactory())));
 51  0
     }
 52  
 
 53  
     private static String nullable(final String str) {
 54  0
         return str == null ? "unknown" : str;
 55  
     }
 56  
 
 57  
     private String getSampleCodeRemoteEjb(final JeeServer server) {
 58  0
         final StringBuilder sb = new StringBuilder();
 59  0
         sb.append("final Properties p = new Properties();\n");
 60  0
         final Properties p = server.getJNDIProperties(false);
 61  0
         p.put(Context.PROVIDER_URL, server.getRemoteURL(getMyIp(), null));
 62  0
         for (Object key : p.keySet()) {
 63  0
             sb.append("p.put(\"");
 64  0
             sb.append(key);
 65  0
             sb.append("\", \"");
 66  0
             sb.append(p.get(key));
 67  0
             sb.append("\");\n");
 68  
         }
 69  0
         sb.append("final Context c = new InitialContext(p);");
 70  0
         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  0
             for (final NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
 80  0
                 for (final InetAddress ia : Collections.list(iface.getInetAddresses())) {
 81  
                     // searching for IPV4 non-localhost address
 82  0
                     if (ia instanceof Inet4Address && ia.getAddress()[0] != 127) {
 83  0
                         return HomePage.getIP(ia);
 84  
                     }
 85  
                 }
 86  
             }
 87  0
         } catch (Exception ex) {
 88  0
             log.log(Level.WARNING, "Failed to detect IP address of this machine", ex);
 89  0
         }
 90  0
         return "127.0.0.1";
 91  
     }
 92  0
     private final static Logger log = Logger.getLogger(AppServer.class.getName());
 93  
 }