KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > targets > TargetIPv6


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.targets;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.GUI;
28 import net.fenyo.gnetwatch.GUI.VisualElement;
29 import net.fenyo.gnetwatch.actions.*;
30 import net.fenyo.gnetwatch.data.EventReachable;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.snmp4j.event.ResponseEvent;
35
36 import java.net.*;
37
38 /**
39  * TargetIPv4 implements an IPv6 capable target.
40  * @author Alexandre Fenyo
41  * @version $Id: TargetIPv6.java,v 1.14 2007/03/09 22:44:20 fenyo Exp $
42  */

43
44 public class TargetIPv6 extends Target {
45   private static Log log = LogFactory.getLog(TargetIPv4.class);
46
47   private Inet6Address address; // not null
48
private SNMPQuerier snmp_querier;
49   private IPQuerier ip_querier;
50
51   /**
52    * Constructor.
53    * @param name target name.
54    * @param address IPv6 address.
55    * @param SNMPManager snmp manager.
56    * @throws AlgorithmException exception.
57    */

58   // GUI thread
59
public TargetIPv6(final String JavaDoc name, final Inet6Address address, final SNMPManager snmp_manager) throws AlgorithmException {
60     super(name);
61     if (address == null) throw new AlgorithmException("name is null");
62     this.address = address;
63     snmp_querier = snmp_manager != null ? snmp_manager.getQuerier(address) : null;
64     setItem(address.getHostAddress());
65     ip_querier = new IPQuerier(address);
66     // may last a long time (DNS resolver)
67
// setDescription(address.getCanonicalHostName());
68
}
69
70   /**
71    * Checks that this host is SNMP capable.
72    * @param none.
73    * @return void.
74    */

75   public void checkSNMPAwareness() {
76     snmp_querier.getSysDescr(new SNMPQuerier.QuerierListener() {
77       public void onResponse(ResponseEvent event) {
78         getGUI().setStatus(getGUI().getConfig().getPattern("discovered_snmp", getAddress().toString().substring(1)));
79         setImageHostSNMP();
80       }
81
82       public void onTimeout(ResponseEvent event) {
83         getGUI().setStatus(getGUI().getConfig().getPattern("snmp_timeout", getAddress().toString().substring(1)));
84       }
85     });
86   }
87
88   /**
89    * Returns the SNMP querier.
90    * @param none.
91    * @return SNMPQuerier querier instance.
92    */

93   public SNMPQuerier getSNMPQuerier() {
94     return snmp_querier;
95   }
96
97   /**
98    * Returns the IP querier.
99    * @param none.
100    * @return IPQuerier querier instance.
101    */

102   public IPQuerier getIPQuerier() {
103     return ip_querier;
104   }
105
106   /**
107    * Returns the IP address.
108    * @param none.
109    * @return Inet4Address IP address.
110    */

111   // any thread
112
public Inet6Address getAddress() {
113     return address;
114   }
115
116   /**
117    * Checks that the parameter can be attached to this target.
118    * @param visual_element parameter to check.
119    * @return true if the parameter can be attached to this target.
120    */

121   public boolean canManageThisChild(final VisualElement visual_element) {
122     if (ActionPing.class.isInstance(visual_element)) return true;
123     if (ActionFlood.class.isInstance(visual_element)) return true;
124     if (ActionSNMP.class.isInstance(visual_element)) return true;
125     if (EventReachable.class.isInstance(visual_element)) return true;
126     if (TargetInterface.class.isInstance(visual_element)) return true;
127     if (ActionHTTP.class.isInstance(visual_element)) return true;
128     if (ActionNmap.class.isInstance(visual_element)) return true;
129     return false;
130   }
131
132   /**
133    * Initializes this target.
134    * @param gui current GUI instance.
135    * @return void.
136    */

137   // final because called by constructor (by means of setItem())
138
protected final void initialize(final GUI gui) {
139     super.initialize(gui);
140     if (gui != null) setImageHost6();
141   }
142
143   /**
144    * Attaches this target to a specific parent.
145    * @param gui current GUI instance.
146    * @param parent parent.
147    * @return true if this target has been succesfully attached.
148    */

149   public boolean addTarget(final GUI gui, final VisualElement parent) {
150     initialize(gui);
151
152     if (!canAddTarget(parent)) return false;
153     if (parent != null && !parent.canManageThisChild(this)) return false;
154
155     final boolean is_new = !getGUI().containsCanonicalInstance(this);
156     final TargetIPv6 target_ipv6 = (TargetIPv6) getGUI().getCanonicalInstance(this);
157
158     if (!getGUI().getVisualTransientAll().contains(target_ipv6)) target_ipv6.setParent(getGUI(), getGUI().getVisualTransientAll());
159
160     if (parent != null) target_ipv6.setParent(getGUI(), parent);
161
162     return is_new;
163   }
164
165   /**
166    * Attaches this target to a specific parent defined by its address.
167    * @param gui current GUI instance.
168    * @param addr_str parent address.
169    * @return true if this target has been succesfully attached.
170    */

171   public static void addTargetIPv6(final GUI gui, final String JavaDoc addr_str) {
172     try {
173       synchronized (gui.sync_tree) {
174         final TargetIPv6 foo = new TargetIPv6(addr_str, (Inet6Address) InetAddress.getByName(addr_str), gui.getSNMPManager());
175         if (gui.containsCanonicalInstance(foo)) return;
176         gui.asyncExecIfNeeded(new Runnable JavaDoc() {
177           public void run() {
178             synchronized (gui.sync_tree) {
179               if (gui.containsCanonicalInstance(foo)) return;
180               if (foo.addTarget(gui, (VisualElement) null) == true) foo.checkSNMPAwareness();
181             }
182           }
183         });
184       }
185     } catch (final AlgorithmException ex) {
186       log.error("Exception", ex);
187     } catch (final UnknownHostException ex) {
188       log.error("Exception", ex);
189     }
190   }
191
192   /**
193    * Compares two targets.
194    * @param o target to compare to.
195    * @return true if the targets are equal.
196    */

197   // any thread
198
public boolean equals(final Object JavaDoc o) {
199     if (this == o) return true;
200     if ((o == null) || (o.getClass() != getClass())) return false;
201     final TargetIPv6 target = (TargetIPv6) o;
202     return getAddress().equals(target.getAddress());
203   }
204
205   /**
206    * Returns the hashcode for this target.
207    * @param none.
208    * @return int hashcode.
209    */

210   // any thread
211
public int hashCode() {
212     return getAddress().hashCode();
213   }
214 }
215
Popular Tags