001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.conf;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.openjpa.lib.util.Localizer;
023:
024: /**
025: * A plugin {@link Value} consisting of plugin name and properties.
026: * Plugins should be specified in the form:<br />
027: * <code><plugin-name>(<prop1>=<val1>, ...)</code><br />
028: * Both the plugin name and prop list are optional, so that the following
029: * forms are also valid:<br />
030: * <code><plugin-name></code><br />
031: * <code><prop1>=<val1> ...</code>
032: * Defaults and aliases on plugin values apply only to the plugin name.
033: *
034: * @author Abe White
035: */
036: public class PluginValue extends ObjectValue {
037:
038: private static final Localizer _loc = Localizer
039: .forPackage(PluginValue.class);
040:
041: private final boolean _singleton;
042: private String _name = null;
043: private String _props = null;
044:
045: public PluginValue(String prop, boolean singleton) {
046: super (prop);
047: _singleton = singleton;
048: }
049:
050: /**
051: * Whether this value is a singleton.
052: */
053: public boolean isSingleton() {
054: return _singleton;
055: }
056:
057: /**
058: * The plugin class name.
059: */
060: public String getClassName() {
061: return _name;
062: }
063:
064: /**
065: * The plugin class name.
066: */
067: public void setClassName(String name) {
068: assertChangeable();
069: String oldName = _name;
070: _name = name;
071: if (!StringUtils.equals(oldName, name)) {
072: if (_singleton)
073: set(null, true);
074: valueChanged();
075: }
076: }
077:
078: /**
079: * The plugin properties.
080: */
081: public String getProperties() {
082: return _props;
083: }
084:
085: /**
086: * The plugin properties.
087: */
088: public void setProperties(String props) {
089: String oldProps = _props;
090: _props = props;
091: if (!StringUtils.equals(oldProps, props)) {
092: if (_singleton)
093: set(null, true);
094: valueChanged();
095: }
096: }
097:
098: /**
099: * Instantiate the plugin as an instance of the given class.
100: */
101: public Object instantiate(Class type, Configuration conf,
102: boolean fatal) {
103: Object obj = newInstance(_name, type, conf, fatal);
104: Configurations.configureInstance(obj, conf, _props,
105: (fatal) ? getProperty() : null);
106: if (_singleton)
107: set(obj, true);
108: return obj;
109: }
110:
111: public void set(Object obj, boolean derived) {
112: if (!_singleton)
113: throw new IllegalStateException(_loc.get("not-singleton",
114: getProperty()).getMessage());
115: super .set(obj, derived);
116: }
117:
118: public String getString() {
119: return Configurations.getPlugin(alias(_name), _props);
120: }
121:
122: public void setString(String str) {
123: assertChangeable();
124: _name = Configurations.getClassName(str);
125: _name = unalias(_name);
126: _props = Configurations.getProperties(str);
127: if (_singleton)
128: set(null, true);
129: valueChanged();
130: }
131:
132: public Class getValueType() {
133: return Object.class;
134: }
135:
136: protected void objectChanged() {
137: Object obj = get();
138: _name = (obj == null) ? unalias(null) : obj.getClass()
139: .getName();
140: _props = null;
141: }
142:
143: protected String getInternalString() {
144: // should never get called
145: throw new IllegalStateException();
146: }
147:
148: protected void setInternalString(String str) {
149: // should never get called
150: throw new IllegalStateException();
151: }
152: }
|