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 | |
|
25 | |
|
26 | |
|
27 | |
public final class XmlConfigParser |
28 | |
implements SnmpConfiguration, SnmpAppContext { |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
private final URL url; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private final Daemon daemon; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
private final List<SnmpManagerConfiguration> managerList; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private final Roots roots; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private final Map<String, String> rootOidMap; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private final Map<ObjectName, String> mBeanOidMap; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
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 | |
|
131 | |
|
132 | |
|
133 | |
public String getListenerAddress () { |
134 | 4 | return this.daemon.getAddress(); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public Integer getListenerPort () { |
142 | 4 | return new Integer(this.daemon.getPort()); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public Integer getListenerSnmpVersion () { |
150 | 4 | return new Integer(this.daemon.getVersion()); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
public String getListenerReadCommunity () { |
158 | 4 | return this.daemon.getReadCommunity(); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String getListenerWriteCommunity () { |
166 | 4 | return this.daemon.getWriteCommunity(); |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public List<SnmpManagerConfiguration> getManagerList () { |
174 | 6 | return this.managerList; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public String getDefaultRootOid () { |
182 | 6 | return this.roots.getDefault(); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public Map<String, String> getRootOidMap () { |
190 | 4 | return this.rootOidMap; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public Map<ObjectName, String> getMBeanOidMap () { |
198 | 4 | return this.mBeanOidMap; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
@Override |
206 | |
public String toString () { |
207 | 1 | return "XmlConfigParser[" + this.url + "]"; |
208 | |
} |
209 | |
|
210 | |
} |