KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > AlgorithmIdentifier


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import org.apache.geronimo.util.asn1.ASN1Encodable;
21 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
22 import org.apache.geronimo.util.asn1.ASN1Sequence;
23 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
24 import org.apache.geronimo.util.asn1.DEREncodable;
25 import org.apache.geronimo.util.asn1.DERObject;
26 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
27 import org.apache.geronimo.util.asn1.DERSequence;
28
29 public class AlgorithmIdentifier
30     extends ASN1Encodable
31 {
32     private DERObjectIdentifier objectId;
33     private DEREncodable parameters;
34     private boolean parametersDefined = false;
35
36     public static AlgorithmIdentifier getInstance(
37         ASN1TaggedObject obj,
38         boolean explicit)
39     {
40         return getInstance(ASN1Sequence.getInstance(obj, explicit));
41     }
42
43     public static AlgorithmIdentifier getInstance(
44         Object JavaDoc obj)
45     {
46         if (obj instanceof AlgorithmIdentifier)
47         {
48             return (AlgorithmIdentifier)obj;
49         }
50
51         if (obj instanceof DERObjectIdentifier)
52         {
53             return new AlgorithmIdentifier((DERObjectIdentifier)obj);
54         }
55
56         if (obj instanceof String JavaDoc)
57         {
58             return new AlgorithmIdentifier((String JavaDoc)obj);
59         }
60
61         if (obj instanceof ASN1Sequence)
62         {
63             return new AlgorithmIdentifier((ASN1Sequence)obj);
64         }
65
66         throw new IllegalArgumentException JavaDoc("unknown object in factory");
67     }
68
69     public AlgorithmIdentifier(
70         DERObjectIdentifier objectId)
71     {
72         this.objectId = objectId;
73     }
74
75     public AlgorithmIdentifier(
76         String JavaDoc objectId)
77     {
78         this.objectId = new DERObjectIdentifier(objectId);
79     }
80
81     public AlgorithmIdentifier(
82         DERObjectIdentifier objectId,
83         DEREncodable parameters)
84     {
85         parametersDefined = true;
86         this.objectId = objectId;
87         this.parameters = parameters;
88     }
89
90     public AlgorithmIdentifier(
91         ASN1Sequence seq)
92     {
93         objectId = (DERObjectIdentifier)seq.getObjectAt(0);
94
95         if (seq.size() == 2)
96         {
97             parametersDefined = true;
98             parameters = seq.getObjectAt(1);
99         }
100         else
101         {
102             parameters = null;
103         }
104     }
105
106     public DERObjectIdentifier getObjectId()
107     {
108         return objectId;
109     }
110
111     public DEREncodable getParameters()
112     {
113         return parameters;
114     }
115
116     /**
117      * Produce an object suitable for an ASN1OutputStream.
118      * <pre>
119      * AlgorithmIdentifier ::= SEQUENCE {
120      * algorithm OBJECT IDENTIFIER,
121      * parameters ANY DEFINED BY algorithm OPTIONAL }
122      * </pre>
123      */

124     public DERObject toASN1Object()
125     {
126         ASN1EncodableVector v = new ASN1EncodableVector();
127
128         v.add(objectId);
129
130         if (parametersDefined)
131         {
132             v.add(parameters);
133         }
134
135         return new DERSequence(v);
136     }
137 }
138
Popular Tags