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 org.apache.openjpa.lib.conf.PluginValue;
22: import org.apache.openjpa.lib.conf.Configuration;
23: import org.apache.openjpa.kernel.BrokerImpl;
24: import org.apache.openjpa.kernel.FinalizingBrokerImpl;
25: import org.apache.openjpa.util.InternalException;
26:
27: /**
28: * Custom {@link PluginValue} that can efficiently create {@link BrokerImpl}
29: * instances.
30: *
31: * @since 0.9.7
32: */
33: public class BrokerValue extends PluginValue {
34:
35: public static final String KEY = "BrokerImpl";
36: public static final String NON_FINALIZING_ALIAS = "non-finalizing";
37: public static final String DEFAULT_ALIAS = "default";
38:
39: private BrokerImpl _templateBroker;
40:
41: public BrokerValue() {
42: super (KEY, false);
43: String[] aliases = new String[] { DEFAULT_ALIAS,
44: FinalizingBrokerImpl.class.getName(),
45: NON_FINALIZING_ALIAS, BrokerImpl.class.getName(), };
46: setAliases(aliases);
47: setDefault(aliases[0]);
48: setString(aliases[0]);
49: }
50:
51: public Object newInstance(String clsName, Class type,
52: Configuration conf, boolean fatal) {
53: // This is not synchronized. If there are concurrent invocations
54: // while _templateBroker is null, we'll just end up with extra
55: // template brokers, which will get safely garbage collected.
56: if (_templateBroker == null)
57: _templateBroker = (BrokerImpl) super .newInstance(clsName,
58: type, conf, fatal);
59: try {
60: return _templateBroker.clone();
61: } catch (CloneNotSupportedException e) {
62: throw new InternalException(e);
63: }
64: }
65: }
|