KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > ProxyMap


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - ProxyMap.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22 package org.snmp4j.agent;
23
24 import java.util.Map JavaDoc;
25 import org.snmp4j.smi.OctetString;
26 import java.util.TreeMap JavaDoc;
27
28 /**
29  * The <code>ProxyMap</code> maps context engine IDs in conjunction with a
30  * proxy usage type to a ProxyForwarder instance.
31  *
32  * @author Frank Fock
33  * @version 1.0
34  */

35 public class ProxyMap {
36
37   private Map JavaDoc proxies = new TreeMap JavaDoc();
38
39   public ProxyMap() {
40   }
41
42   public ProxyForwarder add(ProxyForwarder proxyForwarder,
43                             OctetString contextEngineID,
44                             int proxyType) {
45     return (ProxyForwarder)
46         this.proxies.put(new ProxyKey(contextEngineID, proxyType),
47                          proxyForwarder);
48   }
49
50   public ProxyForwarder remove(OctetString contextEngineID,
51                                int proxyType) {
52     return (ProxyForwarder)
53         this.proxies.remove(new ProxyKey(contextEngineID, proxyType));
54   }
55
56   public ProxyForwarder get(OctetString contextEngineID,
57                             int proxyType) {
58     ProxyForwarder proxy =(ProxyForwarder)
59         this.proxies.get(new ProxyKey(contextEngineID, proxyType));
60     if (proxy == null) {
61       proxy = (ProxyForwarder) this.proxies.get(new ProxyKey(null, proxyType));
62     }
63     return proxy;
64   }
65
66   static class ProxyKey implements Comparable JavaDoc {
67
68     private OctetString contextEngineID;
69     private int proxyType;
70
71     ProxyKey(OctetString contextEngineID, int proxyType) {
72       this.contextEngineID = contextEngineID;
73       this.proxyType = proxyType;
74     }
75
76     public int compareTo(Object JavaDoc o) {
77       ProxyKey other = (ProxyKey)o;
78       if (((contextEngineID == null) && (other.contextEngineID != null)) ||
79           ((other.contextEngineID == null) && (contextEngineID != null)) ||
80           ((other.contextEngineID == null) && (contextEngineID == null)) ||
81           (contextEngineID.equals(other.contextEngineID))) {
82         if ((proxyType == ProxyForwarder.PROXY_TYPE_ALL) ||
83             (other.proxyType == ProxyForwarder.PROXY_TYPE_ALL)) {
84           return 0;
85         }
86         return (proxyType - other.proxyType);
87       }
88       return contextEngineID.compareTo(other.contextEngineID);
89     }
90
91   }
92 }
93
Popular Tags