Skip to content

Commit

Permalink
Minimizing the amount of public methods in the Plugin interface to
Browse files Browse the repository at this point in the history
BIMserver
  • Loading branch information
rubendel committed Oct 16, 2015
1 parent bfc4df0 commit 9dff3fc
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void init(PluginManager pluginManager) throws PluginException {
PluginContext pluginContext = pluginManager.getPluginContext(this);
try {
String schemaName = "IFC2X3_TC1.exp";
InputStream inputStream = pluginContext.getResourceAsInputStream("schema/" + schemaName);
InputStream inputStream = Files.newInputStream(pluginContext.getRootPath().resolve("schema/" + schemaName));
try {
byte[] data = IOUtils.toByteArray(inputStream);
if (!Files.exists(pluginManager.getTempDir())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void init(PluginManager pluginManager) throws PluginException {
PluginContext pluginContext = pluginManager.getPluginContext(this);
try {
String schemaName = "IFC4.exp";
InputStream inputStream = pluginContext.getResourceAsInputStream("schema/" + schemaName);
InputStream inputStream = Files.newInputStream(pluginContext.getRootPath().resolve("schema/" + schemaName));
try {
byte[] data = IOUtils.toByteArray(inputStream);
if (!Files.exists(pluginManager.getTempDir())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -37,6 +39,7 @@
import org.bimserver.models.ifc2x3tc1.IfcSpace;
import org.bimserver.models.ifc2x3tc1.IfcUnitEnum;
import org.bimserver.plugins.ModelHelper;
import org.bimserver.plugins.PluginContext;
import org.bimserver.plugins.deserializers.Deserializer;
import org.bimserver.plugins.deserializers.DeserializerPlugin;
import org.bimserver.plugins.objectidms.HideAllInversesObjectIDM;
Expand Down Expand Up @@ -82,7 +85,10 @@ public void newRevision(RunningService runningService, BimServerClientInterface

Deserializer deserializer = deserializerPlugin.createDeserializer(null);
deserializer.init(model.getPackageMetaData());
InputStream resourceAsInputStream = getPluginManager().getPluginContext(FurniturePlacerServicePlugin.this).getResourceAsInputStream("data/picknicktable.ifc");
PluginContext pluginContext = getPluginManager().getPluginContext(FurniturePlacerServicePlugin.this);
Path pickNickTableFile = pluginContext.getRootPath().resolve("data").resolve("picknicktable.ifc");
InputStream resourceAsInputStream = Files.newInputStream(pickNickTableFile);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(resourceAsInputStream, byteArrayOutputStream);
resourceAsInputStream.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.bimserver.demoplugins.service;

import org.apache.commons.io.IOUtils;
import java.nio.file.Files;
import java.nio.file.Path;

import org.bimserver.interfaces.objects.SObjectType;
import org.bimserver.plugins.services.AbstractAddExtendedDataService;
import org.bimserver.plugins.services.BimServerClientInterface;
Expand All @@ -14,7 +16,7 @@ public HtmlService() {

@Override
public void newRevision(RunningService runningService, BimServerClientInterface bimServerClientInterface, long poid, long roid, String userToken, long soid, SObjectType settings) throws Exception {
byte[] bytes = IOUtils.toByteArray(getPluginContext().getResourceAsInputStream("data/example.html"));
addExtendedData(bytes, "example.html", "HTML Demo Results", "text/html", bimServerClientInterface, roid);
Path path = getPluginContext().getRootPath().resolve("data").resolve("example.html");
addExtendedData(Files.readAllBytes(path), "example.html", "HTML Demo Results", "text/html", bimServerClientInterface, roid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*****************************************************************************/

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -40,9 +40,9 @@ public class FileBasedObjectIDM extends AbstractObjectIDM {

public FileBasedObjectIDM(SchemaDefinition schemaDefinition, PluginContext pluginContext, PackageMetaData packageMetaData) throws ObjectIDMException {
super(schemaDefinition, packageMetaData);
try {
URL ignoreFile = pluginContext.getResourceAsUrl("objectidm.xml");
PackageDefinition packageDefinition = PackageDefinition.readFromFile(ignoreFile);
try {
Path objectIdmXml = pluginContext.getRootPath().resolve("objectidm.xml");
PackageDefinition packageDefinition = PackageDefinition.readFromFile(objectIdmXml);
if (!packageDefinition.getName().equals("Ifc2x3")) {
throw new ObjectIDMException("Package must be Ifc2x3");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -69,6 +72,19 @@ public void writeToFile(File file) throws JAXBException {
marshaller.marshal(this, file);
}

public static PackageDefinition readFromFile(Path file) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(PackageDefinition.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
InputStream newInputStream = Files.newInputStream(file);
try {
Object unmarshal = unmarshaller.unmarshal(newInputStream);
PackageDefinition settings = (PackageDefinition) unmarshal;
return settings;
} finally {
newInputStream.close();
}
}

public static PackageDefinition readFromFile(File file) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(PackageDefinition.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void init(PluginManager pluginManager) throws PluginException {
} else if (os.contains("linux")) {
libraryName = "libifcengine.so";
}
InputStream inputStream = pluginContext.getResourceAsInputStream("lib/" + System.getProperty("sun.arch.data.model") + "/" + libraryName);
InputStream inputStream = Files.newInputStream(pluginContext.getRootPath().resolve("lib/" + System.getProperty("sun.arch.data.model") + "/" + libraryName));
if (inputStream != null) {
try {
Path tmpFolder = pluginManager.getTempDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -20,7 +21,6 @@
import org.bimserver.plugins.VirtualClassLoader;
import org.bimserver.plugins.VirtualFile;
import org.bimserver.plugins.VirtualFileManager;
import org.bimserver.plugins.VirtualFileManager2;
import org.bimserver.plugins.modelchecker.ModelCheckException;
import org.bimserver.plugins.modelchecker.ModelChecker;
import org.slf4j.Logger;
Expand All @@ -31,10 +31,12 @@ public class JavaModelChecker implements ModelChecker {
private static String libPath = System.getProperty("java.class.path");
private ClassLoader classLoader;
private JavaFileManager pluginFileManager;
private Path rootPath;

public JavaModelChecker(ClassLoader classLoader, JavaFileManager pluginFileManager) {
public JavaModelChecker(ClassLoader classLoader, Path rootPath) {
this.classLoader = classLoader;
this.pluginFileManager = pluginFileManager;
this.rootPath = rootPath;
this.pluginFileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public boolean isInitialized() {
@Override
public ModelChecker createModelChecker(PluginConfiguration pluginConfiguration) {
PluginContext pluginContext = pluginManager.getPluginContext(this);
return new JavaModelChecker(pluginContext.getClassLoader(), pluginContext.getFileManager());
return new JavaModelChecker(pluginContext.getClassLoader(), pluginContext.getRootPath());
}
}
8 changes: 5 additions & 3 deletions JavaQueryEngine/src/org/bimserver/jqep/JavaQueryEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bimserver.jqep;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -29,11 +30,12 @@ public class JavaQueryEngine implements QueryEngine {
private static String libPath = System.getProperty("java.class.path");
private final ClassLoader classLoader;
private final JavaFileManager pluginFileManager;
private Path rootPath;

public JavaQueryEngine(ClassLoader classLoader, JavaFileManager pluginFileManager) {
public JavaQueryEngine(ClassLoader classLoader, Path rootPath) {
this.classLoader = classLoader;
this.pluginFileManager = pluginFileManager;
pluginFileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
this.rootPath = rootPath;
this.pluginFileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public boolean isInitialized() {
@Override
public QueryEngine getQueryEngine(PluginConfiguration pluginConfiguration) {
PluginContext pluginContext = pluginManager.getPluginContext(this);
return new JavaQueryEngine(pluginContext.getClassLoader(), pluginContext.getFileManager());
return new JavaQueryEngine(pluginContext.getClassLoader(), pluginContext.getRootPath());
}

@Override
Expand Down
53 changes: 20 additions & 33 deletions Shared/src/org/bimserver/plugins/PluginContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
*****************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
Expand All @@ -34,7 +30,6 @@
import javax.tools.ToolProvider;

import org.bimserver.models.store.Parameter;
import org.bimserver.plugins.web.WebModulePlugin;

public class PluginContext {

Expand Down Expand Up @@ -97,29 +92,25 @@ public boolean isEnabled() {
return enabled;
}

public InputStream getResourceAsInputStream(String name) throws FileNotFoundException {
InputStream resourceAsStream = classLoader.getResourceAsStream(name);
if (resourceAsStream == null) {
File file = new File(location + File.separator + name);
if (file.exists()) {
resourceAsStream = new FileInputStream(file);
}
}
if (resourceAsStream == null && !pluginImplementation.getRequires().isEmpty()) {
for (String dep : pluginImplementation.getRequires()) {
WebModulePlugin webModulePlugin = pluginManager.getWebModulePlugin(dep, true);
InputStream resourceAsInputStream = pluginManager.getPluginContext(webModulePlugin).getResourceAsInputStream(name);
if (resourceAsInputStream != null) {
return resourceAsInputStream;
}
}
}
return resourceAsStream;
}

public URL getResourceAsUrl(String name) {
return classLoader.getResource(name);
}
// public InputStream getResourceAsInputStream(String name) throws FileNotFoundException {
// InputStream resourceAsStream = classLoader.getResourceAsStream(name);
// if (resourceAsStream == null) {
// File file = new File(location + File.separator + name);
// if (file.exists()) {
// resourceAsStream = new FileInputStream(file);
// }
// }
// if (resourceAsStream == null && !pluginImplementation.getRequires().isEmpty()) {
// for (String dep : pluginImplementation.getRequires()) {
// WebModulePlugin webModulePlugin = pluginManager.getWebModulePlugin(dep, true);
// InputStream resourceAsInputStream = pluginManager.getPluginContext(webModulePlugin).getResourceAsInputStream(name);
// if (resourceAsInputStream != null) {
// return resourceAsInputStream;
// }
// }
// }
// return resourceAsStream;
// }

public String getClassLocation() {
return classLocation;
Expand All @@ -129,7 +120,7 @@ public ClassLoader getClassLoader() {
return classLoader;
}

public JavaFileManager getFileManager() {
private JavaFileManager getFileManager() {
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
if (systemJavaCompiler == null) {
throw new RuntimeException("JDK needed");
Expand All @@ -154,10 +145,6 @@ public Path getRootPath() {
return rootPath;
}

public FileSystem getFilesystem() {
return fileSystem;
}

public Parameter getParameter(String name) {
return pluginManager.getParameter(this, name);
}
Expand Down
7 changes: 7 additions & 0 deletions Shared/src/org/bimserver/plugins/VirtualFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,12 @@ public static VirtualFile fromDirectory(File file) throws IOException {

public void copyTo(VirtualFile virtualFile) {
// TODO
}

public void setData(InputStream inputStream) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(inputStream, output);
setData(output.toByteArray());
inputStream.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,42 @@ public FileJarClassLoader(PluginManager pluginManager, ClassLoader parentClassLo
}
}

private void loadEmbeddedJarFileSystems(Path path) {
private void loadEmbeddedJarFileSystems() {
if (!embeddedJarFilesLoaded) {

try {
if (Files.isDirectory(path)) {
for (Path subPath : PathUtils.list(path)) {
loadEmbeddedJarFileSystems(subPath);
}
} else {
// This is annoying, but we are caching the contents of JAR files within JAR files in memory, could not get the JarFileSystem to work with jar:jar:file URI's
// Also there is a problem with not being able to change position within a file, at least in the JarFileSystem
// It looks like there are 2 other solutions to this problem:
// - Copy the embedded JAR files to a tmp directory, and load from there with a JarFileSystem wrapper (at some stage we were doing this for all JAR contents,
// resulted in 50.000 files, which was annoying, but a few JAR files probably won't hurt
// - Don't allow plugins to have embedded JAR's, could force them to extract all dependencies...
//
if (path.getFileName().toString().toLowerCase().endsWith(".jar")) {
JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(path));
try {
JarEntry jarEntry = jarInputStream.getNextJarEntry();
while (jarEntry != null) {
jarContent.put(jarEntry.getName(), IOUtils.toByteArray(jarInputStream));
jarEntry = jarInputStream.getNextJarEntry();
}
} finally {
jarInputStream.close();
loadEmbeddedJarFileSystems(fileSystem.getPath("/"));
embeddedJarFilesLoaded = true;
}
}

private void loadEmbeddedJarFileSystems(Path path) {
try {
if (Files.isDirectory(path)) {
for (Path subPath : PathUtils.list(path)) {
loadEmbeddedJarFileSystems(subPath);
}
} else {
// This is annoying, but we are caching the contents of JAR files within JAR files in memory, could not get the JarFileSystem to work with jar:jar:file URI's
// Also there is a problem with not being able to change position within a file, at least in the JarFileSystem
// It looks like there are 2 other solutions to this problem:
// - Copy the embedded JAR files to a tmp directory, and load from there with a JarFileSystem wrapper (at some stage we were doing this for all JAR contents,
// resulted in 50.000 files, which was annoying, but a few JAR files probably won't hurt
// - Don't allow plugins to have embedded JAR's, could force them to extract all dependencies...
//
if (path.getFileName().toString().toLowerCase().endsWith(".jar")) {
JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(path));
try {
JarEntry jarEntry = jarInputStream.getNextJarEntry();
while (jarEntry != null) {
jarContent.put(jarEntry.getName(), IOUtils.toByteArray(jarInputStream));
jarEntry = jarInputStream.getNextJarEntry();
}
} finally {
jarInputStream.close();
}
}
embeddedJarFilesLoaded = true;
} catch (IOException e) {
LOGGER.error("", e);
}
} catch (IOException e) {
LOGGER.error("", e);
}
}

Expand Down Expand Up @@ -118,7 +121,7 @@ public InputStream getInputStream() throws IOException {
}

private Lazy<InputStream> findPath(final String name) throws IOException {
loadEmbeddedJarFileSystems(fileSystem.getPath("/"));
loadEmbeddedJarFileSystems();
final Path file = this.fileSystem.getPath(name);
if (Files.exists(file)) {
return new Lazy<InputStream>(){
Expand All @@ -144,6 +147,9 @@ public InputStream get() {

@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
if (name.contains("Tuple3f")) {
System.out.println();
}
String fileName = name.replace(".", "/") + ".class";
if (loadedClasses.containsKey(fileName)) {
return loadedClasses.get(fileName);
Expand Down
Loading

0 comments on commit 9dff3fc

Please sign in to comment.