Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2010-2014 Ning, Inc.
* Copyright 2014-2020 Groupon, Inc
* Copyright 2020-2020 Equinix, Inc
* Copyright 2014-2020 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.billing.osgi.pluginconf;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.killbill.billing.osgi.api.config.PluginConfig;
import org.killbill.billing.osgi.api.config.PluginJavaConfig;
import org.killbill.billing.osgi.api.config.PluginLanguage;
import org.killbill.billing.osgi.config.OSGIConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PluginFinder {
static final String SELECTED_VERSION_LINK_NAME = "SET_DEFAULT";
static final String TMP_DIR_NAME = "tmp";
static final String DISABLED_FILE_NAME = "disabled.txt"; // See similar definition in KillbillActivatorBase
static final String IDENTIFIERS_FILE_NAME = "plugin_identifiers.json";
private final Logger logger = LoggerFactory.getLogger(PluginFinder.class);
private final OSGIConfig osgiConfig;
private final Map> allPlugins;
private final Map identifiers;
private final ObjectMapper mapper;
@Inject
public PluginFinder(final OSGIConfig osgiConfig) {
this.osgiConfig = osgiConfig;
this.mapper = new ObjectMapper();
this.allPlugins = new HashMap>();
this.identifiers = new HashMap();
}
public List getLatestJavaPlugins() throws PluginConfigException, IOException {
return getLatestPluginForLanguage(PluginLanguage.JAVA);
}
public List getVersionsForPlugin(final String lookupName, @Nullable final String version) throws PluginConfigException, IOException {
loadPluginsIfRequired(false);
final List result = new LinkedList();
for (final Entry> entry : allPlugins.entrySet()) {
if (entry.getKey().equals(lookupName)) {
for (final PluginConfig cur : entry.getValue()) {
if (version == null || cur.getVersion().equals(version)) {
result.add(cur);
}
}
}
}
return result;
}
public String getPluginVersionSelectedForStart(final String pluginName) {
final LinkedList pluginConfigs = allPlugins.get(pluginName);
return pluginConfigs != null && !pluginConfigs.isEmpty() ? pluginConfigs.get(0).getVersion() : null;
}
public Map> getAllPlugins() {
return Map.copyOf(allPlugins);
}
public void reloadPlugins() throws PluginConfigException, IOException {
loadPluginsIfRequired(true);
}
public PluginIdentifier resolvePluginKey(final String pluginKey) {
return identifiers.get(pluginKey);
}
private List getLatestPluginForLanguage(final PluginLanguage pluginLanguage) throws PluginConfigException, IOException {
loadPluginsIfRequired(false);
final List result = new LinkedList();
for (final LinkedList plugins : allPlugins.values()) {
@SuppressWarnings("unchecked") final T plugin = (T) plugins.get(0);
if (pluginLanguage != plugin.getPluginLanguage()) {
continue;
}
result.add(plugin);
}
return result;
}
private void loadPluginsIfRequired(final boolean reloadPlugins) throws PluginConfigException, IOException {
synchronized (allPlugins) {
if (!reloadPlugins && !allPlugins.isEmpty()) {
return;
}
allPlugins.clear();
readPluginIdentifiers();
loadPluginsForLanguage(PluginLanguage.JAVA);
// Order for each plugin based on DefaultPluginConfig sort method:
// (order first based on SELECTED_VERSION_LINK_NAME and then decreasing version number)
//
for (final Entry> entry : allPlugins.entrySet()) {
final String pluginName = entry.getKey();
final LinkedList versionsForPlugin = entry.getValue();
// If all entries were disabled or the SELECTED_VERSION_LINK_NAME was disabled we end up with nothing, it is as if the plugin did not exist
if (versionsForPlugin.isEmpty()) {
allPlugins.remove(pluginName);
continue;
}
Collections.sort(versionsForPlugin);
// Make sure first entry is set with isSelectedForStart = true
final PluginConfig firstValue = versionsForPlugin.removeFirst();
if (firstValue.getPluginLanguage() != PluginLanguage.JAVA) {
throw new UnsupportedOperationException("Non-Java plugins aren't supported anymore");
}
final PluginConfig newFirstValue = new DefaultPluginJavaConfig((DefaultPluginJavaConfig) firstValue, true);
versionsForPlugin.addFirst(newFirstValue);
}
}
}
private void readPluginIdentifiers() {
final String identifierFileName = osgiConfig.getRootInstallationDir() + "/plugins/" + IDENTIFIERS_FILE_NAME;
final File identifierFile = new File(identifierFileName);
if (!identifierFile.exists() || !identifierFile.isFile()) {
logger.warn("File non existent: Skipping parsing of " + IDENTIFIERS_FILE_NAME);
return;
}
try {
identifiers.clear();
final Map map = mapper.readValue(identifierFile, new TypeReference