Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/xacml/combine/BaseCombiningAlgFactory.java


1   
2   /*
3    * @(#)BaseCombiningAlgFactory.java
4    *
5    * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   *   1. Redistribution of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * 
13   *   2. Redistribution in binary form must reproduce the above copyright
14   *      notice, this list of conditions and the following disclaimer in the
15   *      documentation and/or other materials provided with the distribution.
16   *
17   * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18   * be used to endorse or promote products derived from this software without
19   * specific prior written permission.
20   * 
21   * This software is provided "AS IS," without a warranty of any kind. ALL
22   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32   *
33   * You acknowledge that this software is not designed or intended for use in
34   * the design, construction, operation or maintenance of any nuclear facility.
35   */
36  
37  package com.sun.xacml.combine;
38  
39  import com.sun.xacml.UnknownIdentifierException;
40  
41  import java.net.URI;
42  
43  import java.util.Collections;
44  import java.util.HashMap;
45  import java.util.Iterator;
46  import java.util.Set;
47  
48  
49  /**
50   * This is a basic implementation of <code>CombiningAlgFactory</code>. It
51   * implements the insertion and retrieval methods, but doesn't actually
52   * setup the factory with any algorithms.
53   * <p>
54   * Note that while this class is thread-safe on all creation methods, it
55   * is not safe to add support for a new algorithm while creating an instance
56   * of an algorithm. This follows from the assumption that most people will
57   * initialize these factories up-front, and then start processing without
58   * ever modifying the factories. If you need these mutual operations to
59   * be thread-safe, then you should write a wrapper class that implements
60   * the right synchronization.
61   *
62   * @since 1.2
63   * @author Seth Proctor
64   */
65  public class BaseCombiningAlgFactory extends CombiningAlgFactory
66  {
67  
68      // the map of available combining algorithms
69      private HashMap algMap;
70  
71      /**
72       * Default constructor.
73       */
74      public BaseCombiningAlgFactory() {
75          algMap = new HashMap();
76      }
77  
78      /**
79       * Constructor that configures this factory with an initial set of
80       * supported algorithms.
81       *
82       * @param algorithms a <code>Set</code> of
83       *                   </code>CombiningAlgorithm</code>s
84       *
85       * @throws IllegalArgumentException if any elements of the set are not
86       *                                  </code>CombiningAlgorithm</code>s
87       */
88      public BaseCombiningAlgFactory(Set algorithms) {
89          algMap = new HashMap();
90  
91          Iterator it = algorithms.iterator();
92          while (it.hasNext()) {
93              try {
94                  CombiningAlgorithm alg = (CombiningAlgorithm)(it.next());
95                  algMap.put(alg.getIdentifier().toString(), alg);
96              } catch (ClassCastException cce) {
97                  throw new IllegalArgumentException("an element of the set " +
98                                                     "was not an instance of " +
99                                                     "CombiningAlgorithm");
100             }
101         }
102     }
103 
104     /**
105      * Adds a combining algorithm to the factory. This single instance will
106      * be returned to anyone who asks the factory for an algorithm with the
107      * id given here.
108      *
109      * @param alg the combining algorithm to add
110      *
111      * @throws IllegalArgumentException if the algId is already registered
112      */
113     public void addAlgorithm(CombiningAlgorithm alg) {
114         String algId = alg.getIdentifier().toString();
115 
116         // check that the id doesn't already exist in the factory
117         if (algMap.containsKey(algId))
118             throw new IllegalArgumentException("algorithm already registered: "
119                                                + algId);
120 
121         // add the algorithm
122         algMap.put(algId, alg);
123     }
124 
125     /**
126      * Returns the algorithm identifiers supported by this factory.
127      *
128      * @return a <code>Set</code> of <code>String</code>s
129      */
130     public Set getSupportedAlgorithms() {
131         return Collections.unmodifiableSet(algMap.keySet());
132     }
133 
134     /**
135      * Tries to return the correct combinging algorithm based on the
136      * given algorithm ID.
137      *
138      * @param algId the identifier by which the algorithm is known
139      *
140      * @return a combining algorithm
141      *
142      * @throws UnknownIdentifierException algId is unknown
143      */
144     public CombiningAlgorithm createAlgorithm(URI algId)
145         throws UnknownIdentifierException
146     {
147         String id = algId.toString();
148 
149         if (algMap.containsKey(id)) {
150             return (CombiningAlgorithm)(algMap.get(algId.toString()));
151         } else {
152             throw new UnknownIdentifierException("unknown combining algId: "
153                                                  + id);
154         }
155     }
156 
157 }