Author: jgenender Date: Fri Dec 1 11:09:45 2006 New Revision: 481339 URL: http://svn.apache.org/viewvc?view=rev&rev=481339 Log: Add client configuration Added: geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/ClientConfigBuilder.java geronimo/sandbox/gcache/client/src/main/resources/gcache-client-config-default.xml geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ClientConfigBuilderTest.java geronimo/sandbox/gcache/client/src/test/resources/gcache-client-config.xml Modified: geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/dd/JaxbGcacheClientConfigurationFactory.java Added: geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/ClientConfigBuilder.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/ClientConfigBuilder.java?view=auto&rev=481339 ============================================================================== --- geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/ClientConfigBuilder.java (added) +++ geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/ClientConfigBuilder.java Fri Dec 1 11:09:45 2006 @@ -0,0 +1,155 @@ +/* + * 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.geronimo.gcache.client; + +import java.net.URI; +import java.net.URL; +import java.util.List; + +import net.sf.ehcache.config.CacheConfiguration; +import net.sf.ehcache.config.Configuration; +import net.sf.ehcache.config.DiskStoreConfiguration; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.geronimo.gcache.client.dd.CacheType; +import org.apache.geronimo.gcache.client.dd.DefaultCacheType; +import org.apache.geronimo.gcache.client.dd.DiskStoreType; +import org.apache.geronimo.gcache.client.dd.GcacheClientConfig; +import org.apache.geronimo.gcache.client.dd.JaxbGcacheClientConfigurationFactory; +import org.apache.geronimo.gcache.client.dd.TransportType; +import org.apache.geronimo.gcache.dd.GcacheConfiguration; + +public class ClientConfigBuilder { + + private static final Log log = LogFactory.getLog(ClientConfigBuilder.class); + + private static final String CLASSPATH_CONFIGURATION_FILE = "gcache-client-config.xml"; + + private static final String DEFAULT_CLASSPATH_CONFIGURATION_FILE = "gcache-client-config-default.xml"; + + public GcacheConfiguration getConfig(URL fileURL) { + + GcacheConfiguration config = null; + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + + // Was a file passed to us? + if (fileURL != null) { + config = processConfig(fileURL); + return config; + } + + // Is the configuration file in the path somewhere? + URL configURL = cl.getResource(CLASSPATH_CONFIGURATION_FILE); + if (configURL != null) { + config = processConfig(configURL); + + return config; + } + + // Read the default config + log.warn("Using " + DEFAULT_CLASSPATH_CONFIGURATION_FILE + + " for configuration. No cache configurations are loaded. Please consider having your own."); + + configURL = cl.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE); + if (configURL == null) { + throw new RuntimeException(DEFAULT_CLASSPATH_CONFIGURATION_FILE + " not found, not configuration can be loaded."); + } + config = processConfig(configURL); + return config; + } + + private GcacheConfiguration processConfig(URL url) { + try { + GcacheConfiguration gcacheConfig = new GcacheConfiguration(); + Configuration ehcacheConfig = new Configuration(); + DiskStoreConfiguration diskConfig = null; + CacheConfiguration defaultCache = null; + + JaxbGcacheClientConfigurationFactory factory = new JaxbGcacheClientConfigurationFactory(); + + GcacheClientConfig config = factory.getConfig(url); + + // Get the transport + TransportType transport = config.getTransport(); + if (transport != null) { + gcacheConfig.setTransportName(transport.getName()); + + URI transportURI = new URI(transport.getUri()); + gcacheConfig.setTransportUri(transportURI); + + String discovery = transport.getDiscoveryUri(); + if (discovery != null) { + URI discoveryURI = new URI(discovery); + gcacheConfig.setDiscoveryUri(discoveryURI); + } + } + + // Read the disk store + DiskStoreType diskStore = config.getDiskStore(); + if (diskStore != null) { + String path = diskStore.getPath(); + diskConfig = new DiskStoreConfiguration(); + diskConfig.setPath(path); + } + + // Read the default cache + DefaultCacheType defaultType = config.getDefaultCache(); + if (defaultType != null) { + defaultCache = new CacheConfiguration(); + defaultCache.setDiskExpiryThreadIntervalSeconds(defaultType.getDiskExpiryThreadIntervalSeconds()); + defaultCache.setMaxElementsInMemory(defaultType.getMaxElementsInMemory()); + defaultCache.setEternal(defaultType.isEternal()); + defaultCache.setDiskPersistent(defaultType.isDiskPersistent()); + defaultCache.setOverflowToDisk(defaultType.isOverflowToDisk()); + defaultCache.setTimeToIdleSeconds(defaultType.getTimeToIdleSeconds()); + defaultCache.setTimeToLiveSeconds(defaultType.getTimeToLiveSeconds()); + defaultCache.setMemoryStoreEvictionPolicy(defaultType.getMemoryStoreEvictionPolicy()); + } + + ehcacheConfig.setDefaultCacheConfiguration(defaultCache); + ehcacheConfig.addDiskStore(diskConfig); + + // Read the cache list + List cacheList = config.getCaches(); + for (CacheType cacheType : cacheList) { + CacheConfiguration cache = new CacheConfiguration(); + cache.setName(cacheType.getName()); + cache.setDiskExpiryThreadIntervalSeconds(cacheType.getDiskExpiryThreadIntervalSeconds()); + cache.setMaxElementsInMemory(cacheType.getMaxElementsInMemory()); + cache.setEternal(cacheType.isEternal()); + cache.setDiskPersistent(cacheType.isDiskPersistent()); + cache.setOverflowToDisk(cacheType.isOverflowToDisk()); + cache.setTimeToIdleSeconds(cacheType.getTimeToIdleSeconds()); + cache.setTimeToLiveSeconds(cacheType.getTimeToLiveSeconds()); + cache.setMemoryStoreEvictionPolicy(cacheType.getMemoryStoreEvictionPolicy()); + + ehcacheConfig.addCache(cache); + } + + gcacheConfig.setEhcacheConfig(ehcacheConfig); + + return gcacheConfig; + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} Modified: geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/dd/JaxbGcacheClientConfigurationFactory.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/dd/JaxbGcacheClientConfigurationFactory.java?view=diff&rev=481339&r1=481338&r2=481339 ============================================================================== --- geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/dd/JaxbGcacheClientConfigurationFactory.java (original) +++ geronimo/sandbox/gcache/client/src/main/java/org/apache/geronimo/gcache/client/dd/JaxbGcacheClientConfigurationFactory.java Fri Dec 1 11:09:45 2006 @@ -26,7 +26,7 @@ public static final String SCHEMA = "http://geronimo.apache.org/xml/ns/gcache-client-1.0"; - protected GcacheClientConfig getConfig(URL url) throws Exception { + public GcacheClientConfig getConfig(URL url) throws Exception { return getConfig(url, GcacheClientConfig.class); } Added: geronimo/sandbox/gcache/client/src/main/resources/gcache-client-config-default.xml URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/client/src/main/resources/gcache-client-config-default.xml?view=auto&rev=481339 ============================================================================== --- geronimo/sandbox/gcache/client/src/main/resources/gcache-client-config-default.xml (added) +++ geronimo/sandbox/gcache/client/src/main/resources/gcache-client-config-default.xml Fri Dec 1 11:09:45 2006 @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file Added: geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ClientConfigBuilderTest.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ClientConfigBuilderTest.java?view=auto&rev=481339 ============================================================================== --- geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ClientConfigBuilderTest.java (added) +++ geronimo/sandbox/gcache/client/src/test/java/org/apache/geronimo/gcache/dd/ClientConfigBuilderTest.java Fri Dec 1 11:09:45 2006 @@ -0,0 +1,67 @@ +/* + * 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.geronimo.gcache.dd; + +import java.util.Map; + +import net.sf.ehcache.config.CacheConfiguration; +import net.sf.ehcache.config.Configuration; + +import org.apache.geronimo.gcache.client.ClientConfigBuilder; +import org.testng.annotations.Test; + +public class ClientConfigBuilderTest { + + @Test + public void testConfigBuilder() throws Exception { + ClientConfigBuilder cb = new ClientConfigBuilder(); + + //Pull the configuration + GcacheConfiguration config = cb.getConfig(null); + + //Server and Transport Settings + assert config.getTransportUri().getHost().equals("default"); + assert config.getTransportUri().getScheme().equals("tcp"); + assert config.getDiscoveryUri().getHost().equals("default"); + assert config.getDiscoveryUri().getScheme().equals("multicast"); + + //Check for cache configs + Configuration ehcacheConfig = config.getEhcacheConfig(); + assert ehcacheConfig != null; + + Map configs = ehcacheConfig.getCacheConfigurations(); + assert configs.containsKey("Test"); + assert configs.containsKey("Test2"); + + //Test cache detail info + CacheConfiguration cacheConfig = (CacheConfiguration) configs.get("Test"); + assert 60 == cacheConfig.getDiskExpiryThreadIntervalSeconds(); + assert 1800 == cacheConfig.getTimeToIdleSeconds(); + assert cacheConfig.getMemoryStoreEvictionPolicy().toString().equals("LRU"); + assert 0 == cacheConfig.getTimeToLiveSeconds(); + + //Test a default cache was configured + CacheConfiguration defaultConfig = ehcacheConfig.getDefaultCacheConfiguration(); + assert 120 == defaultConfig.getDiskExpiryThreadIntervalSeconds(); + assert 120 == defaultConfig.getTimeToIdleSeconds(); + assert defaultConfig.getMemoryStoreEvictionPolicy().toString().equals("LRU"); + assert 120 == defaultConfig.getTimeToLiveSeconds(); + } + +} Added: geronimo/sandbox/gcache/client/src/test/resources/gcache-client-config.xml URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/client/src/test/resources/gcache-client-config.xml?view=auto&rev=481339 ============================================================================== --- geronimo/sandbox/gcache/client/src/test/resources/gcache-client-config.xml (added) +++ geronimo/sandbox/gcache/client/src/test/resources/gcache-client-config.xml Fri Dec 1 11:09:45 2006 @@ -0,0 +1,21 @@ + + + + + + + + + + + +