Coverage Report - sk.baka.webvm.HomePage
 
Classes in this File Line Coverage Branch Coverage Complexity
HomePage
90%
37/41
87%
14/16
2.1
HomePage$1
25%
1/4
0%
0/2
2.1
HomePage$2
100%
5/5
N/A
2.1
HomePage$2$1
100%
2/2
N/A
2.1
HomePage$3
100%
5/5
N/A
2.1
HomePage$EnvPropertiesProducer
100%
2/2
N/A
2.1
HomePage$SystemPropertiesProducer
100%
2/2
N/A
2.1
 
 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.ArrayList;
 25  
 import java.util.Collections;
 26  
 import java.util.Comparator;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 import java.util.logging.Level;
 30  
 import java.util.logging.Logger;
 31  
 import org.apache.wicket.markup.html.basic.Label;
 32  
 import org.apache.wicket.markup.html.link.Link;
 33  
 import org.apache.wicket.markup.html.list.ListItem;
 34  
 import org.apache.wicket.markup.html.list.ListView;
 35  
 import org.apache.wicket.model.IModel;
 36  
 import org.apache.wicket.model.LoadableDetachableModel;
 37  
 import sk.baka.tools.javaee.JeeServer;
 38  
 import sk.baka.webvm.misc.Producer;
 39  
 
 40  
 /**
 41  
  * Homepage
 42  
  * @author Martin Vysny
 43  
  */
 44  
 public class HomePage extends WebVMPage {
 45  
 
 46  
     private static final long serialVersionUID = 1L;
 47  
 
 48  
     /**
 49  
      * Constructor that is invoked when page is invoked without a session.
 50  
      */
 51  1
     public HomePage() {
 52  
         // system info
 53  1
         border.add(new Label("os", System.getProperty("os.name") + " " + System.getProperty("os.version")));
 54  1
         border.add(new Label("hw", System.getProperty("os.arch") + "; CPU#: " + Runtime.getRuntime().availableProcessors()));
 55  1
         border.add(new Label("java", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version") + " by " + System.getProperty("java.vm.vendor")));
 56  1
         border.add(new Label("netInterfaces", getAllInterfaces()));
 57  1
         final JeeServer server = JeeServer.getRuntimeNull();
 58  
         // java properties
 59  1
         listMap(border, new SystemPropertiesProducer(), "systemProperties", "sysPropName", "sysPropValue");
 60  
         // environment properties
 61  1
         listMap(border, new EnvPropertiesProducer(), "env", "envName", "envValue");
 62  1
         final Link<Void> link = new Link<Void>("asLink") {
 63  
 
 64  
             @Override
 65  
             public void onClick() {
 66  0
                 if (JeeServer.getRuntimeNull() != null) {
 67  0
                     setResponsePage(new AppServer(JeeServer.getRuntimeNull()));
 68  
                 }
 69  0
             }
 70  
         };
 71  1
         link.add(new Label("as", server == null ? "Unknown" : server.getServerName()));
 72  1
         border.add(link);
 73  1
     }
 74  
 
 75  
     private static String getAllInterfaces() {
 76  
         try {
 77  1
             final StringBuilder sb = new StringBuilder();
 78  1
             boolean first = true;
 79  1
             for (final NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
 80  2
                 if (first) {
 81  1
                     first = false;
 82  
                 } else {
 83  1
                     sb.append("; ");
 84  
                 }
 85  2
                 sb.append(iface.getName()).append(": ");
 86  2
                 for (final InetAddress ia : Collections.list(iface.getInetAddresses())) {
 87  4
                     if (ia instanceof Inet4Address) {
 88  2
                         sb.append(getIP(ia));
 89  
                     }
 90  
                 }
 91  
             }
 92  1
             return sb.toString();
 93  0
         } catch (Exception ex) {
 94  0
             LOG.log(Level.WARNING, "Failed to enumerate interfaces", ex);
 95  0
             return "Failed to enumerate interfaces: " + ex;
 96  
         }
 97  
     }
 98  1
     private final static Logger LOG = Logger.getLogger(HomePage.class.getName());
 99  
 
 100  
     /**
 101  
      * Returns formatted IP address.
 102  
      * @param adr the address object
 103  
      * @return the formatted IP address.
 104  
      */
 105  
     public static String getIP(InetAddress adr) {
 106  2
         final StringBuilder sb = new StringBuilder();
 107  2
         boolean first = true;
 108  10
         for (byte b : adr.getAddress()) {
 109  8
             if (first) {
 110  2
                 first = false;
 111  
             } else {
 112  6
                 sb.append('.');
 113  
             }
 114  8
             int num = b;
 115  8
             if (num < 0) {
 116  0
                 num += 256;
 117  
             }
 118  8
             sb.append(num);
 119  
         }
 120  2
         return sb.toString();
 121  
     }
 122  
 
 123  
     /**
 124  
      * Uses {@link ListView} to list contents of given map.
 125  
      * @param border the application border instance.
 126  
      * @param producer produces the map
 127  
      * @param listId connect the ListView to this wicket ID
 128  
      * @param keyId this wicket ID will display the map key
 129  
      * @param valueId this wicket ID will display the map value
 130  
      */
 131  
     private static void listMap(final AppBorder border, final Producer<Map<String, String>> producer, final String listId, final String keyId, final String valueId) {
 132  2
         final IModel<List<Map.Entry<String, String>>> model = new LoadableDetachableModel<List<Map.Entry<String, String>>>() {
 133  
 
 134  
             private static final long serialVersionUID = 1L;
 135  
 
 136  
             @Override
 137  
             protected List<Map.Entry<String, String>> load() {
 138  2
                 final Map<String, String> map = producer.produce();
 139  2
                 final List<Map.Entry<String, String>> result = new ArrayList<Map.Entry<String, String>>(map.entrySet());
 140  492
                 Collections.sort(result, new Comparator<Map.Entry<String, String>>() {
 141  
 
 142  
                     public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
 143  490
                         return o1.getKey().compareToIgnoreCase(o2.getKey());
 144  
                     }
 145  
                 });
 146  2
                 return result;
 147  
             }
 148  
         };
 149  2
         border.add(new ListView<Map.Entry<String, String>>(listId, model) {
 150  
 
 151  
             private static final long serialVersionUID = 1L;
 152  
 
 153  
             @Override
 154  
             protected void populateItem(final ListItem<Map.Entry<String, String>> item) {
 155  102
                 final Map.Entry<String, String> property = item.getModelObject();
 156  102
                 item.add(new Label(keyId, property.getKey()));
 157  102
                 item.add(new Label(valueId, property.getValue()));
 158  102
             }
 159  
         });
 160  2
     }
 161  
 
 162  3
     private static class SystemPropertiesProducer implements Producer<Map<String, String>> {
 163  
 
 164  
         private static final long serialVersionUID = 1L;
 165  
 
 166  
         @SuppressWarnings(value = "unchecked")
 167  
         public Map<String, String> produce() {
 168  1
             return (Map<String, String>) (Map) System.getProperties();
 169  
         }
 170  
     }
 171  
 
 172  3
     private static class EnvPropertiesProducer implements Producer<Map<String, String>> {
 173  
 
 174  
         private static final long serialVersionUID = 1L;
 175  
 
 176  
         public Map<String, String> produce() {
 177  1
             return System.getenv();
 178  
         }
 179  
     }
 180  
 }