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.conf;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.openjpa.lib.log.Log;
025: import org.apache.openjpa.lib.util.Localizer;
026: import org.apache.openjpa.lib.conf.PluginListValue;
027: import org.apache.openjpa.conf.NoOpCacheMarshaller;
028: import org.apache.openjpa.lib.conf.Configuration;
029:
030: /**
031: * A configuration value for handling and accessing cache marshallers.
032: *
033: * @since 1.1.0
034: */
035: public class CacheMarshallersValue extends PluginListValue {
036:
037: private static final String KEY = "CacheMarshallers";
038: private static final CacheMarshaller NO_OP_CACHE_MARSHALLER = new NoOpCacheMarshaller();
039: private static final Localizer _loc = Localizer
040: .forPackage(CacheMarshallersValue.class);
041:
042: private Configuration _conf;
043: private Map _marshallers;
044: private boolean _initialized;
045:
046: public CacheMarshallersValue(Configuration conf) {
047: super (KEY);
048: _conf = conf;
049: setAlias("default", CacheMarshallerImpl.class.getName());
050: setAlias("none", null);
051: setDefault("none");
052: setString("none");
053: setScope(getClass());
054: }
055:
056: public Object instantiate(Class elemType, Configuration conf,
057: boolean fatal) {
058: CacheMarshaller[] ms = (CacheMarshaller[]) super .instantiate(
059: elemType, conf, fatal);
060: if (ms != null) {
061: _marshallers = new HashMap();
062: for (int i = 0; i < ms.length; i++) {
063: String mid = ms[i].getId();
064: if (mid != null)
065: _marshallers.put(mid, ms[i]);
066: }
067: } else {
068: _marshallers = null;
069: }
070: return ms;
071: }
072:
073: /**
074: * Return the {@link CacheMarshaller} to use for caching metadata of id
075: * <code>id</code>. If no marshaller exists for the id, returns
076: * {@link NoOpCacheMarshaller}.
077: */
078: public CacheMarshaller getMarshallerById(String id) {
079: initialize();
080:
081: CacheMarshaller cm = (CacheMarshaller) _marshallers.get(id);
082: if (cm == null) {
083: if (getLog().isTraceEnabled())
084: getLog().trace(
085: _loc.get("cache-marshaller-not-found", id));
086: return NO_OP_CACHE_MARSHALLER;
087: } else {
088: if (getLog().isTraceEnabled())
089: getLog().trace(
090: _loc.get("cache-marshaller-found", id, cm
091: .getClass().getName()));
092: return cm;
093: }
094: }
095:
096: private Log getLog() {
097: return _conf.getConfigurationLog();
098: }
099:
100: /**
101: * Return the {@link CacheMarshaller} to use for caching metadata of id
102: * <code>id</code>. If no marshaller exists for the id, returns
103: * {@link NoOpCacheMarshaller}.
104: */
105: public static CacheMarshaller getMarshallerById(Configuration c,
106: String id) {
107: CacheMarshallersValue v = ((OpenJPAConfigurationImpl) c).cacheMarshallerPlugins;
108: return v.getMarshallerById(id);
109: }
110:
111: public Map getInstancesAsMap() {
112: return _marshallers;
113: }
114:
115: protected synchronized void initialize() {
116: if (!_initialized) {
117: instantiate(CacheMarshaller.class, _conf);
118: _initialized = true;
119: }
120: }
121: }
|