KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > plugins > loader > AbstractMutableMetaDataLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.plugins.loader;
23
24 import java.lang.annotation.Annotation JavaDoc;
25
26 import org.jboss.metadata.spi.Restricted;
27 import org.jboss.metadata.spi.loader.MutableMetaDataLoader;
28 import org.jboss.metadata.spi.scope.ScopeKey;
29
30 /**
31  * AbstractMutableMetaDataLoader.
32  *
33  * <p>The default behaviour is to assume there are only
34  * annotations with the types and names of the getMetadata()
35  * methods interprets as annotation types and class names.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 57133 $
39  */

40 public abstract class AbstractMutableMetaDataLoader extends BasicMetaDataLoader implements MutableMetaDataLoader
41 {
42    /** Whether restricted items are allowed */
43    private final boolean restricted;
44    
45    /**
46     * Create a new AbstractMutableMetaDataLoader.
47     */

48    public AbstractMutableMetaDataLoader()
49    {
50       this(false);
51    }
52    
53    /**
54     * Create a new AbstractMutableMetaDataLoader.
55     *
56     * @param restricted whether the context is restricted
57     */

58    public AbstractMutableMetaDataLoader(boolean restricted)
59    {
60       this.restricted = restricted;
61    }
62    
63    /**
64     * Create a new AbstractMutableMetaDataLoader.
65     *
66     * @param key the scope key
67     */

68    public AbstractMutableMetaDataLoader(ScopeKey key)
69    {
70       this(key, false);
71    }
72    
73    /**
74     * Create a new AbstractMutableMetaDataLoader.
75     *
76     * @param key the scope key
77     * @param restricted whether the context is restricted
78     */

79    public AbstractMutableMetaDataLoader(ScopeKey key, boolean restricted)
80    {
81       super(key);
82       this.restricted = restricted;
83    }
84
85    /**
86     * Check whether an annotation is retricted
87     *
88     * @param annotation the annotation
89     */

90    public void checkRestricted(Annotation JavaDoc annotation)
91    {
92       if (restricted)
93       {
94          Class JavaDoc<? extends Annotation JavaDoc> annotationType = annotation.annotationType();
95          if (annotationType.isAnnotationPresent(Restricted.class))
96             throw new SecurityException JavaDoc("Context is restricted, not allowed to add " + annotationType.getName());
97       }
98    }
99    
100    /**
101     * Check whether an object is retricted
102     *
103     * @param type the type
104     */

105    public void checkRestricted(Class JavaDoc<?> type)
106    {
107       if (restricted && type.isAnnotationPresent(Restricted.class))
108          throw new SecurityException JavaDoc("Context is restricted, not allowed to add " + type.getName());
109    }
110    
111    @SuppressWarnings JavaDoc("unchecked")
112    public <T> T addMetaData(T metaData, Class JavaDoc<T> type)
113    {
114       if (metaData == null)
115          throw new IllegalArgumentException JavaDoc("Null metaData");
116       if (type == null)
117          throw new IllegalArgumentException JavaDoc("Null type");
118       if (type.isAnnotation() == false)
119          throw new IllegalArgumentException JavaDoc("Only annotation types are supported: " + type.getClass().getName());
120
121       Annotation JavaDoc annotation = (Annotation JavaDoc) metaData;
122       return (T) addAnnotation(annotation);
123    }
124
125    @SuppressWarnings JavaDoc("unchecked")
126    public <T> T removeMetaData(Class JavaDoc<T> type)
127    {
128       if (type == null)
129          throw new IllegalArgumentException JavaDoc("Null type");
130       if (type.isAnnotation() == false)
131          throw new IllegalArgumentException JavaDoc("Only annotation types are supported: " + type.getName());
132
133       return (T) removeAnnotation((Class JavaDoc<Annotation JavaDoc>) type);
134    }
135
136    public <T> T addMetaData(String JavaDoc name, T metaData, Class JavaDoc<T> type)
137    {
138       return addMetaData(metaData, type);
139    }
140
141    public <T> T removeMetaData(String JavaDoc name, Class JavaDoc<T> type)
142    {
143       return removeMetaData(type);
144    }
145 }
146
Popular Tags