Coverage Report - net.sf.snmpadaptor4j.config.XmlConfigParser
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlConfigParser
100 %
41/41
100 %
16/16
1,667
 
 1  
 package net.sf.snmpadaptor4j.config;
 2  
 
 3  
 import java.net.URL;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Collections;
 6  
 import java.util.HashMap;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 import javax.management.ObjectName;
 10  
 import javax.xml.bind.JAXBContext;
 11  
 import javax.xml.bind.JAXBElement;
 12  
 import javax.xml.bind.Unmarshaller;
 13  
 import net.sf.snmpadaptor4j.SnmpAppContext;
 14  
 import net.sf.snmpadaptor4j.SnmpConfiguration;
 15  
 import net.sf.snmpadaptor4j.SnmpManagerConfiguration;
 16  
 import net.sf.snmpadaptor4j.config.jaxb.Config;
 17  
 import net.sf.snmpadaptor4j.config.jaxb.Daemon;
 18  
 import net.sf.snmpadaptor4j.config.jaxb.MBean;
 19  
 import net.sf.snmpadaptor4j.config.jaxb.Manager;
 20  
 import net.sf.snmpadaptor4j.config.jaxb.Root;
 21  
 import net.sf.snmpadaptor4j.config.jaxb.Roots;
 22  
 
 23  
 /**
 24  
  * XML parser of SNMP configuration.
 25  
  * @author <a href="http://fr.linkedin.com/in/jpminetti/">Jean-Philippe MINETTI</a>
 26  
  */
 27  
 public final class XmlConfigParser
 28  
                 implements SnmpConfiguration, SnmpAppContext {
 29  
 
 30  
         /**
 31  
          * {@link URL} to the SNMP configuration file.
 32  
          */
 33  
         private final URL url;
 34  
 
 35  
         /**
 36  
          * XSD object representing the daemon configuration.
 37  
          */
 38  
         private final Daemon daemon;
 39  
 
 40  
         /**
 41  
          * List of managers where to send all notifications (SNMP traps).
 42  
          */
 43  
         private final List<SnmpManagerConfiguration> managerList;
 44  
 
 45  
         /**
 46  
          * XSD object containing all root OIDs.
 47  
          */
 48  
         private final Roots roots;
 49  
 
 50  
         /**
 51  
          * Map of root OIDs where the attributes of the application will stay.
 52  
          */
 53  
         private final Map<String, String> rootOidMap;
 54  
 
 55  
         /**
 56  
          * Map of MBean OIDs.
 57  
          */
 58  
         private final Map<ObjectName, String> mBeanOidMap;
 59  
 
 60  
         /**
 61  
          * Creates and returns a new instance of {@link XmlConfigParser}.
 62  
          * @param url {@link URL} to the SNMP configuration file.
 63  
          * @return New instance of {@link XmlConfigParser}.
 64  
          * @throws Exception Exception if an error occurred.
 65  
          */
 66  
         public static XmlConfigParser newInstance (final URL url) throws Exception {
 67  5
                 final JAXBContext jaxbContext = JAXBContext.newInstance(Config.class.getPackage().getName());
 68  5
                 final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
 69  
                 @SuppressWarnings("unchecked")
 70  5
                 final JAXBElement<Config> jaxbElement = (JAXBElement<Config>) unmarshaller.unmarshal(url);
 71  5
                 final Config config = jaxbElement.getValue();
 72  
 
 73  
                 // managerList
 74  5
                 final List<SnmpManagerConfiguration> managerList = new ArrayList<SnmpManagerConfiguration>();
 75  5
                 if (config.getManagers() != null) {
 76  4
                         for (final Manager manager : config.getManagers().getManager()) {
 77  8
                                 managerList.add(new SnmpManagerConfiguration(manager.getAddress(), manager.getPort(), manager.getVersion(), manager.getCommunity()));
 78  
                         }
 79  
                 }
 80  
 
 81  
                 // rootOidMap
 82  5
                 final Map<String, String> rootOidMap = new HashMap<String, String>();
 83  5
                 for (final Root root : config.getRoots().getRoot()) {
 84  12
                         if (root.getOid().trim().length() > 0) {
 85  8
                                 rootOidMap.put(root.getId(), root.getOid());
 86  
                         }
 87  
                 }
 88  
 
 89  
                 // mBeanOidMap
 90  5
                 final Map<ObjectName, String> mBeanOidMap = new HashMap<ObjectName, String>();
 91  5
                 if (config.getMbeans() != null) {
 92  
                         String oid;
 93  4
                         for (final MBean mBean : config.getMbeans().getMbean()) {
 94  16
                                 oid = null;
 95  16
                                 if (mBean.getRoot() != null) {
 96  12
                                         oid = rootOidMap.get(mBean.getRoot());
 97  
                                 }
 98  16
                                 if (oid == null) {
 99  8
                                         oid = config.getRoots().getDefault();
 100  
                                 }
 101  16
                                 oid = oid + "." + mBean.getOid();
 102  16
                                 mBeanOidMap.put(new ObjectName(mBean.getName()), oid);
 103  
                         }
 104  
                 }
 105  
 
 106  5
                 return new XmlConfigParser(url, config.getDaemon(), managerList, config.getRoots(), rootOidMap, mBeanOidMap);
 107  
         }
 108  
 
 109  
         /**
 110  
          * Constructor.
 111  
          * @param url {@link URL} to the SNMP configuration file.
 112  
          * @param daemon XSD object representing the daemon configuration.
 113  
          * @param managerList List of managers where to send all notifications (SNMP traps).
 114  
          * @param roots XSD object containing all root OIDs.
 115  
          * @param rootOidMap Map of root OIDs where the attributes of the application will stay.
 116  
          * @param mBeanOidMap Map of MBean OIDs.
 117  
          */
 118  
         private XmlConfigParser (final URL url, final Daemon daemon, final List<SnmpManagerConfiguration> managerList, final Roots roots,
 119  
                         final Map<String, String> rootOidMap, final Map<ObjectName, String> mBeanOidMap) {
 120  5
                 super();
 121  5
                 this.url = url;
 122  5
                 this.daemon = daemon;
 123  5
                 this.managerList = Collections.unmodifiableList(managerList);
 124  5
                 this.roots = roots;
 125  5
                 this.rootOidMap = Collections.unmodifiableMap(rootOidMap);
 126  5
                 this.mBeanOidMap = Collections.unmodifiableMap(mBeanOidMap);
 127  5
         }
 128  
 
 129  
         /*
 130  
          * {@inheritDoc}
 131  
          * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerAddress()
 132  
          */
 133  
         public String getListenerAddress () {
 134  4
                 return this.daemon.getAddress();
 135  
         }
 136  
 
 137  
         /*
 138  
          * {@inheritDoc}
 139  
          * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerPort()
 140  
          */
 141  
         public Integer getListenerPort () {
 142  4
                 return new Integer(this.daemon.getPort());
 143  
         }
 144  
 
 145  
         /*
 146  
          * {@inheritDoc}
 147  
          * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerSnmpVersion()
 148  
          */
 149  
         public Integer getListenerSnmpVersion () {
 150  4
                 return new Integer(this.daemon.getVersion());
 151  
         }
 152  
 
 153  
         /*
 154  
          * {@inheritDoc}
 155  
          * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerReadCommunity()
 156  
          */
 157  
         public String getListenerReadCommunity () {
 158  4
                 return this.daemon.getReadCommunity();
 159  
         }
 160  
 
 161  
         /*
 162  
          * {@inheritDoc}
 163  
          * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerWriteCommunity()
 164  
          */
 165  
         public String getListenerWriteCommunity () {
 166  4
                 return this.daemon.getWriteCommunity();
 167  
         }
 168  
 
 169  
         /*
 170  
          * {@inheritDoc}
 171  
          * @see net.sf.snmpadaptor4j.SnmpConfiguration#getManagerList()
 172  
          */
 173  
         public List<SnmpManagerConfiguration> getManagerList () {
 174  6
                 return this.managerList;
 175  
         }
 176  
 
 177  
         /*
 178  
          * {@inheritDoc}
 179  
          * @see net.sf.snmpadaptor4j.SnmpAppContext#getDefaultRootOid()
 180  
          */
 181  
         public String getDefaultRootOid () {
 182  6
                 return this.roots.getDefault();
 183  
         }
 184  
 
 185  
         /*
 186  
          * {@inheritDoc}
 187  
          * @see net.sf.snmpadaptor4j.SnmpAppContext#getRootOidMap()
 188  
          */
 189  
         public Map<String, String> getRootOidMap () {
 190  4
                 return this.rootOidMap;
 191  
         }
 192  
 
 193  
         /*
 194  
          * {@inheritDoc}
 195  
          * @see net.sf.snmpadaptor4j.SnmpAppContext#getMBeanOidMap()
 196  
          */
 197  
         public Map<ObjectName, String> getMBeanOidMap () {
 198  4
                 return this.mBeanOidMap;
 199  
         }
 200  
 
 201  
         /*
 202  
          * {@inheritDoc}
 203  
          * @see java.lang.Object#toString()
 204  
          */
 205  
         @Override
 206  
         public String toString () {
 207  1
                 return "XmlConfigParser[" + this.url + "]";
 208  
         }
 209  
 
 210  
 }