KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > server > Device


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.mrtg.server;
26
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 import java.io.IOException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 class Device {
38     private String JavaDoc host = "";
39     private String JavaDoc community = "";
40     private String JavaDoc descr = "";
41     private boolean active = true;
42     private Vector JavaDoc links = new Vector JavaDoc();
43
44     Device() { }
45
46     Device(Node JavaDoc routerNode) {
47         NodeList JavaDoc nodes = routerNode.getChildNodes();
48         for(int i = 0; i < nodes.getLength(); i++) {
49             Node JavaDoc node = nodes.item(i);
50             String JavaDoc name = node.getNodeName();
51             Node JavaDoc firstChild = node.getFirstChild();
52             String JavaDoc value = (firstChild != null)? firstChild.getNodeValue().trim(): null;
53             if(name.equals("host")) {
54                 setHost(value);
55             }
56             else if(name.equals("community")) {
57                 setCommunity(value);
58             }
59             else if(name.equals("description")) {
60                 setDescr(value);
61             }
62             else if(name.equals("active")) {
63                 setActive(new Boolean JavaDoc(node.getFirstChild().getNodeValue().trim()).booleanValue());
64             }
65             else if(name.equals("interface")) {
66                 links.add(new Port(node));
67             }
68         }
69     }
70
71     String JavaDoc getHost() {
72         return host;
73     }
74
75     void setHost(String JavaDoc host) {
76         if(host != null) {
77             this.host = host;
78         }
79     }
80
81     String JavaDoc getCommunity() {
82         return community;
83     }
84
85     void setCommunity(String JavaDoc community) {
86         if(community != null) {
87             this.community = community;
88         }
89     }
90
91     String JavaDoc getDescr() {
92         return descr;
93     }
94
95     void setDescr(String JavaDoc descr) {
96         if(descr != null) {
97             this.descr = descr;
98         }
99     }
100
101     boolean isActive() {
102         return active;
103     }
104
105     boolean getActive() {
106         return active;
107     }
108
109     void setActive(boolean active) {
110         this.active = active;
111     }
112
113     Vector JavaDoc getLinks() {
114         return links;
115     }
116
117     void setLinks(Vector JavaDoc links) {
118         this.links = links;
119     }
120
121     public String JavaDoc toString() {
122         String JavaDoc buff = new String JavaDoc();
123         buff += "Router: " + host + " -- " + "community=" + community + ", ";
124         buff += "descr=" + descr + ", ";
125         buff += "active=" + active + "\n";
126         // dump links
127
for(int i = 0; i < links.size(); i++) {
128             Port link = (Port) links.get(i);
129             buff += " Link: " + link + "\n";
130         }
131         return buff;
132     }
133
134     Port getLinkByIfDescr(String JavaDoc ifDescr) {
135         for(int i = 0; i < links.size(); i++) {
136             Port link = (Port) links.get(i);
137             if(link.getIfDescr().equalsIgnoreCase(ifDescr)) {
138                 return link;
139             }
140         }
141         return null;
142     }
143
144     void addLink(Port link) {
145         links.add(link);
146     }
147
148     void removeLink(Port link) {
149         links.remove(link);
150     }
151
152     int getLinkCount() {
153         return links.size();
154     }
155
156     String JavaDoc[] getAvailableLinks() throws IOException JavaDoc {
157         Poller comm = null;
158         try {
159             comm = new Poller(host, community);
160             Map JavaDoc links = comm.walkIfDescr();
161             return (String JavaDoc[]) links.values().toArray(new String JavaDoc[0]);
162         }
163         finally {
164             if(comm != null) {
165                 comm.close();
166             }
167         }
168     }
169
170     Hashtable JavaDoc getRouterInfo() {
171         Hashtable JavaDoc table = new Hashtable JavaDoc();
172         table.put("host", host);
173         table.put("community", community);
174         table.put("descr", descr);
175         table.put("active", new Boolean JavaDoc(active));
176         // add link info
177
Vector JavaDoc linkData = new Vector JavaDoc();
178         for (int i = 0; i < links.size(); i++) {
179             Port link = (Port) links.get(i);
180             linkData.add(link.getLinkInfo());
181         }
182         table.put("links", linkData);
183         return table;
184     }
185
186     void appendXml(Element JavaDoc root) {
187         Document JavaDoc doc = root.getOwnerDocument();
188         Element JavaDoc routerElem = doc.createElement("router");
189         root.appendChild(routerElem);
190         Element JavaDoc hostElem = doc.createElement("host");
191         hostElem.appendChild(doc.createTextNode(host));
192         routerElem.appendChild(hostElem);
193         Element JavaDoc commElem = doc.createElement("community");
194         commElem.appendChild(doc.createTextNode(community));
195         routerElem.appendChild(commElem);
196         Element JavaDoc descrElem = doc.createElement("description");
197         descrElem.appendChild(doc.createTextNode(descr));
198         routerElem.appendChild(descrElem);
199         Element JavaDoc activeElem = doc.createElement("active");
200         activeElem.appendChild(doc.createTextNode("" + active));
201         routerElem.appendChild(activeElem);
202         for (int i = 0; i < links.size(); i++) {
203             Port link = (Port) links.elementAt(i);
204             link.appendXml(routerElem);
205         }
206     }
207 }
Popular Tags