Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added plugin archive source abstraction #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 40 additions & 35 deletions pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class DefaultPluginManager implements PluginManager {
/**
* The plugins repository.
*/
private PluginSource source;
private File pluginsDirectory;

private ExtensionFinder extensionFinder;
Expand Down Expand Up @@ -94,24 +95,39 @@ public class DefaultPluginManager implements PluginManager {
private PluginFactory pluginFactory;
private ExtensionFactory extensionFactory;
private PluginStatusProvider pluginStatusProvider;
private FileFilter fileFilter;

/**
* The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
*/
public DefaultPluginManager() {
this.pluginsDirectory = createPluginsDirectory();
this.pluginsDirectory = createPluginsDirectory();
this.source = new PluginDirectorySource(this.pluginsDirectory);

initialize();
initialize();
}

/**
* Constructs DefaultPluginManager which the given plugins directory.
*
* @param pluginsDirectory
* the directory to search for plugins
* @param pluginsDirectory the directory to search for plugins
*/
public DefaultPluginManager(File pluginsDirectory) {
this.pluginsDirectory = pluginsDirectory;
this.source = new PluginDirectorySource(this.pluginsDirectory);

initialize();
}

/**
* Constructs DefaultPluginManager which the given plugins directory.
*
* @param pluginsDirectory the directory to search for plugins
* @param source the plugins archives source
*/
public DefaultPluginManager(File pluginsDirectory, PluginSource source) {
this.pluginsDirectory = pluginsDirectory;
this.source = source;

initialize();
}
Expand Down Expand Up @@ -343,16 +359,13 @@ public void loadPlugins() {
}

// expand all plugin archives
FileFilter zipFilter = new ZipFileFilter();
File[] zipFiles = pluginsDirectory.listFiles(zipFilter);
if (zipFiles != null) {
for (File zipFile : zipFiles) {
try {
expandPluginArchive(zipFile);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
List<File> pluginArchives = source.getPluginArchives(fileFilter);
for (File archiveFile : pluginArchives) {
try {
expandPluginArchive(archiveFile);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}

// check for no plugins
Expand Down Expand Up @@ -447,7 +460,7 @@ public boolean disablePlugin(String pluginId) {
if (!pluginStatusProvider.disablePlugin(pluginId)) {
return false;
}

log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());

return true;
Expand Down Expand Up @@ -508,29 +521,12 @@ public boolean deletePlugin(String pluginId) {
}

File pluginFolder = new File(pluginsDirectory, pluginWrapper.getPluginPath());
File pluginZip = null;

FileFilter zipFilter = new ZipFileFilter();
File[] zipFiles = pluginsDirectory.listFiles(zipFilter);
if (zipFiles != null) {
// strip prepended / from the plugin path
String dirName = pluginWrapper.getPluginPath().substring(1);
// find the zip file that matches the plugin path
for (File zipFile : zipFiles) {
String name = zipFile.getName().substring(0, zipFile.getName().lastIndexOf('.'));
if (name.equals(dirName)) {
pluginZip = zipFile;
break;
}
}
}

if (pluginFolder.exists()) {
FileUtils.delete(pluginFolder);
}
if (pluginZip != null && pluginZip.exists()) {
FileUtils.delete(pluginZip);
}

source.deletePluginArchive(pluginWrapper, fileFilter);

return true;
}
Expand Down Expand Up @@ -609,7 +605,15 @@ public Version getVersion() {
return (version != null) ? Version.createVersion(version) : Version.ZERO;
}

/**
public FileFilter getFileFilter() {
return fileFilter;
}

public void setZipFilter(FileFilter fileFilter) {
this.fileFilter = fileFilter;
}

/**
* Add the possibility to override the PluginDescriptorFinder.
* By default if getRuntimeMode() returns RuntimeMode.DEVELOPMENT than a
* PropertiesPluginDescriptorFinder is returned else this method returns
Expand Down Expand Up @@ -712,6 +716,7 @@ protected ExtensionFactory createExtensionFactory() {
}

private void initialize() {
fileFilter = new ZipFileFilter();
plugins = new HashMap<String, PluginWrapper>();
pluginClassLoaders = new HashMap<String, PluginClassLoader>();
pathToIdMap = new HashMap<String, String>();
Expand Down
65 changes: 65 additions & 0 deletions pf4j/src/main/java/ro/fortsoft/pf4j/PluginDirectorySource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2012 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
* the License. You may obtain a copy of the License in the LICENSE file, or 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 ro.fortsoft.pf4j;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import ro.fortsoft.pf4j.util.FileUtils;

/**
* @author Decebal Suiu
* @author Mário Franco
*/
public class PluginDirectorySource implements PluginSource {

private final File directory;

public PluginDirectorySource(File directory) {
this.directory = directory;
}

@Override
public List<File> getPluginArchives(FileFilter filter) {
File[] listFiles = directory.listFiles(filter);
if (listFiles != null) {
return Arrays.asList(listFiles);
}
return new ArrayList<>();
}

@Override
public boolean deletePluginArchive(PluginWrapper pluginWrapper, FileFilter filter) {
File[] listFiles = directory.listFiles(filter);
if (listFiles != null) {
File pluginArchive = null;
// strip prepended / from the plugin path
String dirName = pluginWrapper.getPluginPath().substring(1);
// find the zip file that matches the plugin path
for (File archive : listFiles) {
String name = archive.getName().substring(0, archive.getName().lastIndexOf('.'));
if (name.equals(dirName)) {
pluginArchive = archive;
break;
}
}
if (pluginArchive != null && pluginArchive.exists()) {
return FileUtils.delete(pluginArchive);
}
}
return false;
}

}
51 changes: 51 additions & 0 deletions pf4j/src/main/java/ro/fortsoft/pf4j/PluginMultipleSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2012 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
* the License. You may obtain a copy of the License in the LICENSE file, or 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 ro.fortsoft.pf4j;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

/**
* @author Decebal Suiu
* @author Mário Franco
*/
public class PluginMultipleSource implements PluginSource {

private final PluginSource[] sources;

public PluginMultipleSource(PluginSource... sources) {
this.sources = sources;
}

@Override
public List<File> getPluginArchives(FileFilter filter) {
List<File> listFiles = new ArrayList<>();
for (PluginSource source : sources) {
listFiles.addAll(source.getPluginArchives(filter));
}
return listFiles;
}

@Override
public boolean deletePluginArchive(PluginWrapper pluginWrapper, FileFilter filter) {
for (PluginSource source : sources) {
if (source.deletePluginArchive(pluginWrapper, filter)) {
return true;
}
}
return false;
}

}
39 changes: 39 additions & 0 deletions pf4j/src/main/java/ro/fortsoft/pf4j/PluginSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2012 Decebal Suiu
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
* the License. You may obtain a copy of the License in the LICENSE file, or 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 ro.fortsoft.pf4j;

import java.io.File;
import java.io.FileFilter;
import java.util.List;

/**
* @author Decebal Suiu
* @author Mário Franco
*/
public interface PluginSource {

/**
* List all plugin archive filed
* @param filter the file filter
* @return a list of files
*/
public List<File> getPluginArchives(FileFilter filter);

/**
* Removes a plugin from the source
* @param pluginWrapper the plugin information
* @param filter the file filter
* @return true if deleted
*/
public boolean deletePluginArchive(PluginWrapper pluginWrapper, FileFilter filter);
}