KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > algorithms > JCEMapper


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

17 package com.sun.org.apache.xml.internal.security.algorithms;
18
19
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24
25 import com.sun.org.apache.xml.internal.security.Init;
26 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
27 import org.w3c.dom.Element JavaDoc;
28
29
30
31 /**
32  * This class maps algorithm identifier URIs to JAVA JCE class names.
33  *
34  * @author $Author: raul $
35  */

36 public class JCEMapper {
37
38    /** {@link java.util.logging} logging facility */
39     static java.util.logging.Logger JavaDoc log =
40         java.util.logging.Logger.getLogger(JCEMapper.class.getName());
41
42
43    
44    private static Map JavaDoc uriToJCEName = new HashMap JavaDoc();
45    
46    private static Map JavaDoc algorithmsMap = new HashMap JavaDoc();
47
48    private static String JavaDoc providerName = null;
49    /**
50     * Method init
51     *
52     * @param mappingElement
53     * @throws Exception
54     */

55    public static void init(Element JavaDoc mappingElement) throws Exception JavaDoc {
56
57       loadAlgorithms((Element JavaDoc)mappingElement.getElementsByTagName("Algorithms").item(0));
58    }
59
60    static void loadAlgorithms( Element JavaDoc algorithmsEl) {
61        Element JavaDoc[] algorithms = XMLUtils.selectNodes(algorithmsEl.getFirstChild(),Init.CONF_NS,"Algorithm");
62        for (int i = 0 ;i < algorithms.length ;i ++) {
63            Element JavaDoc el = algorithms[i];
64            String JavaDoc id = el.getAttribute("URI");
65            String JavaDoc jceName = el.getAttribute("JCEName");
66            uriToJCEName.put(id, jceName);
67            algorithmsMap.put(id, new Algorithm(el));
68        }
69    }
70
71    static Algorithm getAlgorithmMapping(String JavaDoc algoURI) {
72        return ((Algorithm)algorithmsMap.get(algoURI));
73    }
74
75    /**
76     * Method translateURItoJCEID
77     *
78     * @param AlgorithmURI
79     * @return the JCE standard name corresponding to the given URI
80     *
81     */

82    public static String JavaDoc translateURItoJCEID(String JavaDoc AlgorithmURI) {
83       if (true)
84           if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Request for URI " + AlgorithmURI);
85
86       String JavaDoc jceName = (String JavaDoc) uriToJCEName.get(AlgorithmURI);
87       return jceName;
88    }
89
90    /**
91     * Method getAlgorithmClassFromURI
92     * NOTE(Raul Benito) It seems a buggy function the loop doesn't do
93     * anything??
94     * @param AlgorithmURI
95     * @return the class name that implements this algorithm
96     *
97     */

98    public static String JavaDoc getAlgorithmClassFromURI(String JavaDoc AlgorithmURI) {
99        if (true)
100            if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Request for URI " + AlgorithmURI);
101
102        return ((Algorithm) algorithmsMap.get(AlgorithmURI)).algorithmClass;
103    }
104
105    /**
106     * Returns the keylength in bit for a particular algorithm.
107     *
108     * @param AlgorithmURI
109     * @return The length of the key used in the alogrithm
110     */

111    public static int getKeyLengthFromURI(String JavaDoc AlgorithmURI) {
112        return Integer.parseInt(((Algorithm) algorithmsMap.get(AlgorithmURI)).keyLength);
113    }
114
115    /**
116     * Method getJCEKeyAlgorithmFromURI
117     *
118     * @param AlgorithmURI
119     * @return The KeyAlgorithm for the given URI.
120     *
121     */

122    public static String JavaDoc getJCEKeyAlgorithmFromURI(String JavaDoc AlgorithmURI) {
123
124         return ((Algorithm) algorithmsMap.get(AlgorithmURI)).requiredKey;
125
126    }
127
128    /**
129     * Gets the default Provider for obtaining the security algorithms
130     * @return the default providerId.
131     */

132    public static String JavaDoc getProviderId() {
133         return providerName;
134    }
135    
136    /**
137     * Sets the default Provider for obtaining the security algorithms
138     * @param provider the default providerId.
139     */

140    public static void setProviderId(String JavaDoc provider) {
141         providerName=provider;
142    }
143    
144    /**
145     * Represents the Algorithm xml element
146     */

147    public static class Algorithm {
148         String JavaDoc algorithmClass;
149         String JavaDoc keyLength;
150             String JavaDoc requiredKey;
151         /**
152          * Gets data from element
153          * @param el
154          */

155         public Algorithm(Element JavaDoc el) {
156             algorithmClass=el.getAttribute("AlgorithmClass");
157             keyLength=el.getAttribute("KeyLength");
158             requiredKey=el.getAttribute("RequiredKey");
159         }
160    }
161 }
162
Popular Tags