commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1593768 - in /commons/proper/jcs/trunk: ./ commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ commons-jcs-jcache-openjpa/ commons-jcs-jcache-openjpa/src/ commons-jcs-jcache-openjpa/src/main/ commons-jcs-jcach...
Date Sun, 11 May 2014 10:39:23 GMT
Author: rmannibucau
Date: Sun May 11 10:39:22 2014
New Revision: 1593768

URL: http://svn.apache.org/r1593768
Log:
basic openjpa module for jcache, a lot to test and review but basic wiring is here

Added:
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/pom.xml
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCache.java
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheManager.java
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDerivation.java
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheQueryCache.java
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/org.apache.openjpa.lib.conf.ProductDerivation
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/
    commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheTest.java
Modified:
    commons/proper/jcs/trunk/commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ExtraJCacheExtension.java
    commons/proper/jcs/trunk/pom.xml

Modified: commons/proper/jcs/trunk/commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ExtraJCacheExtension.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ExtraJCacheExtension.java?rev=1593768&r1=1593767&r2=1593768&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ExtraJCacheExtension.java
(original)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-extras/src/main/java/org/apache/commons/jcs/jcache/extras/cdi/ExtraJCacheExtension.java
Sun May 11 10:39:22 2014
@@ -31,11 +31,18 @@ import java.util.Properties;
 // add default CacheProvider and CacheManager
 public class ExtraJCacheExtension implements Extension
 {
+    private static final boolean ACTIVATED = "true".equals(System.getProperty("org.apache.jcs.extra.cdi",
"true"));
+
     private boolean cacheManagerFound = false;
     private boolean cacheProviderFound = false;
 
     public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
     {
+        if (!ACTIVATED)
+        {
+            return;
+        }
+
         if (cacheManagerFound && cacheProviderFound)
         {
             return;
@@ -59,6 +66,11 @@ public class ExtraJCacheExtension implem
 
     public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
     {
+        if (!ACTIVATED)
+        {
+            return;
+        }
+
         if (cacheManagerFound && cacheProviderFound) {
             return;
         }

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/pom.xml?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/pom.xml (added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/pom.xml Sun May 11 10:39:22 2014
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>commons-jcs</artifactId>
+    <groupId>org.apache.commons</groupId>
+    <version>2.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>commons-jcs-jcache-openjpa</artifactId>
+  <name>Apache Commons JCS :: JCache OpenJPA</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-jpa_2.0_spec</artifactId>
+      <version>1.1</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.cache</groupId>
+      <artifactId>cache-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.openjpa</groupId>
+      <artifactId>openjpa</artifactId>
+      <version>2.3.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.11</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.derby</groupId>
+      <artifactId>derby</artifactId>
+      <version>10.10.1.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-jcs-jcache</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCache.java?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCache.java
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCache.java
Sun May 11 10:39:22 2014
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.jcs.jcache.openjpa;
+
+import org.apache.openjpa.datacache.AbstractDataCache;
+import org.apache.openjpa.datacache.DataCacheManager;
+import org.apache.openjpa.datacache.DataCachePCData;
+import org.apache.openjpa.util.OpenJPAId;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class OpenJPAJCacheDataCache extends AbstractDataCache
+{
+    private static final String OPENJPA_PREFIX = "openjpa.datacache.";
+
+    private final Lock lock = new ReentrantLock();
+    private OpenJPAJCacheDataCacheManager manager;
+
+    @Override
+    public void initialize(final DataCacheManager manager)
+    {
+        super.initialize(manager);
+        this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager);
+    }
+
+    @Override
+    protected DataCachePCData getInternal(final Object oid)
+    {
+        Object result = null;
+        if (OpenJPAId.class.isInstance(oid))
+        {
+            final Class<?> cls = OpenJPAId.class.cast(oid).getType();
+            Cache<Object, Object> cache = manager.getOrCreateCache(OPENJPA_PREFIX,
cls.getName());
+            if (cache == null)
+            {
+                return null;
+            }
+            else
+            {
+                result = cache.get(oid);
+            }
+        }
+        else
+        {
+            final CacheManager cacheManager = manager.getCacheManager();
+            for (final String cacheName : cacheManager.getCacheNames())
+            {
+                if (!cacheName.startsWith(OPENJPA_PREFIX))
+                {
+                    continue;
+                }
+
+                result = cacheManager.getCache(cacheName).get(oid);
+                if (result != null)
+                {
+                    break;
+                }
+            }
+        }
+        if (result == null)
+        {
+            return null;
+        }
+        return DataCachePCData.class.cast(result);
+    }
+
+    @Override
+    protected DataCachePCData putInternal(final Object oid, final DataCachePCData pc)
+    {
+        manager.getOrCreateCache(OPENJPA_PREFIX, pc.getType().getName()).put(oid, pc);
+        return pc;
+    }
+
+    @Override
+    protected DataCachePCData removeInternal(final Object oid)
+    {
+        if (OpenJPAId.class.isInstance(oid))
+        {
+            final Object remove = manager.getOrCreateCache(OPENJPA_PREFIX, OpenJPAId.class.cast(oid).getType().getName()).getAndRemove(oid);
+            if (remove == null)
+            {
+                return null;
+            }
+            return DataCachePCData.class.cast(remove);
+        }
+        return null;
+    }
+
+    @Override
+    protected void removeAllInternal(final Class<?> cls, final boolean subclasses)
+    {
+        final String name;
+        if (subclasses)
+        {
+            name = cls.getSuperclass().getName();
+        }
+        else
+        {
+            name = cls.getName();
+        }
+        manager.getOrCreateCache(OPENJPA_PREFIX, name).removeAll();
+    }
+
+    @Override
+    protected void clearInternal()
+    {
+        final CacheManager cacheManager = manager.getCacheManager();
+        for (final String cacheName : cacheManager.getCacheNames())
+        {
+            if (!cacheName.startsWith(OPENJPA_PREFIX))
+            {
+                continue;
+            }
+            cacheManager.getCache(cacheName).clear();
+        }
+    }
+
+    @Override
+    protected boolean pinInternal(final Object oid)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected boolean unpinInternal(final Object oid)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void writeLock()
+    {
+        lock.lock();
+    }
+
+    @Override
+    public void writeUnlock()
+    {
+        lock.unlock();
+    }
+}

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheManager.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheManager.java?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheManager.java
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheManager.java
Sun May 11 10:39:22 2014
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.jcs.jcache.openjpa;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.datacache.DataCacheManagerImpl;
+import org.apache.openjpa.lib.conf.ObjectValue;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import javax.cache.Caching;
+import javax.cache.configuration.FactoryBuilder;
+import javax.cache.configuration.MutableConfiguration;
+import javax.cache.expiry.CreatedExpiryPolicy;
+import javax.cache.expiry.Duration;
+import javax.cache.expiry.ExpiryPolicy;
+import javax.cache.spi.CachingProvider;
+import java.util.Properties;
+
+public class OpenJPAJCacheDataCacheManager extends DataCacheManagerImpl
+{
+    private CachingProvider provider;
+    private CacheManager cacheManager;
+
+    @Override
+    public void initialize(final OpenJPAConfiguration conf, final ObjectValue dataCache,
final ObjectValue queryCache)
+    {
+        super.initialize(conf, dataCache, queryCache);
+        provider = Caching.getCachingProvider();
+        cacheManager = provider.getCacheManager(
+                provider.getDefaultURI(), provider.getDefaultClassLoader(), new Properties());
// todo get props
+    }
+
+    @Override
+    public void close()
+    {
+        super.close();
+        cacheManager.close();
+        provider.close();
+    }
+
+    Cache<Object, Object> getOrCreateCache(final String prefix, final String entity)
+    {
+        final String name = entity;
+        final String internalName = prefix + name;
+        Cache<Object, Object> cache = cacheManager.getCache(internalName);
+        if (cache == null)
+        {
+            cache = cacheManager.createCache(internalName,
+                    new MutableConfiguration<Object, Object>().setStoreByValue(false)
+                            .setExpiryPolicyFactory(new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new
CreatedExpiryPolicy(Duration.FIVE_MINUTES))));
+        }
+        return cache;
+    }
+
+    CacheManager getCacheManager()
+    {
+        return cacheManager;
+    }
+}

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDerivation.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDerivation.java?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDerivation.java
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDerivation.java
Sun May 11 10:39:22 2014
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.jcs.jcache.openjpa;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
+import org.apache.openjpa.event.SingleJVMRemoteCommitProvider;
+import org.apache.openjpa.lib.conf.AbstractProductDerivation;
+import org.apache.openjpa.lib.conf.Configuration;
+
+public class OpenJPAJCacheDerivation extends AbstractProductDerivation
+{
+    @Override
+    public boolean beforeConfigurationLoad(Configuration conf)
+    {
+        if (OpenJPAConfiguration.class.isInstance(conf)) {
+            final OpenJPAConfigurationImpl oconf = OpenJPAConfigurationImpl.class.cast(conf);
+            oconf.dataCacheManagerPlugin.setAlias("jcache", OpenJPAJCacheDataCacheManager.class.getName());
+            oconf.dataCachePlugin.setAlias("jcache", OpenJPAJCacheDataCache.class.getName());
+            oconf.queryCachePlugin.setAlias("jcache", OpenJPAJCacheQueryCache.class.getName());
+            oconf.remoteProviderPlugin.setAlias("none", SingleJVMRemoteCommitProvider .class.getName());
+        }
+        return false;
+    }
+
+    @Override
+    public int getType()
+    {
+        return 0;
+    }
+}

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheQueryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheQueryCache.java?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheQueryCache.java
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheQueryCache.java
Sun May 11 10:39:22 2014
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.jcs.jcache.openjpa;
+
+import org.apache.openjpa.datacache.AbstractQueryCache;
+import org.apache.openjpa.datacache.DataCacheManager;
+import org.apache.openjpa.datacache.QueryKey;
+import org.apache.openjpa.datacache.QueryResult;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class OpenJPAJCacheQueryCache extends AbstractQueryCache
+{
+    private static final String OPENJPA_PREFIX = "openjpa.querycache.";
+    private static final String QUERY_CACHE_NAME = "query";
+
+    private final Lock lock = new ReentrantLock();
+    private OpenJPAJCacheDataCacheManager manager;
+
+    @Override
+    public void initialize(final DataCacheManager manager)
+    {
+        super.initialize(manager);
+        this.manager = OpenJPAJCacheDataCacheManager.class.cast(manager);
+    }
+
+    @Override
+    protected void clearInternal()
+    {
+        final CacheManager cacheManager = manager.getCacheManager();
+        for (final String cacheName : cacheManager.getCacheNames())
+        {
+            if (!cacheName.startsWith(OPENJPA_PREFIX))
+            {
+                continue;
+            }
+            cacheManager.getCache(cacheName).clear();
+        }
+    }
+
+    @Override
+    protected Collection keySet()
+    {
+        final Collection<QueryKey> keys = new LinkedList<QueryKey>();
+        for (final Cache.Entry<Object, Object> entry : queryCache())
+        {
+            keys.add(QueryKey.class.cast(entry.getKey()));
+        }
+        return keys;
+    }
+
+    @Override
+    protected QueryResult getInternal(final QueryKey qk)
+    {
+        return QueryResult.class.cast(queryCache().get(qk));
+    }
+
+    private Cache<Object, Object> queryCache()
+    {
+        return manager.getOrCreateCache(OPENJPA_PREFIX, QUERY_CACHE_NAME);
+    }
+
+    @Override
+    protected QueryResult putInternal(final QueryKey qk, final QueryResult oids)
+    {
+        queryCache().put(qk, oids);
+        return oids;
+    }
+
+    @Override
+    protected QueryResult removeInternal(final QueryKey qk)
+    {
+        final Object remove = queryCache().getAndRemove(qk);
+        if (remove == null)
+        {
+            return null;
+        }
+        return QueryResult.class.cast(remove);
+    }
+
+    @Override
+    protected boolean pinInternal(final QueryKey qk)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    protected boolean unpinInternal(final QueryKey qk)
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void writeLock()
+    {
+        lock.lock();
+    }
+
+    @Override
+    public void writeUnlock()
+    {
+        lock.unlock();
+    }
+}

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/org.apache.openjpa.lib.conf.ProductDerivation
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/org.apache.openjpa.lib.conf.ProductDerivation?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/org.apache.openjpa.lib.conf.ProductDerivation
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/main/resources/META-INF/services/org.apache.openjpa.lib.conf.ProductDerivation
Sun May 11 10:39:22 2014
@@ -0,0 +1 @@
+org.apache.commons.jcs.jcache.openjpa.OpenJPAJCacheDerivation

Added: commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheTest.java?rev=1593768&view=auto
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheTest.java
(added)
+++ commons/proper/jcs/trunk/commons-jcs-jcache-openjpa/src/test/java/org/apache/commons/jcs/jcache/openjpa/OpenJPAJCacheDataCacheTest.java
Sun May 11 10:39:22 2014
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.jcs.jcache.openjpa;
+
+import org.apache.derby.jdbc.EmbeddedDriver;
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.junit.Test;
+
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Persistence;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class OpenJPAJCacheDataCacheTest
+{
+    @Test
+    public void cacheMe()
+    {
+        final Properties props = new Properties();
+        props.setProperty("openjpa.MetaDataFactory", "jpa(Types=" + MyEntity.class.getName()
+ ")");
+        props.setProperty("openjpa.ConnectionDriverName", EmbeddedDriver.class.getName());
+        props.setProperty("openjpa.ConnectionURL", "jdbc:derby:memory:test;create=true");
+        props.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema");
+        props.setProperty("openjpa.DataCacheManager", "jcache");
+        props.setProperty("openjpa.DataCache", "jcache");
+        props.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
+
+        final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test-jcache",
props);
+        final OpenJPAConfiguration conf = OpenJPAEntityManagerFactorySPI.class.cast(emf).getConfiguration();
+
+        final EntityManager em = emf.createEntityManager();
+
+        final MyEntity entity = new MyEntity();
+        entity.setName("cacheMe1");
+        em.getTransaction().begin();
+        em.persist(entity);
+        em.getTransaction().commit();
+        assertNotNull(conf.getDataCacheManagerInstance().getDataCache("default"));
+
+        assertThat(conf.getDataCacheManagerInstance(), instanceOf(OpenJPAJCacheDataCacheManager.class));
+        assertThat(conf.getDataCacheManagerInstance().getDataCache("default"), instanceOf(OpenJPAJCacheDataCache.class));
+        assertTrue(conf.getDataCacheManagerInstance().getDataCache("default").contains(
+                JPAFacadeHelper.toOpenJPAObjectId(conf.getMetaDataRepositoryInstance()
+                        .getCachedMetaData(MyEntity.class), entity.getId())
+        ));
+
+        em.close();
+
+        emf.close();
+    }
+
+    @Entity
+    public static class MyEntity
+    {
+        @Id
+        @GeneratedValue
+        private long id;
+        private String name;
+
+        public long getId()
+        {
+            return id;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public void setName(final String name)
+        {
+            this.name = name;
+        }
+    }
+}

Modified: commons/proper/jcs/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/pom.xml?rev=1593768&r1=1593767&r2=1593768&view=diff
==============================================================================
--- commons/proper/jcs/trunk/pom.xml (original)
+++ commons/proper/jcs/trunk/pom.xml Sun May 11 10:39:22 2014
@@ -141,6 +141,7 @@
     <module>commons-jcs-jcache</module>
     <module>commons-jcs-tck-tests</module>
     <module>commons-jcs-jcache-extras</module>
+    <module>commons-jcs-jcache-openjpa</module>
   </modules>
 
   <developers>



Mime
View raw message