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