KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > request > AbstractRequest


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - AbstractRequest.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 package org.snmp4j.agent.request;
22
23 import java.util.*;
24
25 import org.snmp4j.smi.*;
26 import org.snmp4j.mp.SnmpConstants;
27 import org.snmp4j.PDU;
28
29 /**
30  * The <code>AbstractRequest</code> implements common elements of SNMP and
31  * AgentX requests and might be also used for other sub-agent request types.
32  *
33  * @author Frank Fock
34  * @version 1.0
35  */

36 public abstract class AbstractRequest implements Request {
37
38   protected List subrequests;
39   protected int phase = PHASE_INIT;
40   protected int errorStatus = 0;
41   protected int repeaterStartIndex;
42   protected int repeaterRowSize;
43   protected int reprocessCounter = 0;
44   protected int transactionID;
45
46   public AbstractRequest() {
47   }
48
49   public abstract boolean isBulkRequest();
50
51   public SubRequest find(OID prefix) {
52     for (Iterator it = iterator(); it.hasNext(); ) {
53       SubRequest sreq = (SubRequest) it.next();
54       if (sreq.getVariableBinding().getOid().startsWith(prefix)) {
55         return sreq;
56       }
57     }
58     return null;
59   }
60
61   protected synchronized void initSubRequests() {
62     if (subrequests == null) {
63       setupSubRequests();
64     }
65   }
66
67   abstract protected void setupSubRequests();
68
69   abstract protected int getMaxPhase();
70
71   public int nextPhase() {
72     if (phase >= getMaxPhase()) {
73       throw new NoSuchElementException("Requested phase does not exists");
74     }
75     resetCompletionStatus();
76     switch (phase) {
77       case Request.PHASE_2PC_PREPARE: {
78         if (getErrorStatus() != PDU.noError) {
79           phase = Request.PHASE_2PC_CLEANUP;
80         }
81         else {
82           phase = Request.PHASE_2PC_COMMIT;
83         }
84         break;
85       }
86       case Request.PHASE_2PC_COMMIT: {
87         if (getErrorStatus() != PDU.noError) {
88           phase = Request.PHASE_2PC_UNDO;
89         }
90         else {
91           phase = Request.PHASE_2PC_CLEANUP;
92         }
93         break;
94       }
95       case Request.PHASE_2PC_UNDO: {
96         phase = Request.PHASE_2PC_CLEANUP;
97         break;
98       }
99       default: {
100         phase = Request.PHASE_2PC_PREPARE;
101         break;
102       }
103     }
104     return phase;
105   }
106
107   public boolean isComplete() {
108     return ((getErrorStatus() != PDU.noError) ||
109             ((getPhase() >= getMaxPhase()) && isPhaseComplete()));
110   }
111
112   public SubRequest get(int index) {
113     return (SubRequest) subrequests.get(index);
114   }
115
116   public int getPhase() {
117     return phase;
118   }
119
120   public int getErrorIndex() {
121     if (errorStatus == SnmpConstants.SNMP_ERROR_SUCCESS) {
122       return 0;
123     }
124     initSubRequests();
125     int index = 1;
126     for (Iterator it = subrequests.iterator(); it.hasNext(); index++) {
127       SubRequest sreq = (SubRequest) it.next();
128       if (sreq.getStatus().getErrorStatus() !=
129           SnmpConstants.SNMP_ERROR_SUCCESS) {
130         return index;
131       }
132     }
133     return 0;
134   }
135
136   public int getErrorStatus() {
137     initSubRequests();
138     if (errorStatus == SnmpConstants.SNMP_ERROR_SUCCESS) {
139       for (Iterator it = subrequests.iterator(); it.hasNext();) {
140         SubRequest sreq = (SubRequest) it.next();
141         if (sreq.getStatus().getErrorStatus() !=
142             SnmpConstants.SNMP_ERROR_SUCCESS) {
143           return sreq.getStatus().getErrorStatus();
144         }
145       }
146     }
147     return errorStatus;
148   }
149
150   public int getTransactionID() {
151     return transactionID;
152   }
153
154   public void setPhase(int phase) throws NoSuchElementException {
155     if ((phase < 0) || (phase > getMaxPhase())) {
156       throw new NoSuchElementException("Illegal phase identifier: "+phase);
157     }
158     if (this.phase != phase) {
159       resetCompletionStatus();
160     }
161     this.phase = phase;
162   }
163
164   protected void resetCompletionStatus() {
165     initSubRequests();
166     for (Iterator it = subrequests.iterator(); it.hasNext();) {
167       SubRequest subReq = (SubRequest) it.next();
168       subReq.getStatus().setPhaseComplete(false);
169       subReq.getStatus().setProcessed(false);
170     }
171   }
172
173   public synchronized void resetProcessedStatus() {
174     for (Iterator it = subrequests.iterator(); it.hasNext(); ) {
175       SubRequest sreq = (SubRequest) it.next();
176       sreq.getStatus().setProcessed(sreq.getStatus().isPhaseComplete());
177     }
178   }
179
180   public void setErrorStatus(int errorStatus) {
181     this.errorStatus = errorStatus;
182   }
183
184   public boolean equals(Object JavaDoc obj) {
185     if (obj instanceof Request) {
186       return (transactionID == ((Request)obj).getTransactionID());
187     }
188     return false;
189   }
190
191   public int hashCode() {
192     return transactionID;
193   }
194
195   public int getReprocessCounter() {
196     return reprocessCounter;
197   }
198
199   public void incReprocessCounter() {
200     ++reprocessCounter;
201   }
202
203   public String JavaDoc toString() {
204     return getClass().getName()+"[phase="+phase+",errorStatus="+errorStatus+
205         ",transactionID="+transactionID+
206         ",repeaterStartIndex="+repeaterStartIndex+
207         ",repeaterRowSize="+repeaterRowSize+
208         ",reprocessCounter="+reprocessCounter+
209         ",subrequests="+subrequests+"]";
210   }
211
212
213 }
214
Popular Tags