Changes for checkstyle 8.32
[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 import org.infinispan.manager.DefaultCacheManager;
25 import org.infinispan.manager.EmbeddedCacheManager;
26 import org.onap.policy.apex.context.ContextException;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * The Class InfinispanManager holds the Infinispan cache manager for a JVM.
32  */
33 public class InfinispanManager {
34     // Logger for this class
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class);
36
37     // The Infinispan Cache Manager
38     private EmbeddedCacheManager cacheManager;
39
40     /**
41      * Constructor, set up an Infinispan cache manager.
42      *
43      * @param infinispanDistributorParameters the infinispan distributor parameters
44      * @throws ContextException On errors connecting to Infinispan
45      */
46     public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters)
47                     throws ContextException {
48         LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters);
49
50         setSystemProperties(infinispanDistributorParameters);
51
52         try {
53             LOGGER.debug("starting infinispan cache manager using specified configuration . . .");
54             cacheManager = new DefaultCacheManager(infinispanDistributorParameters.getConfigFile());
55             LOGGER.debug("started infinispan cache manager using specified configuration");
56         } catch (final IOException ioException) {
57             final String errorMessage = "failed to start infinispan cache manager, "
58                             + "no infinispan configuration found on local file system or in classpath, "
59                             + "try setting Infinspan \"configFile\" parameter";
60             LOGGER.error(errorMessage);
61             throw new ContextException(errorMessage, ioException);
62         } catch (final Exception e) {
63             LOGGER.error("failed to start infinispan cache manager using specified configuration", e);
64             throw new ContextException("failed to start infinispan cache manager using specified configuration", e);
65         }
66
67         // Start the cache manager
68         cacheManager.start();
69
70         Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook());
71
72         LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters);
73     }
74
75     /**
76      * Shutdown the manager.
77      */
78     public void shutdown() {
79         if (cacheManager == null) {
80             return;
81         }
82
83         cacheManager.stop();
84         cacheManager = null;
85     }
86
87     /**
88      * Get the cache manager.
89      *
90      * @return the infinispan cache manager
91      */
92     public EmbeddedCacheManager getCacheManager() {
93         return cacheManager;
94     }
95
96     /**
97      * Set system properties used by Infinispan.
98      *
99      * @param infinispanDistributorParameters The parameter values to set are passed as properties
100      */
101     private void setSystemProperties(final InfinispanDistributorParameters infinispanDistributorParameters) {
102         System.setProperty("java.net.preferIPv4Stack",
103                         Boolean.toString(infinispanDistributorParameters.preferIPv4Stack()));
104         System.setProperty("jgroups.bind_addr", infinispanDistributorParameters.getjGroupsBindAddress());
105     }
106
107     /**
108      * Private class to implement the shutdown hook for this infinispan manager.
109      */
110     public class InfinspanManagerShutdownHook extends Thread {
111         /**
112          * {@inheritDoc}.
113          */
114         @Override
115         public void run() {
116             shutdown();
117         }
118     }
119 }