From 8e3b22b3e36c0ba3962022765a5f9b69a8e18c27 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 16 Jan 2019 17:25:57 +0100 Subject: [PATCH] [New] Added PolicyHandler.isPolicyDefAvailable() --- .../strolch/policy/DefaultPolicyHandler.java | 14 ++++++++ .../java/li/strolch/policy/PolicyHandler.java | 34 ++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java b/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java index 2b5340d1d..fe4ad8d68 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/DefaultPolicyHandler.java @@ -81,6 +81,7 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand @Override public T getPolicy(PolicyDef policyDef, StrolchTransaction tx) { + DBC.PRE.assertNotNull("policyDef must not be null!", policyDef); DBC.PRE.assertNotNull("tx must not be null!", tx); try { @@ -94,6 +95,19 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand } } + @Override + public boolean isPolicyDefAvailable(PolicyDef policyDef) { + DBC.PRE.assertNotNull("policyDef must not be null!", policyDef); + + if (policyDef instanceof KeyPolicyDef) + return this.classByTypeMap.containsElement(policyDef.getType(), policyDef.getValue()); + + if (policyDef instanceof JavaPolicyDef) + return true; + + throw new IllegalArgumentException("Unhandled PolicyDev instance " + policyDef.getClass()); + } + @Override public Class visit(JavaPolicyDef policyDef) throws ClassNotFoundException { String value = policyDef.getValue(); diff --git a/li.strolch.agent/src/main/java/li/strolch/policy/PolicyHandler.java b/li.strolch.agent/src/main/java/li/strolch/policy/PolicyHandler.java index a7713d896..2e728d9e7 100644 --- a/li.strolch.agent/src/main/java/li/strolch/policy/PolicyHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/policy/PolicyHandler.java @@ -1,12 +1,12 @@ /* * Copyright 2015 Robert von Burg - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,35 +23,45 @@ import li.strolch.persistence.api.StrolchTransaction; *

* The {@link PolicyHandler} is Strolch's mechanism of dependency injection *

- * + * *

* Objects which require delegation can use a {@link PolicyConfiguration} element and then retrieve a StrolchPolicy * instance from this {@link PolicyHandler}. *

- * + * *

* {@link PolicyConfiguration} have a mapping of a policy type, i.e. an interface for a specific delegation. This * interface has concrete implementations which are then returned by the {@link PolicyHandler} depending on the current * configuration *

- * + * *

* The resolving of a policy instance is handled by a {@link PolicyDefVisitor} *

- * + * * @author Robert von Burg */ public interface PolicyHandler { /** * Instantiates the actual policy by resolving the {@link PolicyDef} using a {@link PolicyDefVisitor} - * + * * @param policyDef - * the {@link PolicyDef} referencing a concrete policy + * the {@link PolicyDef} referencing a concrete policy * @param tx - * the current transaction for which the policy is instantiated - * + * the current transaction for which the policy is instantiated + * * @return the instantiated instance of the referenced policy */ - public T getPolicy(PolicyDef policyDef, StrolchTransaction tx); + T getPolicy(PolicyDef policyDef, StrolchTransaction tx); + + /** + * Returns true, if the policy definition is known + * + * @param policyDef + * the policy definition to check for + * + * @return true if the policy definition is known, false otherwise + */ + boolean isPolicyDefAvailable(PolicyDef policyDef); }