org.killbill.billing.osgi.pluginconf.PluginFinder Maven / Gradle / Ivy
/*
* Copyright 2010-2013 Ning, Inc.
* Copyright 2014 Groupon, Inc
* Copyright 2014 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.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
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.api.config.PluginRubyConfig;
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 getLatestRubyPlugins() throws PluginConfigException, IOException {
return getLatestPluginForLanguage(PluginLanguage.RUBY);
}
public List getVersionsForPlugin(final String lookupName, @Nullable final String version) throws PluginConfigException, IOException {
loadPluginsIfRequired(false);
final List result = new LinkedList();
for (final String pluginName : allPlugins.keySet()) {
if (pluginName.equals(lookupName)) {
for (final PluginConfig cur : allPlugins.get(pluginName)) {
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 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 String pluginName : allPlugins.keySet()) {
@SuppressWarnings("unchecked") final T plugin = (T) allPlugins.get(pluginName).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.RUBY);
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)
//
final Iterator pluginNamesIterator = allPlugins.keySet().iterator();
while (pluginNamesIterator.hasNext()) {
final String pluginName = pluginNamesIterator.next();
final LinkedList versionsForPlugin = allPlugins.get(pluginName);
// 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()) {
pluginNamesIterator.remove();
continue;
}
Collections.sort(versionsForPlugin);
// Make sure first entry is set with isSelectedForStart = true
final PluginConfig firstValue = versionsForPlugin.removeFirst();
final PluginConfig newFirstValue = firstValue.getPluginLanguage() == PluginLanguage.RUBY ?
new DefaultPluginRubyConfig((DefaultPluginRubyConfig) firstValue, true) :
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy