1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.openjpa.lib.conf;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.openjpa.lib.util.Localizer;
23
24 /**
25 * A plugin {@link Value} consisting of plugin name and properties.
26 * Plugins should be specified in the form:<br />
27 * <code><plugin-name>(<prop1>=<val1>, ...)</code><br />
28 * Both the plugin name and prop list are optional, so that the following
29 * forms are also valid:<br />
30 * <code><plugin-name></code><br />
31 * <code><prop1>=<val1> ...</code>
32 * Defaults and aliases on plugin values apply only to the plugin name.
33 *
34 * @author Abe White
35 */
36 public class PluginValue extends ObjectValue {
37
38 private static final Localizer _loc = Localizer.forPackage
39 (PluginValue.class);
40
41 private final boolean _singleton;
42 private String _name = null;
43 private String _props = null;
44
45 public PluginValue(String prop, boolean singleton) {
46 super(prop);
47 _singleton = singleton;
48 }
49
50 /**
51 * Whether this value is a singleton.
52 */
53 public boolean isSingleton() {
54 return _singleton;
55 }
56
57 /**
58 * The plugin class name.
59 */
60 public String getClassName() {
61 return _name;
62 }
63
64 /**
65 * The plugin class name.
66 */
67 public void setClassName(String name) {
68 assertChangeable();
69 String oldName = _name;
70 _name = name;
71 if (!StringUtils.equals(oldName, name)) {
72 if (_singleton)
73 set(null, true);
74 valueChanged();
75 }
76 }
77
78 /**
79 * The plugin properties.
80 */
81 public String getProperties() {
82 return _props;
83 }
84
85 /**
86 * The plugin properties.
87 */
88 public void setProperties(String props) {
89 String oldProps = _props;
90 _props = props;
91 if (!StringUtils.equals(oldProps, props)) {
92 if (_singleton)
93 set(null, true);
94 valueChanged();
95 }
96 }
97
98 /**
99 * Instantiate the plugin as an instance of the given class.
100 */
101 public Object instantiate(Class type, Configuration conf, boolean fatal) {
102 Object obj = newInstance(_name, type, conf, fatal);
103 Configurations.configureInstance(obj, conf, _props,
104 (fatal) ? getProperty() : null);
105 if (_singleton)
106 set(obj, true);
107 return obj;
108 }
109
110 public void set(Object obj, boolean derived) {
111 if (!_singleton)
112 throw new IllegalStateException(_loc.get("not-singleton",
113 getProperty()).getMessage());
114 super.set(obj, derived);
115 }
116
117 public String getString() {
118 return Configurations.getPlugin(alias(_name), _props);
119 }
120
121 public void setString(String str) {
122 assertChangeable();
123 _name = Configurations.getClassName(str);
124 _name = unalias(_name);
125 _props = Configurations.getProperties(str);
126 if (_singleton)
127 set(null, true);
128 valueChanged();
129 }
130
131 public Class getValueType() {
132 return Object.class;
133 }
134
135 protected void objectChanged() {
136 Object obj = get();
137 _name = (obj == null) ? unalias(null) : obj.getClass().getName();
138 _props = null;
139 }
140
141 protected String getInternalString() {
142 // should never get called
143 throw new IllegalStateException();
144 }
145
146 protected void setInternalString(String str) {
147 // should never get called
148 throw new IllegalStateException();
149 }
150 }