View Javadoc

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