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; 17 18 import java.io.Serializable; 19 import java.util.Properties; 20 21 import javax.naming.Context; 22 import javax.naming.InitialContext; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import de.smartics.properties.api.core.app.JndiContext; 27 28 /** 29 * Responsible to read JNDI configuration properties to connect to a cache 30 * stored via JNDI. 31 */ 32 public class CacheConfigurationJndiLoader implements Serializable 33 { 34 // ********************************* Fields ********************************* 35 36 // --- constants ------------------------------------------------------------ 37 38 /** 39 * The class version identifier. 40 * <p> 41 * The value of this constant is {@value}. 42 * </p> 43 */ 44 private static final long serialVersionUID = 1L; 45 46 // --- members -------------------------------------------------------------- 47 48 // ****************************** Initializer ******************************* 49 50 // ****************************** Constructors ****************************** 51 52 /** 53 * Default constructor. 54 */ 55 public CacheConfigurationJndiLoader() 56 { 57 } 58 59 // ****************************** Inner Classes ***************************** 60 61 // ********************************* Methods ******************************** 62 63 // --- init ----------------------------------------------------------------- 64 65 // --- get&set -------------------------------------------------------------- 66 67 // --- business ------------------------------------------------------------- 68 69 /** 70 * Loads the cache configuration from JNDI. 71 * 72 * @return the cache configuration. 73 * @throws CacheException on any problem loading the cache. 74 */ 75 public Properties load() throws CacheException 76 { 77 try 78 { 79 final Context context = new InitialContext(); 80 final JndiContext lookup = JndiContext.boot(context); 81 final String jndiName = lookup.lookup(CacheConfigurationLoader.JNDI_NAME); 82 83 final String sourceId = lookup.getSourceId(); 84 if (StringUtils.isBlank(jndiName)) 85 { 86 throw new CacheException(CacheMessageBean.missingProperty(sourceId, 87 CacheConfigurationLoader.JNDI_NAME)); 88 } 89 90 final Properties properties = new Properties(); 91 properties.setProperty(CacheConfigurationLoader.CACHE_ID, sourceId); 92 properties.setProperty(CacheConfigurationLoader.JNDI_NAME, jndiName); 93 return properties; 94 } 95 catch (final Exception e) 96 { 97 throw new CacheException(CacheMessageBean.configFailure(e, 98 JndiContext.JNDI_CONTEXT_PATH_BOOT)); 99 } 100 } 101 102 // --- object basics -------------------------------------------------------- 103 104 }