@@ -48,6 +48,7 @@ public class PolicyParser {
4848 protected final XContentParser policyParser ;
4949 protected final String policyName ;
5050 private final boolean isExternalPlugin ;
51+ private final Map <String , Class <?>> externalEntitlements ;
5152
5253 static String getEntitlementTypeName (Class <? extends Entitlement > entitlementClass ) {
5354 var entitlementClassName = entitlementClass .getSimpleName ();
@@ -66,9 +67,16 @@ static String getEntitlementTypeName(Class<? extends Entitlement> entitlementCla
6667 }
6768
6869 public PolicyParser (InputStream inputStream , String policyName , boolean isExternalPlugin ) throws IOException {
70+ this (inputStream , policyName , isExternalPlugin , EXTERNAL_ENTITLEMENTS );
71+ }
72+
73+ // package private for tests
74+ PolicyParser (InputStream inputStream , String policyName , boolean isExternalPlugin , Map <String , Class <?>> externalEntitlements )
75+ throws IOException {
6976 this .policyParser = YamlXContent .yamlXContent .createParser (XContentParserConfiguration .EMPTY , Objects .requireNonNull (inputStream ));
7077 this .policyName = policyName ;
7178 this .isExternalPlugin = isExternalPlugin ;
79+ this .externalEntitlements = externalEntitlements ;
7280 }
7381
7482 public Policy parsePolicy () {
@@ -124,14 +132,29 @@ protected Scope parseScope(String scopeName) throws IOException {
124132
125133 protected Entitlement parseEntitlement (String scopeName , String entitlementType ) throws IOException {
126134 XContentLocation startLocation = policyParser .getTokenLocation ();
127- Class <?> entitlementClass = EXTERNAL_ENTITLEMENTS .get (entitlementType );
135+ Class <?> entitlementClass = externalEntitlements .get (entitlementType );
128136
129137 if (entitlementClass == null ) {
130138 throw newPolicyParserException (scopeName , "unknown entitlement type [" + entitlementType + "]" );
131139 }
132140
133- Constructor <?> entitlementConstructor = entitlementClass .getConstructors ()[0 ];
134- ExternalEntitlement entitlementMetadata = entitlementConstructor .getAnnotation (ExternalEntitlement .class );
141+ Constructor <?> entitlementConstructor = null ;
142+ ExternalEntitlement entitlementMetadata = null ;
143+ for (var ctor : entitlementClass .getConstructors ()) {
144+ var metadata = ctor .getAnnotation (ExternalEntitlement .class );
145+ if (metadata != null ) {
146+ if (entitlementMetadata != null ) {
147+ throw new IllegalStateException (
148+ "entitlement class ["
149+ + entitlementClass .getName ()
150+ + "] has more than one constructor annotated with ExternalEntitlement"
151+ );
152+ }
153+ entitlementConstructor = ctor ;
154+ entitlementMetadata = metadata ;
155+ }
156+
157+ }
135158 if (entitlementMetadata == null ) {
136159 throw newPolicyParserException (scopeName , "unknown entitlement type [" + entitlementType + "]" );
137160 }
0 commit comments