01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.conf;
20:
21: import java.util.Collections;
22: import java.util.Hashtable;
23: import java.util.Map;
24:
25: import org.apache.openjpa.lib.conf.Configuration;
26: import org.apache.openjpa.lib.conf.PluginValue;
27: import org.apache.openjpa.lib.util.concurrent.ConcurrentMap;
28: import org.apache.openjpa.lib.util.concurrent.ConcurrentHashMap;
29: import org.apache.openjpa.lib.util.ParseException;
30: import org.apache.openjpa.util.CacheMap;
31:
32: /**
33: * A cache of compiled queries.
34: *
35: * @author Abe White
36: * @since 0.9.6 (also existed in prior versions of Kodo)
37: * @nojavadoc
38: */
39: public class QueryCompilationCacheValue extends PluginValue {
40:
41: public static final String[] ALIASES = { "true",
42: CacheMap.class.getName(), "all",
43: ConcurrentHashMap.class.getName(), "false", null, };
44:
45: public QueryCompilationCacheValue(String prop) {
46: super (prop, true);
47: setAliases(ALIASES);
48: setDefault(ALIASES[0]);
49: setClassName(ALIASES[1]);
50: }
51:
52: public Object newInstance(String clsName, Class type,
53: Configuration conf, boolean fatal) {
54: // make sure map handles concurrency
55: Map map;
56:
57: try {
58: map = (Map) super .newInstance(clsName, type, conf, fatal);
59: } catch (ParseException pe) {
60: // OPENJPA256: this class differs from most plugins in that
61: // the plugin type is the standard java interface Map.class (rather
62: // than an openjpa-specific interface), which means that the
63: // ClassLoader used to load the implementation will be the system
64: // class loader; this presents a problem if OpenJPA is not in the
65: // system classpath, so work around the problem by catching
66: // the ParseException (which is what we wrap the
67: // ClassNotFoundException in) and try again, this time using
68: // this class' ClassLoader.
69: map = (Map) super .newInstance(clsName,
70: QueryCompilationCacheValue.class, conf, fatal);
71: } catch (IllegalArgumentException iae) {
72: // OPENJPA256: this class differs from most plugins in that
73: // the plugin type is the standard java interface Map.class (rather
74: // than an openjpa-specific interface), which means that the
75: // ClassLoader used to load the implementation will be the system
76: // class loader; this presents a problem if OpenJPA is not in the
77: // system classpath, so work around the problem by catching
78: // the IllegalArgumentException (which is what we wrap the
79: // ClassNotFoundException in) and try again, this time using
80: // this class' ClassLoader.
81: map = (Map) super .newInstance(clsName,
82: QueryCompilationCacheValue.class, conf, fatal);
83: }
84:
85: if (map != null
86: && !(map instanceof Hashtable)
87: && !(map instanceof CacheMap)
88: && !(map instanceof ConcurrentMap)
89: && !(map.getClass().getName()
90: .startsWith("java.util.concurrent")))
91: map = Collections.synchronizedMap(map);
92: return map;
93: }
94: }
|