From 01bc0d257b38743372af91cb88269524634db7d3 Mon Sep 17 00:00:00 2001 From: Jeremy Bauer Date: Fri, 29 Mar 2013 17:48:02 +0000 Subject: [PATCH] Disable logging during brokerfactory de-serialization. Added type checking of plugin values. git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@1462558 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/kernel/AbstractBrokerFactory.java | 6 +++++ .../apache/openjpa/lib/conf/PluginValue.java | 24 +++++++++++++++++++ .../openjpa/lib/conf/localizer.properties | 3 +++ 3 files changed, 33 insertions(+) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java index aff638325..5cbf25a4a 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractBrokerFactory.java @@ -476,7 +476,13 @@ public abstract class AbstractBrokerFactory _brokers = new ConcurrentReferenceHashSet( ConcurrentReferenceHashSet.WEAK); + // turn off logging while de-serializing BrokerFactory + String saveLogConfig = _conf.getLog(); + _conf.setLog("none"); makeReadOnly(); + // re-enable any logging which was in effect + _conf.setLog(saveLogConfig); + return this; } diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/PluginValue.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/PluginValue.java index e649dfc61..362072abd 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/PluginValue.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/PluginValue.java @@ -19,7 +19,9 @@ package org.apache.openjpa.lib.conf; import org.apache.commons.lang.StringUtils; +import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.util.Localizer; +import org.apache.openjpa.lib.util.ParseException; /** * A plugin {@link Value} consisting of plugin name and properties. @@ -99,6 +101,20 @@ public class PluginValue extends ObjectValue { */ public Object instantiate(Class type, Configuration conf, boolean fatal) { Object obj = newInstance(_name, type, conf, fatal); + + // ensure plugin value is compatible with plugin type + if (obj != null && !type.isAssignableFrom(obj.getClass())) { + Log log = (conf == null) ? null : conf.getConfigurationLog(); + String msg = getIncompatiblePluginMessage(obj, type); + if (log != null && log.isErrorEnabled()) { + log.error(msg); + } + if (fatal) { + throw new ParseException(msg); + } + return null; + } + Configurations.configureInstance(obj, conf, _props, (fatal) ? getProperty() : null); if (_singleton) @@ -106,6 +122,14 @@ public class PluginValue extends ObjectValue { return obj; } + private String getIncompatiblePluginMessage(Object obj, Class type) { + return _loc.get("incompatible-plugin", + new Object[]{ _name, + obj == null ? null : obj.getClass().getName(), + type == null ? null : type.getName() + }).toString(); + } + public void set(Object obj, boolean derived) { if (!_singleton) throw new IllegalStateException(_loc.get("not-singleton", diff --git a/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties b/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties index 36b4a1539..21435931f 100644 --- a/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties +++ b/openjpa-lib/src/main/resources/org/apache/openjpa/lib/conf/localizer.properties @@ -112,3 +112,6 @@ Id-displayorder: 50 Id-expert: true not-dynamic: Can not modify "{0}" to "{1}" because the property is not dynamic. +incompatible-plugin: The plugin "{0}" of value type "{1}" is not compatible with \ + the expected plugin type "{2}". Update the configuration property with a value that \ + is compatible with the plugin.