Return-Path: Delivered-To: apmail-incubator-open-jpa-commits-archive@locus.apache.org Received: (qmail 2301 invoked from network); 28 Jun 2006 19:35:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Jun 2006 19:35:37 -0000 Received: (qmail 29138 invoked by uid 500); 28 Jun 2006 19:35:36 -0000 Delivered-To: apmail-incubator-open-jpa-commits-archive@incubator.apache.org Received: (qmail 29114 invoked by uid 500); 28 Jun 2006 19:35:36 -0000 Mailing-List: contact open-jpa-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: open-jpa-dev@incubator.apache.org Delivered-To: mailing list open-jpa-commits@incubator.apache.org Received: (qmail 29081 invoked by uid 99); 28 Jun 2006 19:35:36 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jun 2006 12:35:36 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jun 2006 12:35:29 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 8D11B1A984A; Wed, 28 Jun 2006 12:35:09 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r417856 [3/22] - in /incubator/openjpa/trunk/openjpa-lib: java/ main/ main/java/ main/java/org/apache/openjpa/lib/ant/ main/java/org/apache/openjpa/lib/conf/ main/java/org/apache/openjpa/lib/jdbc/ main/java/org/apache/openjpa/lib/log/ main/... Date: Wed, 28 Jun 2006 19:34:40 -0000 To: open-jpa-commits@incubator.apache.org From: pcl@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060628193509.8D11B1A984A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/Configurations.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/Configurations.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/Configurations.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/Configurations.java Wed Jun 28 12:34:33 2006 @@ -15,11 +15,6 @@ */ package org.apache.openjpa.lib.conf; - -import java.io.*; -import java.util.*; -import javax.naming.*; - import org.apache.commons.lang.exception.*; import org.apache.openjpa.lib.log.*; @@ -27,646 +22,656 @@ import serp.util.*; +import java.io.*; + +import java.util.*; + +import javax.naming.*; + /** - *

Utility methods dealing with configuration.

+ *

Utility methods dealing with configuration.

* - * @author Abe White - * @nojavadoc - */ -public class Configurations -{ - private static final Localizer _loc = Localizer.forPackage - (Configurations.class); - - - /** - * Return the class name from the given plugin string, or null if none. - */ - public static String getClassName (String plugin) - { - return getPluginComponent (plugin, true); - } - - - /** - * Return the properties part of the given plugin string, or null if none. - */ - public static String getProperties (String plugin) - { - return getPluginComponent (plugin, false); - } - - - /** - * Return either the class name or properties string from a plugin string. - */ - private static String getPluginComponent (String plugin, boolean clsName) - { - if (plugin != null) - plugin = plugin.trim (); - if (plugin == null || plugin.length () == 0) - return null; - - int openParen = -1; - if (plugin.charAt (plugin.length () - 1) == ')') - openParen = plugin.indexOf ('('); - if (openParen == -1) - { - int eq = plugin.indexOf ('='); - if (eq == -1) - return (clsName) ? plugin : null; - return (clsName) ? null : plugin; - } - - // clsName(props) form - if (clsName) - return plugin.substring (0, openParen).trim (); - String prop = plugin.substring (openParen + 1, - plugin.length () - 1).trim (); - return (prop.length () == 0) ? null : prop; - } - - - /** - * Combine the given class name and properties into a plugin string. - */ - public static String getPlugin (String clsName, String props) - { - if (clsName == null || clsName.length () == 0) - return props; - if (props == null || props.length () == 0) - return clsName; - return clsName + "(" + props + ")"; - } - - - /** - * Create the instance with the given class name, using the given - * class loader. No configuration of the instance is performed by - * this method. - */ - public static Object newInstance (String clsName, ClassLoader loader) - { - return newInstance (clsName, null, null, loader, true); - } - - - /** - * Create and configure an instance with the given class name and - * properties. - */ - public static Object newInstance (String clsName, Configuration conf, - String props, ClassLoader loader) - { - Object obj = newInstance (clsName, null, conf, loader, true); - configureInstance (obj, conf, props); - return obj; - } - - - /** - * Helper method used by members of this package to instantiate plugin - * values. - */ - static Object newInstance (String clsName, Value val, Configuration conf, - ClassLoader loader, boolean fatal) - { - if (clsName == null || clsName.length () == 0) - return null; - if (loader == null && conf != null) - loader = conf.getClass ().getClassLoader (); - - Class cls = null; - try - { - cls = Strings.toClass (clsName, loader); - } - catch (RuntimeException re) - { - if (val != null) - re = getCreateException (clsName, val, re); - if (fatal) - throw re; - Log log = (conf == null) ? null : conf.getConfigurationLog (); - if (log != null && log.isErrorEnabled ()) - log.error (re); - return null; - } - - try - { - return cls.newInstance (); - } - catch (Exception e) - { - RuntimeException re = new NestableRuntimeException (_loc.get - ("obj-create", cls), e); - if (fatal) - throw re; - Log log = (conf == null) ? null : conf.getConfigurationLog (); - if (log != null && log.isErrorEnabled ()) - log.error (re); - return null; - } - } - - - /** - * Helper method to throw an informative description on instantiation - * error. - */ - private static RuntimeException getCreateException (String clsName, - Value val, Exception e) - { - // re-throw the exception with some better information - final String msg; - final Object[] params; - - String alias = val.alias (clsName); - String[] aliases = val.getAliases (); - String[] keys; - if (aliases.length == 0) - keys = aliases; - else - { - keys = new String[aliases.length / 2]; - for (int i = 0; i < aliases.length; i += 2) - keys[i / 2] = aliases[i]; - } - - String closest; - if (keys.length == 0) - { - msg = "invalid-plugin"; - params = new Object[] { val.getProperty (), alias, e.toString (), }; - } - else if ((closest = StringDistance.getClosestLevenshteinDistance - (alias, keys, 0.5f)) == null) - { - msg = "invalid-plugin-aliases"; - params = new Object[] { - val.getProperty (), alias, e.toString (), - new TreeSet (Arrays.asList (keys)), - }; - } - else - { - msg = "invalid-plugin-aliases-hint"; - params = new Object[] { - val.getProperty (), alias, e.toString (), - new TreeSet (Arrays.asList (keys)), closest, - }; - } - return new ParseException (_loc.get (msg, params), e); - } - - - /** - * Configures the given object with the given properties by - * matching the properties string to the object's setter - * methods. The properties string should be in the form - * "prop1=val1, prop2=val2 ...". Does not validate that setter - * methods exist for the properties. - * - * @throws RuntimeException on configuration error - */ - public static void configureInstance (Object obj, Configuration conf, - String properties) - { - configureInstance (obj, conf, properties, null); - } - - - /** - * Configures the given object with the given properties by - * matching the properties string to the object's setter - * methods. The properties string should be in the form - * "prop1=val1, prop2=val2 ...". Validates that setter methods - * exist for the properties. - * - * @throws RuntimeException on configuration error - */ - public static void configureInstance (Object obj, Configuration conf, - String properties, String configurationName) - { - if (obj == null) - return; - - Properties props = null; - if (properties != null && properties.length () > 0) - props = parseProperties (properties); - configureInstance (obj, conf, props, configurationName); - } - - - /** - * Configures the given object with the given properties by - * matching the properties string to the object's setter - * methods. Does not validate that setter methods exist for the - * properties. - * - * @throws RuntimeException on configuration error - */ - public static void configureInstance (Object obj, Configuration conf, - Properties properties) - { - configureInstance (obj, conf, properties, null); - } - - - /** - * Configures the given object with the given properties by - * matching the properties string to the object's setter - * methods. If configurationName is - * non-null, validates that setter methods exist for - * the properties. - * - * @throws RuntimeException on configuration error - */ - public static void configureInstance (Object obj, Configuration conf, - Properties properties, String configurationName) - { - if (obj == null) - return; - - Options opts = null; - if (properties instanceof Options) - opts = (Options) properties; - else if (properties != null) - { - opts = new Options (); - opts.putAll (properties); - } - - Configurable configurable = null; - if (conf != null && obj instanceof Configurable) - configurable = (Configurable) obj; - - if (configurable != null) - { - configurable.setConfiguration (conf); - configurable.startConfiguration (); - } - if (opts != null) - { - Map invalidEntries = opts.setInto (obj); - if (obj instanceof GenericConfigurable) - ((GenericConfigurable) obj).setInto (invalidEntries); - - if (!invalidEntries.isEmpty () && configurationName != null) - { - String msg = null; - String first = (String) invalidEntries.keySet ().iterator () - .next (); - if (invalidEntries.keySet ().size () == 1 && - first.indexOf ('.') == -1) - { - // if there's just one misspelling and this is not a - // path traversal, check for near misses. - Collection options = - Options.findOptionsFor (obj.getClass ()); - String close = StringDistance.getClosestLevenshteinDistance - (first, options, 0.75f); - if (close != null) - msg = _loc.get ("invalid-config-param-hint", - new Object[] { - configurationName, - obj.getClass (), - first, - close, - options, - }); - } - - if (msg == null) - { - msg = _loc.get ("invalid-config-params", new String[] { - configurationName, - obj.getClass ().getName (), - invalidEntries.keySet ().toString (), - Options.findOptionsFor (obj.getClass ()).toString (), - }); - } - throw new ParseException (msg); - } - } - if (configurable != null) - configurable.endConfiguration (); - } - - - /** - * Turn a set of properties into a comma-separated string. - */ - public static String serializeProperties (Map map) - { - if (map == null || map.isEmpty ()) - return null; - - StringBuffer buf = new StringBuffer (); - Map.Entry entry; - String val; - for (Iterator itr = map.entrySet ().iterator (); itr.hasNext ();) - { - entry = (Map.Entry) itr.next (); - if (buf.length () > 0) - buf.append (", "); - buf.append (entry.getKey ()).append ('='); - val = String.valueOf (entry.getValue ()); - if (val.indexOf (',') != -1) - buf.append ('"').append (val).append ('"'); - else - buf.append (val); - } - return buf.toString (); - } - - - /** - * Parse a set of properties from a comma-separated string. - */ - public static Options parseProperties (String properties) - { - Options opts = new Options (); - if (properties == null) - return opts; - properties = properties.trim (); - if (properties.length () == 0) - return opts; - - try - { - String[] props = Strings.split (properties, ",", 0); - int idx; - char quote; - String prop; - String val; - for (int i = 0; i < props.length; i++) - { - idx = props[i].indexOf ('='); - if (idx == -1) - { - // if the key is not assigned to any value, set the - // value to the same thing as the key, and continue. - // This permits GenericConfigurable instances to - // behave meaningfully. We might consider setting the - // value to some well-known "value was not set, but - // key is present" string so that instances getting - // values injected can differentiate between a mentioned - // property and one set to a particular value. - prop = props[i]; - val = prop; - } - else - { - prop = props[i].substring (0, idx).trim (); - val = props[i].substring (idx + 1).trim (); - } - - // if the value is quoted, read until the end quote - if (((val.startsWith ("\"") && val.endsWith ("\"")) - || (val.startsWith ("'") && val.endsWith ("'"))) - && val.length () > 1) - val = val.substring (1, val.length () - 1); - else if (val.startsWith ("\"") || val.startsWith ("'")) - { - quote = val.charAt (0); - StringBuffer buf = new StringBuffer (val.substring (1)); - int quotIdx; - while (++i < props.length) - { - buf.append (","); - - quotIdx = props[i].indexOf (quote); - if (quotIdx != -1) - { - buf.append (props[i].substring (0, quotIdx)); - if (quotIdx + 1 < props[i].length ()) - buf.append (props[i].substring (quotIdx + 1)); - break; - } - else - buf.append (props[i]); - } - - val = buf.toString (); - } - - opts.put (prop, val); - } - return opts; - } - catch (RuntimeException re) - { - throw new ParseException (_loc.get ("prop-parse", properties), re); - } - } - - - /** - * Set the given {@link Configuration} instance from the command line - * options provided. All property names of the given configuration are - * recognized; additionally, if a properties or - * p argument exists, the resource it - * points to will be loaded and set into the given configuration instance. - * It can point to either a file or a resource name. - */ - public static void populateConfiguration (Configuration conf, Options opts) - { - String props = opts.removeProperty ("properties", "p", null); - if (props != null && props.length () > 0) - { - File file = new File (props); - ConfigurationProvider provider; - if (file.isFile ()) - provider = load (file, null); - else - { - file = new File ("META-INF" + File.separatorChar + props); - if (file.isFile ()) - provider = load (file, null); - else - provider = load (props, null); - } - provider.setInto (conf); - } - opts.setInto (conf); - } - - - /** - * Return a {@link ConfigurationProvider} that has parsed system defaults, - * or null if no provider or defaults found. - */ - public static ConfigurationProvider loadDefaults (ClassLoader loader) - { - if (loader == null) - loader = Thread.currentThread ().getContextClassLoader (); - Class[] impls = Services.getImplementorClasses - (ConfigurationProvider.class, loader); - ConfigurationProvider provider = null; - StringBuffer errs = null; - for (int i = 0; i < impls.length; i++) - { - provider = newProvider (impls[i]); - try - { - if (provider != null && provider.loadDefaults (loader)) - return provider; - } - catch (MissingResourceException mre) - { - throw mre; - } - catch (Exception e) - { - if (errs == null) - errs = new StringBuffer (); - else - errs.append (", "); - errs.append (e.toString ()); - } - } - if (errs != null) - throw new MissingResourceException (errs.toString (), - Configurations.class.getName (), "defaults"); - return null; - } - - - /** - * Return a new new configuration provider instance of the given class, - * or null if the class cannot be instantiated. - */ - private static ConfigurationProvider newProvider (Class cls) - { - try - { - return (ConfigurationProvider) cls.newInstance (); - } - catch (InstantiationException e) - { - return null; - } - catch (IllegalAccessException e) - { - return null; - } - } - - - /** - * Return a {@link ConfigurationProvider} that has parsed the given - * resource. Throws {@link MissingResourceException} if resource does - * not exist. - */ - public static ConfigurationProvider load (String resource, - ClassLoader loader) - { - if (resource == null || resource.length () == 0) - return null; - - if (loader == null) - loader = Thread.currentThread ().getContextClassLoader (); - Class[] impls = Services.getImplementorClasses - (ConfigurationProvider.class, loader); - ConfigurationProvider provider = null; - StringBuffer errs = null; - for (int i = 0; i < impls.length; i++) - { - provider = newProvider (impls[i]); - try - { - if (provider != null && provider.load (resource, loader)) - return provider; - } - catch (MissingResourceException mre) - { - throw mre; - } - catch (Exception e) - { - if (errs == null) - errs = new StringBuffer (); - else - errs.append (", "); - errs.append (e.toString ()); - } - } - String msg = (errs == null) ? resource : errs.toString (); - throw new MissingResourceException (msg, - Configurations.class.getName (), resource); - } - - - /** - * Return a {@link ConfigurationProvider} that has parsed the given - * file. Throws {@link MissingResourceException} if file does - * not exist. - */ - public static ConfigurationProvider load (File file, ClassLoader loader) - { - if (file == null) - return null; - - if (loader == null) - loader = Thread.currentThread ().getContextClassLoader (); - Class[] impls = Services.getImplementorClasses - (ConfigurationProvider.class, loader); - ConfigurationProvider provider = null; - StringBuffer errs = null; - for (int i = 0; i < impls.length; i++) - { - provider = newProvider (impls[i]); - try - { - if (provider != null && provider.load (file)) - return provider; - } - catch (MissingResourceException mre) - { - throw mre; - } - catch (Exception e) - { - if (errs == null) - errs = new StringBuffer (); - else - errs.append (", "); - errs.append (e.toString ()); - } - } - String msg = (errs == null) ? file.toString () : errs.toString (); - throw new MissingResourceException (msg, - Configurations.class.getName (), file.toString ()); - } - - - /** - * Looks up the given name in JNDI. If the name is null, null is - * returned. - */ - public static Object lookup (String name) - { - if (name == null || name.length () == 0) - return null; - - Context ctx = null; - try - { - ctx = new InitialContext (); - return ctx.lookup (name); - } - catch (NamingException ne) - { - throw new NestableRuntimeException (_loc.get ("naming-err", name), - ne); - } - finally - { - if (ctx != null) - try { ctx.close (); } catch (Exception e) {} - } - } + * @author Abe White + * @nojavadoc */ +public class Configurations { + private static final Localizer _loc = Localizer.forPackage(Configurations.class); + + /** + * Return the class name from the given plugin string, or null if none. + */ + public static String getClassName(String plugin) { + return getPluginComponent(plugin, true); + } + + /** + * Return the properties part of the given plugin string, or null if none. + */ + public static String getProperties(String plugin) { + return getPluginComponent(plugin, false); + } + + /** + * Return either the class name or properties string from a plugin string. + */ + private static String getPluginComponent(String plugin, boolean clsName) { + if (plugin != null) { + plugin = plugin.trim(); + } + + if ((plugin == null) || (plugin.length() == 0)) { + return null; + } + + int openParen = -1; + + if (plugin.charAt(plugin.length() - 1) == ')') { + openParen = plugin.indexOf('('); + } + + if (openParen == -1) { + int eq = plugin.indexOf('='); + + if (eq == -1) { + return (clsName) ? plugin : null; + } + + return (clsName) ? null : plugin; + } + + // clsName(props) form + if (clsName) { + return plugin.substring(0, openParen).trim(); + } + + String prop = plugin.substring(openParen + 1, plugin.length() - 1).trim(); + + return (prop.length() == 0) ? null : prop; + } + + /** + * Combine the given class name and properties into a plugin string. + */ + public static String getPlugin(String clsName, String props) { + if ((clsName == null) || (clsName.length() == 0)) { + return props; + } + + if ((props == null) || (props.length() == 0)) { + return clsName; + } + + return clsName + "(" + props + ")"; + } + + /** + * Create the instance with the given class name, using the given + * class loader. No configuration of the instance is performed by + * this method. + */ + public static Object newInstance(String clsName, ClassLoader loader) { + return newInstance(clsName, null, null, loader, true); + } + + /** + * Create and configure an instance with the given class name and + * properties. + */ + public static Object newInstance(String clsName, Configuration conf, + String props, ClassLoader loader) { + Object obj = newInstance(clsName, null, conf, loader, true); + configureInstance(obj, conf, props); + + return obj; + } + + /** + * Helper method used by members of this package to instantiate plugin + * values. + */ + static Object newInstance(String clsName, Value val, Configuration conf, + ClassLoader loader, boolean fatal) { + if ((clsName == null) || (clsName.length() == 0)) { + return null; + } + + if ((loader == null) && (conf != null)) { + loader = conf.getClass().getClassLoader(); + } + + Class cls = null; + + try { + cls = Strings.toClass(clsName, loader); + } catch (RuntimeException re) { + if (val != null) { + re = getCreateException(clsName, val, re); + } + + if (fatal) { + throw re; + } + + Log log = (conf == null) ? null : conf.getConfigurationLog(); + + if ((log != null) && log.isErrorEnabled()) { + log.error(re); + } + + return null; + } + + try { + return cls.newInstance(); + } catch (Exception e) { + RuntimeException re = new NestableRuntimeException(_loc.get( + "obj-create", cls), e); + + if (fatal) { + throw re; + } + + Log log = (conf == null) ? null : conf.getConfigurationLog(); + + if ((log != null) && log.isErrorEnabled()) { + log.error(re); + } + + return null; + } + } + + /** + * Helper method to throw an informative description on instantiation + * error. + */ + private static RuntimeException getCreateException(String clsName, + Value val, Exception e) { + // re-throw the exception with some better information + final String msg; + final Object[] params; + + String alias = val.alias(clsName); + String[] aliases = val.getAliases(); + String[] keys; + + if (aliases.length == 0) { + keys = aliases; + } else { + keys = new String[aliases.length / 2]; + + for (int i = 0; i < aliases.length; i += 2) + keys[i / 2] = aliases[i]; + } + + String closest; + + if (keys.length == 0) { + msg = "invalid-plugin"; + params = new Object[] { val.getProperty(), alias, e.toString(), }; + } else if ((closest = StringDistance.getClosestLevenshteinDistance( + alias, keys, 0.5f)) == null) { + msg = "invalid-plugin-aliases"; + params = new Object[] { + val.getProperty(), alias, e.toString(), + new TreeSet(Arrays.asList(keys)), + }; + } else { + msg = "invalid-plugin-aliases-hint"; + params = new Object[] { + val.getProperty(), alias, e.toString(), + new TreeSet(Arrays.asList(keys)), closest, + }; + } + + return new ParseException(_loc.get(msg, params), e); + } + + /** + * Configures the given object with the given properties by + * matching the properties string to the object's setter + * methods. The properties string should be in the form + * "prop1=val1, prop2=val2 ...". Does not validate that setter + * methods exist for the properties. + * + * @throws RuntimeException on configuration error + */ + public static void configureInstance(Object obj, Configuration conf, + String properties) { + configureInstance(obj, conf, properties, null); + } + + /** + * Configures the given object with the given properties by + * matching the properties string to the object's setter + * methods. The properties string should be in the form + * "prop1=val1, prop2=val2 ...". Validates that setter methods + * exist for the properties. + * + * @throws RuntimeException on configuration error + */ + public static void configureInstance(Object obj, Configuration conf, + String properties, String configurationName) { + if (obj == null) { + return; + } + + Properties props = null; + + if ((properties != null) && (properties.length() > 0)) { + props = parseProperties(properties); + } + + configureInstance(obj, conf, props, configurationName); + } + + /** + * Configures the given object with the given properties by + * matching the properties string to the object's setter + * methods. Does not validate that setter methods exist for the + * properties. + * + * @throws RuntimeException on configuration error + */ + public static void configureInstance(Object obj, Configuration conf, + Properties properties) { + configureInstance(obj, conf, properties, null); + } + + /** + * Configures the given object with the given properties by + * matching the properties string to the object's setter + * methods. If configurationName is + * non-null, validates that setter methods exist for + * the properties. + * + * @throws RuntimeException on configuration error + */ + public static void configureInstance(Object obj, Configuration conf, + Properties properties, String configurationName) { + if (obj == null) { + return; + } + + Options opts = null; + + if (properties instanceof Options) { + opts = (Options) properties; + } else if (properties != null) { + opts = new Options(); + opts.putAll(properties); + } + + Configurable configurable = null; + + if ((conf != null) && obj instanceof Configurable) { + configurable = (Configurable) obj; + } + + if (configurable != null) { + configurable.setConfiguration(conf); + configurable.startConfiguration(); + } + + if (opts != null) { + Map invalidEntries = opts.setInto(obj); + + if (obj instanceof GenericConfigurable) { + ((GenericConfigurable) obj).setInto(invalidEntries); + } + + if (!invalidEntries.isEmpty() && (configurationName != null)) { + String msg = null; + String first = (String) invalidEntries.keySet().iterator().next(); + + if ((invalidEntries.keySet().size() == 1) && + (first.indexOf('.') == -1)) { + // if there's just one misspelling and this is not a + // path traversal, check for near misses. + Collection options = Options.findOptionsFor(obj.getClass()); + String close = StringDistance.getClosestLevenshteinDistance(first, + options, 0.75f); + + if (close != null) { + msg = _loc.get("invalid-config-param-hint", + new Object[] { + configurationName, obj.getClass(), first, + close, options, + }); + } + } + + if (msg == null) { + msg = _loc.get("invalid-config-params", + new String[] { + configurationName, obj.getClass().getName(), + invalidEntries.keySet().toString(), + Options.findOptionsFor(obj.getClass()).toString(), + }); + } + + throw new ParseException(msg); + } + } + + if (configurable != null) { + configurable.endConfiguration(); + } + } + + /** + * Turn a set of properties into a comma-separated string. + */ + public static String serializeProperties(Map map) { + if ((map == null) || map.isEmpty()) { + return null; + } + + StringBuffer buf = new StringBuffer(); + Map.Entry entry; + String val; + + for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) { + entry = (Map.Entry) itr.next(); + + if (buf.length() > 0) { + buf.append(", "); + } + + buf.append(entry.getKey()).append('='); + val = String.valueOf(entry.getValue()); + + if (val.indexOf(',') != -1) { + buf.append('"').append(val).append('"'); + } else { + buf.append(val); + } + } + + return buf.toString(); + } + + /** + * Parse a set of properties from a comma-separated string. + */ + public static Options parseProperties(String properties) { + Options opts = new Options(); + + if (properties == null) { + return opts; + } + + properties = properties.trim(); + + if (properties.length() == 0) { + return opts; + } + + try { + String[] props = Strings.split(properties, ",", 0); + int idx; + char quote; + String prop; + String val; + + for (int i = 0; i < props.length; i++) { + idx = props[i].indexOf('='); + + if (idx == -1) { + // if the key is not assigned to any value, set the + // value to the same thing as the key, and continue. + // This permits GenericConfigurable instances to + // behave meaningfully. We might consider setting the + // value to some well-known "value was not set, but + // key is present" string so that instances getting + // values injected can differentiate between a mentioned + // property and one set to a particular value. + prop = props[i]; + val = prop; + } else { + prop = props[i].substring(0, idx).trim(); + val = props[i].substring(idx + 1).trim(); + } + + // if the value is quoted, read until the end quote + if (((val.startsWith("\"") && val.endsWith("\"")) || + (val.startsWith("'") && val.endsWith("'"))) && + (val.length() > 1)) { + val = val.substring(1, val.length() - 1); + } else if (val.startsWith("\"") || val.startsWith("'")) { + quote = val.charAt(0); + + StringBuffer buf = new StringBuffer(val.substring(1)); + int quotIdx; + + while (++i < props.length) { + buf.append(","); + + quotIdx = props[i].indexOf(quote); + + if (quotIdx != -1) { + buf.append(props[i].substring(0, quotIdx)); + + if ((quotIdx + 1) < props[i].length()) { + buf.append(props[i].substring(quotIdx + 1)); + } + + break; + } else { + buf.append(props[i]); + } + } + + val = buf.toString(); + } + + opts.put(prop, val); + } + + return opts; + } catch (RuntimeException re) { + throw new ParseException(_loc.get("prop-parse", properties), re); + } + } + + /** + * Set the given {@link Configuration} instance from the command line + * options provided. All property names of the given configuration are + * recognized; additionally, if a properties or + * p argument exists, the resource it + * points to will be loaded and set into the given configuration instance. + * It can point to either a file or a resource name. + */ + public static void populateConfiguration(Configuration conf, Options opts) { + String props = opts.removeProperty("properties", "p", null); + + if ((props != null) && (props.length() > 0)) { + File file = new File(props); + ConfigurationProvider provider; + + if (file.isFile()) { + provider = load(file, null); + } else { + file = new File("META-INF" + File.separatorChar + props); + + if (file.isFile()) { + provider = load(file, null); + } else { + provider = load(props, null); + } + } + + provider.setInto(conf); + } + + opts.setInto(conf); + } + + /** + * Return a {@link ConfigurationProvider} that has parsed system defaults. + */ + public static ConfigurationProvider loadDefaults(ClassLoader loader) { + if (loader == null) { + loader = Thread.currentThread().getContextClassLoader(); + } + + Class[] impls = Services.getImplementorClasses(ConfigurationProvider.class, + loader); + ConfigurationProvider provider = null; + StringBuffer errs = null; + + for (int i = 0; i < impls.length; i++) { + provider = newProvider(impls[i]); + + try { + if ((provider != null) && provider.loadDefaults(loader)) { + return provider; + } + } catch (MissingResourceException mre) { + throw mre; + } catch (Exception e) { + if (errs == null) { + errs = new StringBuffer(); + } else { + errs.append(", "); + } + + errs.append(e.toString()); + } + } + + if (errs != null) { + throw new MissingResourceException(errs.toString(), + Configurations.class.getName(), "defaults"); + } + + return null; + } + + /** + * Return a new new configuration provider instance of the given class, + * or null if the class cannot be instantiated. + */ + private static ConfigurationProvider newProvider(Class cls) { + try { + return (ConfigurationProvider) cls.newInstance(); + } catch (Throwable e) { + return null; + } + } + + /** + * Return a {@link ConfigurationProvider} that has parsed the given + * resource. Throws {@link MissingResourceException} if resource does + * not exist. + */ + public static ConfigurationProvider load(String resource, ClassLoader loader) { + if ((resource == null) || (resource.length() == 0)) { + return null; + } + + if (loader == null) { + loader = Thread.currentThread().getContextClassLoader(); + } + + Class[] impls = Services.getImplementorClasses(ConfigurationProvider.class, + loader); + ConfigurationProvider provider = null; + StringBuffer errs = null; + + for (int i = 0; i < impls.length; i++) { + provider = newProvider(impls[i]); + + try { + if ((provider != null) && provider.load(resource, loader)) { + return provider; + } + } catch (MissingResourceException mre) { + throw mre; + } catch (Exception e) { + if (errs == null) { + errs = new StringBuffer(); + } else { + errs.append(", "); + } + + errs.append(e.toString()); + } + } + + String msg = (errs == null) ? resource : errs.toString(); + throw new MissingResourceException(msg, Configurations.class.getName(), + resource); + } + + /** + * Return a {@link ConfigurationProvider} that has parsed the given + * file. Throws {@link MissingResourceException} if file does + * not exist. + */ + public static ConfigurationProvider load(File file, ClassLoader loader) { + if (file == null) { + return null; + } + + if (loader == null) { + loader = Thread.currentThread().getContextClassLoader(); + } + + Class[] impls = Services.getImplementorClasses(ConfigurationProvider.class, + loader); + ConfigurationProvider provider = null; + StringBuffer errs = null; + + for (int i = 0; i < impls.length; i++) { + provider = newProvider(impls[i]); + + try { + if ((provider != null) && provider.load(file)) { + return provider; + } + } catch (MissingResourceException mre) { + throw mre; + } catch (Exception e) { + if (errs == null) { + errs = new StringBuffer(); + } else { + errs.append(", "); + } + + errs.append(e.toString()); + } + } + + String msg = (errs == null) ? file.toString() : errs.toString(); + throw new MissingResourceException(msg, Configurations.class.getName(), + file.toString()); + } + + /** + * Looks up the given name in JNDI. If the name is null, null is + * returned. + */ + public static Object lookup(String name) { + if ((name == null) || (name.length() == 0)) { + return null; + } + + Context ctx = null; + + try { + ctx = new InitialContext(); + + return ctx.lookup(name); + } catch (NamingException ne) { + throw new NestableRuntimeException(_loc.get("naming-err", name), ne); + } finally { + if (ctx != null) { + try { + ctx.close(); + } catch (Exception e) { + } + } + } + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/DoubleValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/DoubleValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/DoubleValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/DoubleValue.java Wed Jun 28 12:34:33 2006 @@ -17,69 +17,57 @@ /** - * A double {@link Value}. + * A double {@link Value}. * - * @author Marc Prud'hommeaux + * @author Marc Prud'hommeaux */ -public class DoubleValue - extends Value -{ - private double value; - - - public DoubleValue (String prop) - { - super (prop); - } - - - public Class getValueType () - { - return double.class; - } - - - /** - * The internal value. - */ - public void set (double value) - { - double oldValue = this.value; - this.value = value; - if (oldValue != value) - valueChanged (); - } - - - /** - * The internal value. - */ - public double get () - { - return value; - } - - - protected String getInternalString () - { - return String.valueOf (value); - } - - - protected void setInternalString (String val) - { - if (val == null || val.length () == 0) - set (0D); - else - set (Double.parseDouble (val)); - } - - - protected void setInternalObject (Object obj) - { - if (obj == null) - set (0D); - else - set (((Number) obj).doubleValue ()); - } +public class DoubleValue extends Value { + private double value; + + public DoubleValue(String prop) { + super(prop); + } + + public Class getValueType() { + return double.class; + } + + /** + * The internal value. + */ + public void set(double value) { + double oldValue = this.value; + this.value = value; + + if (oldValue != value) { + valueChanged(); + } + } + + /** + * The internal value. + */ + public double get() { + return value; + } + + protected String getInternalString() { + return String.valueOf(value); + } + + protected void setInternalString(String val) { + if ((val == null) || (val.length() == 0)) { + set(0D); + } else { + set(Double.parseDouble(val)); + } + } + + protected void setInternalObject(Object obj) { + if (obj == null) { + set(0D); + } else { + set(((Number) obj).doubleValue()); + } + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/FileValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/FileValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/FileValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/FileValue.java Wed Jun 28 12:34:33 2006 @@ -15,71 +15,55 @@ */ package org.apache.openjpa.lib.conf; -import java.io.*; - import org.apache.commons.lang.*; +import java.io.*; + /** - * A {@link File} {@link Value}. + * A {@link File} {@link Value}. * - * @author Marc Prud'hommeaux + * @author Marc Prud'hommeaux */ -public class FileValue - extends Value -{ - private File value; - - - public FileValue (String prop) - { - super (prop); - } - - - public Class getValueType () - { - return File.class; - } - - - /** - * The internal value. - */ - public void set (File value) - { - File oldValue = this.value; - this.value = value; - if (!ObjectUtils.equals (oldValue, value)) - valueChanged (); - } +public class FileValue extends Value { + private File value; - - /** - * The internal value. - */ - public File get () - { - return value; - } - - - protected String getInternalString () - { - return (value == null) ? null : value.getAbsolutePath (); - } - - - protected void setInternalString (String val) - { - set (new File (val)); - } - - - protected void setInternalObject (Object obj) - { - set ((File) obj); - } + public FileValue(String prop) { + super(prop); + } + + public Class getValueType() { + return File.class; + } + + /** + * The internal value. + */ + public void set(File value) { + File oldValue = this.value; + this.value = value; + + if (!ObjectUtils.equals(oldValue, value)) { + valueChanged(); + } + } + + /** + * The internal value. + */ + public File get() { + return value; + } + + protected String getInternalString() { + return (value == null) ? null : value.getAbsolutePath(); + } + + protected void setInternalString(String val) { + set(new File(val)); + } + + protected void setInternalObject(Object obj) { + set((File) obj); + } } - - Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/GenericConfigurable.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/GenericConfigurable.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/GenericConfigurable.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/GenericConfigurable.java Wed Jun 28 12:34:33 2006 @@ -15,27 +15,25 @@ */ package org.apache.openjpa.lib.conf; - import java.util.*; /** - *

Implementations of this interface may perform additional - * generic configuration with any key-value pairs that cannot be set - * into the object via the normal {@link org.apache.openjpa.lib.util.Options#setInto} - * means.

+ *

Implementations of this interface may perform additional + * generic configuration with any key-value pairs that cannot be set + * into the object via the normal {@link org.apache.openjpa.lib.util.Options#setInto} + * means.

* - * @author Patrick Linskey + * @author Patrick Linskey */ -public interface GenericConfigurable -{ - /** - * Perform any generic configuration based on the data in - * m. This method should remove any values in - * m that have been successfully processed; if any - * values remain in m after this method is executed, - * an exception will be thrown identifying those key-value pairs - * as invalid. - */ - public void setInto (Map m); +public interface GenericConfigurable { + /** + * Perform any generic configuration based on the data in + * m. This method should remove any values in + * m that have been successfully processed; if any + * values remain in m after this method is executed, + * an exception will be thrown identifying those key-value pairs + * as invalid. + */ + public void setInto(Map m); } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/IntValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/IntValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/IntValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/IntValue.java Wed Jun 28 12:34:33 2006 @@ -15,71 +15,61 @@ */ package org.apache.openjpa.lib.conf; +import serp.util.*; + /** - * An int {@link Value}. + * An int {@link Value}. * - * @author Marc Prud'hommeaux + * @author Marc Prud'hommeaux */ -public class IntValue - extends Value -{ - private int value; - - - public IntValue (String prop) - { - super (prop); - } - - - public Class getValueType () - { - return int.class; - } - - - /** - * The internal value. - */ - public void set (int value) - { - int oldValue = this.value; - this.value = value; - if (value != oldValue) - valueChanged (); - } - - - /** - * The internal value. - */ - public int get () - { - return this.value; - } - - - protected String getInternalString () - { - return String.valueOf (this.value); - } - - - protected void setInternalString (String val) - { - if (val == null || val.length () == 0) - set (0); - else - set (Integer.parseInt (val)); - } - - - protected void setInternalObject (Object obj) - { - if (obj == null) - set (0); - else - set (((Number) obj).intValue ()); - } +public class IntValue extends Value { + private int value; + + public IntValue(String prop) { + super(prop); + } + + public Class getValueType() { + return int.class; + } + + /** + * The internal value. + */ + public void set(int value) { + int oldValue = this.value; + this.value = value; + + if (value != oldValue) { + valueChanged(); + } + } + + /** + * The internal value. + */ + public int get() { + return this.value; + } + + protected String getInternalString() { + return String.valueOf(this.value); + } + + protected void setInternalString(String val) { + if ((val == null) || (val.length() == 0)) { + set(0); + } else { + set(Integer.parseInt(val)); + } + } + + protected void setInternalObject(Object obj) { + if (obj == null) { + set(0); + } else { + set(((Number) obj).intValue()); + } + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/MapConfigurationProvider.java Wed Jun 28 12:34:33 2006 @@ -15,99 +15,91 @@ */ package org.apache.openjpa.lib.conf; - -import java.io.*; -import java.util.*; +import org.apache.commons.collections.*; import org.apache.openjpa.lib.log.*; import org.apache.openjpa.lib.util.*; +import java.io.*; + +import java.util.*; + /** - *

Simple configuration provider that sets configuration based on a - * provided map.

+ *

Simple configuration provider that sets configuration based on a + * provided map.

* - * @author Abe White - * @nojavadoc - */ -public class MapConfigurationProvider - implements ConfigurationProvider -{ - private static final Localizer _loc = Localizer.forPackage - (MapConfigurationProvider.class); - - private Map _props = null; - - - /** - * Construct with null properties. - */ - public MapConfigurationProvider () - { - } - - - /** - * Constructor; supply properties map. - */ - public MapConfigurationProvider (Map props) - { - addProperties (props); - } - - - public boolean loadDefaults (ClassLoader loader) - throws Exception - { - return false; - } - - - public boolean load (String resource, ClassLoader loader) - throws Exception - { - return false; - } - - - public boolean load (File file) - throws Exception - { - return false; - } - - - public Map getProperties () - { - return _props; - } - - - public void addProperties (Map props) - { - if (props == null || props.isEmpty ()) - return; - if (_props == null) - _props = props; - else - _props.putAll (props); - } - - - public void setInto (Configuration conf) - { - setInto (conf, conf.getConfigurationLog ()); - } - - - /** - * Set properties into configuration. If the log is non-null, will log - * a TRACE message about the set. - */ - protected void setInto (Configuration conf, Log log) - { - if (log != null && log.isTraceEnabled ()) - log.trace (_loc.get ("conf-load", _props)); - conf.fromProperties (_props); - } + * @author Abe White + * @nojavadoc */ +public class MapConfigurationProvider implements ConfigurationProvider { + private static final Localizer _loc = Localizer.forPackage(MapConfigurationProvider.class); + private Map _props = null; + + /** + * Construct with null properties. + */ + public MapConfigurationProvider() { + } + + /** + * Constructor; supply properties map. + */ + public MapConfigurationProvider(Map props) { + addProperties(props); + } + + public boolean loadDefaults(ClassLoader loader) throws Exception { + return false; + } + + public boolean load(String resource, ClassLoader loader) + throws Exception { + return false; + } + + public boolean load(File file) throws Exception { + return false; + } + + public Map getProperties() { + return (_props == null) ? Collections.EMPTY_MAP : _props; + } + + public void addProperties(Map props) { + if ((props == null) || props.isEmpty()) { + return; + } + + if (_props == null) { + _props = new HashMap(); + } + + _props.putAll(props); + } + + public Object addProperty(String key, Object value) { + if (_props == null) { + _props = new HashMap(); + } + + return _props.put(key, value); + } + + public void setInto(Configuration conf) { + setInto(conf, conf.getConfigurationLog()); + } + + /** + * Set properties into configuration. If the log is non-null, will log + * a TRACE message about the set. + */ + protected void setInto(Configuration conf, Log log) { + if ((log != null) && log.isTraceEnabled()) { + log.trace(_loc.get("conf-load", getProperties())); + } + + if (_props != null) { + conf.fromProperties(_props); + } + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/ObjectValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/ObjectValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/ObjectValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/ObjectValue.java Wed Jun 28 12:34:33 2006 @@ -15,100 +15,103 @@ */ package org.apache.openjpa.lib.conf; - import org.apache.commons.lang.*; import org.apache.openjpa.lib.util.*; /** - *

An object {@link Value}.

+ *

An object {@link Value}.

* - * @author Abe White + * @author Abe White */ -public class ObjectValue - extends Value -{ - private static final Localizer _loc = Localizer.forPackage - (ObjectValue.class); - - private Object _value = null; - - - public ObjectValue (String prop) - { - super (prop); - } - - - /** - * The internal value. - */ - public Object get () - { - return _value; - } - - - /** - * The internal value. - */ - public void set (Object obj) - { - set (obj, false); - } - - - /** - * The internal value. - * - * @param derived if true, this value was derived from other properties - */ - public void set (Object obj, boolean derived) - { - Object oldValue = _value; - _value = obj; - if (!derived && !ObjectUtils.equals (obj, oldValue)) - { - objectChanged (); - valueChanged (); - } - } - - - public Class getValueType () - { - return Object.class; - } - - - /** - * Implement this method to synchronize internal data with the new - * object value. - */ - protected void objectChanged () - { - } - - - protected String getInternalString () - { - return null; - } - - - protected void setInternalString (String str) - { - if (str == null) - set (null); - else - throw new IllegalArgumentException (_loc.get ("cant-set-string", - getProperty ())); - } - - - protected void setInternalObject (Object obj) - { - set (obj); - } -} +public class ObjectValue extends Value { + private static final Localizer _loc = Localizer.forPackage(ObjectValue.class); + private Object _value = null; + + public ObjectValue(String prop) { + super(prop); + } + + /** + * The internal value. + */ + public Object get() { + return _value; + } + + /** + * The internal value. + */ + public void set(Object obj) { + set(obj, false); + } + + /** + * The internal value. + * + * @param derived if true, this value was derived from other properties + */ + public void set(Object obj, boolean derived) { + Object oldValue = _value; + _value = obj; + + if (!derived && !ObjectUtils.equals(obj, oldValue)) { + objectChanged(); + valueChanged(); + } + } + + /** + * Instantiate the object as an instance of the given class. Equivalent + * to instantiate (type, conf, true). + */ + public Object instantiate(Class type, Configuration conf) { + return instantiate(type, conf, true); + } + + /** + * Instantiate the object as an instance of the given class. + */ + public Object instantiate(Class type, Configuration conf, boolean fatal) { + throw new UnsupportedOperationException(); + } + + /** + * Allow subclasses to instantiate additional plugins. This method does + * not perform configuration. + */ + public Object newInstance(String clsName, Class type, Configuration conf, + boolean fatal) { + return Configurations.newInstance(clsName, this, conf, + type.getClassLoader(), fatal); + } + + public Class getValueType() { + return Object.class; + } + + /** + * Implement this method to synchronize internal data with the new + * object value. + */ + protected void objectChanged() { + } + + protected String getInternalString() { + return null; + } + + protected void setInternalString(String str) { + if (str == null) { + set(null); + } else { + throw new IllegalArgumentException(_loc.get("cant-set-string", + getProperty())); + } + } + + protected void setInternalObject(Object obj) { + set(obj); + } +} Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginListValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginListValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginListValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginListValue.java Wed Jun 28 12:34:33 2006 @@ -15,249 +15,228 @@ */ package org.apache.openjpa.lib.conf; +import org.apache.openjpa.lib.util.*; import java.lang.reflect.*; + import java.util.*; /** - *

A list of plugins. Defaults and aliases on plugin lists apply only - * to individual class names.

+ *

A list of plugins. Defaults and aliases on plugin lists apply only + * to individual class names.

* - * @see PluginValue - * @author Abe White + * @see PluginValue + * @author Abe White */ -public class PluginListValue - extends ObjectValue -{ - private static final String[] EMPTY = new String[0]; - - private String[] _names = EMPTY; - private String[] _props = EMPTY; - - - public PluginListValue (String prop) - { - super (prop); - } - - - /** - * The plugin class names. - */ - public String[] getClassNames () - { - return _names; - } - - - /** - * The plugin class names. - */ - public void setClassNames (String[] names) - { - if (names == null) - names = EMPTY; - _names = names; - set (null, true); - valueChanged (); - } - - - /** - * The plugin properties. - */ - public String[] getProperties () - { - return _props; - } - - - /** - * The plugin properties. - */ - public void setProperties (String[] props) - { - if (props == null) - props = EMPTY; - _props = props; - set (null, true); - valueChanged (); - } - - - /** - * Instantiate the plugins as instances of the given class. - */ - public Object[] instantiate (Class elemType, Configuration conf) - { - return instantiate (elemType, conf, true); - } - - - /** - * Instantiate the plugins as instances of the given class. - */ - public Object[] instantiate (Class elemType, Configuration conf, - boolean fatal) - { - Object[] ret; - if (_names.length == 0) - ret = (Object[]) Array.newInstance (elemType, 0); - else - { - ret = (Object[]) Array.newInstance (elemType, _names.length); - for (int i = 0; i < ret.length; i++) - { - ret[i] = newInstance (_names[i], elemType, conf, fatal); - Configurations.configureInstance (ret[i], conf, _props[i], - getProperty ()); - } - } - set (ret, true); - return ret; - } - - - /** - * Allow subclasses to instantiate additional plugins. This method does - * not perform configuration. - */ - public Object newInstance (String clsName, Class type, - Configuration conf, boolean fatal) - { - return Configurations.newInstance (clsName, this, conf, - type.getClassLoader (), fatal); - } - - - /** - * Override to recognize aliases of the class name without the attached - * properties string. - */ - public String getString () - { - if (_names.length == 0) - return null; - - StringBuffer buf = new StringBuffer (); - for (int i = 0; i < _names.length; i++) - { - if (i > 0) - buf.append (", "); - buf.append (Configurations.getPlugin (alias(_names[i]), _props[i])); - } - if (buf.length () == 0) - return null; - return buf.toString (); - } - - - /** - * Override to recognize aliases of the plugin name without the attached - * properties string. - */ - public void setString (String str) - { - if (str == null || str.length () == 0) - str = getDefault (); - if (str == null || str.length () == 0) - { - _names = EMPTY; - _props = EMPTY; - set (null, true); - valueChanged (); - return; - } - - // split up the string; each element might be a class name, or a - // class name with properties settings - List plugins = new ArrayList (); - StringBuffer plugin = new StringBuffer (); - boolean inParen = false; - char c; - for (int i = 0; i < str.length (); i++) - { - c = str.charAt (i); - switch (c) - { - case '(': - inParen = true; - plugin.append (c); - break; - case ')': - inParen = false; - plugin.append (c); - break; - case ',': - if (inParen) - plugin.append (c); - else - { - plugins.add (plugin.toString ()); - plugin = new StringBuffer (); - } - break; - default: - plugin.append (c); - } - } - if (plugin.length () > 0) - plugins.add (plugin.toString ()); - - // parse each plugin element into its name and properties - List names = new ArrayList (); - List props = new ArrayList (); - String clsName; - for (int i = 0; i < plugins.size (); i++) - { - str = (String) plugins.get (i); - clsName = unalias (Configurations.getClassName (str)); - if (clsName != null) - { - names.add (clsName); - props.add (Configurations.getProperties (str)); - } - } - _names = (String[]) names.toArray (new String[names.size ()]); - _props = (String[]) props.toArray (new String[props.size ()]); - set (null, true); - valueChanged (); - } - - - public Class getValueType () - { - return Object[].class; - } - - - protected void objectChanged () - { - Object[] vals = (Object[]) get (); - if (vals == null || vals.length == 0) - _names = EMPTY; - else - { - _names = new String[vals.length]; - for (int i = 0; i < vals.length; i++) - _names[i] = (vals[i] == null) ? null - : vals[i].getClass ().getName (); - } - _props = EMPTY; - } - - - protected String getInternalString () - { - // should never get called - throw new IllegalStateException (); - } - - - protected void setInternalString (String str) - { - // should never get called - throw new IllegalStateException (); - } +public class PluginListValue extends ObjectValue { + private static final String[] EMPTY = new String[0]; + private static final Localizer _loc = Localizer.forPackage(PluginListValue.class); + private String[] _names = EMPTY; + private String[] _props = EMPTY; + + public PluginListValue(String prop) { + super(prop); + } + + /** + * The plugin class names. + */ + public String[] getClassNames() { + return _names; + } + + /** + * The plugin class names. + */ + public void setClassNames(String[] names) { + if (names == null) { + names = EMPTY; + } + + _names = names; + set(null, true); + valueChanged(); + } + + /** + * The plugin properties. + */ + public String[] getProperties() { + return _props; + } + + /** + * The plugin properties. + */ + public void setProperties(String[] props) { + if (props == null) { + props = EMPTY; + } + + _props = props; + set(null, true); + valueChanged(); + } + + /** + * Instantiate the plugins as instances of the given class. + */ + public Object instantiate(Class elemType, Configuration conf, boolean fatal) { + Object[] ret; + + if (_names.length == 0) { + ret = (Object[]) Array.newInstance(elemType, 0); + } else { + ret = (Object[]) Array.newInstance(elemType, _names.length); + + for (int i = 0; i < ret.length; i++) { + ret[i] = newInstance(_names[i], elemType, conf, fatal); + Configurations.configureInstance(ret[i], conf, _props[i], + getProperty()); + } + } + + set(ret, true); + + return ret; + } + + /** + * Override to recognize aliases of the class name without the attached + * properties string. + */ + public String getString() { + if (_names.length == 0) { + return null; + } + + StringBuffer buf = new StringBuffer(); + + for (int i = 0; i < _names.length; i++) { + if (i > 0) { + buf.append(", "); + } + + buf.append(Configurations.getPlugin(alias(_names[i]), _props[i])); + } + + if (buf.length() == 0) { + return null; + } + + return buf.toString(); + } + + /** + * Override to recognize aliases of the plugin name without the attached + * properties string. + */ + public void setString(String str) { + if ((str == null) || (str.length() == 0)) { + str = getDefault(); + } + + if ((str == null) || (str.length() == 0)) { + _names = EMPTY; + _props = EMPTY; + set(null, true); + valueChanged(); + + return; + } + + // split up the string; each element might be a class name, or a + // class name with properties settings + List plugins = new ArrayList(); + StringBuffer plugin = new StringBuffer(); + boolean inParen = false; + char c; + + for (int i = 0; i < str.length(); i++) { + c = str.charAt(i); + + switch (c) { + case '(': + inParen = true; + plugin.append(c); + + break; + + case ')': + inParen = false; + plugin.append(c); + + break; + + case ',': + + if (inParen) { + plugin.append(c); + } else { + plugins.add(plugin.toString()); + plugin = new StringBuffer(); + } + + break; + + default: + plugin.append(c); + } + } + + if (plugin.length() > 0) { + plugins.add(plugin.toString()); + } + + // parse each plugin element into its name and properties + List names = new ArrayList(); + List props = new ArrayList(); + String clsName; + + for (int i = 0; i < plugins.size(); i++) { + str = (String) plugins.get(i); + clsName = unalias(Configurations.getClassName(str)); + + if (clsName != null) { + names.add(clsName); + props.add(Configurations.getProperties(str)); + } + } + + _names = (String[]) names.toArray(new String[names.size()]); + _props = (String[]) props.toArray(new String[props.size()]); + set(null, true); + valueChanged(); + } + + public Class getValueType() { + return Object[].class; + } + + protected void objectChanged() { + Object[] vals = (Object[]) get(); + + if ((vals == null) || (vals.length == 0)) { + _names = EMPTY; + } else { + _names = new String[vals.length]; + + for (int i = 0; i < vals.length; i++) + _names[i] = (vals[i] == null) ? null + : vals[i].getClass().getName(); + } + + _props = EMPTY; + } + + protected String getInternalString() { + // should never get called + throw new IllegalStateException(); + } + + protected void setInternalString(String str) { + // should never get called + throw new IllegalStateException(); + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/PluginValue.java Wed Jun 28 12:34:33 2006 @@ -15,188 +15,145 @@ */ package org.apache.openjpa.lib.conf; - import org.apache.commons.lang.*; import org.apache.openjpa.lib.util.*; /** - *

A plugin {@link Value} consisting of plugin name and properties. - * Plugins should be specified in the form:
- * <plugin-name>(<prop1>=<val1>, ...)
- * Both the plugin name and prop list are optional, so that the following - * forms are also valid:
- * <plugin-name>
- * <prop1>=<val1> ...

+ *

A plugin {@link Value} consisting of plugin name and properties. + * Plugins should be specified in the form:
+ * <plugin-name>(<prop1>=<val1>, ...)
+ * Both the plugin name and prop list are optional, so that the following + * forms are also valid:
+ * <plugin-name>
+ * <prop1>=<val1> ...

* - *

Defaults and aliases on plugin values apply only to the plugin name.

+ *

Defaults and aliases on plugin values apply only to the plugin name.

* - * @author Abe White + * @author Abe White */ -public class PluginValue - extends ObjectValue -{ - private static final Localizer _loc = Localizer.forPackage - (PluginValue.class); - - private final boolean _singleton; - private String _name = null; - private String _props = null; - - - public PluginValue (String prop, boolean singleton) - { - super (prop); - _singleton = singleton; - } - - - /** - * Whether this value is a singleton. - */ - public boolean isSingleton () - { - return _singleton; - } - - - /** - * The plugin class name. - */ - public String getClassName () - { - return _name; - } - - - /** - * The plugin class name. - */ - public void setClassName (String name) - { - String oldName = _name; - _name = name; - if (!StringUtils.equals (oldName, name)) - { - if (_singleton) - set (null, true); - valueChanged (); - } - } - - - /** - * The plugin properties. - */ - public String getProperties () - { - return _props; - } - - - /** - * The plugin properties. - */ - public void setProperties (String props) - { - String oldProps = _props; - _props = props; - if (!StringUtils.equals (oldProps, props)) - { - if (_singleton) - set (null, true); - valueChanged (); - } - } - - - /** - * Instantiate the plugin as an instance of the given class. Equivalent - * to instantiate (type, conf, true). - */ - public Object instantiate (Class type, Configuration conf) - { - return instantiate (type, conf, true); - } - - - /** - * Instantiate the plugin as an instance of the given class. - */ - public Object instantiate (Class type, Configuration conf, boolean fatal) - { - Object obj = newInstance (_name, type, conf, true); - Configurations.configureInstance (obj, conf, _props, - (fatal) ? getProperty () : null); - if (_singleton) - set (obj, true); - return obj; - } - - - /** - * Allow subclasses to instantiate additional plugins. This method does - * not perform configuration. - */ - public Object newInstance (String clsName, Class type, - Configuration conf, boolean fatal) - { - return Configurations.newInstance (clsName, this, conf, - type.getClassLoader (), fatal); - } - - - public void set (Object obj, boolean derived) - { - if (!_singleton) - throw new IllegalStateException (_loc.get ("not-singleton", - getProperty ())); - super.set (obj, derived); - } - - - public String getString () - { - return Configurations.getPlugin (alias (_name), _props); - } - - - public void setString (String str) - { - _name = Configurations.getClassName (str); - _name = unalias (_name); - _props = Configurations.getProperties (str); - if (_singleton) - set (null, true); - valueChanged (); - } - - - public Class getValueType () - { - return Object.class; - } - - - protected void objectChanged () - { - Object obj = get (); - _name = (obj == null) ? unalias (null) : obj.getClass ().getName (); - _props = null; - } - - - protected String getInternalString () - { - // should never get called - throw new IllegalStateException (); - } - - - protected void setInternalString (String str) - { - // should never get called - throw new IllegalStateException (); - } +public class PluginValue extends ObjectValue { + private static final Localizer _loc = Localizer.forPackage(PluginValue.class); + private final boolean _singleton; + private String _name = null; + private String _props = null; + + public PluginValue(String prop, boolean singleton) { + super(prop); + _singleton = singleton; + } + + /** + * Whether this value is a singleton. + */ + public boolean isSingleton() { + return _singleton; + } + + /** + * The plugin class name. + */ + public String getClassName() { + return _name; + } + + /** + * The plugin class name. + */ + public void setClassName(String name) { + String oldName = _name; + _name = name; + + if (!StringUtils.equals(oldName, name)) { + if (_singleton) { + set(null, true); + } + + valueChanged(); + } + } + + /** + * The plugin properties. + */ + public String getProperties() { + return _props; + } + + /** + * The plugin properties. + */ + public void setProperties(String props) { + String oldProps = _props; + _props = props; + + if (!StringUtils.equals(oldProps, props)) { + if (_singleton) { + set(null, true); + } + + valueChanged(); + } + } + + /** + * Instantiate the plugin as an instance of the given class. + */ + public Object instantiate(Class type, Configuration conf, boolean fatal) { + Object obj = newInstance(_name, type, conf, fatal); + Configurations.configureInstance(obj, conf, _props, + (fatal) ? getProperty() : null); + + if (_singleton) { + set(obj, true); + } + + return obj; + } + + public void set(Object obj, boolean derived) { + if (!_singleton) { + throw new IllegalStateException(_loc.get("not-singleton", + getProperty())); + } + + super.set(obj, derived); + } + + public String getString() { + return Configurations.getPlugin(alias(_name), _props); + } + + public void setString(String str) { + _name = Configurations.getClassName(str); + _name = unalias(_name); + _props = Configurations.getProperties(str); + + if (_singleton) { + set(null, true); + } + + valueChanged(); + } + + public Class getValueType() { + return Object.class; + } + + protected void objectChanged() { + Object obj = get(); + _name = (obj == null) ? unalias(null) : obj.getClass().getName(); + _props = null; + } + + protected String getInternalString() { + // should never get called + throw new IllegalStateException(); + } + + protected void setInternalString(String str) { + // should never get called + throw new IllegalStateException(); + } } Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringListValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringListValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringListValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringListValue.java Wed Jun 28 12:34:33 2006 @@ -15,73 +15,57 @@ */ package org.apache.openjpa.lib.conf; - import serp.util.*; /** - *

A comma-separated list of string values.

+ *

A comma-separated list of string values.

* - * @author Abe White + * @author Abe White */ -public class StringListValue - extends Value -{ - public static final String[] EMPTY = new String[0]; - - private String[] _values = EMPTY; - - - public StringListValue (String prop) - { - super (prop); - } - - - /** - * The internal value. - */ - public void set (String[] values) - { - _values = (values == null) ? EMPTY : values; - valueChanged (); - } - - - /** - * The internal value. - */ - public String[] get () - { - return _values; - } - - - public Class getValueType () - { - return String[].class; - } - - - protected String getInternalString () - { - return Strings.join (_values, ", "); - } - - - protected void setInternalString (String val) - { - String[] vals = Strings.split (val, ",", 0); - if (vals != null) - for (int i = 0; i < vals.length; i++) - vals[i] = vals[i].trim (); - set (vals); - } - - - protected void setInternalObject (Object obj) - { - set ((String[]) obj); - } +public class StringListValue extends Value { + public static final String[] EMPTY = new String[0]; + private String[] _values = EMPTY; + + public StringListValue(String prop) { + super(prop); + } + + /** + * The internal value. + */ + public void set(String[] values) { + _values = (values == null) ? EMPTY : values; + valueChanged(); + } + + /** + * The internal value. + */ + public String[] get() { + return _values; + } + + public Class getValueType() { + return String[].class; + } + + protected String getInternalString() { + return Strings.join(_values, ", "); + } + + protected void setInternalString(String val) { + String[] vals = Strings.split(val, ",", 0); + + if (vals != null) { + for (int i = 0; i < vals.length; i++) + vals[i] = vals[i].trim(); + } + + set(vals); + } + + protected void setInternalObject(Object obj) { + set((String[]) obj); + } } - Modified: incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringValue.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringValue.java?rev=417856&r1=415364&r2=417856&view=diff ============================================================================== --- incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringValue.java (original) +++ incubator/openjpa/trunk/openjpa-lib/main/java/org/apache/openjpa/lib/conf/StringValue.java Wed Jun 28 12:34:33 2006 @@ -19,64 +19,49 @@ /** - * A string {@link Value}. + * A string {@link Value}. * - * @author Marc Prud'hommeaux + * @author Marc Prud'hommeaux */ -public class StringValue - extends Value -{ - private String value; +public class StringValue extends Value { + private String value; - - public StringValue (String prop) - { - super (prop); - } - - - public Class getValueType () - { - return String.class; - } - - - /** - * The internal value. - */ - public String get () - { - return value; - } - - - /** - * The internal value. - */ - public void set (String value) - { - String oldValue = this.value; - this.value = value; - if (!StringUtils.equals (value, oldValue)) - valueChanged (); - } - - - protected String getInternalString () - { - return get (); - } - - - protected void setInternalString (String val) - { - set (val); - } - - - protected void setInternalObject (Object obj) - { - set ((String) obj); - } + public StringValue(String prop) { + super(prop); + } + + public Class getValueType() { + return String.class; + } + + /** + * The internal value. + */ + public String get() { + return value; + } + + /** + * The internal value. + */ + public void set(String value) { + String oldValue = this.value; + this.value = value; + + if (!StringUtils.equals(value, oldValue)) { + valueChanged(); + } + } + + protected String getInternalString() { + return get(); + } + + protected void setInternalString(String val) { + set(val); + } + + protected void setInternalObject(Object obj) { + set((String) obj); + } } -