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 java.lang.reflect.Array;
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.apache.commons.lang.StringUtils;
026:
027: /**
028: * A list of plugins. Defaults and aliases on plugin lists apply only
029: * to individual class names.
030: *
031: * @author Abe White
032: * @see PluginValue
033: */
034: public class PluginListValue extends ObjectValue {
035:
036: private static final String[] EMPTY = new String[0];
037:
038: private String[] _names = EMPTY;
039: private String[] _props = EMPTY;
040:
041: public PluginListValue(String prop) {
042: super (prop);
043: }
044:
045: /**
046: * The plugin class names.
047: */
048: public String[] getClassNames() {
049: return _names;
050: }
051:
052: /**
053: * The plugin class names.
054: */
055: public void setClassNames(String[] names) {
056: if (names == null)
057: names = EMPTY;
058: _names = names;
059: set(null, true);
060: valueChanged();
061: }
062:
063: /**
064: * The plugin properties.
065: */
066: public String[] getProperties() {
067: return _props;
068: }
069:
070: /**
071: * The plugin properties.
072: */
073: public void setProperties(String[] props) {
074: if (props == null)
075: props = EMPTY;
076: _props = props;
077: set(null, true);
078: valueChanged();
079: }
080:
081: /**
082: * Instantiate the plugins as instances of the given class.
083: */
084: public Object instantiate(Class elemType, Configuration conf,
085: boolean fatal) {
086: Object[] ret;
087: if (_names.length == 0)
088: ret = (Object[]) Array.newInstance(elemType, 0);
089: else {
090: ret = (Object[]) Array.newInstance(elemType, _names.length);
091: for (int i = 0; i < ret.length; i++) {
092: ret[i] = newInstance(_names[i], elemType, conf, fatal);
093: Configurations.configureInstance(ret[i], conf,
094: _props[i], getProperty());
095: }
096: }
097: set(ret, true);
098: return ret;
099: }
100:
101: /**
102: * Override to recognize aliases of the class name without the attached
103: * properties string.
104: */
105: public String getString() {
106: if (_names.length == 0)
107: return null;
108:
109: StringBuffer buf = new StringBuffer();
110: for (int i = 0; i < _names.length; i++) {
111: if (i > 0)
112: buf.append(", ");
113: buf.append(Configurations.getPlugin(alias(_names[i]),
114: (i < _props.length) ? _props[i] : null));
115: }
116: if (buf.length() == 0)
117: return null;
118: return buf.toString();
119: }
120:
121: /**
122: * Override to recognize aliases of the plugin name without the attached
123: * properties string.
124: */
125: public void setString(String str) {
126: if (StringUtils.isEmpty(str))
127: str = getDefault();
128: if (StringUtils.isEmpty(str)) {
129: _names = EMPTY;
130: _props = EMPTY;
131: set(null, true);
132: valueChanged();
133: return;
134: }
135:
136: // split up the string; each element might be a class name, or a
137: // class name with properties settings
138: List plugins = new ArrayList();
139: StringBuffer plugin = new StringBuffer();
140: boolean inParen = false;
141: char c;
142: for (int i = 0; i < str.length(); i++) {
143: c = str.charAt(i);
144: switch (c) {
145: case '(':
146: inParen = true;
147: plugin.append(c);
148: break;
149: case ')':
150: inParen = false;
151: plugin.append(c);
152: break;
153: case ',':
154: if (inParen)
155: plugin.append(c);
156: else {
157: plugins.add(plugin.toString());
158: plugin = new StringBuffer();
159: }
160: break;
161: default:
162: plugin.append(c);
163: }
164: }
165: if (plugin.length() > 0)
166: plugins.add(plugin.toString());
167:
168: // parse each plugin element into its name and properties
169: List names = new ArrayList();
170: List props = new ArrayList();
171: String clsName;
172: for (int i = 0; i < plugins.size(); i++) {
173: str = (String) plugins.get(i);
174: clsName = unalias(Configurations.getClassName(str));
175: if (clsName != null) {
176: names.add(clsName);
177: props.add(Configurations.getProperties(str));
178: }
179: }
180: _names = (String[]) names.toArray(new String[names.size()]);
181: _props = (String[]) props.toArray(new String[props.size()]);
182: set(null, true);
183: valueChanged();
184: }
185:
186: public Class getValueType() {
187: return Object[].class;
188: }
189:
190: protected void objectChanged() {
191: Object[] vals = (Object[]) get();
192: if (vals == null || vals.length == 0)
193: _names = EMPTY;
194: else {
195: _names = new String[vals.length];
196: for (int i = 0; i < vals.length; i++)
197: _names[i] = (vals[i] == null) ? null : vals[i]
198: .getClass().getName();
199: }
200: _props = EMPTY;
201: }
202:
203: protected String getInternalString() {
204: // should never get called
205: throw new IllegalStateException();
206: }
207:
208: protected void setInternalString(String str) {
209: // should never get called
210: throw new IllegalStateException();
211: }
212: }
|