mirror of https://github.com/pf4j/pf4j.git
decebals
6 years ago
18 changed files with 334 additions and 136 deletions
@ -0,0 +1,88 @@
|
||||
/* |
||||
* Copyright (C) 2012-present the original author or authors. |
||||
* |
||||
* 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 org.pf4j; |
||||
|
||||
import org.pf4j.util.FileUtils; |
||||
|
||||
import java.io.File; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Load all information needed by a plugin. |
||||
* This means add to the plugin's {@link ClassLoader} all the jar files and |
||||
* all the class files specified in the {@link PluginClasspath}. |
||||
* |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class BasePluginLoader implements PluginLoader { |
||||
|
||||
protected PluginManager pluginManager; |
||||
protected PluginClasspath pluginClasspath; |
||||
|
||||
public BasePluginLoader(PluginManager pluginManager, PluginClasspath pluginClasspath) { |
||||
this.pluginManager = pluginManager; |
||||
this.pluginClasspath = pluginClasspath; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isApplicable(Path pluginPath) { |
||||
return Files.exists(pluginPath); |
||||
} |
||||
|
||||
@Override |
||||
public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) { |
||||
PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginPath, pluginDescriptor); |
||||
|
||||
loadClasses(pluginPath, pluginClassLoader); |
||||
loadJars(pluginPath, pluginClassLoader); |
||||
|
||||
return pluginClassLoader; |
||||
} |
||||
|
||||
protected PluginClassLoader createPluginClassLoader(Path pluginPath, PluginDescriptor pluginDescriptor) { |
||||
return new PluginClassLoader(pluginManager, pluginDescriptor, getClass().getClassLoader()); |
||||
} |
||||
|
||||
/** |
||||
* Add all {@code *.class} files from {@link PluginClasspath#getClassesDirectories()} |
||||
* to the plugin's {@link ClassLoader}. |
||||
*/ |
||||
protected void loadClasses(Path pluginPath, PluginClassLoader pluginClassLoader) { |
||||
for (String directory : pluginClasspath.getClassesDirectories()) { |
||||
File file = pluginPath.resolve(directory).toFile(); |
||||
if (file.exists() && file.isDirectory()) { |
||||
pluginClassLoader.addFile(file); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Add all {@code *.jar} files from {@link PluginClasspath#getJarsDirectories()} |
||||
* to the plugin's {@link ClassLoader}. |
||||
*/ |
||||
protected void loadJars(Path pluginPath, PluginClassLoader pluginClassLoader) { |
||||
for (String jarsDirectory : pluginClasspath.getJarsDirectories()) { |
||||
Path file = pluginPath.resolve(jarsDirectory); |
||||
List<File> jars = FileUtils.getJars(file); |
||||
for (File jar : jars) { |
||||
pluginClassLoader.addFile(jar); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@
|
||||
/* |
||||
* Copyright (C) 2012-present the original author or authors. |
||||
* |
||||
* 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 org.pf4j; |
||||
|
||||
/** |
||||
* Load all information needed by a plugin from {@link DevelopmentPluginClasspath}. |
||||
* |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class DevelopmentPluginLoader extends BasePluginLoader { |
||||
|
||||
public DevelopmentPluginLoader(PluginManager pluginManager) { |
||||
super(pluginManager, new DevelopmentPluginClasspath()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright (C) 2012-present the original author or authors. |
||||
* |
||||
* 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 org.pf4j; |
||||
|
||||
import org.pf4j.util.AndFileFilter; |
||||
import org.pf4j.util.DirectoryFileFilter; |
||||
import org.pf4j.util.HiddenFilter; |
||||
import org.pf4j.util.NameFileFilter; |
||||
import org.pf4j.util.NotFileFilter; |
||||
import org.pf4j.util.OrFileFilter; |
||||
|
||||
import java.io.FileFilter; |
||||
import java.nio.file.Path; |
||||
|
||||
/** |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class DevelopmentPluginRepository extends BasePluginRepository { |
||||
|
||||
public static final String MAVEN_BUILD_DIR = "target"; |
||||
public static final String GRADLE_BUILD_DIR = "build"; |
||||
|
||||
public DevelopmentPluginRepository(Path pluginsRoot) { |
||||
super(pluginsRoot); |
||||
|
||||
AndFileFilter pluginsFilter = new AndFileFilter(new DirectoryFileFilter()); |
||||
pluginsFilter.addFileFilter(new NotFileFilter(createHiddenPluginFilter())); |
||||
setFilter(pluginsFilter); |
||||
} |
||||
|
||||
protected FileFilter createHiddenPluginFilter() { |
||||
OrFileFilter hiddenPluginFilter = new OrFileFilter(new HiddenFilter()); |
||||
|
||||
// skip default build output folders since these will cause errors in the logs
|
||||
hiddenPluginFilter |
||||
.addFileFilter(new NameFileFilter(MAVEN_BUILD_DIR)) |
||||
.addFileFilter(new NameFileFilter(GRADLE_BUILD_DIR)); |
||||
|
||||
return hiddenPluginFilter; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,61 @@
|
||||
/* |
||||
* Copyright (C) 2012-present the original author or authors. |
||||
* |
||||
* 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 org.pf4j; |
||||
|
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.io.TempDir; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
|
||||
/** |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class DevelopmentPluginRepositoryTest { |
||||
|
||||
@TempDir |
||||
Path pluginsPath; |
||||
|
||||
@BeforeEach |
||||
public void setUp() throws IOException { |
||||
// standard maven/gradle bin folder - these should be skipped in development mode because the cause errors
|
||||
Files.createDirectory(pluginsPath.resolve(DevelopmentPluginRepository.MAVEN_BUILD_DIR)); |
||||
Files.createDirectory(pluginsPath.resolve(DevelopmentPluginRepository.GRADLE_BUILD_DIR)); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetPluginArchivesInDevelopmentMode() { |
||||
PluginRepository repository = new DevelopmentPluginRepository(pluginsPath); |
||||
|
||||
List<Path> pluginsPaths = repository.getPluginsPaths(); |
||||
|
||||
// target and build should be ignored
|
||||
assertEquals(0, pluginsPaths.size()); |
||||
assertPathDoesNotExists(pluginsPaths, pluginsPath.resolve(DevelopmentPluginRepository.MAVEN_BUILD_DIR)); |
||||
assertPathDoesNotExists(pluginsPaths, pluginsPath.resolve(DevelopmentPluginRepository.GRADLE_BUILD_DIR)); |
||||
} |
||||
|
||||
private void assertPathDoesNotExists(List<Path> paths, Path path) { |
||||
assertFalse(paths.contains(path), "The directory must not contain the file " + path); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue