Skip to content

Commit

Permalink
[KARAF-6017] - added cache module
Browse files Browse the repository at this point in the history
  • Loading branch information
awrb committed Nov 21, 2022
1 parent 1051919 commit 5c2c204
Show file tree
Hide file tree
Showing 38 changed files with 1,982 additions and 3 deletions.
17 changes: 17 additions & 0 deletions assemblies/features/standard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,23 @@
<scope>provided</scope>
</dependency>

<!-- cache deps -->
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.commands</artifactId>
<scope>provided</scope>
</dependency>

<!-- eventadmin deps -->
<dependency>
<groupId>org.apache.karaf.services</groupId>
Expand Down
12 changes: 12 additions & 0 deletions assemblies/features/standard/src/main/feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,18 @@ jul.layout.type = simple
</config>
</feature>

<feature name="cache" description="Provides a simple JSR107 based caching facade" version="${project.version}">
<feature>scr</feature>
<bundle>mvn:javax.cache/cache-api/${spec.javax.cache-api.version}</bundle>
<bundle>mvn:org.ehcache/ehcache/${ehcache.version}</bundle>
<bundle>mvn:org.apache.karaf.cache/org.apache.karaf.cache.api/${project.version}</bundle>
<bundle>mvn:org.apache.karaf.cache/org.apache.karaf.cache.core/${project.version}</bundle>
<conditional>
<condition>shell</condition>
<bundle>mvn:org.apache.karaf.cache/org.apache.karaf.cache.commands/${project.version}</bundle>
</conditional>
</feature>

<feature name="documentation" description="Documentation of Karaf project in HTML" version="${project.version}">
<feature>pax-web-war</feature>
<bundle>mvn:org.apache.karaf/manual/${project.version}</bundle>
Expand Down
27 changes: 27 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,22 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.commands</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.karaf.services</groupId>
<artifactId>org.apache.karaf.services.staticcm</artifactId>
Expand Down Expand Up @@ -1599,6 +1615,11 @@
<artifactId>istack-commons-runtime</artifactId>
<version>3.0.10</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>

<!-- specs -->
<dependency>
Expand Down Expand Up @@ -1641,6 +1662,12 @@
<artifactId>jakarta.mail-api</artifactId>
<version>${spec.mail.version}</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>${spec.javax.cache-api.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
53 changes: 53 additions & 0 deletions cache/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--
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.
-->

<parent>
<artifactId>cache</artifactId>
<groupId>org.apache.karaf.cache</groupId>
<version>4.4.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>org.apache.karaf.cache.api</artifactId>
<name>Apache Karaf :: Cache :: API</name>

<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
*
* 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.karaf.cache.api;

import javax.cache.Cache;
import javax.cache.configuration.Configuration;
import java.net.URL;
import java.util.List;

/**
* A simple caching facade that lets use caching using JSR 107 APIs.
*/
public interface CacheService {

/**
* Creates a new cache.
* @param name name of the cache to create
* @param configuration cache configuration, such as its key and value type.
* Specific configurations (such as max size and caching duration)
* will depend on the cache provider.
* @param <K> type of cache keys, e.g. Long or String
* @param <V> type of cache values, e.g. Long or String
*/
<K, V> void createCache(String name, Configuration<K, V> configuration);

/**
* Creates a new cache from a configuration file (located in Karaf's etc directory).
* @param configFile config file
* @param classLoader class loader can be passed in case the cache complex custom keys and/or values
*/
void createCache(URL configFile, ClassLoader classLoader);

/**
* Get a single cached value by providing cache's name and a key.
* @param name name of the cache to get the value from
* @param key key of the cached item
* @param <K> cache key type
* @param <V> cache value type
* @return
*/
<K, V> V get(String name, K key);

/**
* Store a new value in a cache.
* @param name name of the cache to put new value into
* @param key key under which the value will be cached
* @param value value to cache
* @param <K> cache key type
* @param <V> cache value type
*/
<K, V> void put(String name, K key, V value);

/**
* Get the cache object directly.
* @param name name of the cache
* @param <K> cache key type
* @param <V> cache value type
* @return
*/
<K, V> Cache<K, V> getCache(String name);

/**
* Invalidates a cache.
* @param name name of the cache to invalidate
*/
void invalidateCache(String name);

/**
* Lists all available caches by their names.
* @return list of cache names
*/
List<String> listCaches();
}
69 changes: 69 additions & 0 deletions cache/commands/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">

<!--
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.
-->
<parent>
<artifactId>cache</artifactId>
<groupId>org.apache.karaf.cache</groupId>
<version>4.4.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Apache Karaf :: Cache :: Commands</name>

<artifactId>org.apache.karaf.cache.commands</artifactId>

<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>org.apache.karaf.cache.api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-services-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Karaf-Commands>*</Karaf-Commands>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.lifecycle.Reference;

import javax.cache.Cache;
import javax.cache.configuration.Configuration;

public abstract class CacheCommandSupport implements Action
{

@Reference
private CacheService cacheService;

@Override
public Object execute() throws Exception
{
if (cacheService == null) {
throw new IllegalStateException("CacheService not found");
}
return doExecute(cacheService);
}

protected abstract Object doExecute(CacheService cacheService) throws Exception;

@SuppressWarnings("unchecked")
protected Object castKey(String cacheName, Object key) {
Cache cache = cacheService.getCache(cacheName);
if (cache == null) {
throw new IllegalArgumentException("Cache " + cacheName + " not found!");
}
return cast(key, cacheService.getCache(cacheName).getConfiguration(Configuration.class).getKeyType());
}

@SuppressWarnings("unchecked")
protected Object castValue(String cacheName, Object value) {
return cast(value, cacheService.getCache(cacheName).getConfiguration(Configuration.class).getValueType());
}

private Object cast(Object argument, Class type)
{
if (type.equals(Short.class)) {
return Short.parseShort((String) argument);
}
if (type.equals(Integer.class)) {
return Integer.parseInt((String) argument);
}
if (type.equals(Long.class)) {
return Long.parseLong((String) argument);
}
if (type.equals(Boolean.class)) {
return Boolean.parseBoolean((String) argument);
}

return argument;
}
}

0 comments on commit 5c2c204

Please sign in to comment.