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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.distribution.infinispan;
24 import java.io.IOException;
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;
33 * The Class InfinispanManager holds the Infinispan cache manager for a JVM.
35 public class InfinispanManager {
36 // Logger for this class
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class);
39 // The Infinispan Cache Manager
41 private EmbeddedCacheManager cacheManager;
44 * Constructor, set up an Infinispan cache manager.
46 * @param infinispanDistributorParameters the infinispan distributor parameters
47 * @throws ContextException On errors connecting to Infinispan
49 public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters)
50 throws ContextException {
51 LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters);
53 setSystemProperties(infinispanDistributorParameters);
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);
70 // Start the cache manager
73 Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook());
75 LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters);
79 * Shutdown the manager.
81 public void shutdown() {
82 if (cacheManager == null) {
91 * Set system properties used by Infinispan.
93 * @param infinispanDistributorParameters The parameter values to set are passed as properties
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());
102 * Private class to implement the shutdown hook for this infinispan manager.
104 public class InfinspanManagerShutdownHook extends Thread {