Browse Source

add support hidden filter for plugins directory (.svn, .cvs, ...)

pull/3/head
Decebal Suiu 11 years ago
parent
commit
c6cec30c88
  1. 89
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  2. 33
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginLoader.java
  3. 76
      pf4j/src/main/java/ro/fortsoft/pf4j/util/AndFileFilter.java
  4. 13
      pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectoryFileFilter.java
  5. 17
      pf4j/src/main/java/ro/fortsoft/pf4j/util/ExtensionFileFilter.java
  6. 30
      pf4j/src/main/java/ro/fortsoft/pf4j/util/HiddenFilter.java
  7. 4
      pf4j/src/main/java/ro/fortsoft/pf4j/util/JarFileFilter.java
  8. 36
      pf4j/src/main/java/ro/fortsoft/pf4j/util/NotFileFilter.java
  9. 4
      pf4j/src/main/java/ro/fortsoft/pf4j/util/ZipFileFilter.java

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

@ -13,7 +13,7 @@
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -24,11 +24,14 @@ import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.AndFileFilter;
import ro.fortsoft.pf4j.util.CompoundClassLoader; import ro.fortsoft.pf4j.util.CompoundClassLoader;
import ro.fortsoft.pf4j.util.DirectoryFilter; import ro.fortsoft.pf4j.util.DirectoryFileFilter;
import ro.fortsoft.pf4j.util.FileUtils; import ro.fortsoft.pf4j.util.FileUtils;
import ro.fortsoft.pf4j.util.HiddenFilter;
import ro.fortsoft.pf4j.util.NotFileFilter;
import ro.fortsoft.pf4j.util.Unzip; import ro.fortsoft.pf4j.util.Unzip;
import ro.fortsoft.pf4j.util.ZipFilter; import ro.fortsoft.pf4j.util.ZipFileFilter;
/** /**
* Default implementation of the PluginManager interface. * Default implementation of the PluginManager interface.
@ -200,9 +203,9 @@ public class DefaultPluginManager implements PluginManager {
} }
// expand all plugin archives // expand all plugin archives
FilenameFilter zipFilter = new ZipFilter(); FileFilter zipFilter = new ZipFileFilter();
String[] zipFiles = pluginsDirectory.list(zipFilter); File[] zipFiles = pluginsDirectory.listFiles(zipFilter);
for (String zipFile : zipFiles) { for (File zipFile : zipFiles) {
try { try {
expandPluginArchive(zipFile); expandPluginArchive(zipFile);
} catch (IOException e) { } catch (IOException e) {
@ -211,15 +214,18 @@ public class DefaultPluginManager implements PluginManager {
} }
// check for no plugins // check for no plugins
FilenameFilter directoryFilter = new DirectoryFilter(); List<FileFilter> filterList = new ArrayList<FileFilter>();
String[] directories = pluginsDirectory.list(directoryFilter); filterList.add(new DirectoryFileFilter());
filterList.add(new NotFileFilter(createHiddenPluginFilter()));
FileFilter pluginsFilter = new AndFileFilter(filterList);
File[] directories = pluginsDirectory.listFiles(pluginsFilter);
if (directories.length == 0) { if (directories.length == 0) {
log.info("No plugins"); log.info("No plugins");
return; return;
} }
// load any plugin from plugins directory // load any plugin from plugins directory
for (String directory : directories) { for (File directory : directories) {
try { try {
loadPlugin(directory); loadPlugin(directory);
} catch (PluginException e) { } catch (PluginException e) {
@ -268,15 +274,36 @@ public class DefaultPluginManager implements PluginManager {
return null; return null;
} }
private void loadPlugin(String fileName) throws PluginException { /**
// test for plugin directory * Add the possibility to override the PluginDescriptorFinder.
File pluginDirectory = new File(pluginsDirectory, fileName); */
if (!pluginDirectory.isDirectory()) { protected PluginDescriptorFinder createPluginDescriptorFinder() {
return; return new DefaultPluginDescriptorFinder();
} }
/**
* Add the possibility to override the ExtensionFinder.
*/
protected ExtensionFinder createExtensionFinder() {
return new DefaultExtensionFinder(compoundClassLoader);
}
protected boolean isPluginDisabled(String pluginId) {
if (enabledPlugins.isEmpty()) {
return disabledPlugins.contains(pluginId);
}
return !enabledPlugins.contains(pluginId);
}
protected FileFilter createHiddenPluginFilter() {
return new HiddenFilter();
}
private void loadPlugin(File pluginDirectory) throws PluginException {
// try to load the plugin // try to load the plugin
String pluginPath = "/".concat(fileName); String pluginName = pluginDirectory.getName();
String pluginPath = "/".concat(pluginName);
// test for plugin duplication // test for plugin duplication
if (plugins.get(pathToIdMap.get(pluginPath)) != null) { if (plugins.get(pathToIdMap.get(pluginPath)) != null) {
@ -318,37 +345,15 @@ public class DefaultPluginManager implements PluginManager {
pluginClassLoaders.put(pluginId, pluginClassLoader); pluginClassLoaders.put(pluginId, pluginClassLoader);
} }
/** private void expandPluginArchive(File pluginArchiveFile) throws IOException {
* Add the possibility to override the PluginDescriptorFinder. String fileName = pluginArchiveFile.getName();
*/
protected PluginDescriptorFinder createPluginDescriptorFinder() {
return new DefaultPluginDescriptorFinder();
}
/**
* Add the possibility to override the ExtensionFinder.
*/
protected ExtensionFinder createExtensionFinder() {
return new DefaultExtensionFinder(compoundClassLoader);
}
protected boolean isPluginDisabled(String pluginId) {
if (enabledPlugins.isEmpty()) {
return disabledPlugins.contains(pluginId);
}
return !enabledPlugins.contains(pluginId);
}
private void expandPluginArchive(String fileName) throws IOException {
File pluginArchiveFile = new File(pluginsDirectory, fileName);
long pluginArchiveDate = pluginArchiveFile.lastModified(); long pluginArchiveDate = pluginArchiveFile.lastModified();
String pluginName = fileName.substring(0, fileName.length() - 4); String pluginName = fileName.substring(0, fileName.length() - 4);
File pluginDirectory = new File(pluginsDirectory, pluginName); File pluginDirectory = new File(pluginsDirectory, pluginName);
// check if exists directory or the '.zip' file is "newer" than directory // check if exists directory or the '.zip' file is "newer" than directory
if (!pluginDirectory.exists() || (pluginArchiveDate > pluginDirectory.lastModified())) { if (!pluginDirectory.exists() || (pluginArchiveDate > pluginDirectory.lastModified())) {
log.debug("Expand plugin archive '{}' in '{}'", pluginArchiveFile, pluginDirectory); log.debug("Expand plugin archive '{}' in '{}'", pluginArchiveFile, pluginDirectory);
// create directorie for plugin // create directory for plugin
pluginDirectory.mkdirs(); pluginDirectory.mkdirs();
// expand '.zip' file // expand '.zip' file

33
pf4j/src/main/java/ro/fortsoft/pf4j/PluginLoader.java

@ -13,15 +13,15 @@
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FileFilter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Vector; import java.util.Vector;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.DirectoryFilter; import ro.fortsoft.pf4j.util.DirectoryFileFilter;
import ro.fortsoft.pf4j.util.JarFilter; import ro.fortsoft.pf4j.util.JarFileFilter;
/** /**
* Load all informations needed by a plugin. * Load all informations needed by a plugin.
@ -77,20 +77,20 @@ class PluginLoader {
return loadClasses() && loadJars(); return loadClasses() && loadJars();
} }
private void getJars(Vector<String> v, File file) { private void getJars(Vector<File> bucket, File file) {
FilenameFilter jarFilter = new JarFilter(); FileFilter jarFilter = new JarFileFilter();
FilenameFilter directoryFilter = new DirectoryFilter(); FileFilter directoryFilter = new DirectoryFileFilter();
if (file.exists() && file.isDirectory() && file.isAbsolute()) { if (file.exists() && file.isDirectory() && file.isAbsolute()) {
String[] jars = file.list(jarFilter); File[] jars = file.listFiles(jarFilter);
for (int i = 0; (jars != null) && (i < jars.length); ++i) { for (int i = 0; (jars != null) && (i < jars.length); ++i) {
v.addElement(jars[i]); bucket.addElement(jars[i]);
} }
String[] directoryList = file.list(directoryFilter); File[] directories = file.listFiles(directoryFilter);
for (int i = 0; (directoryList != null) && (i < directoryList.length); ++i) { for (int i = 0; (directories != null) && (i < directories.length); ++i) {
File directory = new File(file, directoryList[i]); File directory = directories[i];
getJars(v, directory); getJars(bucket, directory);
} }
} }
} }
@ -122,13 +122,12 @@ class PluginLoader {
// make 'jarDirectory' absolute // make 'jarDirectory' absolute
libDirectory = libDirectory.getAbsoluteFile(); libDirectory = libDirectory.getAbsoluteFile();
Vector<String> jars = new Vector<String>(); Vector<File> jars = new Vector<File>();
getJars(jars, libDirectory); getJars(jars, libDirectory);
for (String jar : jars) { for (File jar : jars) {
File jarFile = new File(libDirectory, jar);
try { try {
pluginClassLoader.addURL(jarFile.toURI().toURL()); pluginClassLoader.addURL(jar.toURI().toURL());
log.debug("Added '{}' to the class loader path", jarFile); log.debug("Added '{}' to the class loader path", jar);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
log.error(e.getMessage(), e); log.error(e.getMessage(), e);

76
pf4j/src/main/java/ro/fortsoft/pf4j/util/AndFileFilter.java

@ -0,0 +1,76 @@
/*
* Copyright 2013 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.util;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* This filter providing conditional AND logic across a list of
* file filters. This filter returns <code>true</code> if all filters in the
* list return <code>true</code>. Otherwise, it returns <code>false</code>.
* Checking of the file filter list stops when the first filter returns
* <code>false</code>.
*
* @author Decebal Suiu
*/
public class AndFileFilter implements FileFilter {
/** The list of file filters. */
private List<FileFilter> fileFilters;
public AndFileFilter() {
this.fileFilters = new ArrayList<FileFilter>();
}
public AndFileFilter(List<FileFilter> fileFilters) {
this.fileFilters = new ArrayList<FileFilter>(fileFilters);
}
public void addFileFilter(FileFilter fileFilter) {
fileFilters.add(fileFilter);
}
public List<FileFilter> getFileFilters() {
return Collections.unmodifiableList(fileFilters);
}
public boolean removeFileFilter(FileFilter fileFilter) {
return fileFilters.remove(fileFilter);
}
public void setFileFilters(List<FileFilter> fileFilters) {
this.fileFilters = new ArrayList<FileFilter>(fileFilters);
}
@Override
public boolean accept(File file) {
if (this.fileFilters.size() == 0) {
return false;
}
for (Iterator<FileFilter> iter = this.fileFilters.iterator(); iter.hasNext();) {
FileFilter fileFilter = (FileFilter) iter.next();
if (!fileFilter.accept(file)) {
return false;
}
}
return true;
}
}

13
pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectoryFilter.java → pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectoryFileFilter.java

@ -14,22 +14,17 @@ package ro.fortsoft.pf4j.util;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FilenameFilter;
/** /**
* Filter accepts files that are directories.
*
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class DirectoryFilter implements FileFilter, FilenameFilter { public class DirectoryFileFilter implements FileFilter {
/** @Override
* Accepts any file ending in .jar. The case of the filename is ignored.
*/
public boolean accept(File file) { public boolean accept(File file) {
return file.isDirectory(); return file.isDirectory();
} }
public boolean accept(File dir, String name) {
return accept(new File(dir, name));
}
} }

17
pf4j/src/main/java/ro/fortsoft/pf4j/util/ExtensionFilter.java → pf4j/src/main/java/ro/fortsoft/pf4j/util/ExtensionFileFilter.java

@ -13,30 +13,25 @@
package ro.fortsoft.pf4j.util; package ro.fortsoft.pf4j.util;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FileFilter;
/** /**
* Filter accepts any file ending in extension. The case of the filename is ignored.
*
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class ExtensionFilter implements FilenameFilter { public class ExtensionFileFilter implements FileFilter {
private String extension; private String extension;
public ExtensionFilter(String extension) { public ExtensionFileFilter(String extension) {
this.extension = extension; this.extension = extension;
} }
/** @Override
* Accepts any file ending in extension. The case of the filename is ignored.
*/
public boolean accept(File file) { public boolean accept(File file) {
// perform a case insensitive check. // perform a case insensitive check.
return file.getName().toUpperCase().endsWith(extension.toUpperCase()); return file.getName().toUpperCase().endsWith(extension.toUpperCase());
} }
@Override
public boolean accept(File dir, String name) {
return accept(new File(dir, name));
}
} }

30
pf4j/src/main/java/ro/fortsoft/pf4j/util/HiddenFilter.java

@ -0,0 +1,30 @@
/*
* Copyright 2013 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.util;
import java.io.File;
import java.io.FileFilter;
/**
* Filter that only accepts hidden files.
*
* @author decebal.suiu
*/
public class HiddenFilter implements FileFilter {
@Override
public boolean accept(File file) {
return file.isHidden();
}
}

4
pf4j/src/main/java/ro/fortsoft/pf4j/util/JarFilter.java → pf4j/src/main/java/ro/fortsoft/pf4j/util/JarFileFilter.java

@ -18,14 +18,14 @@ package ro.fortsoft.pf4j.util;
* *
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class JarFilter extends ExtensionFilter { public class JarFileFilter extends ExtensionFileFilter {
/** /**
* The extension that this filter will search for. * The extension that this filter will search for.
*/ */
private static final String JAR_EXTENSION = ".JAR"; private static final String JAR_EXTENSION = ".JAR";
public JarFilter() { public JarFileFilter() {
super(JAR_EXTENSION); super(JAR_EXTENSION);
} }

36
pf4j/src/main/java/ro/fortsoft/pf4j/util/NotFileFilter.java

@ -0,0 +1,36 @@
/*
* Copyright 2013 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.util;
import java.io.File;
import java.io.FileFilter;
/**
* This filter produces a logical NOT of the filters specified.
*
* @author Decebal Suiu
*/
public class NotFileFilter implements FileFilter {
private FileFilter filter;
public NotFileFilter(FileFilter filter) {
this.filter = filter;
}
@Override
public boolean accept(File file) {
return !filter.accept(file);
}
}

4
pf4j/src/main/java/ro/fortsoft/pf4j/util/ZipFilter.java → pf4j/src/main/java/ro/fortsoft/pf4j/util/ZipFileFilter.java

@ -18,14 +18,14 @@ package ro.fortsoft.pf4j.util;
* *
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class ZipFilter extends ExtensionFilter { public class ZipFileFilter extends ExtensionFileFilter {
/** /**
* The extension that this filter will search for. * The extension that this filter will search for.
*/ */
private static final String ZIP_EXTENSION = ".ZIP"; private static final String ZIP_EXTENSION = ".ZIP";
public ZipFilter() { public ZipFileFilter() {
super(ZIP_EXTENSION); super(ZIP_EXTENSION);
} }
Loading…
Cancel
Save