View Javadoc

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  		final JAXBContext jaxbContext = JAXBContext.newInstance(Config.class.getPackage().getName());
68  		final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
69  		@SuppressWarnings("unchecked")
70  		final JAXBElement<Config> jaxbElement = (JAXBElement<Config>) unmarshaller.unmarshal(url);
71  		final Config config = jaxbElement.getValue();
72  
73  		// managerList
74  		final List<SnmpManagerConfiguration> managerList = new ArrayList<SnmpManagerConfiguration>();
75  		if (config.getManagers() != null) {
76  			for (final Manager manager : config.getManagers().getManager()) {
77  				managerList.add(new SnmpManagerConfiguration(manager.getAddress(), manager.getPort(), manager.getVersion(), manager.getCommunity()));
78  			}
79  		}
80  
81  		// rootOidMap
82  		final Map<String, String> rootOidMap = new HashMap<String, String>();
83  		for (final Root root : config.getRoots().getRoot()) {
84  			if (root.getOid().trim().length() > 0) {
85  				rootOidMap.put(root.getId(), root.getOid());
86  			}
87  		}
88  
89  		// mBeanOidMap
90  		final Map<ObjectName, String> mBeanOidMap = new HashMap<ObjectName, String>();
91  		if (config.getMbeans() != null) {
92  			String oid;
93  			for (final MBean mBean : config.getMbeans().getMbean()) {
94  				oid = null;
95  				if (mBean.getRoot() != null) {
96  					oid = rootOidMap.get(mBean.getRoot());
97  				}
98  				if (oid == null) {
99  					oid = config.getRoots().getDefault();
100 				}
101 				oid = oid + "." + mBean.getOid();
102 				mBeanOidMap.put(new ObjectName(mBean.getName()), oid);
103 			}
104 		}
105 
106 		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 		super();
121 		this.url = url;
122 		this.daemon = daemon;
123 		this.managerList = Collections.unmodifiableList(managerList);
124 		this.roots = roots;
125 		this.rootOidMap = Collections.unmodifiableMap(rootOidMap);
126 		this.mBeanOidMap = Collections.unmodifiableMap(mBeanOidMap);
127 	}
128 
129 	/*
130 	 * {@inheritDoc}
131 	 * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerAddress()
132 	 */
133 	public String getListenerAddress () {
134 		return this.daemon.getAddress();
135 	}
136 
137 	/*
138 	 * {@inheritDoc}
139 	 * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerPort()
140 	 */
141 	public Integer getListenerPort () {
142 		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 		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 		return this.daemon.getReadCommunity();
159 	}
160 
161 	/*
162 	 * {@inheritDoc}
163 	 * @see net.sf.snmpadaptor4j.api.SnmpDaemonConfiguration#getListenerWriteCommunity()
164 	 */
165 	public String getListenerWriteCommunity () {
166 		return this.daemon.getWriteCommunity();
167 	}
168 
169 	/*
170 	 * {@inheritDoc}
171 	 * @see net.sf.snmpadaptor4j.SnmpConfiguration#getManagerList()
172 	 */
173 	public List<SnmpManagerConfiguration> getManagerList () {
174 		return this.managerList;
175 	}
176 
177 	/*
178 	 * {@inheritDoc}
179 	 * @see net.sf.snmpadaptor4j.SnmpAppContext#getDefaultRootOid()
180 	 */
181 	public String getDefaultRootOid () {
182 		return this.roots.getDefault();
183 	}
184 
185 	/*
186 	 * {@inheritDoc}
187 	 * @see net.sf.snmpadaptor4j.SnmpAppContext#getRootOidMap()
188 	 */
189 	public Map<String, String> getRootOidMap () {
190 		return this.rootOidMap;
191 	}
192 
193 	/*
194 	 * {@inheritDoc}
195 	 * @see net.sf.snmpadaptor4j.SnmpAppContext#getMBeanOidMap()
196 	 */
197 	public Map<ObjectName, String> getMBeanOidMap () {
198 		return this.mBeanOidMap;
199 	}
200 
201 	/*
202 	 * {@inheritDoc}
203 	 * @see java.lang.Object#toString()
204 	 */
205 	@Override
206 	public String toString () {
207 		return "XmlConfigParser[" + this.url + "]";
208 	}
209 
210 }