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;
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;
31 * The Class InfinispanManager holds the Infinispan cache manager for a JVM.
33 public class InfinispanManager {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(InfinispanManager.class);
37 // The Infinispan Cache Manager
38 private EmbeddedCacheManager cacheManager;
41 * Constructor, set up an Infinispan cache manager.
43 * @param infinispanDistributorParameters the infinispan distributor parameters
44 * @throws ContextException On errors connecting to Infinispan
46 public InfinispanManager(final InfinispanDistributorParameters infinispanDistributorParameters)
47 throws ContextException {
48 LOGGER.entry("Creating Infinispan Manager: " + infinispanDistributorParameters);
50 setSystemProperties(infinispanDistributorParameters);
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);
67 // Start the cache manager
70 Runtime.getRuntime().addShutdownHook(new InfinspanManagerShutdownHook());
72 LOGGER.exit("Created Infinispan Manager: " + infinispanDistributorParameters);
76 * Shutdown the manager.
78 public void shutdown() {
79 if (cacheManager == null) {
88 * Get the cache manager.
90 * @return the infinispan cache manager
92 public EmbeddedCacheManager getCacheManager() {
97 * Set system properties used by Infinispan.
99 * @param infinispanDistributorParameters The parameter values to set are passed as properties
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());
108 * Private class to implement the shutdown hook for this infinispan manager.
110 public class InfinspanManagerShutdownHook extends Thread {