1    /*
2     * Copyright 2008 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.accesscontrol.attributes;
19   
20   import java.util.Collections;
21   import java.util.HashMap;
22   import java.util.HashSet;
23   import java.util.List;
24   import java.util.Map;
25   import java.util.Set;
26   
27   import org.jdom.Element;
28   import org.torweg.pulse.configuration.AbstractConfigBean;
29   import org.torweg.pulse.configuration.ConfigurationException;
30   
31   /**
32    * the configuration of the {@code AttributeFactory}.
33    * 
34    * @author Daniel Dietz, Thomas Weber
35    * @version $Revision: 1809 $
36    */
37   public class AttributeFactoryConfig extends AbstractConfigBean {
38   
39       /**
40        * serialVersionUID.
41        */
42       private static final long serialVersionUID = 387801084189595719L;
43   
44       /**
45        * the attribute-value-mapping of the {@code AttributeFactory}.
46        */
47       private final Map<Class<AbstractAttribute<?>>, Set<Class<AbstractValue<?>>>> attributeValueMapping = new HashMap<Class<AbstractAttribute<?>>, Set<Class<AbstractValue<?>>>>();
48   
49       /**
50        * the attribute-checks-mapping of the {@code AttributeFactory}.
51        */
52       private final Map<Class<AbstractAttribute<?>>, Set<Class<AbstractTypedCheck<?>>>> attributeChecksMapping = new HashMap<Class<AbstractAttribute<?>>, Set<Class<AbstractTypedCheck<?>>>>();
53   
54       /**
55        * returns the classes of the known {@code AbstractAttribute}s.
56        * 
57        * @return an unmodifiable view of the classes of the known
58        *         {@code AbstractAttribute}s
59        */
60       public final Set<Class<AbstractAttribute<?>>> getAttributes() {
61           return Collections.unmodifiableSet(this.attributeValueMapping.keySet());
62       }
63   
64       /**
65        * returns the classes of the available {@code AbstractValue}s for the
66        * passed {@code AbstractAttribute}-class.
67        * 
68        * @param clazz
69        *            the attribute-{@code Class} to retrieve the values for
70        * @return an unmodifiable view of the classes of the available
71        *         {@code AbstractValue}s for the passed
72        *         {@code AbstractAttribute}-class
73        */
74       public final Set<Class<AbstractValue<?>>> getAttributeValues(
75               final Class<AbstractAttribute<?>> clazz) {
76           return Collections.unmodifiableSet(this.attributeValueMapping
77                   .get(clazz));
78       }
79   
80       /**
81        * returns a set with the available typed checks for the given attribute
82        * class.
83        * 
84        * @param clazz
85        *            the attribute class
86        * @return an unmodifiable set with the available checks
87        */
88       public final Set<Class<AbstractTypedCheck<?>>> getTypedChecks(
89               final Class<AbstractAttribute<?>> clazz) {
90           return Collections.unmodifiableSet(this.attributeChecksMapping
91                   .get(clazz));
92       }
93   
94       /**
95        * initializes the {@code ConfigBean}.
96        * 
97        * @param conf
98        *            the configuration-XML
99        * 
100       * @see org.torweg.pulse.configuration.ConfigBean#init(org.jdom.Element)
101       */
102      @SuppressWarnings("unchecked")
103      public void init(final Element conf) {
104          this.attributeValueMapping.clear();
105          this.attributeChecksMapping.clear();
106          try {
107              for (Element attribute : (List<Element>) conf
108                      .getChildren("attribute")) {
109                  Class<AbstractAttribute<?>> clazz = (Class<AbstractAttribute<?>>) Class
110                          .forName(attribute.getAttributeValue("class"));
111                  HashSet<Class<AbstractValue<?>>> valueSet = new HashSet<Class<AbstractValue<?>>>();
112                  HashSet<Class<AbstractTypedCheck<?>>> checkSet = new HashSet<Class<AbstractTypedCheck<?>>>();
113                  for (Element element : (List<Element>) attribute.getChildren()) {
114                      if (element.getName().equals("value")) {
115                          valueSet.add((Class<AbstractValue<?>>) Class
116                                  .forName(element.getAttributeValue("class")));
117                      } else if (element.getName().equals("check")) {
118                          checkSet.add((Class<AbstractTypedCheck<?>>) Class
119                                  .forName(element.getAttributeValue("class")));
120                      }
121                  }
122                  this.attributeValueMapping.put(clazz, valueSet);
123                  this.attributeChecksMapping.put(clazz, checkSet);
124              }
125          } catch (ClassNotFoundException e) {
126              throw new ConfigurationException(e.getLocalizedMessage(), e);
127          }
128  
129      }
130  }
131