KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > util > DefaultPDUFactory


1 /*_############################################################################
2   _##
3   _## SNMP4J - DefaultPDUFactory.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (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
22 package org.snmp4j.util;
23
24 import org.snmp4j.*;
25 import org.snmp4j.mp.SnmpConstants;
26
27 /**
28  * The <code>DefaultPDUFactory</code> is a default implementation of the
29  * <code>PDUFactory</code> interface. It creates PDUs depending on the
30  * target's message processing model. That is, a {@link PDUv1} instance is
31  * created for a SNMPv1 target whereas a {@link ScopedPDU} is created
32  * for a SNMPv3 target. In all other cases a {@link PDU} instance is created.
33  *
34  * @author Frank Fock
35  * @version 1.7.3
36  * @since 1.0.4
37  */

38 public class DefaultPDUFactory implements PDUFactory {
39
40   private int pduType = PDU.GET;
41
42   /**
43    * Creates a PDU factory for the {@link PDU#GET} PDU type.
44    */

45   public DefaultPDUFactory() {
46   }
47
48   /**
49    * Creates a PDU factory for the specified PDU type.
50    * @param pduType
51    * a PDU type as specified by {@link PDU}.
52    */

53   public DefaultPDUFactory(int pduType) {
54     setPduType(pduType);
55   }
56
57   public void setPduType(int pduType) {
58     this.pduType = pduType;
59   }
60
61   public int getPduType() {
62     return pduType;
63   }
64
65   /**
66    * Create a <code>PDU</code> instance for the supplied target.
67    *
68    * @param target the <code>Target</code> where the PDU to be created will be
69    * sent.
70    * @return PDU a PDU instance that is compatible with the supplied target.
71    */

72   public PDU createPDU(Target target) {
73     return createPDU(target, pduType);
74   }
75
76   /**
77    * Create a <code>PDU</code> instance for the supplied target.
78    *
79    * @param target the <code>Target</code> where the PDU to be created will be
80    * sent.
81    * @param pduType
82    * a PDU type as specified by {@link PDU}.
83    * @return PDU
84    * a PDU instance that is compatible with the supplied target.
85    */

86   public static PDU createPDU(Target target, int pduType) {
87     PDU request = createPDU(target.getVersion());
88     request.setType(pduType);
89     return request;
90   }
91
92   /**
93    * Create a <code>PDU</code> instance for the specified SNMP version.
94    * @param targetVersion
95    * a SNMP version as defined by {@link SnmpConstants}.
96    * @return
97    * a PDU instance that is compatible with the supplied target SNMP version.
98    * @since 1.7.3
99    */

100   public static PDU createPDU(int targetVersion) {
101     PDU request;
102     switch (targetVersion) {
103       case SnmpConstants.version3: {
104         request = new ScopedPDU();
105         break;
106       }
107       case SnmpConstants.version1: {
108         request = new PDUv1();
109         break;
110       }
111       default:
112         request = new PDU();
113     }
114     return request;
115   }
116 }
117
Popular Tags