# sprint-1 Move cache classes from 'org.apache.ignite'
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/76390dfb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/76390dfb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/76390dfb Branch: refs/heads/ignite-86 Commit: 76390dfb75d3877a767aab24f28792c137d9dc9d Parents: f9b49ab Author: sboikov <sboi...@gridgain.com> Authored: Mon Jan 26 10:17:13 2015 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Mon Jan 26 10:17:13 2015 +0300 ---------------------------------------------------------------------- .../services/javax.cache.spi.CachingProvider | 2 +- .../org/apache/ignite/IgniteCacheMXBean.java | 72 ---- .../org/apache/ignite/IgniteCacheManager.java | 372 ------------------- .../apache/ignite/IgniteCachingProvider.java | 190 ---------- .../org/apache/ignite/cache/CacheManager.java | 372 +++++++++++++++++++ .../apache/ignite/cache/CachingProvider.java | 189 ++++++++++ .../processors/cache/IgniteCacheProxy.java | 8 +- .../apache/ignite/mbean/IgniteCacheMXBean.java | 72 ++++ .../cache/IgniteCachingProviderSelfTest.java | 14 +- 9 files changed, 645 insertions(+), 646 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/META-INF/services/javax.cache.spi.CachingProvider ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/META-INF/services/javax.cache.spi.CachingProvider b/modules/core/src/main/java/META-INF/services/javax.cache.spi.CachingProvider index eb232dc..59edf9d 100644 --- a/modules/core/src/main/java/META-INF/services/javax.cache.spi.CachingProvider +++ b/modules/core/src/main/java/META-INF/services/javax.cache.spi.CachingProvider @@ -1 +1 @@ -org.apache.ignite.IgniteCachingProvider +org.apache.ignite.cache.CachingProvider http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java deleted file mode 100644 index 26cace6..0000000 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.ignite; - -import javax.cache.*; -import javax.cache.configuration.*; -import javax.cache.management.*; - -/** - * - */ -public class IgniteCacheMXBean implements CacheMXBean { - /** */ - private final Cache<?, ?> cache; - - /** - * @param cache Cache. - */ - public IgniteCacheMXBean(Cache<?, ?> cache) { - this.cache = cache; - } - - /** {@inheritDoc} */ - @Override public String getKeyType() { - return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName(); - } - - /** {@inheritDoc} */ - @Override public String getValueType() { - return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName(); - } - - /** {@inheritDoc} */ - @Override public boolean isReadThrough() { - return cache.getConfiguration(CompleteConfiguration.class).isReadThrough(); - } - - /** {@inheritDoc} */ - @Override public boolean isWriteThrough() { - return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough(); - } - - /** {@inheritDoc} */ - @Override public boolean isStoreByValue() { - return cache.getConfiguration(CompleteConfiguration.class).isStoreByValue(); - } - - /** {@inheritDoc} */ - @Override public boolean isStatisticsEnabled() { - return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled(); - } - - /** {@inheritDoc} */ - @Override public boolean isManagementEnabled() { - return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java deleted file mode 100644 index 763b552..0000000 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java +++ /dev/null @@ -1,372 +0,0 @@ -/* - * 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.ignite; - -import org.apache.ignite.cache.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.lang.*; -import org.apache.ignite.internal.util.typedef.*; - -import javax.cache.*; -import javax.cache.configuration.*; -import javax.cache.spi.*; -import javax.management.*; -import java.net.*; -import java.util.*; -import java.util.concurrent.atomic.*; - -/** - * - */ -public class IgniteCacheManager implements CacheManager { - /** */ - private final Map<String, IgniteBiTuple<Ignite, IgniteCacheMXBean>> igniteMap = new HashMap<>(); - - /** */ - private final URI uri; - - /** */ - private final CachingProvider cachingProvider; - - /** */ - private final ClassLoader clsLdr; - - /** */ - private final Properties props; - - /** */ - private final AtomicBoolean closed = new AtomicBoolean(); - - /** - * @param uri Uri. - * @param cachingProvider Caching provider. - * @param clsLdr Class loader. - * @param props Properties. - */ - public IgniteCacheManager(URI uri, CachingProvider cachingProvider, ClassLoader clsLdr, Properties props) { - this.uri = uri; - this.cachingProvider = cachingProvider; - this.clsLdr = clsLdr; - this.props = props; - } - - /** {@inheritDoc} */ - @Override public CachingProvider getCachingProvider() { - return cachingProvider; - } - - /** {@inheritDoc} */ - @Override public URI getURI() { - return uri; - } - - /** {@inheritDoc} */ - @Override public ClassLoader getClassLoader() { - return clsLdr; - } - - /** {@inheritDoc} */ - @Override public Properties getProperties() { - return props; - } - - /** {@inheritDoc} */ - @Override public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cacheName, C cacheCfg) - throws IllegalArgumentException { - ensureNotClosed(); - - if (cacheCfg == null) - throw new NullPointerException(); - - if (cacheName == null) - throw new NullPointerException(); - - if (!(cacheCfg instanceof CompleteConfiguration)) - throw new UnsupportedOperationException("Configuration is not supported: " + cacheCfg); - - if (cacheCfg instanceof CacheConfiguration) { - String cfgCacheName = ((CacheConfiguration)cacheCfg).getName(); - - if (cfgCacheName != null) { - if (!cacheName.equals(cfgCacheName)) - throw new IllegalArgumentException(); - } - else { - cacheCfg = (C)new CacheConfiguration((CompleteConfiguration)cacheCfg); - - ((CacheConfiguration)cacheCfg).setName(cacheName); - } - } - - IgniteCache<K, V> res; - - synchronized (igniteMap) { - if (igniteMap.containsKey(cacheName)) - throw new CacheException("Cache already exists [cacheName=" + cacheName + ", manager=" + uri + ']'); - - Ignite ignite; - - if (uri.equals(cachingProvider.getDefaultURI())) { - IgniteConfiguration cfg = new IgniteConfiguration(); - cfg.setGridName("grid-for-" + cacheName); - - cfg.setCacheConfiguration(new CacheConfiguration((CompleteConfiguration)cacheCfg)); - - cfg.getCacheConfiguration()[0].setName(cacheName); - - try { - ignite = Ignition.start(cfg); - } - catch (IgniteCheckedException e) { - throw new CacheException(e); - } - } - else - throw new UnsupportedOperationException(); - - res = ignite.jcache(cacheName); - - igniteMap.put(cacheName, new T2<>(ignite, new IgniteCacheMXBean(res))); - } - - if (((CompleteConfiguration)cacheCfg).isManagementEnabled()) - enableManagement(cacheName, true); - - return res; - } - - /** - * @param cacheName Cache name. - */ - private <K, V> IgniteCache<K, V> findCache(String cacheName) { - IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; - - synchronized (igniteMap) { - tuple = igniteMap.get(cacheName); - } - - if (tuple == null) - return null; - - return tuple.get1().jcache(cacheName); - } - - /** {@inheritDoc} */ - @Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valType) { - ensureNotClosed(); - - Cache<K, V> cache = findCache(cacheName); - - if (cache != null) { - if(!keyType.isAssignableFrom(cache.getConfiguration(Configuration.class).getKeyType())) - throw new ClassCastException(); - - if(!valType.isAssignableFrom(cache.getConfiguration(Configuration.class).getValueType())) - throw new ClassCastException(); - } - - return cache; - } - - /** {@inheritDoc} */ - @Override public <K, V> Cache<K, V> getCache(String cacheName) { - ensureNotClosed(); - - IgniteCache<K, V> cache = findCache(cacheName); - - if (cache != null) { - if(cache.getConfiguration(Configuration.class).getKeyType() != Object.class) - throw new IllegalArgumentException(); - - if(cache.getConfiguration(Configuration.class).getValueType() != Object.class) - throw new IllegalArgumentException(); - } - - return cache; - } - - /** {@inheritDoc} */ - @Override public Iterable<String> getCacheNames() { - if (isClosed()) - return Collections.emptySet(); // javadoc of #getCacheNames() says that IllegalStateException should be - // thrown but CacheManagerTest.close_cachesEmpty() require empty collection. - - String[] resArr; - - synchronized (igniteMap) { - resArr = igniteMap.keySet().toArray(new String[igniteMap.keySet().size()]); - } - - return Collections.unmodifiableCollection(Arrays.asList(resArr)); - } - - /** - * @param ignite Ignite. - */ - public boolean isManagedIgnite(Ignite ignite) { - synchronized (igniteMap) { - for (IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple : igniteMap.values()) { - if (ignite.equals(tuple.get1())) - return true; - } - } - - return false; - } - - /** {@inheritDoc} */ - @Override public void destroyCache(String cacheName) { - ensureNotClosed(); - - if (cacheName == null) - throw new NullPointerException(); - - IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; - - synchronized (igniteMap) { - tuple = igniteMap.remove(cacheName); - } - - if (tuple != null) { - try { - tuple.get1().close(); - } - catch (Exception ignored) { - - } - - ObjectName objName = getObjectName(cacheName); - - MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer(); - - for (ObjectName n : mBeanSrv.queryNames(objName, null)) { - try { - mBeanSrv.unregisterMBean(n); - } - catch (Exception ignored) { - - } - } - } - } - - /** - * @param cacheName Cache name. - */ - private ObjectName getObjectName(String cacheName) { - String mBeanName = "javax.cache:type=CacheConfiguration,CacheManager=" - + uri.toString().replaceAll(",|:|=|\n", ".") - + ",Cache=" + cacheName.replaceAll(",|:|=|\n", "."); - - try { - return new ObjectName(mBeanName); - } - catch (MalformedObjectNameException e) { - throw new CacheException("Failed to create MBean name: " + mBeanName, e); - } - } - - /** {@inheritDoc} */ - @Override public void enableManagement(String cacheName, boolean enabled) { - ensureNotClosed(); - - if (cacheName == null) - throw new NullPointerException(); - - IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; - - synchronized (igniteMap) { - tuple = igniteMap.get(cacheName); - } - - ObjectName objName = getObjectName(cacheName); - MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer(); - - try { - if (enabled) { - if(mBeanSrv.queryNames(objName, null).isEmpty()) - mBeanSrv.registerMBean(tuple.get2(), objName); - } - else { - for (ObjectName n : mBeanSrv.queryNames(objName, null)) - mBeanSrv.unregisterMBean(n); - - } - } - catch (InstanceAlreadyExistsException | InstanceNotFoundException ignored) { - - } - catch (MBeanRegistrationException | NotCompliantMBeanException e) { - throw new CacheException(e); - } - } - - /** {@inheritDoc} */ - @Override public void enableStatistics(String cacheName, boolean enabled) { - ensureNotClosed(); - - if (cacheName == null) - throw new NullPointerException(); - - throw new UnsupportedOperationException(); - } - - /** - * - */ - private void ensureNotClosed() throws IllegalStateException { - if (closed.get()) - throw new IllegalStateException("Cache manager are closed [uri=" + uri + ", classLoader=" + clsLdr + ']'); - } - - /** {@inheritDoc} */ - @Override public void close() { - if (closed.compareAndSet(false, true)) { - IgniteBiTuple<Ignite, IgniteCacheMXBean>[] ignites; - - synchronized (igniteMap) { - ignites = igniteMap.values().toArray(new IgniteBiTuple[igniteMap.values().size()]); - } - - for (IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple : ignites) { - try { - tuple.get1().close(); - } - catch (Exception ignored) { - // Ignore any exceptions according to javadoc of javax.cache.CacheManager#close() - } - } - } - } - - /** {@inheritDoc} */ - @Override public boolean isClosed() { - return closed.get(); - } - - /** {@inheritDoc} */ - @Override public <T> T unwrap(Class<T> clazz) { - if(clazz.isAssignableFrom(getClass())) - return clazz.cast(this); - -// if(clazz.isAssignableFrom(ignite.getClass())) -// return clazz.cast(ignite); - - throw new IllegalArgumentException(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/IgniteCachingProvider.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCachingProvider.java b/modules/core/src/main/java/org/apache/ignite/IgniteCachingProvider.java deleted file mode 100644 index 0a60a55..0000000 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCachingProvider.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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.ignite; - -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.jetbrains.annotations.*; - -import javax.cache.*; -import javax.cache.configuration.*; -import javax.cache.spi.*; -import java.net.*; -import java.util.*; - -/** - * - */ -public class IgniteCachingProvider implements CachingProvider { - /** */ - private static final URI DEFAULT_URI; - - /** - * - */ - static { - URI uri = null; - - try { - URL dfltCfgURL = U.resolveGridGainUrl(GridGainEx.DFLT_CFG); - - if (dfltCfgURL != null) - uri = dfltCfgURL.toURI(); - } - catch (URISyntaxException ignored) { - // No-op. - } - - if (uri == null) - uri = URI.create("ignite://default"); - - DEFAULT_URI = uri; - } - - /** */ - public static final Properties DFLT_PROPS = new Properties(); - - /** */ - private final Map<ClassLoader, Map<URI, IgniteCacheManager>> cacheManagers = new WeakHashMap<>(); - - /** {@inheritDoc} */ - @Override public CacheManager getCacheManager(@Nullable URI uri, ClassLoader clsLdr, Properties props) { - if (uri == null) - uri = getDefaultURI(); - - if (clsLdr == null) - clsLdr = getDefaultClassLoader(); - - synchronized (cacheManagers) { - Map<URI, IgniteCacheManager> uriMap = cacheManagers.get(clsLdr); - - if (uriMap == null) { - uriMap = new HashMap<>(); - - cacheManagers.put(clsLdr, uriMap); - } - - IgniteCacheManager mgr = uriMap.get(uri); - - if (mgr == null || mgr.isClosed()) { - mgr = new IgniteCacheManager(uri, this, clsLdr, props); - - uriMap.put(uri, mgr); - } - - return mgr; - } - } - - /** {@inheritDoc} */ - @Override public ClassLoader getDefaultClassLoader() { - return getClass().getClassLoader(); - } - - /** {@inheritDoc} */ - @Override public URI getDefaultURI() { - return DEFAULT_URI; - } - - /** {@inheritDoc} */ - @Override public Properties getDefaultProperties() { - return DFLT_PROPS; - } - - /** {@inheritDoc} */ - @Override public CacheManager getCacheManager(URI uri, ClassLoader clsLdr) { - return getCacheManager(uri, clsLdr, getDefaultProperties()); - } - - /** {@inheritDoc} */ - @Override public CacheManager getCacheManager() { - return getCacheManager(getDefaultURI(), getDefaultClassLoader()); - } - - /** - * @param cache Cache. - */ - public CacheManager findManager(IgniteCache<?,?> cache) { - Ignite ignite = cache.unwrap(Ignite.class); - - synchronized (cacheManagers) { - for (Map<URI, IgniteCacheManager> map : cacheManagers.values()) { - for (IgniteCacheManager manager : map.values()) { - if (manager.isManagedIgnite(ignite)) - return manager; - } - } - } - - return null; - } - - /** {@inheritDoc} */ - @Override public void close() { - Collection<IgniteCacheManager> mgrs = new ArrayList<>(); - - synchronized (cacheManagers) { - for (Map<URI, IgniteCacheManager> uriMap : cacheManagers.values()) - mgrs.addAll(uriMap.values()); - - cacheManagers.clear(); - } - - for (IgniteCacheManager mgr : mgrs) - mgr.close(); - } - - /** {@inheritDoc} */ - @Override public void close(ClassLoader clsLdr) { - Collection<IgniteCacheManager> mgrs; - - synchronized (cacheManagers) { - Map<URI, IgniteCacheManager> uriMap = cacheManagers.remove(clsLdr); - - if (uriMap == null) - return; - - mgrs = uriMap.values(); - } - - for (IgniteCacheManager mgr : mgrs) - mgr.close(); - } - - /** {@inheritDoc} */ - @Override public void close(URI uri, ClassLoader clsLdr) { - IgniteCacheManager mgr; - - synchronized (cacheManagers) { - Map<URI, IgniteCacheManager> uriMap = cacheManagers.get(clsLdr); - - if (uriMap == null) - return; - - mgr = uriMap.remove(uri); - } - - if (mgr != null) - mgr.close(); - } - - /** {@inheritDoc} */ - @Override public boolean isSupported(OptionalFeature optionalFeature) { - return false; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/cache/CacheManager.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheManager.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheManager.java new file mode 100644 index 0000000..4880a09 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheManager.java @@ -0,0 +1,372 @@ +/* + * 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.ignite.cache; + +import org.apache.ignite.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.mbean.*; + +import javax.cache.*; +import javax.cache.configuration.*; +import javax.management.*; +import java.net.*; +import java.util.*; +import java.util.concurrent.atomic.*; + +/** + * Implementation of JSR-107 {@link javax.cache.CacheManager}. + */ +public class CacheManager implements javax.cache.CacheManager { + /** */ + private final Map<String, IgniteBiTuple<Ignite, IgniteCacheMXBean>> igniteMap = new HashMap<>(); + + /** */ + private final URI uri; + + /** */ + private final CachingProvider cachingProvider; + + /** */ + private final ClassLoader clsLdr; + + /** */ + private final Properties props; + + /** */ + private final AtomicBoolean closed = new AtomicBoolean(); + + /** + * @param uri Uri. + * @param cachingProvider Caching provider. + * @param clsLdr Class loader. + * @param props Properties. + */ + public CacheManager(URI uri, CachingProvider cachingProvider, ClassLoader clsLdr, Properties props) { + this.uri = uri; + this.cachingProvider = cachingProvider; + this.clsLdr = clsLdr; + this.props = props; + } + + /** {@inheritDoc} */ + @Override public CachingProvider getCachingProvider() { + return cachingProvider; + } + + /** {@inheritDoc} */ + @Override public URI getURI() { + return uri; + } + + /** {@inheritDoc} */ + @Override public ClassLoader getClassLoader() { + return clsLdr; + } + + /** {@inheritDoc} */ + @Override public Properties getProperties() { + return props; + } + + /** {@inheritDoc} */ + @Override public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String cacheName, C cacheCfg) + throws IllegalArgumentException { + ensureNotClosed(); + + if (cacheCfg == null) + throw new NullPointerException(); + + if (cacheName == null) + throw new NullPointerException(); + + if (!(cacheCfg instanceof CompleteConfiguration)) + throw new UnsupportedOperationException("Configuration is not supported: " + cacheCfg); + + if (cacheCfg instanceof CacheConfiguration) { + String cfgCacheName = ((CacheConfiguration)cacheCfg).getName(); + + if (cfgCacheName != null) { + if (!cacheName.equals(cfgCacheName)) + throw new IllegalArgumentException(); + } + else { + cacheCfg = (C)new CacheConfiguration((CompleteConfiguration)cacheCfg); + + ((CacheConfiguration)cacheCfg).setName(cacheName); + } + } + + IgniteCache<K, V> res; + + synchronized (igniteMap) { + if (igniteMap.containsKey(cacheName)) + throw new CacheException("Cache already exists [cacheName=" + cacheName + ", manager=" + uri + ']'); + + Ignite ignite; + + if (uri.equals(cachingProvider.getDefaultURI())) { + IgniteConfiguration cfg = new IgniteConfiguration(); + cfg.setGridName("grid-for-" + cacheName); + + cfg.setCacheConfiguration(new CacheConfiguration((CompleteConfiguration)cacheCfg)); + + cfg.getCacheConfiguration()[0].setName(cacheName); + + try { + ignite = Ignition.start(cfg); + } + catch (IgniteCheckedException e) { + throw new CacheException(e); + } + } + else + throw new UnsupportedOperationException(); + + res = ignite.jcache(cacheName); + + igniteMap.put(cacheName, new T2<>(ignite, new IgniteCacheMXBean(res))); + } + + if (((CompleteConfiguration)cacheCfg).isManagementEnabled()) + enableManagement(cacheName, true); + + return res; + } + + /** + * @param cacheName Cache name. + */ + private <K, V> IgniteCache<K, V> findCache(String cacheName) { + IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; + + synchronized (igniteMap) { + tuple = igniteMap.get(cacheName); + } + + if (tuple == null) + return null; + + return tuple.get1().jcache(cacheName); + } + + /** {@inheritDoc} */ + @Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valType) { + ensureNotClosed(); + + Cache<K, V> cache = findCache(cacheName); + + if (cache != null) { + if(!keyType.isAssignableFrom(cache.getConfiguration(Configuration.class).getKeyType())) + throw new ClassCastException(); + + if(!valType.isAssignableFrom(cache.getConfiguration(Configuration.class).getValueType())) + throw new ClassCastException(); + } + + return cache; + } + + /** {@inheritDoc} */ + @Override public <K, V> Cache<K, V> getCache(String cacheName) { + ensureNotClosed(); + + IgniteCache<K, V> cache = findCache(cacheName); + + if (cache != null) { + if(cache.getConfiguration(Configuration.class).getKeyType() != Object.class) + throw new IllegalArgumentException(); + + if(cache.getConfiguration(Configuration.class).getValueType() != Object.class) + throw new IllegalArgumentException(); + } + + return cache; + } + + /** {@inheritDoc} */ + @Override public Iterable<String> getCacheNames() { + if (isClosed()) + return Collections.emptySet(); // javadoc of #getCacheNames() says that IllegalStateException should be + // thrown but CacheManagerTest.close_cachesEmpty() require empty collection. + + String[] resArr; + + synchronized (igniteMap) { + resArr = igniteMap.keySet().toArray(new String[igniteMap.keySet().size()]); + } + + return Collections.unmodifiableCollection(Arrays.asList(resArr)); + } + + /** + * @param ignite Ignite. + */ + public boolean isManagedIgnite(Ignite ignite) { + synchronized (igniteMap) { + for (IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple : igniteMap.values()) { + if (ignite.equals(tuple.get1())) + return true; + } + } + + return false; + } + + /** {@inheritDoc} */ + @Override public void destroyCache(String cacheName) { + ensureNotClosed(); + + if (cacheName == null) + throw new NullPointerException(); + + IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; + + synchronized (igniteMap) { + tuple = igniteMap.remove(cacheName); + } + + if (tuple != null) { + try { + tuple.get1().close(); + } + catch (Exception ignored) { + + } + + ObjectName objName = getObjectName(cacheName); + + MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer(); + + for (ObjectName n : mBeanSrv.queryNames(objName, null)) { + try { + mBeanSrv.unregisterMBean(n); + } + catch (Exception ignored) { + + } + } + } + } + + /** + * @param cacheName Cache name. + */ + private ObjectName getObjectName(String cacheName) { + String mBeanName = "javax.cache:type=CacheConfiguration,CacheManager=" + + uri.toString().replaceAll(",|:|=|\n", ".") + + ",Cache=" + cacheName.replaceAll(",|:|=|\n", "."); + + try { + return new ObjectName(mBeanName); + } + catch (MalformedObjectNameException e) { + throw new CacheException("Failed to create MBean name: " + mBeanName, e); + } + } + + /** {@inheritDoc} */ + @Override public void enableManagement(String cacheName, boolean enabled) { + ensureNotClosed(); + + if (cacheName == null) + throw new NullPointerException(); + + IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple; + + synchronized (igniteMap) { + tuple = igniteMap.get(cacheName); + } + + ObjectName objName = getObjectName(cacheName); + MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer(); + + try { + if (enabled) { + if(mBeanSrv.queryNames(objName, null).isEmpty()) + mBeanSrv.registerMBean(tuple.get2(), objName); + } + else { + for (ObjectName n : mBeanSrv.queryNames(objName, null)) + mBeanSrv.unregisterMBean(n); + + } + } + catch (InstanceAlreadyExistsException | InstanceNotFoundException ignored) { + + } + catch (MBeanRegistrationException | NotCompliantMBeanException e) { + throw new CacheException(e); + } + } + + /** {@inheritDoc} */ + @Override public void enableStatistics(String cacheName, boolean enabled) { + ensureNotClosed(); + + if (cacheName == null) + throw new NullPointerException(); + + throw new UnsupportedOperationException(); + } + + /** + * + */ + private void ensureNotClosed() throws IllegalStateException { + if (closed.get()) + throw new IllegalStateException("Cache manager are closed [uri=" + uri + ", classLoader=" + clsLdr + ']'); + } + + /** {@inheritDoc} */ + @Override public void close() { + if (closed.compareAndSet(false, true)) { + IgniteBiTuple<Ignite, IgniteCacheMXBean>[] ignites; + + synchronized (igniteMap) { + ignites = igniteMap.values().toArray(new IgniteBiTuple[igniteMap.values().size()]); + } + + for (IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple : ignites) { + try { + tuple.get1().close(); + } + catch (Exception ignored) { + // Ignore any exceptions according to javadoc of javax.cache.CacheManager#close() + } + } + } + } + + /** {@inheritDoc} */ + @Override public boolean isClosed() { + return closed.get(); + } + + /** {@inheritDoc} */ + @Override public <T> T unwrap(Class<T> clazz) { + if(clazz.isAssignableFrom(getClass())) + return clazz.cast(this); + +// if(clazz.isAssignableFrom(ignite.getClass())) +// return clazz.cast(ignite); + + throw new IllegalArgumentException(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/cache/CachingProvider.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CachingProvider.java b/modules/core/src/main/java/org/apache/ignite/cache/CachingProvider.java new file mode 100644 index 0000000..1b6c945 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/cache/CachingProvider.java @@ -0,0 +1,189 @@ +/* + * 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.ignite.cache; + +import org.apache.ignite.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.jetbrains.annotations.*; + +import javax.cache.configuration.*; +import java.net.*; +import java.util.*; + +/** + * Implementation of JSR-107 {@link javax.cache.spi.CachingProvider}. + */ +public class CachingProvider implements javax.cache.spi.CachingProvider { + /** */ + private static final URI DEFAULT_URI; + + /** + * + */ + static { + URI uri = null; + + try { + URL dfltCfgURL = U.resolveGridGainUrl(GridGainEx.DFLT_CFG); + + if (dfltCfgURL != null) + uri = dfltCfgURL.toURI(); + } + catch (URISyntaxException ignored) { + // No-op. + } + + if (uri == null) + uri = URI.create("ignite://default"); + + DEFAULT_URI = uri; + } + + /** */ + public static final Properties DFLT_PROPS = new Properties(); + + /** */ + private final Map<ClassLoader, Map<URI, CacheManager>> cacheManagers = new WeakHashMap<>(); + + /** {@inheritDoc} */ + @Override public javax.cache.CacheManager getCacheManager(@Nullable URI uri, ClassLoader clsLdr, Properties props) { + if (uri == null) + uri = getDefaultURI(); + + if (clsLdr == null) + clsLdr = getDefaultClassLoader(); + + synchronized (cacheManagers) { + Map<URI, CacheManager> uriMap = cacheManagers.get(clsLdr); + + if (uriMap == null) { + uriMap = new HashMap<>(); + + cacheManagers.put(clsLdr, uriMap); + } + + CacheManager mgr = uriMap.get(uri); + + if (mgr == null || mgr.isClosed()) { + mgr = new CacheManager(uri, this, clsLdr, props); + + uriMap.put(uri, mgr); + } + + return mgr; + } + } + + /** {@inheritDoc} */ + @Override public ClassLoader getDefaultClassLoader() { + return getClass().getClassLoader(); + } + + /** {@inheritDoc} */ + @Override public URI getDefaultURI() { + return DEFAULT_URI; + } + + /** {@inheritDoc} */ + @Override public Properties getDefaultProperties() { + return DFLT_PROPS; + } + + /** {@inheritDoc} */ + @Override public javax.cache.CacheManager getCacheManager(URI uri, ClassLoader clsLdr) { + return getCacheManager(uri, clsLdr, getDefaultProperties()); + } + + /** {@inheritDoc} */ + @Override public javax.cache.CacheManager getCacheManager() { + return getCacheManager(getDefaultURI(), getDefaultClassLoader()); + } + + /** + * @param cache Cache. + */ + public javax.cache.CacheManager findManager(IgniteCache<?,?> cache) { + Ignite ignite = cache.unwrap(Ignite.class); + + synchronized (cacheManagers) { + for (Map<URI, CacheManager> map : cacheManagers.values()) { + for (CacheManager manager : map.values()) { + if (manager.isManagedIgnite(ignite)) + return manager; + } + } + } + + return null; + } + + /** {@inheritDoc} */ + @Override public void close() { + Collection<CacheManager> mgrs = new ArrayList<>(); + + synchronized (cacheManagers) { + for (Map<URI, CacheManager> uriMap : cacheManagers.values()) + mgrs.addAll(uriMap.values()); + + cacheManagers.clear(); + } + + for (CacheManager mgr : mgrs) + mgr.close(); + } + + /** {@inheritDoc} */ + @Override public void close(ClassLoader clsLdr) { + Collection<CacheManager> mgrs; + + synchronized (cacheManagers) { + Map<URI, CacheManager> uriMap = cacheManagers.remove(clsLdr); + + if (uriMap == null) + return; + + mgrs = uriMap.values(); + } + + for (CacheManager mgr : mgrs) + mgr.close(); + } + + /** {@inheritDoc} */ + @Override public void close(URI uri, ClassLoader clsLdr) { + CacheManager mgr; + + synchronized (cacheManagers) { + Map<URI, CacheManager> uriMap = cacheManagers.get(clsLdr); + + if (uriMap == null) + return; + + mgr = uriMap.remove(uri); + } + + if (mgr != null) + mgr.close(); + } + + /** {@inheritDoc} */ + @Override public boolean isSupported(OptionalFeature optionalFeature) { + return false; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java index 94ee239..eacb5b3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java @@ -753,11 +753,11 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements } /** {@inheritDoc} */ - @Override public CacheManager getCacheManager() { + @Override public javax.cache.CacheManager getCacheManager() { // TODO IGNITE-45 (Support start/close/destroy cache correctly) - IgniteCachingProvider provider = (IgniteCachingProvider)Caching.getCachingProvider( - IgniteCachingProvider.class.getName(), - IgniteCachingProvider.class.getClassLoader()); + CachingProvider provider = (CachingProvider)Caching.getCachingProvider( + CachingProvider.class.getName(), + CachingProvider.class.getClassLoader()); if (provider == null) return null; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/main/java/org/apache/ignite/mbean/IgniteCacheMXBean.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/mbean/IgniteCacheMXBean.java b/modules/core/src/main/java/org/apache/ignite/mbean/IgniteCacheMXBean.java new file mode 100644 index 0000000..0e87956 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/mbean/IgniteCacheMXBean.java @@ -0,0 +1,72 @@ +/* + * 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.ignite.mbean; + +import javax.cache.*; +import javax.cache.configuration.*; +import javax.cache.management.*; + +/** + * + */ +public class IgniteCacheMXBean implements CacheMXBean { + /** */ + private final Cache<?, ?> cache; + + /** + * @param cache Cache. + */ + public IgniteCacheMXBean(Cache<?, ?> cache) { + this.cache = cache; + } + + /** {@inheritDoc} */ + @Override public String getKeyType() { + return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName(); + } + + /** {@inheritDoc} */ + @Override public String getValueType() { + return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName(); + } + + /** {@inheritDoc} */ + @Override public boolean isReadThrough() { + return cache.getConfiguration(CompleteConfiguration.class).isReadThrough(); + } + + /** {@inheritDoc} */ + @Override public boolean isWriteThrough() { + return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough(); + } + + /** {@inheritDoc} */ + @Override public boolean isStoreByValue() { + return cache.getConfiguration(CompleteConfiguration.class).isStoreByValue(); + } + + /** {@inheritDoc} */ + @Override public boolean isStatisticsEnabled() { + return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled(); + } + + /** {@inheritDoc} */ + @Override public boolean isManagementEnabled() { + return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/76390dfb/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachingProviderSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachingProviderSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachingProviderSelfTest.java index c88bd01..559d741 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachingProviderSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCachingProviderSelfTest.java @@ -18,12 +18,12 @@ package org.apache.ignite.internal.processors.cache; import com.google.common.collect.*; -import org.apache.ignite.*; import org.apache.ignite.cache.*; +import org.apache.ignite.cache.CachingProvider; import org.apache.ignite.configuration.*; import javax.cache.*; -import javax.cache.spi.*; +import javax.cache.CacheManager; import java.util.*; /** @@ -88,9 +88,9 @@ public class IgniteCachingProviderSelfTest extends IgniteCacheAbstractTest { * */ public void testStartIgnite() { - CachingProvider cachingProvider = Caching.getCachingProvider(); + javax.cache.spi.CachingProvider cachingProvider = Caching.getCachingProvider(); - assert cachingProvider instanceof IgniteCachingProvider; + assert cachingProvider instanceof CachingProvider; CacheManager cacheMgr = cachingProvider.getCacheManager(); @@ -113,14 +113,14 @@ public class IgniteCachingProviderSelfTest extends IgniteCacheAbstractTest { } /** - * + * @throws Exception If failed. */ public void testCloseManager() throws Exception { startGridsMultiThreaded(1); - CachingProvider cachingProvider = Caching.getCachingProvider(); + javax.cache.spi.CachingProvider cachingProvider = Caching.getCachingProvider(); - assert cachingProvider instanceof IgniteCachingProvider; + assert cachingProvider instanceof CachingProvider; CacheManager cacheMgr = cachingProvider.getCacheManager();