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 */
019package org.apache.commons.jcs.jcache.openjpa;
020
021import org.apache.openjpa.conf.OpenJPAConfiguration;
022import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
023import org.apache.openjpa.event.SingleJVMRemoteCommitProvider;
024import org.apache.openjpa.lib.conf.AbstractProductDerivation;
025import org.apache.openjpa.lib.conf.Configuration;
026import org.apache.openjpa.lib.conf.ConfigurationProvider;
027import org.apache.openjpa.lib.conf.Configurations;
028
029import java.util.Map;
030
031public class OpenJPAJCacheDerivation extends AbstractProductDerivation
032{
033    private static final String JCACHE_NAME = "jcache";
034
035    @Override
036    public boolean beforeConfigurationLoad(final Configuration conf)
037    {
038        if (OpenJPAConfiguration.class.isInstance(conf)) {
039            final OpenJPAConfigurationImpl oconf = OpenJPAConfigurationImpl.class.cast(conf);
040            oconf.dataCacheManagerPlugin.setAlias(JCACHE_NAME, OpenJPAJCacheDataCacheManager.class.getName());
041            oconf.dataCachePlugin.setAlias(JCACHE_NAME, OpenJPAJCacheDataCache.class.getName());
042            oconf.queryCachePlugin.setAlias(JCACHE_NAME, OpenJPAJCacheQueryCache.class.getName());
043        }
044        return super.beforeConfigurationLoad(conf);
045    }
046
047    @Override
048    public boolean beforeConfigurationConstruct(final ConfigurationProvider cp)
049    {
050        final Map<?, ?> props = cp.getProperties();
051        final Object dcm = Configurations.getProperty("DataCacheManager", props);
052        if (dcm != null && JCACHE_NAME.equals(dcm))
053        {
054            if (Configurations.getProperty("DataCache", props) == null)
055            {
056                cp.addProperty("openjpa.DataCache", JCACHE_NAME);
057            }
058            if (Configurations.getProperty("QueryCache", props) == null)
059            {
060                cp.addProperty("openjpa.QueryCache", JCACHE_NAME);
061            }
062            if (Configurations.getProperty("RemoteCommitProvider", props) == null)
063            {
064                cp.addProperty("openjpa.RemoteCommitProvider", SingleJVMRemoteCommitProvider.class.getName());
065            }
066        }
067        return super.beforeConfigurationConstruct(cp);
068    }
069
070    @Override
071    public int getType()
072    {
073        return TYPE_FEATURE;
074    }
075}