http://code.google.com/p/reflections/
Damit lässt sich sehr einfach eine einfache Plug-In Architektur erstellen, um - in diesem Fall - zur Laufzeit Policies zu laden, die eine Policy-Klasse erweitert.
Bemerkung: wie zu erwarten war, funktioniert das natürlich - wie immer in solchen Fällen - unter JBoss nicht, wegen Klassenlader-Problemen. Wir suchen die Lösung noch...
This file contains 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.security.pdp; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
/** | |
* Policies mit dieser Annotation werden vom PolicyDispatcher nicht geladen und damit auch nicht ausgewertet. | |
* @author C709360 | |
* | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface IgnorePolicy { | |
} |
This file contains 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.security.pdp; | |
import java.lang.annotation.Annotation; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import org.reflections.Reflections; | |
import ch.schumm.security.policy.Policy; | |
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.security.policy"); | |
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 PolicFinderException(e); | |
} catch (IllegalAccessException e) { | |
throw new PolicFinderException(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(); | |
} | |
} |
Keine Kommentare:
Kommentar veröffentlichen