Utility methods dealing with configuration.
| Method from org.apache.openjpa.lib.conf.Configurations Detail: |
public static String combinePlugins(String orig,
String override) {
if (StringUtils.isEmpty(orig))
return override;
if (StringUtils.isEmpty(override))
return orig;
String origCls = getClassName(orig);
String overrideCls = getClassName(override);
String cls;
if (StringUtils.isEmpty(origCls))
cls = overrideCls;
else if (StringUtils.isEmpty(overrideCls))
cls = origCls;
else if (!origCls.equals(overrideCls))
return override; // completely different plugin
else
cls = origCls;
String origProps = getProperties(orig);
String overrideProps = getProperties(override);
if (StringUtils.isEmpty(origProps))
return getPlugin(cls, overrideProps);
if (StringUtils.isEmpty(overrideProps))
return getPlugin(cls, origProps);
Properties props = parseProperties(origProps);
props.putAll(parseProperties(overrideProps));
return getPlugin(cls, serializeProperties(props));
}
Return a plugin string that combines the properties of the given plugin
strings, where properties of override will override the
same properties of orig. |
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 ...". Does not validate that setter
methods exist for the properties. |
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. Does not validate that setter methods exist for the properties. |
public static void configureInstance(Object obj,
Configuration conf,
String properties,
String configurationName) {
if (obj == null)
return;
Properties props = null;
if (!StringUtils.isEmpty(properties))
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. The properties string should be in the form
"prop1=val1, prop2=val2 ...". Validates that setter methods
exist for the properties. |
public static void configureInstance(Object obj,
Configuration conf,
Properties properties,
String configurationName) {
if (obj == null)
return;
Options opts;
if (properties instanceof Options)
opts = (Options) properties;
else {
opts = new Options();
if (properties != null)
opts.putAll(properties);
}
Configurable configurable = null;
if (conf != null && obj instanceof Configurable)
configurable = (Configurable) obj;
if (configurable != null) {
configurable.setConfiguration(conf);
configurable.startConfiguration();
}
Options invalidEntries = opts.setInto(obj);
if (obj instanceof GenericConfigurable)
((GenericConfigurable) obj).setInto(invalidEntries);
if (!invalidEntries.isEmpty() && configurationName != null) {
Localizer.Message 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 = 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(),
findOptionsFor(obj.getClass()).toString(), });
}
throw new ParseException(msg);
}
if (configurable != null)
configurable.endConfiguration();
}
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. |
public static boolean containsProperty(String partialKey,
Map props) {
if (partialKey == null || props == null || props.isEmpty())
return false;
else
return props.containsKey(
ProductDerivations.getConfigurationKey(partialKey, props));
}
Test whether the map contains the given partial key, prefixed with any
possible configuration prefix. |
public static String getClassName(String plugin) {
return getPluginComponent(plugin, true);
}
Return the class name from the given plugin string, or null if none. |
public static List getFullyQualifiedAnchorsInPropertiesLocation(Options opts) {
String props = opts.getProperty("properties", "p", null);
if (props != null) {
int anchorPosition = props.indexOf("#");
if (anchorPosition > -1)
return Arrays.asList(new String[] { props });
}
return ProductDerivations.getFullyQualifiedAnchorsInPropertiesLocation(
props);
}
Return a List of all the fully-qualified anchors specified in the
properties location listed in opts. If no properties
location is listed in opts, this returns whatever the
product derivations can find in their default configurations.
If the properties location specified in opts already
contains an anchor spec, this returns that anchor. Note that in this
fully-qualified-input case, the logic involving product derivations
and resource parsing is short-circuited, so this method
should not be used as a means to test that a particular anchor is
defined in a given location by invoking with a fully-qualified anchor.
This does not mutate opts. |
public static String getPlugin(String clsName,
String props) {
if (StringUtils.isEmpty(clsName))
return props;
if (StringUtils.isEmpty(props))
return clsName;
return clsName + "(" + props + ")";
}
Combine the given class name and properties into a plugin string. |
public static String getProperties(String plugin) {
return getPluginComponent(plugin, false);
}
Return the properties part of the given plugin string, or null if none. |
public static Object getProperty(String partialKey,
Map m) {
if (partialKey == null || m == null || m.isEmpty())
return null;
else
return m.get(ProductDerivations.getConfigurationKey(partialKey, m));
}
Get the property under the given partial key, prefixed with any possible
configuration prefix. |
public static Object lookup(String name) {
if (StringUtils.isEmpty(name))
return null;
Context ctx = null;
try {
ctx = new InitialContext();
return ctx.lookup(name);
} catch (NamingException ne) {
throw new NestableRuntimeException(
_loc.get("naming-err", name).getMessage(), ne);
} finally {
if (ctx != null)
try { ctx.close(); } catch (Exception e) {}
}
}
Looks up the given name in JNDI. If the name is null, null is returned. |
public static Object newInstance(String clsName,
ClassLoader loader) {
return newInstance(clsName, null, null, loader, true);
}
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,
Configuration conf,
String props,
ClassLoader loader) {
Object obj = newInstance(clsName, null, conf, loader, true);
configureInstance(obj, conf, props);
return obj;
}
Create and configure an instance with the given class name and
properties. |
static Object newInstance(String clsName,
Value val,
Configuration conf,
ClassLoader loader,
boolean fatal) {
if (StringUtils.isEmpty(clsName))
return null;
Class cls = null;
while (cls == null) {
// can't have a null reference in the map, so use symbolic
// constant as key
Object key = loader == null ? NULL_LOADER : loader;
Map loaderCache = (Map) _loaders.get(key);
if (loaderCache == null) { // We don't have a cache for this loader.
loaderCache = new ConcurrentHashMap();
_loaders.put(key, loaderCache);
} else { // We have a cache for this loader.
cls = (Class) loaderCache.get(clsName);
}
if (cls == null) {
try {
cls = Strings.toClass(clsName, findDerivedLoader(conf,
loader));
loaderCache.put(clsName, cls);
} catch (RuntimeException re) {
if (loader != null) // Try one more time with loader=null
loader = null;
else {
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(_loc
.get("plugin-creation-exception", val), re);
return null;
}
}
}
}
try {
return AccessController.doPrivileged(
J2DoPrivHelper.newInstanceAction(cls));
} catch (Exception e) {
if (e instanceof PrivilegedActionException) {
e = ((PrivilegedActionException) e).getException();
}
RuntimeException re = new NestableRuntimeException(_loc.get
("obj-create", cls).getMessage(), e);
if (fatal)
throw re;
Log log = (conf == null) ? null : conf.getConfigurationLog();
if (log != null && log.isErrorEnabled())
log.error(_loc.get("plugin-creation-exception", val), re);
return null;
}
}
Helper method used by members of this package to instantiate plugin
values. |
public static Options parseProperties(String properties) {
Options opts = new Options();
properties = StringUtils.trimToNull(properties);
if (properties == null)
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);
}
}
Parse a set of properties from a comma-separated string. |
public static void populateConfiguration(Configuration conf,
Options opts) {
String props = opts.removeProperty("properties", "p", null);
ConfigurationProvider provider;
if (!StringUtils.isEmpty(props)) {
String path = props;
String anchor = null;
int idx = path.lastIndexOf('#");
if (idx != -1) {
if (idx < path.length() - 1)
anchor = path.substring(idx + 1);
path = path.substring(0, idx);
if (path.length() == 0)
throw new MissingResourceException(_loc.get("anchor-only",
props).getMessage(), Configurations.class.getName(),
props);
}
File file = new File(path);
if (((Boolean) AccessController.doPrivileged(J2DoPrivHelper
.isFileAction(file))).booleanValue())
provider = ProductDerivations.load(file, anchor, null);
else {
file = new File("META-INF" + File.separatorChar + path);
if (((Boolean) AccessController.doPrivileged(J2DoPrivHelper
.isFileAction(file))).booleanValue())
provider = ProductDerivations.load(file, anchor, null);
else
provider = ProductDerivations.load(path, anchor, null);
}
if (provider != null)
provider.setInto(conf);
else
throw new MissingResourceException(_loc.get("no-provider",
props).getMessage(), Configurations.class.getName(),
props);
} else {
provider = ProductDerivations.loadDefaults(null);
if (provider != null)
provider.setInto(conf);
}
opts.setInto(conf);
}
Set the given 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 Object removeProperty(String partialKey,
Map props) {
if (partialKey == null || props == null || props.isEmpty())
return null;
if (containsProperty(partialKey, props))
return props.remove(ProductDerivations.getConfigurationKey(
partialKey, props));
else
return null;
}
Remove the property under the given partial key, prefixed with any
possible configuration prefix. |
public static boolean runAgainstAllAnchors(Options opts,
Configurations.Runnable runnable) {
if (opts.containsKey("help") || opts.containsKey("-help")) {
return false;
}
List anchors =
Configurations.getFullyQualifiedAnchorsInPropertiesLocation(opts);
// We use 'properties' below; get rid of 'p' to avoid conflicts. This
// relies on knowing what getFullyQualifiedAnchorsInPropertiesLocation
// looks for.
if (opts.containsKey("p"))
opts.remove("p");
boolean ret = true;
if (anchors.size() == 0) {
ret = launchRunnable(opts, runnable);
} else {
for (Iterator iter = anchors.iterator(); iter.hasNext(); ) {
Options clonedOptions = (Options) opts.clone();
clonedOptions.setProperty("properties", iter.next().toString());
ret &= launchRunnable(clonedOptions, runnable);
}
}
return ret;
}
Runs runnable against all the anchors in the configuration
pointed to by opts. Each invocation gets a fresh clone of
opts with the properties option set
appropriately. |
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();
}
Turn a set of properties into a comma-separated string. |