
com.mycila.plugin.spi.DefaultPluginCache Maven / Gradle / Ivy
/**
* Copyright (C) 2008 Mathieu Carbou
*
* Licensed 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 com.mycila.plugin.spi;
import com.mycila.plugin.api.Plugin;
import com.mycila.plugin.api.PluginBinding;
import com.mycila.plugin.api.PluginCache;
import com.mycila.plugin.api.PluginLoader;
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* @author Mathieu Carbou ([email protected])
*/
final class DefaultPluginCache implements PluginCache {
final SortedMap> plugins = new TreeMap>();
final PluginLoader loader;
boolean loaded;
DefaultPluginCache(PluginLoader loader) {
this.loader = loader;
}
public void clear() {
synchronized (plugins) {
plugins.clear();
loaded = false;
}
}
public void registerPlugin(String name, T plugin) {
if (PluginUtils.isEmpty(name)) {
throw new IllegalArgumentException("Not a valid plugin name: must not be empty");
}
plugins.put(name, new Binding(name).withPlugin(plugin));
}
public void registerPlugins(Map plugins) {
for (Map.Entry entry : plugins.entrySet()) {
registerPlugin(entry.getKey(), entry.getValue());
}
}
public void removePlugins(String... pluginNames) {
for (String name : pluginNames) {
this.plugins.remove(name);
}
}
public SortedMap> getBindings() {
if (!loaded) {
synchronized (plugins) {
if (!loaded) {
for (PluginBinding binding : loader.loadPlugins()) {
plugins.put(binding.getName(), binding);
}
loaded = true;
}
}
}
return Collections.unmodifiableSortedMap(plugins);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy