1   package com.mccrory.scott.spumoni;
2   
3   ////////////////////////////////////////////////////////////////////////////////
4   // Copyright (C) 2002  Scott McCrory
5   //
6   // This program is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public License
8   // as published by the Free Software Foundation; either version 2
9   // of the License, or (at your option) any later version.
10  //
11  // This program is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  // GNU General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public License
17  // along with this program; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  ////////////////////////////////////////////////////////////////////////////////
20  
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.util.HashMap;
25  import java.util.Vector;
26  
27  /**
28   * <P><code>StatsProgramList</code> is a collection of StatsProgram objects.
29   * It also contains method used to operate on that group of objects.</P>
30   *
31   * @author <a href="mailto:smccrory@users.sourceforge.net">Scott McCrory</a>.
32   * @version CVS $Id: StatsProgramList.java,v 1.10 2002/08/04 22:04:53 smccrory Exp $
33   */
34  public class StatsProgramList extends java.util.Vector {
35  
36      /**
37       * StatsProgramList constructor comment.
38       */
39      public StatsProgramList() {
40          super();
41      }
42  
43      /**
44       * StatsValueList constructor with preset minimum size.
45       * @param initialCapacity Initial capacity
46       */
47      public StatsProgramList(int initialCapacity) {
48          super(initialCapacity);
49      }
50  
51      /**
52       * StatsValueList constructor with preset minimum size and capacity increment.
53       * @param initialCapacity Initial capacity
54       * @param capacityIncrement Capacity increment
55       */
56      public StatsProgramList(int initialCapacity, int capacityIncrement) {
57          super(initialCapacity, capacityIncrement);
58      }
59  
60      /**
61       * Convenience constructor for creating a new StatsProgramList and populating
62       * it with a single StatsProgram.
63       * @param sProg The StatsProgram which will be immediately added
64       */
65      public StatsProgramList(StatsProgram sProg) {
66          this();
67          this.add(sProg);
68      }
69  
70      /**
71       * Searches for OID conflicts and returns a list of StatsProgram objects
72       * and their offending StatsValue components.  This should give the user
73       * everything they need to resolve the issue(s).
74       * @return StatsProgramList
75       */
76      public StatsProgramList getOidConflicts() {
77  
78          // We use a HashMap to temporarily store OIDs and their
79          // encapsulating Objects to later fully inform the user of their
80          // evil OID conflict transgressions
81          HashMap oidHashMap = new HashMap();
82          StatsProgramList programOidConflicts = new StatsProgramList();
83  
84          // Iterate through each one of our StatsProgram objects
85          for (int statsProgIndex = 0; statsProgIndex < this.size(); statsProgIndex++) {
86  
87              StatsProgram newStatsProgram = (StatsProgram) this.get(statsProgIndex);
88              StatsValueList svl = newStatsProgram.getStatsValueList();
89  
90              // Iterate through each StatsProgram's StatsValue objects
91              for (int statsValueIndex = 0;
92                  statsValueIndex < svl.size();
93                  statsValueIndex++) {
94  
95                  StatsValue sv = (StatsValue) svl.get(statsValueIndex);
96                  String oid = sv.getSnmpOid();
97  
98                  // Check non-null oids for conflicts
99                  if (oid != null && oid.length() > 0) {
100 
101                     // Check to see if oid is already on our HashMap
102                     StatsProgram earlierStatsProgram = (StatsProgram) oidHashMap.get(oid);
103                     if (earlierStatsProgram != null) {
104 
105                         // The oid is already there, so add both the original
106                         // and the newly conflicting StatsProgram to our
107                         // conflict list
108                         programOidConflicts.add(earlierStatsProgram);
109                         programOidConflicts.add(newStatsProgram);
110 
111                     }
112                     else {
113 
114                         // Not already there, so put this on the oidHashMap
115                         oidHashMap.put(oid, newStatsProgram);
116                     }
117 
118                 }
119 
120             }
121 
122         }
123 
124         return programOidConflicts;
125 
126     }
127 
128     /**
129      * Returns a StatsValue's value String corresponding to the specified OID.
130      * @return java.lang.String
131      * @param oid java.lang.String
132      * @exception java.util.NoSuchElementException if the OID isn't found.
133      */
134     public String getValueByOid(String oid)
135         throws java.util.NoSuchElementException {
136 
137         for (int i = 0; i < this.size(); i++) {
138 
139             StatsProgram sProg = (StatsProgram) get(i);
140             StatsValueList statsValueList = sProg.getStatsValueList();
141             try {
142                 return (statsValueList.getValueByOid(oid));
143             }
144             catch (java.util.NoSuchElementException e) {
145             }
146 
147         }
148 
149         throw new java.util.NoSuchElementException("OID " + oid + " not found");
150 
151     }
152 
153     /**
154      * Returns a vector of all of the OIDs in the contained StatsPrograms
155      * @return A vector of all of the OIDs in the contained StatsPrograms
156      */
157     public Vector getOids() {
158 
159         Vector oids = new Vector();
160 
161         for (int i = 0; i < this.size(); i++) {
162 
163             StatsProgram sProg = (StatsProgram) get(i);
164             Vector newOids = sProg.getOids();
165             oids.addAll(newOids);
166 
167         }
168 
169         return oids;
170 
171     }
172 
173     /**
174      * We override the <code>readObject</code> method here to prevent
175      * deserialization of our class for security reasons.
176      *
177      * @param in java.io.ObjectInputStream
178      * @throws IOException thrown if a problem occurs
179      **/
180     private final void readObject(ObjectInputStream in) throws IOException {
181 
182         throw new IOException("Object cannot be deserialized");
183 
184     }
185 
186     /**
187      * Unsets all of the values.
188      * Typically done just before another round of stats collection.
189      */
190     public void unsetValues() {
191 
192         for (int j = 0; j < this.size(); j++) {
193             StatsProgram sProg = (StatsProgram) get(j);
194             sProg.unsetStatsValues();
195         }
196 
197     }
198 
199     /**
200      * We override the <code>writeObject</code> method here to prevent
201      * serialization of our class for security reasons.
202      *
203      * @param out java.io.ObjectOutputStream
204      * @throws IOException thrown if a problem occurs
205      **/
206     private final void writeObject(ObjectOutputStream out) throws IOException {
207 
208         throw new IOException("Object cannot be serialized");
209 
210     }
211 
212 }