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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.distribution.infinispan;
23 import java.io.IOException;
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;
32 * The Class InfinispanManager holds the Infinispan cache manager for a JVM.
34 public class InfinispanManager {
35 // Logger for this class
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class);
38 // The Infinispan Cache Manager
39 private EmbeddedCacheManager cacheManager;
42 * Constructor, set up an Infinispan cache manager.
44 * @param infinispanDistributorParameters the infinispan distributor parameters
45 * @throws ContextException On errors connecting to Infinispan
47 public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters)
48 throws ContextException {
49 LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters);
51 setSystemProperties(infinispanDistributorParameters);
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);
68 // Start the cache manager
71 Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook());
73 LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters);
77 * Shutdown the manager.
79 public void shutdown() {
80 if (cacheManager == null) {
89 * Get the cache manager.
91 * @return the infinispan cache manager
93 public EmbeddedCacheManager getCacheManager() {
98 * Set system properties used by Infinispan.
100 * @param infinispanDistributorParameters The parameter values to set are passed as properties
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());
109 * Private class to implement the shutdown hook for this infinispan manager.
111 public class InfinspanManagerShutdownHook extends Thread {