[New] Added PolicyHandler.isPolicyDefAvailable()

This commit is contained in:
Robert von Burg 2019-01-16 17:25:57 +01:00
parent 368247726b
commit 8e3b22b3e3
2 changed files with 36 additions and 12 deletions

View File

@ -81,6 +81,7 @@ public class DefaultPolicyHandler extends StrolchComponent implements PolicyHand
@Override @Override
public <T extends StrolchPolicy> T getPolicy(PolicyDef policyDef, StrolchTransaction tx) { public <T extends StrolchPolicy> T getPolicy(PolicyDef policyDef, StrolchTransaction tx) {
DBC.PRE.assertNotNull("policyDef must not be null!", policyDef);
DBC.PRE.assertNotNull("tx must not be null!", tx); DBC.PRE.assertNotNull("tx must not be null!", tx);
try { 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 @Override
public <T> Class<T> visit(JavaPolicyDef policyDef) throws ClassNotFoundException { public <T> Class<T> visit(JavaPolicyDef policyDef) throws ClassNotFoundException {
String value = policyDef.getValue(); String value = policyDef.getValue();

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch> * Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -23,35 +23,45 @@ import li.strolch.persistence.api.StrolchTransaction;
* <p> * <p>
* The {@link PolicyHandler} is Strolch's mechanism of dependency injection * The {@link PolicyHandler} is Strolch's mechanism of dependency injection
* </p> * </p>
* *
* <p> * <p>
* Objects which require delegation can use a {@link PolicyConfiguration} element and then retrieve a StrolchPolicy * Objects which require delegation can use a {@link PolicyConfiguration} element and then retrieve a StrolchPolicy
* instance from this {@link PolicyHandler}. * instance from this {@link PolicyHandler}.
* </p> * </p>
* *
* <p> * <p>
* {@link PolicyConfiguration} have a mapping of a policy type, i.e. an interface for a specific delegation. This * {@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 * interface has concrete implementations which are then returned by the {@link PolicyHandler} depending on the current
* configuration * configuration
* </p> * </p>
* *
* <p> * <p>
* The resolving of a policy instance is handled by a {@link PolicyDefVisitor} * The resolving of a policy instance is handled by a {@link PolicyDefVisitor}
* <p> * <p>
* *
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*/ */
public interface PolicyHandler { public interface PolicyHandler {
/** /**
* Instantiates the actual policy by resolving the {@link PolicyDef} using a {@link PolicyDefVisitor} * Instantiates the actual policy by resolving the {@link PolicyDef} using a {@link PolicyDefVisitor}
* *
* @param policyDef * @param policyDef
* the {@link PolicyDef} referencing a concrete policy * the {@link PolicyDef} referencing a concrete policy
* @param tx * @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 * @return the instantiated instance of the referenced policy
*/ */
public <T extends StrolchPolicy> T getPolicy(PolicyDef policyDef, StrolchTransaction tx); <T extends StrolchPolicy> 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);
} }