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.Set;
21   
22   import org.torweg.pulse.configuration.ConfigBean;
23   import org.torweg.pulse.configuration.DeprecatedConfigurable;
24   import org.torweg.pulse.configuration.ConfigurationException;
25   import org.torweg.pulse.service.PulseException;
26   
27   /**
28    * the factory for the attributes.
29    * 
30    * @author Daniel Dietz, Thomas Weber
31    * @version $Revision: 1914 $
32    */
33   public class AttributeFactory implements DeprecatedConfigurable {
34   
35       /**
36        * the configuration.
37        */
38       private AttributeFactoryConfig config;
39   
40       /**
41        * returns the classes of the known {@code AbstractAttribute}s.
42        * 
43        * @return the classes of the known {@code AbstractAttribute}s
44        * 
45        * @see org.torweg.pulse.accesscontrol.attributes.AttributeFactoryConfig#getAttributes()
46        */
47       public final Set<Class<AbstractAttribute<?>>> getAttributes() {
48           return this.config.getAttributes();
49       }
50   
51       /**
52        * returns the classes of the available {@code AbstractValue}s for the
53        * passed {@code AbstractAttribute}-class.
54        * 
55        * @param clazz
56        *            the attribute-{@code Class} to retrieve the values for
57        * 
58        * @return the classes of the available {@code AbstractValue}s for the
59        *         passed {@code AbstractAttribute}-class
60        * 
61        * @see org.torweg.pulse.accesscontrol.attributes.AttributeFactoryConfig#getAttributeValues(java.lang.Class)
62        */
63       public final Set<Class<AbstractValue<?>>> getAttributeValues(
64               final Class<AbstractAttribute<?>> clazz) {
65           return this.config.getAttributeValues(clazz);
66       }
67   
68       /**
69        * returns a set with the available typed checks for the given attribute
70        * class.
71        * 
72        * @param clazz
73        *            the attribute class
74        * @return a set with the available checks
75        */
76       public final Set<Class<AbstractTypedCheck<?>>> getTypedChecks(
77               final Class<AbstractAttribute<?>> clazz) {
78           return this.config.getTypedChecks(clazz);
79       }
80   
81       /**
82        * returns an instance of the given check.
83        * 
84        * @param c
85        *            the check class
86        * @return an instance of the check
87        */
88       public final AbstractTypedCheck<?> getTypedCheck(
89               final Class<AbstractTypedCheck<?>> c) {
90           try {
91               return c.newInstance();
92           } catch (Exception e) {
93               throw new ConfigurationException("Cannot instantiate typed check "
94                       + c.getCanonicalName() + ": " + e.getLocalizedMessage(), e);
95           }
96       }
97   
98       /**
99        * returns an {@code AbstractAttribute} of the passed class.
100       * 
101       * @param clazz
102       *            the class to build the attribute of
103       * 
104       * @return an {@code AbstractAttribute} of the passed class
105       */
106      public final AbstractAttribute<?> getAttribute(
107              final Class<AbstractAttribute<?>> clazz) {
108          return getAttribute(clazz, null, false);
109      }
110  
111      /**
112       * returns an {@code AbstractAttribute} of the passed class.
113       * 
114       * @param clazz
115       *            the class to build the attribute of
116       * @param name
117       *            the name to be set for the attribute
118       * @return an {@code AbstractAttribute} of the passed class
119       */
120      public final AbstractAttribute<?> getAttribute(
121              final Class<AbstractAttribute<?>> clazz, final String name) {
122          return getAttribute(clazz, name, false);
123      }
124  
125      /**
126       * returns an {@code AbstractAttribute} of the passed class.
127       * 
128       * @param clazz
129       *            the class to build the attribute of
130       * @param isSystem
131       *            specifies a system-attribute
132       * 
133       * @return an {@code AbstractAttribute} of the passed class
134       */
135      public final AbstractAttribute<?> getAttribute(
136              final Class<AbstractAttribute<?>> clazz, final boolean isSystem) {
137          return getAttribute(clazz, null, isSystem);
138      }
139  
140      /**
141       * returns an {@code AbstractAttribute} of the passed class.
142       * 
143       * @param clazz
144       *            the class to build the attribute of
145       * @param name
146       *            the name to be set for the attribute
147       * @param isSystem
148       *            specifies a system-attribute
149       * 
150       * @return an {@code AbstractAttribute} of the passed class
151       */
152      public final AbstractAttribute<?> getAttribute(
153              final Class<AbstractAttribute<?>> clazz, final String name,
154              final boolean isSystem) {
155          try {
156              return clazz.newInstance().getAttributeInstance(name, isSystem);
157          } catch (InstantiationException e) {
158              throw new PulseException(clazz.getCanonicalName() + ": "
159                      + e.getLocalizedMessage(), e);
160          } catch (IllegalAccessException e) {
161              throw new PulseException(clazz.getCanonicalName() + ": "
162                      + e.getLocalizedMessage(), e);
163          }
164      }
165  
166      /**
167       * initializes the {@code AttributeFactory} with the given
168       * {@code ConfigBean}.
169       * 
170       * @param c
171       *            the {@code ConfigBean}
172       * 
173       * @see org.torweg.pulse.configuration.DeprecatedConfigurable#init(org.torweg.pulse.configuration.ConfigBean)
174       */
175      public void init(final ConfigBean c) {
176          this.config = (AttributeFactoryConfig) c;
177      }
178  }
179