1 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 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 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 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 |