Browse Source

review #41

pull/43/head
Decebal Suiu 10 years ago
parent
commit
56a7ee47ac
  1. 52
      pf4j/src/main/java/ro/fortsoft/pf4j/CompoundPluginRepository.java
  2. 63
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 67
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java
  4. 41
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginRepository.java

52
pf4j/src/main/java/ro/fortsoft/pf4j/CompoundPluginRepository.java

@ -0,0 +1,52 @@
/*
* 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.util.ArrayList;
import java.util.List;
/**
* @author Decebal Suiu
* @author Mário Franco
*/
public class CompoundPluginRepository implements PluginRepository {
private final PluginRepository[] repositories;
public CompoundPluginRepository(PluginRepository... repositories) {
this.repositories = repositories;
}
@Override
public List<File> getPluginArchives() {
List<File> file = new ArrayList<>();
for (PluginRepository repository : repositories) {
file.addAll(repository.getPluginArchives());
}
return file;
}
@Override
public boolean deletePluginArchive(String pluginPath) {
for (PluginRepository repository : repositories) {
if (repository.deletePluginArchive(pluginPath)) {
return true;
}
}
return false;
}
}

63
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java

@ -34,9 +34,6 @@ public class DefaultPluginManager implements PluginManager {
public static final String DEFAULT_PLUGINS_DIRECTORY = "plugins"; public static final String DEFAULT_PLUGINS_DIRECTORY = "plugins";
public static final String DEVELOPMENT_PLUGINS_DIRECTORY = "../plugins"; public static final String DEVELOPMENT_PLUGINS_DIRECTORY = "../plugins";
/**
* The plugins repository.
*/
private File pluginsDirectory; private File pluginsDirectory;
private ExtensionFinder extensionFinder; private ExtensionFinder extensionFinder;
@ -95,20 +92,24 @@ public class DefaultPluginManager implements PluginManager {
private ExtensionFactory extensionFactory; private ExtensionFactory extensionFactory;
private PluginStatusProvider pluginStatusProvider; private PluginStatusProvider pluginStatusProvider;
/**
* The plugins repository.
*/
private PluginRepository pluginRepository;
/** /**
* The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins"). * The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
*/ */
public DefaultPluginManager() { public DefaultPluginManager() {
this.pluginsDirectory = createPluginsDirectory(); this.pluginsDirectory = createPluginsDirectory();
initialize(); initialize();
} }
/** /**
* Constructs DefaultPluginManager which the given plugins directory. * Constructs DefaultPluginManager which the given plugins directory.
* *
* @param pluginsDirectory * @param pluginsDirectory the directory to search for plugins
* the directory to search for plugins
*/ */
public DefaultPluginManager(File pluginsDirectory) { public DefaultPluginManager(File pluginsDirectory) {
this.pluginsDirectory = pluginsDirectory; this.pluginsDirectory = pluginsDirectory;
@ -343,16 +344,13 @@ public class DefaultPluginManager implements PluginManager {
} }
// expand all plugin archives // expand all plugin archives
FileFilter zipFilter = new ZipFileFilter(); List<File> pluginArchives = pluginRepository.getPluginArchives();
File[] zipFiles = pluginsDirectory.listFiles(zipFilter); for (File archiveFile : pluginArchives) {
if (zipFiles != null) { try {
for (File zipFile : zipFiles) { expandPluginArchive(archiveFile);
try { } catch (IOException e) {
expandPluginArchive(zipFile); log.error(e.getMessage(), e);
} catch (IOException e) { }
log.error(e.getMessage(), e);
}
}
} }
// check for no plugins // check for no plugins
@ -447,7 +445,7 @@ public class DefaultPluginManager implements PluginManager {
if (!pluginStatusProvider.disablePlugin(pluginId)) { if (!pluginStatusProvider.disablePlugin(pluginId)) {
return false; return false;
} }
log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion()); log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return true; return true;
@ -508,29 +506,12 @@ public class DefaultPluginManager implements PluginManager {
} }
File pluginFolder = new File(pluginsDirectory, pluginWrapper.getPluginPath()); 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()) { if (pluginFolder.exists()) {
FileUtils.delete(pluginFolder); FileUtils.delete(pluginFolder);
} }
if (pluginZip != null && pluginZip.exists()) {
FileUtils.delete(pluginZip); pluginRepository.deletePluginArchive(pluginWrapper.getPluginPath());
}
return true; return true;
} }
@ -651,8 +632,11 @@ public class DefaultPluginManager implements PluginManager {
return new DefaultPluginStatusProvider(pluginsDirectory); return new DefaultPluginStatusProvider(pluginsDirectory);
} }
protected boolean isPluginDisabled(String pluginId) { protected PluginRepository createPluginRepository() {
return new DefaultPluginRepository(pluginsDirectory, new ZipFileFilter());
}
protected boolean isPluginDisabled(String pluginId) {
return pluginStatusProvider.isPluginDisabled(pluginId); return pluginStatusProvider.isPluginDisabled(pluginId);
} }
@ -729,11 +713,12 @@ public class DefaultPluginManager implements PluginManager {
pluginDescriptorFinder = createPluginDescriptorFinder(); pluginDescriptorFinder = createPluginDescriptorFinder();
extensionFinder = createExtensionFinder(); extensionFinder = createExtensionFinder();
pluginStatusProvider = createPluginStatusProvider(); pluginStatusProvider = createPluginStatusProvider();
pluginRepository = createPluginRepository();
System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
} }
private PluginWrapper loadPluginDirectory(File pluginDirectory) throws PluginException { private PluginWrapper loadPluginDirectory(File pluginDirectory) throws PluginException {
// try to load the plugin // try to load the plugin
String pluginName = pluginDirectory.getName(); String pluginName = pluginDirectory.getName();
String pluginPath = "/".concat(pluginName); String pluginPath = "/".concat(pluginName);

67
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java

@ -0,0 +1,67 @@
/*
* 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 ro.fortsoft.pf4j.util.FileUtils;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author Decebal Suiu
* @author Mário Franco
*/
public class DefaultPluginRepository implements PluginRepository {
private final File directory;
private final FileFilter filter;
public DefaultPluginRepository(File directory, FileFilter filter) {
this.directory = directory;
this.filter = filter;
}
@Override
public List<File> getPluginArchives() {
File[] files = directory.listFiles(filter);
return (files != null) ? Arrays.asList(files) : Collections.<File>emptyList();
}
@Override
public boolean deletePluginArchive(String pluginPath) {
File[] files = directory.listFiles(filter);
if (files != null) {
File pluginArchive = null;
// strip prepended "/" from the plugin path
String dirName = pluginPath.substring(1);
// find the zip file that matches the plugin path
for (File archive : files) {
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;
}
}

41
pf4j/src/main/java/ro/fortsoft/pf4j/PluginRepository.java

@ -0,0 +1,41 @@
/*
* 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.util.List;
/**
* Directory whose contents are .zip files used as plugins.
*
* @author Decebal Suiu
* @author Mário Franco
*/
public interface PluginRepository {
/**
* List all plugin archive filed.
*
* @return a list of files
*/
public List<File> getPluginArchives();
/**
* Removes a plugin from the repository.
*
* @param pluginPath the plugin path
* @return true if deleted
*/
public boolean deletePluginArchive(String pluginPath);
}
Loading…
Cancel
Save