⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp4jsession.java

📁 snmp hibernate 源码, 类似hibernate的映射.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
								"Asser Failed, IpAddress length must be 4. length="
										+ length);
					StringBuffer sb = new StringBuffer();
					sb.append(indexOids[j]).append('.');
					sb.append(indexOids[j + 1]).append('.');
					sb.append(indexOids[j + 2]).append('.');
					sb.append(indexOids[j + 3]);
					indexField.set(entry, sb.toString());
				} else {
					throw new RuntimeException("Unknow smiType: " + smiType);
				}
				j += length;
			} else {
				throw new RuntimeException(
						"Assert Failed! Unknow index length.");
			}
		}
	}

	private String intAry2Str(int[] oidValue) {
		StringBuffer sb = new StringBuffer();
		for (int i : oidValue) {
			sb.append(i).append('.');
		}
		if (sb.length() > 0)
			sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	private byte[] copyBytes(int[] srcArray, int srcPos, byte[] destBytes) {
		for (int k = 0; k < destBytes.length; k++) {
			destBytes[k] = (byte) (srcArray[srcPos + k] & 0xff);
		}
		return destBytes;
	}

	private Field[] getIndexFields(Class clazz) {
		Field[] fields = clazz.getDeclaredFields();
		List<Field> list = new ArrayList<Field>();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].isAnnotationPresent(MibIndex.class)) {
				fields[i].setAccessible(true);
				list.add(fields[i]);
			}
		}
		Field[] indexFields = list.toArray(new Field[list.size()]);
		// bubble sort.
		for (int i = 0; i < indexFields.length; i++) {
			for (int j = i + 1; j < indexFields.length; j++) {
				Field top = indexFields[i];
				int no = top.getAnnotation(MibIndex.class).no();
				Field another = indexFields[j];
				int anotherNo = another.getAnnotation(MibIndex.class).no();
				if (anotherNo < no) {
					indexFields[i] = another;
					indexFields[j] = top;
				}
			}
		}
		return indexFields;
	}

	private int[] extractIndexOids(OID firstRespOid, OID firstReqOid) {
		int[] respOids = firstRespOid.getValue();
		int[] reqOids = firstReqOid.getValue();
		int[] indexOids = new int[respOids.length - reqOids.length];
		System.arraycopy(respOids, reqOids.length, indexOids, 0,
				indexOids.length);
		return indexOids;
	}

	private boolean isTableEnd(OID firstReqOid, OID firstRespOid) {
		// TODO: END OF MIB
		return !firstRespOid.startsWith(firstReqOid);
	}

	private void fillProperties(Object object, PDU pdu)
			throws InstantiationException, IllegalAccessException {
		Field[] propFields = getPropFields(object.getClass());
		Vector variableBindings = pdu.getVariableBindings();
		for (Field propField : propFields) {
			propField.setAccessible(true);
			MibObjectType mot = propField.getAnnotation(MibObjectType.class);
			OID oid = new OID(mot.oid());
			Variable variable = findVariableByOid(oid, variableBindings);
			if (variable != null) {
				Object value = variable.getValue();
				if (mot.smiType() == SmiType.DISPLAY_STRING) {
					value = new String((byte[]) value);
				} else if (mot.smiType() == SmiType.OID) {
					value = ((OID) variable).toString();
				}
				propField.set(object, value);
			}
		}
	}

	private Variable findVariableByOid(OID oid, Vector variableBindings) {
		for (Iterator it = variableBindings.iterator(); it.hasNext();) {
			VariableBinding vb = (VariableBinding) it.next();
			if (vb.getOid().startsWith(oid)) {
				return vb.getVariable();
			}
		}
		return null;
	}

	private PDU newSetPDU(Object entry) throws IllegalArgumentException,
			IllegalAccessException, SecurityException, NoSuchMethodException,
			InstantiationException, InvocationTargetException {
		PDU pdu = new PDU();
		pdu.setType(PDU.SET);
		OID indexOid = buildIndexOid(entry);
		Field[] writePropFields = getWritePropFields(entry.getClass());
		for (Field writeField : writePropFields) {
			writeField.setAccessible(true);
			MibObjectType mot = writeField.getAnnotation(MibObjectType.class);
			OID oid = new OID(mot.oid());
			if (indexOid != null) {
				oid.append(indexOid);
			}
			Class type = writeField.getType();
			Constructor constructor = getSmiTypeProvider().getSmiType(
					mot.smiType()).getConstructor(new Class[] { type });
			Variable variable = (Variable) constructor
					.newInstance(new Object[] { writeField.get(entry) });
			pdu.add(new VariableBinding(oid, variable));
		}
		return pdu;
	}

	private PDU newDeletPDU(Object entry) throws IllegalArgumentException,
			IllegalAccessException, SnmpAnnotationException {
		PDU pdu = new PDU();
		pdu.setType(PDU.SET);
		pdu.add(newDeleteVB(entry));
		return pdu;
	}

	private VariableBinding newDeleteVB(Object entry)
			throws IllegalAccessException, SnmpAnnotationException {
		OID indexOid = buildIndexOid(entry);
		if (indexOid == null)
			throw new SnmpAnnotationException(new NullPointerException(
					"No index oid."));
		RowStatus rowStatus = entry.getClass().getAnnotation(RowStatus.class);
		if (rowStatus == null)
			throw new SnmpAnnotationException(new NullPointerException(
					"No RowStatus Annotation."));
		OID oid = new OID(rowStatus.oid());
		oid.append(indexOid);
		Integer32 var = new Integer32(rowStatus.delete());
		VariableBinding vb = new VariableBinding(oid, var);
		return vb;
	}

	private PDU newCreatePDU(Object entry) throws IllegalArgumentException,
			SecurityException, IllegalAccessException, NoSuchMethodException,
			InstantiationException, InvocationTargetException,
			SnmpAnnotationException {
		PDU pdu = newSetPDU(entry);
		pdu.add(newCreateVB(entry));
		return pdu;
	}

	private VariableBinding newCreateVB(Object entry)
			throws IllegalAccessException, SnmpAnnotationException {
		OID indexOid = buildIndexOid(entry);
		checkIndexOid(indexOid);
		RowStatus rowStatus = entry.getClass().getAnnotation(RowStatus.class);
		checkRowStatusAnnotation(rowStatus);
		OID oid = new OID(rowStatus.oid());
		oid.append(indexOid);
		Integer32 var = new Integer32(rowStatus.create());
		return new VariableBinding(oid, var);
	}

	private PDU newGetPDU(Class scalarClass) {
		PDU pdu = new PDU();
		pdu.setType(PDU.GET);
		Field[] propFields = getPropFields(scalarClass);
		for (Field propField : propFields) {
			MibObjectType mib = propField.getAnnotation(MibObjectType.class);
			pdu.add(new VariableBinding(new OID(mib.oid())));
		}
		return pdu;
	}

	private PDU newGetPDU(Class scalarClass, String[] fields)
			throws SecurityException, NoSuchFieldException {
		PDU pdu = new PDU();
		pdu.setType(PDU.GET);
		for (String fn : fields) {
			Field field = scalarClass.getDeclaredField(fn);
			MibObjectType mib = field.getAnnotation(MibObjectType.class);
			pdu.add(new VariableBinding(new OID(mib.oid())));
		}
		return pdu;
	}

	private <T> T get(Class<T> scalarClass, PDU reqPDU) throws IOException,
			SnmpException, SnmpAnnotationException {
		try {
			checkReqError(reqPDU);
			ResponseEvent event = snmp4J.get(reqPDU, getReadTarget());
			checkEventError(event);
			PDU resPDU = event.getResponse();
			checkResError(resPDU);
			T mibObj = scalarClass.newInstance();
			fillProperties(mibObj, resPDU);
			return mibObj;
		} catch (InstantiationException e) {
			throw new SnmpAnnotationException(e);
		} catch (IllegalAccessException e) {
			throw new SnmpAnnotationException(e);
		}
	}

	private PDU newGetEntryPDU(Object entry) throws IllegalArgumentException,
			IllegalAccessException {
		PDU pdu = new PDU();
		pdu.setType(PDU.GET);
		OID indexOid = buildIndexOid(entry);
		Field[] propFields = getPropFields(entry.getClass());
		for (Field propField : propFields) {
			MibObjectType mib = propField.getAnnotation(MibObjectType.class);
			OID oid = new OID(mib.oid());
			if (indexOid != null) {
				oid.append(indexOid);
			}
			pdu.add(new VariableBinding(oid));
		}
		return pdu;
	}

	private PDU newGetNextFirstEntryPDU(Class entryClass) {
		PDU pdu = new PDU();
		pdu.setType(PDU.GETNEXT);
		Field[] propFields = getPropFields(entryClass);
		for (Field propField : propFields) {
			MibObjectType mib = propField.getAnnotation(MibObjectType.class);
			pdu.add(new VariableBinding(new OID(mib.oid())));
		}
		if (pdu.size() <= 0) {
			// in some mib, there are only indices.
			Field[] indexFields = getIndexFields(entryClass);
			if (indexFields.length > 0) {
				MibObjectType mot = indexFields[0]
						.getAnnotation(MibObjectType.class);
				pdu.add(new VariableBinding(new OID(mot.oid())));
			}
		}
		return pdu;
	}

	private PDU newGetNextEntryPDU(Object entry)
			throws IllegalArgumentException, IllegalAccessException {
		PDU pdu = newGetEntryPDU(entry);
		pdu.setType(PDU.GETNEXT);
		if (pdu.size() <= 0) {
			OID indexOid = buildIndexOid(entry);
			Field[] indexFields = getIndexFields(entry.getClass());
			if (indexFields.length > 0) {
				MibObjectType mib = indexFields[0]
						.getAnnotation(MibObjectType.class);
				OID oid = new OID(mib.oid());
				if (indexOid != null) {
					oid.append(indexOid);
				}
				pdu.add(new VariableBinding(oid));
			}
		}
		return pdu;
	}

	private PDU newGetEntryPDU(Object entry, String[] fields)
			throws IllegalArgumentException, IllegalAccessException,
			SecurityException, NoSuchFieldException {
		PDU pdu = new PDU();
		pdu.setType(PDU.GET);
		OID indexOid = buildIndexOid(entry);
		for (String fn : fields) {
			Field field = entry.getClass().getDeclaredField(fn);
			MibObjectType mib = field.getAnnotation(MibObjectType.class);
			OID oid = new OID(mib.oid());
			if (indexOid != null) {
				oid.append(indexOid);
			}
			pdu.add(new VariableBinding(oid));
		}
		return pdu;
	}

	private <T> T getEntryByIndex(T entry, PDU reqPDU)
			throws IOException, SnmpException, InstantiationException,
			IllegalAccessException {
		checkReqError(reqPDU);
		ResponseEvent event = snmp4J.get(reqPDU, getReadTarget());
		checkEventError(event);
		PDU resPDU = event.getResponse();
		checkResError(resPDU);
		fillProperties(entry, resPDU);
		return entry;
	}

	private <T> T buildEntryWithIndices(Class<T> entryClass, Serializable indices)
			throws InstantiationException, IllegalAccessException {
		T entry = entryClass.newInstance();
		Field[] indexFields = getIndexFields(entryClass);
		if (indexFields.length == 1) {
			indexFields[0].set(entry, indices);
		} else {
			Serializable[] indicesArray = (Serializable[]) indices;
			for (int i = 0; i < indexFields.length; i++) {
				indexFields[i].set(entry, indicesArray[i]);
			}
		}
		return entry;
	}

	private Field[] getPropFields(Class clazz) {
		List<Field> list = new ArrayList<Field>();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			if (field.isAnnotationPresent(MibObjectType.class)
					&& !field.isAnnotationPresent(MibIndex.class)) {
				list.add(field);
			}
		}
		return (Field[]) list.toArray(new Field[list.size()]);
	}

	private void checkRowStatusAnnotation(RowStatus rowStatus)
			throws SnmpAnnotationException {
		if (rowStatus == null) {
			throw new SnmpAnnotationException(new NullPointerException(
					"No RowStatus Annotation."));
		}
	}

	private void checkIndexOid(OID indexOid) throws SnmpAnnotationException {
		if (indexOid == null) {
			throw new SnmpAnnotationException(new NullPointerException(
					"No index oid."));
		}
	}

	private void checkResError(PDU resPDU) throws SnmpException {
		if (resPDU == null) {
			SnmpException e = new SnmpException(SnmpException.NO_RESPONSE_PDU, -1);
			e.setSnmpErrorMsgProvider(getSnmpErrorMsgProvider());
			throw e;
		}
		if (resPDU.getErrorStatus() != 0) {
			SnmpException e = new SnmpException(resPDU.getErrorStatus(), resPDU
					.getErrorIndex());
			e.setSnmpErrorMsgProvider(getSnmpErrorMsgProvider());
			throw e;
		}
	}

	private void checkReqError(PDU reqPDU) {
		if (reqPDU.size() == 0) {
			throw new IllegalArgumentException("No declarative mib object.");
		}
	}

	private void checkEventError(ResponseEvent event) throws SnmpException {
		if (event.getError() != null) {
			SnmpException e = new SnmpException(event.getError());
			e.setSnmpErrorMsgProvider(getSnmpErrorMsgProvider());
			throw e;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -