1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.spi.config.cache.infinispan; 17 18 import static de.smartics.properties.spi.config.cache.CacheConfigurationLoader.CACHE_ID; 19 import static de.smartics.properties.spi.config.cache.CacheConfigurationLoader.JNDI_NAME; 20 21 import java.util.Properties; 22 23 import javax.naming.InitialContext; 24 import javax.naming.NamingException; 25 26 import org.infinispan.manager.EmbeddedCacheManager; 27 28 import de.smartics.properties.spi.config.cache.CacheConfigurationLoader; 29 import de.smartics.properties.spi.config.cache.CacheException; 30 import de.smartics.properties.spi.config.cache.CacheMessageBean; 31 32 /** 33 * This service looks up the jndi name in the cache.properties file and fetches 34 * the CacheManager using this name from the jndi context. 35 */ 36 public final class JndiCacheManagerLookupService 37 { 38 // ********************************* Fields ********************************* 39 40 // --- constants ------------------------------------------------------------ 41 42 // --- members -------------------------------------------------------------- 43 44 // ****************************** Initializer ******************************* 45 46 // ****************************** Constructors ****************************** 47 48 /** 49 * Default. 50 */ 51 public JndiCacheManagerLookupService() 52 { 53 54 } 55 56 // ****************************** Inner Classes ***************************** 57 58 // ********************************* Methods ******************************** 59 60 // --- init ----------------------------------------------------------------- 61 62 // --- get&set -------------------------------------------------------------- 63 64 // --- business ------------------------------------------------------------- 65 66 /** 67 * Creates the embedded cache manager. 68 * 69 * @return the embedded cache manager. 70 */ 71 public EmbeddedCacheManager createManager() 72 { 73 final CacheConfigurationLoader loader = new CacheConfigurationLoader(); 74 final Properties properties = loader.load(); 75 final String mappedName = properties.getProperty(JNDI_NAME); 76 final EmbeddedCacheManager manager = 77 lookup(properties.getProperty(CACHE_ID), mappedName); 78 return manager; 79 } 80 81 private EmbeddedCacheManager lookup(final String cacheId, 82 final String mappedName) 83 { 84 try 85 { 86 final InitialContext context = new InitialContext(); 87 final Object o = context.lookup(mappedName); 88 final EmbeddedCacheManager manager = (EmbeddedCacheManager) o; 89 return manager; 90 } 91 catch (final NamingException e) 92 { 93 throw new CacheException(CacheMessageBean.jndi(e, cacheId, mappedName)); 94 } 95 } 96 // --- object basics -------------------------------------------------------- 97 98 }