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 java.security.AccessController;
22
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.openjpa.lib.util.J2DoPrivHelper;
25 import org.apache.openjpa.lib.util.Localizer;
26 import org.apache.openjpa.lib.util.ReferenceMap;
27 import org.apache.openjpa.lib.util.concurrent.ConcurrentReferenceHashMap;
28
29 /**
30 * An object {@link Value}.
31 *
32 * @author Abe White
33 */
34 public class ObjectValue extends Value {
35
36 private static final Localizer _loc = Localizer.forPackage
37 (ObjectValue.class);
38
39 // cache the types' classloader
40 private static ConcurrentReferenceHashMap _classloaderCache =
41 new ConcurrentReferenceHashMap(ReferenceMap.HARD, ReferenceMap.WEAK);
42
43 private Object _value = null;
44
45 public ObjectValue(String prop) {
46 super(prop);
47 }
48
49 /**
50 * The internal value.
51 */
52 public Object get() {
53 return _value;
54 }
55
56 /**
57 * The internal value.
58 */
59 public void set(Object obj) {
60 set(obj, false);
61 }
62
63 /**
64 * The internal value.
65 *
66 * @param derived if true, this value was derived from other properties
67 */
68 public void set(Object obj, boolean derived) {
69 if (!derived) assertChangeable();
70 Object oldValue = _value;
71 _value = obj;
72 if (!derived && !ObjectUtils.equals(obj, oldValue)) {
73 objectChanged();
74 valueChanged();
75 }
76 }
77
78 /**
79 * Instantiate the object as an instance of the given class. Equivalent
80 * to <code>instantiate(type, conf, true)</code>.
81 */
82 public Object instantiate(Class type, Configuration conf) {
83 return instantiate(type, conf, true);
84 }
85
86 /**
87 * Instantiate the object as an instance of the given class.
88 */
89 public Object instantiate(Class type, Configuration conf, boolean fatal) {
90 throw new UnsupportedOperationException();
91 }
92
93 /**
94 * Allow subclasses to instantiate additional plugins. This method does
95 * not perform configuration.
96 */
97 public Object newInstance(String clsName, Class type, Configuration conf,
98 boolean fatal) {
99 ClassLoader cl = (ClassLoader) _classloaderCache.get(type);
100 if (cl == null) {
101 cl = (ClassLoader) AccessController.doPrivileged(
102 J2DoPrivHelper.getClassLoaderAction(type));
103 if (cl == null) { // System classloader is returned as null
104 cl = (ClassLoader) AccessController.doPrivileged(
105 J2DoPrivHelper.getSystemClassLoaderAction());
106 }
107 _classloaderCache.put(type, cl);
108 }
109 return Configurations.newInstance(clsName, this, conf, cl, fatal);
110 }
111
112 public Class getValueType() {
113 return Object.class;
114 }
115
116 /**
117 * Implement this method to synchronize internal data with the new
118 * object value.
119 */
120 protected void objectChanged() {
121 }
122
123 protected String getInternalString() {
124 return null;
125 }
126
127 protected void setInternalString(String str) {
128 if (str == null)
129 set(null);
130 else
131 throw new IllegalArgumentException(_loc.get("cant-set-string",
132 getProperty()).getMessage());
133 }
134
135 protected void setInternalObject(Object obj) {
136 set(obj);
137 }
138 }