All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bimserver.jqep.JavaQueryEngine Maven / Gradle / Ivy

The newest version!
package org.bimserver.jqep;

/******************************************************************************
 * Copyright (C) 2009-2016  BIMserver.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see {@literal}.
 *****************************************************************************/

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;

import org.bimserver.emf.IfcModelInterface;
import org.bimserver.plugins.ModelHelper;
import org.bimserver.plugins.Reporter;
import org.bimserver.plugins.VirtualClassLoader;
import org.bimserver.plugins.VirtualFile;
import org.bimserver.plugins.VirtualFileManager;
import org.bimserver.plugins.queryengine.QueryEngine;
import org.bimserver.shared.exceptions.PluginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JavaQueryEngine implements QueryEngine {

	private static final Logger LOGGER = LoggerFactory.getLogger(JavaQueryEngine.class);
	private static String libPath = System.getProperty("java.class.path");
	private final ClassLoader classLoader;
	private final JavaFileManager pluginFileManager;

	public JavaQueryEngine(ClassLoader classLoader, Path rootPath) throws PluginException {
		this.classLoader = classLoader;
		JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
		if (systemJavaCompiler == null) {
			throw new PluginException("No JDK installed");
		}
		this.pluginFileManager = systemJavaCompiler.getStandardFileManager(null, null, null);
	}

	@Override
	public IfcModelInterface query(IfcModelInterface model, String code, Reporter reporter, ModelHelper modelHelper) {
		try {
			QueryInterface queryInterface = createQueryInterface(code);
			queryInterface.query(model, reporter, modelHelper);
			return modelHelper.getTargetModel();
		} catch (Exception e) {
			LOGGER.error("", e);
			reporter.error(e);
		}
		return null;
	}

	public static void addJarFolder(File libDir) {
		if (libDir.exists() && libDir.isDirectory()) {
			for (File file : libDir.listFiles()) {
				if (file.getName().endsWith(".jar")) {
					libPath += file.getAbsolutePath() + File.pathSeparator;
				}
			}
		}
		LOGGER.debug("libPath: " + libPath);
	}
	
	private void getJavaFiles(List fileList, VirtualFile baseDir) {
		for (VirtualFile f : baseDir.listFiles()) {
			if (f.isDirectory()) {
				getJavaFiles(fileList, f);
			} else {
				if (f.getName().endsWith(".java")) {
					fileList.add(f);
				}
			}
		}
	}

	private QueryInterface createQueryInterface(String code) throws CompileException {
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		if (compiler == null) {
			throw new CompileException("JDK needed for compile tasks");
		}
		VirtualFile baseDir = new VirtualFile();
		VirtualFile file = baseDir.createFile("org/bimserver/jqep/Query.java");
		file.setStringContent(code);
		VirtualFileManager myFileManager = new VirtualFileManager(pluginFileManager, classLoader, baseDir);

		List fileList = new ArrayList();
		getJavaFiles(fileList, baseDir);
		List compilationUnits = baseDir.getAllJavaFileObjects();

		List options = new ArrayList();
		options.add("-cp");
		options.add(libPath);
		options.add("-target");
		options.add("7");

		DiagnosticCollector diagnosticsCollector = new DiagnosticCollector();
		compiler.getTask(null, myFileManager, diagnosticsCollector, options, null, compilationUnits).call();
		List> diagnostics = diagnosticsCollector.getDiagnostics();
		for (Diagnostic d : diagnostics) {
			if (d.getKind() == Kind.ERROR) {
				throw new CompileException(d.getMessage(Locale.ENGLISH));
			} else if (d.getKind() == Kind.WARNING) {
				throw new CompileException(d.getMessage(Locale.ENGLISH));
			}
		}
		VirtualClassLoader loader = new VirtualClassLoader(classLoader, baseDir);
		try {
			Class loadClass = loader.loadClass("org.bimserver.jqep.Query");
			QueryInterface newInstance = (QueryInterface) loadClass.newInstance();
			return newInstance;
		} catch (Exception e) {
			throw new CompileException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy