Created
February 15, 2013 15:05
-
-
Save rschumm/4960902 to your computer and use it in GitHub Desktop.
Findet über das Reflections Framework Policies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.schumm.authorization.pdp; | |
import java.lang.annotation.Annotation; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import org.reflections.Reflections; | |
/** | |
* | |
* @author rs | |
* | |
* TODO Siehe http://code.google.com/p/reflections/issues/detail?id=93&can=1&q=Jboss und | |
* http://code.google.com/p/reflections/wiki/JBossIntegration <br/> | |
* Evtl. können wir das mit dem pre-scanner von Reflections lösen, sodass die Policies zur Build-Zeit gefunden und | |
* dann konfiguriert werden. | |
*/ | |
@Deprecated | |
public class ReflectionPolicyFinder { | |
/** | |
* Findet alle Policies im Packet ch.schumm.security.policy - ausser Policies mit der {@link IgnorePolicy} | |
* Annotation, z.B. die Default-Policy. | |
* @return | |
*/ | |
public static List<Policy> scanForPolicies() { | |
Reflections reflections = new Reflections("ch.schumm.authorization.policies"); | |
Set<Class<? extends Policy>> policyTypes = reflections.getSubTypesOf(Policy.class); | |
ArrayList<Policy> policiesReturn = new ArrayList<Policy>(); | |
for (Class<? extends Policy> policyType : policyTypes) { | |
// if (policyType != DefaultPolicy.class) { | |
if (!policyHasIgnoreAnnotation(policyType)) { | |
try { | |
Policy policy = policyType.newInstance(); | |
policiesReturn.add(policy); | |
System.out.println(policy); | |
} catch (InstantiationException e) { | |
throw new PolicyException(e); | |
} catch (IllegalAccessException e) { | |
throw new PolicyException(e); | |
} | |
} | |
} | |
return policiesReturn; | |
} | |
private static boolean policyHasIgnoreAnnotation(Class<? extends Policy> policyType) { | |
boolean hasIgnoreAnotation = false; | |
Annotation[] declaredAnnotations = policyType.getDeclaredAnnotations(); | |
for (Annotation annotation : declaredAnnotations) { | |
if (annotation.annotationType() == IgnorePolicy.class) { | |
hasIgnoreAnotation = true; | |
} | |
} | |
return hasIgnoreAnotation; | |
} | |
public static void main(String[] args) { | |
scanForPolicies(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment