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.conf;
20
21 import java.util.Map;
22
23 import org.apache.openjpa.lib.conf.Configuration;
24 import org.apache.openjpa.lib.conf.PluginValue;
25 import org.apache.openjpa.conf.CacheMarshallersValue;
26 import org.apache.openjpa.meta.MetaDataRepository;
27
28 /**
29 * A {@link PluginValue} that interacts with the {@link CacheMarshaller}
30 * to cache the metadata repository between executions.
31 *
32 * @since 1.1.0
33 */
34 public class MetaDataRepositoryValue
35 extends PluginValue {
36
37 private static final String KEY = "MetaDataRepository";
38
39 public MetaDataRepositoryValue() {
40 super(KEY, false);
41 String[] aliases = new String[] {
42 "default",
43 MetaDataRepository.class.getName()
44 };
45 setAliases(aliases);
46 setDefault(aliases[0]);
47 setString(aliases[0]);
48 }
49
50 public Object instantiate(Class type, Configuration c, boolean fatal) {
51 MetaDataRepository repos = null;
52 OpenJPAConfiguration conf = (OpenJPAConfiguration) c;
53
54 Object[] os = (Object[]) CacheMarshallersValue.getMarshallerById(
55 conf, MetaDataCacheMaintenance.class.getName())
56 .load();
57 if (os != null) {
58 repos = (MetaDataRepository) os[0];
59 if (os[1] != null)
60 // It's a bit odd that we do this in MetaDataRepositoryValue.
61 // We need to serialize all the various bits of configuration
62 // together; maybe we can move the caching logic somewhere
63 // else?
64 conf.getQueryCompilationCacheInstance().putAll((Map) os[1]);
65 }
66
67 if (repos == null)
68 return super.instantiate(type, c, fatal);
69 else
70 return repos;
71 }
72
73
74 }