16b022abbf85b1cd7a9a531953ec917bf1890900
[policy/apex-pdp.git] / plugins / plugins-context / plugins-context-distribution / plugins-context-distribution-infinispan / src / main / java / org / onap / policy / apex / plugins / context / distribution / infinispan / InfinispanManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.distribution.infinispan;
22
23 import java.io.IOException;
24
25 import org.infinispan.manager.DefaultCacheManager;
26 import org.infinispan.manager.EmbeddedCacheManager;
27 import org.onap.policy.apex.context.ContextException;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * The Class InfinispanManager holds the Infinispan cache manager for a JVM.
33  */
34 public class InfinispanManager {
35     // Logger for this class
36     private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class);
37
38     // The Infinispan Cache Manager
39     private EmbeddedCacheManager cacheManager;
40
41     /**
42      * Constructor, set up an Infinispan cache manager.
43      *
44      * @param infinispanDistributorParameters the infinispan distributor parameters
45      * @throws ContextException On errors connecting to Infinispan
46      */
47     public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters)
48                     throws ContextException {
49         LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters);
50
51         setSystemProperties(infinispanDistributorParameters);
52
53         try {
54             LOGGER.debug("starting infinispan cache manager using specified configuration . . .");
55             cacheManager = new DefaultCacheManager(infinispanDistributorParameters.getConfigFile());
56             LOGGER.debug("started infinispan cache manager using specified configuration");
57         } catch (final IOException ioException) {
58             final String errorMessage = "failed to start infinispan cache manager, "
59                             + "no infinispan configuration found on local file system or in classpath, "
60                             + "try setting Infinspan \"configFile\" parameter";
61             LOGGER.error(errorMessage);
62             throw new ContextException(errorMessage, ioException);
63         } catch (final Exception e) {
64             LOGGER.error("failed to start infinispan cache manager using specified configuration", e);
65             throw new ContextException("failed to start infinispan cache manager using specified configuration", e);
66         }
67
68         // Start the cache manager
69         cacheManager.start();
70
71         Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook());
72
73         LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters);
74     }
75
76     /**
77      * Shutdown the manager.
78      */
79     public void shutdown() {
80         if (cacheManager == null) {
81             return;
82         }
83
84         cacheManager.stop();
85         cacheManager = null;
86     }
87
88     /**
89      * Get the cache manager.
90      *
91      * @return the infinispan cache manager
92      */
93     public EmbeddedCacheManager getCacheManager() {
94         return cacheManager;
95     }
96
97     /**
98      * Set system properties used by Infinispan.
99      *
100      * @param infinispanDistributorParameters The parameter values to set are passed as properties
101      */
102     private void setSystemProperties(final InfinispanDistributorParameters infinispanDistributorParameters) {
103         System.setProperty("java.net.preferIPv4Stack",
104                         Boolean.toString(infinispanDistributorParameters.preferIPv4Stack()));
105         System.setProperty("jgroups.bind_addr", infinispanDistributorParameters.getjGroupsBindAddress());
106     }
107
108     /**
109      * Private class to implement the shutdown hook for this infinispan manager.
110      */
111     public class InfinspanManagerShutdownHook extends Thread {
112         /**
113          * {@inheritDoc}.
114          */
115         @Override
116         public void run() {
117             shutdown();
118         }
119     }
120 }