Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jcache enhancements #325

Merged
merged 2 commits into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ and Distribution License("CDDL") (collectively, the "License"). You
*/
package fish.payara.cdi.jsr107;

import fish.payara.cdi.jsr107.impl.NamedCache;
import com.hazelcast.core.HazelcastInstance;
import fish.payara.nucleus.hazelcast.HazelcastCore;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.Factory;
import javax.cache.configuration.FactoryBuilder;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;


/**
*
* @author steve
Expand All @@ -48,7 +52,7 @@ CachingProvider getCachingProvider() {
@Dependent
@Produces
CacheManager getCacheManager(InjectionPoint point) {
// TBD look for scoped caching providers in JNDI
CacheManager result = null;
return hazelcastCore.getCachingProvider().getCacheManager();
}

Expand All @@ -57,6 +61,77 @@ CacheManager getCacheManager(InjectionPoint point) {
HazelcastInstance getHazelcast() {
return hazelcastCore.getInstance();
}


/**
* Produces a Cache for injection. If the @NamedCache annotation is present the
* cache will be created based on the values of the annotation field
*
* Otherwise the cache will be created with the cache name being equal
* to the fully qualified name of the class into which it is injected
* @param ip
* @return
*/
@Produces
@SuppressWarnings("unchecked")
public Cache createCache(InjectionPoint ip) {
Cache result = null;

//determine the cache name first start with the default name
String cacheName = ip.getMember().getDeclaringClass().getCanonicalName();
NamedCache ncqualifier = ip.getAnnotated().getAnnotation(NamedCache.class);
CacheManager manager = getCacheManager(ip);


if (ncqualifier != null) { // configure the cache based on the annotation
String qualifierName = ncqualifier.cacheName();
if (!"".equals(cacheName)) {
cacheName = qualifierName;
}
Class keyClass = ncqualifier.keyClass();
Class valueClass = ncqualifier.valueClass();
result = manager.getCache(cacheName, keyClass, valueClass);
if (result == null) {
MutableConfiguration<Object, Object> config = new MutableConfiguration<>();
config.setTypes(keyClass, valueClass);

// determine the expiry policy
Class expiryPolicyFactoryClass = ncqualifier.expiryPolicyFactoryClass();
if (!"Object".equals(expiryPolicyFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(expiryPolicyFactoryClass);
config.setExpiryPolicyFactory(factory);
}

// determine the cache writer if any
Class writerFactoryClass = ncqualifier.cacheWriterFactoryClass();
if (!"Object".equals(writerFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(writerFactoryClass);
config.setCacheWriterFactory(factory);
}
config.setWriteThrough(ncqualifier.writeThrough());

// determine the cache loader if any
Class loaderFactoryClass = ncqualifier.cacheLoaderFactoryClass();
if (!"Object".equals(loaderFactoryClass.getSimpleName())) {
Factory factory = FactoryBuilder.factoryOf(loaderFactoryClass);
config.setCacheLoaderFactory(factory);
}
config.setReadThrough(ncqualifier.readThrough());

config.setManagementEnabled(ncqualifier.managementEnabled());
config.setStatisticsEnabled(ncqualifier.statisticsEnabled());
result = manager.createCache(cacheName, config);
}
} else { // configure a "raw" cache
result = manager.getCache(cacheName);
if (result == null) {
MutableConfiguration<Object, Object> config = new MutableConfiguration<>();
config.setManagementEnabled(true);
config.setStatisticsEnabled(true);
result = manager.createCache(cacheName, config);
}
}
return result;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*

DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright (c) 2015 C2B2 Consulting Limited. All rights reserved.

The contents of this file are subject to the terms of the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
or packager/legal/LICENSE.txt. See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at packager/legal/LICENSE.txt.
*/
package fish.payara.cdi.jsr107.impl;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to be applied to a Cache @Inject point to define the cache configuration
* for the Producer to configure the cache
* @author steve
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD, FIELD, PARAMETER})
public @interface NamedCache {
/**
* The name of the Cache in the Cache Manager
* @return
*/
String cacheName() default "";

/**
* The class of the Cache Keys
* @return
*/
Class keyClass() default Object.class;

/**
* The class of the cache values
* @return
*/
Class valueClass() default Object.class;

/**
* Are statistics enabled for the cache
* @return
*/
boolean statisticsEnabled() default false;

/**
* Is Managemenet Enabled for the Cache
* @return
*/
boolean managementEnabled() default false;

/**
* Is the cache configured for read through. If this is set to true a CacheLoader factory
* class must also be specified
* @return
*/
boolean readThrough() default false;

/**
* Is the cache configured for write through. If this is set a CacheWriter factory
* class must be specified
* @return
*/
boolean writeThrough() default false;

/**
* The factory class of the CacheLoader to be attached to the cache
* @return
*/
Class cacheLoaderFactoryClass() default Object.class;

/**
* The factory class of the CacheWriter to be attached to the cache
* @return
*/
Class cacheWriterFactoryClass() default Object.class;

/**
* The class of the expiry policy factory used to create an expiry policy for the cache
*/
Class expiryPolicyFactoryClass() default Object.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ and Distribution License("CDDL") (collectively, the "License"). You
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
* Packages up an object into a Serializable value
* @author steve
*/
public class PayaraValueHolder implements Externalizable {
public class PayaraValueHolder<T> implements Externalizable {

private static final long serialVersionUID = -4600378937394648370L;

Expand All @@ -40,7 +39,7 @@ public PayaraValueHolder() {

}

public PayaraValueHolder(Object value) throws IOException {
public PayaraValueHolder(T value) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(value);
Expand All @@ -49,13 +48,14 @@ public PayaraValueHolder(Object value) throws IOException {
baos.close();
}

public Object getValue() throws IOException, ClassNotFoundException {
@SuppressWarnings("unchecked")
public T getValue() throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
PayaraTCCLObjectInputStream ois = new PayaraTCCLObjectInputStream(bais);
Object result = ois.readObject();
ois.close();
bais.close();
return result;
return (T)result;
}

@Override
Expand Down