1 22 package org.jboss.metadata.plugins.loader; 23 24 import java.lang.annotation.Annotation ; 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 40 public abstract class AbstractMutableMetaDataLoader extends BasicMetaDataLoader implements MutableMetaDataLoader 41 { 42 43 private final boolean restricted; 44 45 48 public AbstractMutableMetaDataLoader() 49 { 50 this(false); 51 } 52 53 58 public AbstractMutableMetaDataLoader(boolean restricted) 59 { 60 this.restricted = restricted; 61 } 62 63 68 public AbstractMutableMetaDataLoader(ScopeKey key) 69 { 70 this(key, false); 71 } 72 73 79 public AbstractMutableMetaDataLoader(ScopeKey key, boolean restricted) 80 { 81 super(key); 82 this.restricted = restricted; 83 } 84 85 90 public void checkRestricted(Annotation annotation) 91 { 92 if (restricted) 93 { 94 Class <? extends Annotation > annotationType = annotation.annotationType(); 95 if (annotationType.isAnnotationPresent(Restricted.class)) 96 throw new SecurityException ("Context is restricted, not allowed to add " + annotationType.getName()); 97 } 98 } 99 100 105 public void checkRestricted(Class <?> type) 106 { 107 if (restricted && type.isAnnotationPresent(Restricted.class)) 108 throw new SecurityException ("Context is restricted, not allowed to add " + type.getName()); 109 } 110 111 @SuppressWarnings ("unchecked") 112 public <T> T addMetaData(T metaData, Class <T> type) 113 { 114 if (metaData == null) 115 throw new IllegalArgumentException ("Null metaData"); 116 if (type == null) 117 throw new IllegalArgumentException ("Null type"); 118 if (type.isAnnotation() == false) 119 throw new IllegalArgumentException ("Only annotation types are supported: " + type.getClass().getName()); 120 121 Annotation annotation = (Annotation ) metaData; 122 return (T) addAnnotation(annotation); 123 } 124 125 @SuppressWarnings ("unchecked") 126 public <T> T removeMetaData(Class <T> type) 127 { 128 if (type == null) 129 throw new IllegalArgumentException ("Null type"); 130 if (type.isAnnotation() == false) 131 throw new IllegalArgumentException ("Only annotation types are supported: " + type.getName()); 132 133 return (T) removeAnnotation((Class <Annotation >) type); 134 } 135 136 public <T> T addMetaData(String name, T metaData, Class <T> type) 137 { 138 return addMetaData(metaData, type); 139 } 140 141 public <T> T removeMetaData(String name, Class <T> type) 142 { 143 return removeMetaData(type); 144 } 145 } 146 | Popular Tags |