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.infinispan;
17  
18  import javax.naming.InitialContext;
19  import javax.naming.NamingException;
20  
21  import org.infinispan.manager.EmbeddedCacheManager;
22  
23  import de.smartics.properties.api.config.cache.CacheConfiguration;
24  import de.smartics.properties.spi.config.cache.CacheConfigurationLoader;
25  import de.smartics.properties.spi.config.cache.CacheException;
26  import de.smartics.properties.spi.config.cache.CacheMessageBean;
27  
28  /**
29   * This service looks up the jndi name in the cache.properties file and fetches
30   * the CacheManager using this name from the jndi context.
31   */
32  public final class JndiCacheManagerLookupService
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    // ****************************** Initializer *******************************
41  
42    // ****************************** Constructors ******************************
43  
44    /**
45     * Default.
46     */
47    public JndiCacheManagerLookupService()
48    {
49  
50    }
51  
52    // ****************************** Inner Classes *****************************
53  
54    // ********************************* Methods ********************************
55  
56    // --- init -----------------------------------------------------------------
57  
58    // --- get&set --------------------------------------------------------------
59  
60    // --- business -------------------------------------------------------------
61  
62    /**
63     * Creates the embedded cache manager.
64     *
65     * @return the embedded cache manager.
66     * @throws CacheException on any problem loading the cache.
67     */
68    public CacheManagerProxy createManager() throws CacheException
69    {
70      final CacheConfigurationLoader loader = new CacheConfigurationLoader();
71      final CacheConfiguration properties = loader.load();
72      final String mappedName = properties.getJndiName();
73      final String cacheName = properties.getCacheName();
74      final EmbeddedCacheManager manager =
75          lookup(properties.getCacheId(), mappedName);
76  
77      final CacheManagerProxy proxy = new CacheManagerProxy(manager, cacheName);
78  
79      return proxy;
80    }
81  
82    private EmbeddedCacheManager lookup(final String cacheId,
83        final String mappedName)
84    {
85      try
86      {
87        final InitialContext context = new InitialContext();
88        final Object o = context.lookup(mappedName);
89        final EmbeddedCacheManager manager = (EmbeddedCacheManager) o;
90        return manager;
91      }
92      catch (final NamingException e)
93      {
94        throw new CacheException(CacheMessageBean.jndi(e, cacheId, mappedName));
95      }
96    }
97    // --- object basics --------------------------------------------------------
98  
99  }