View Javadoc
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.commons.jcs.jcache.openjpa;
20  
21  import org.apache.openjpa.conf.OpenJPAConfiguration;
22  import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
23  import org.apache.openjpa.event.SingleJVMRemoteCommitProvider;
24  import org.apache.openjpa.lib.conf.AbstractProductDerivation;
25  import org.apache.openjpa.lib.conf.Configuration;
26  import org.apache.openjpa.lib.conf.ConfigurationProvider;
27  import org.apache.openjpa.lib.conf.Configurations;
28  
29  import java.util.Map;
30  
31  public class OpenJPAJCacheDerivation extends AbstractProductDerivation
32  {
33      private static final String JCACHE_NAME = "jcache";
34  
35      @Override
36      public boolean beforeConfigurationLoad(final Configuration conf)
37      {
38          if (OpenJPAConfiguration.class.isInstance(conf)) {
39              final OpenJPAConfigurationImpl oconf = OpenJPAConfigurationImpl.class.cast(conf);
40              oconf.dataCacheManagerPlugin.setAlias(JCACHE_NAME, OpenJPAJCacheDataCacheManager.class.getName());
41              oconf.dataCachePlugin.setAlias(JCACHE_NAME, OpenJPAJCacheDataCache.class.getName());
42              oconf.queryCachePlugin.setAlias(JCACHE_NAME, OpenJPAJCacheQueryCache.class.getName());
43          }
44          return super.beforeConfigurationLoad(conf);
45      }
46  
47      @Override
48      public boolean beforeConfigurationConstruct(final ConfigurationProvider cp)
49      {
50          final Map<?, ?> props = cp.getProperties();
51          final Object dcm = Configurations.getProperty("DataCacheManager", props);
52          if (dcm != null && JCACHE_NAME.equals(dcm))
53          {
54              if (Configurations.getProperty("DataCache", props) == null)
55              {
56                  cp.addProperty("openjpa.DataCache", JCACHE_NAME);
57              }
58              if (Configurations.getProperty("QueryCache", props) == null)
59              {
60                  cp.addProperty("openjpa.QueryCache", JCACHE_NAME);
61              }
62              if (Configurations.getProperty("RemoteCommitProvider", props) == null)
63              {
64                  cp.addProperty("openjpa.RemoteCommitProvider", SingleJVMRemoteCommitProvider.class.getName());
65              }
66          }
67          return super.beforeConfigurationConstruct(cp);
68      }
69  
70      @Override
71      public int getType()
72      {
73          return TYPE_FEATURE;
74      }
75  }