001: /*_############################################################################
002: _##
003: _## SNMP4J - DefaultPDUFactory.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.util;
022:
023: import org.snmp4j.*;
024: import org.snmp4j.mp.SnmpConstants;
025:
026: /**
027: * The <code>DefaultPDUFactory</code> is a default implementation of the
028: * <code>PDUFactory</code> interface. It creates PDUs depending on the
029: * target's message processing model. That is, a {@link PDUv1} instance is
030: * created for a SNMPv1 target whereas a {@link ScopedPDU} is created
031: * for a SNMPv3 target. In all other cases a {@link PDU} instance is created.
032: *
033: * @author Frank Fock
034: * @version 1.7.3
035: * @since 1.0.4
036: */
037: public class DefaultPDUFactory implements PDUFactory {
038:
039: private int pduType = PDU.GET;
040:
041: /**
042: * Creates a PDU factory for the {@link PDU#GET} PDU type.
043: */
044: public DefaultPDUFactory() {
045: }
046:
047: /**
048: * Creates a PDU factory for the specified PDU type.
049: * @param pduType
050: * a PDU type as specified by {@link PDU}.
051: */
052: public DefaultPDUFactory(int pduType) {
053: setPduType(pduType);
054: }
055:
056: public void setPduType(int pduType) {
057: this .pduType = pduType;
058: }
059:
060: public int getPduType() {
061: return pduType;
062: }
063:
064: /**
065: * Create a <code>PDU</code> instance for the supplied target.
066: *
067: * @param target the <code>Target</code> where the PDU to be created will be
068: * sent.
069: * @return PDU a PDU instance that is compatible with the supplied target.
070: */
071: public PDU createPDU(Target target) {
072: return createPDU(target, pduType);
073: }
074:
075: /**
076: * Create a <code>PDU</code> instance for the supplied target.
077: *
078: * @param target the <code>Target</code> where the PDU to be created will be
079: * sent.
080: * @param pduType
081: * a PDU type as specified by {@link PDU}.
082: * @return PDU
083: * a PDU instance that is compatible with the supplied target.
084: */
085: public static PDU createPDU(Target target, int pduType) {
086: PDU request = createPDU(target.getVersion());
087: request.setType(pduType);
088: return request;
089: }
090:
091: /**
092: * Create a <code>PDU</code> instance for the specified SNMP version.
093: * @param targetVersion
094: * a SNMP version as defined by {@link SnmpConstants}.
095: * @return
096: * a PDU instance that is compatible with the supplied target SNMP version.
097: * @since 1.7.3
098: */
099: public static PDU createPDU(int targetVersion) {
100: PDU request;
101: switch (targetVersion) {
102: case SnmpConstants.version3: {
103: request = new ScopedPDU();
104: break;
105: }
106: case SnmpConstants.version1: {
107: request = new PDUv1();
108: break;
109: }
110: default:
111: request = new PDU();
112: }
113: return request;
114: }
115: }
|