use of org.bimserver.plugins.classloaders.FileJarClassLoader in project BIMserver by opensourceBIM.
the class PluginManager method loadFromPluginDir.
public PluginBundle loadFromPluginDir(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, SPluginBundleVersion pluginBundleVersion, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
if (!Files.exists(target)) {
throw new PluginException(target.toString() + " not found");
}
SPluginBundle sPluginBundle = new SPluginBundle();
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try (JarFile jarFile = new JarFile(target.toFile())) {
ZipEntry entry = jarFile.getEntry("META-INF/maven/" + pluginBundleVersion.getGroupId() + "/" + pluginBundleVersion.getArtifactId() + "/pom.xml");
Model model = mavenreader.read(jarFile.getInputStream(entry));
sPluginBundle.setOrganization(model.getOrganization().getName());
sPluginBundle.setName(model.getName());
DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
// TODO Skip, we should also check the version though
} else {
PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
if (strictDependencyChecking) {
VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
String version = pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
if (versionRange.containsVersion(artifactVersion)) {
// OK
} else {
throw new Exception("Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + version + ") does not comply to the required version (" + dependency.getVersion() + ")");
}
} else {
LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
}
} else {
if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
} else {
MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());
try {
Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader, depJarFile);
jarClassLoaders.add(jarClassLoader);
delegatingClassLoader.add(jarClassLoader);
} catch (Exception e) {
}
}
}
}
}
return loadPlugin(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, plugins, delegatingClassLoader);
}
}
use of org.bimserver.plugins.classloaders.FileJarClassLoader in project BIMserver by opensourceBIM.
the class PluginManager method update.
public PluginBundle update(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, SPluginBundle sPluginBundle, SPluginBundleVersion pluginBundleVersion, Path jarFile, Path pomFile, List<SPluginInformation> plugins) throws Exception {
PluginBundle existingPluginBundle = pluginBundleIdentifierToPluginBundle.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier());
if (existingPluginBundle == null) {
throw new UserException("No previous version of plugin bundle " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " found");
}
try {
existingPluginBundle.close();
if (pluginBundleIdentifierToPluginBundle.remove(pluginBundleVersionIdentifier.getPluginBundleIdentifier()) == null) {
LOGGER.warn("Previous version of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
}
PluginBundleVersionIdentifier currentVersion = pluginBundleIdentifierToCurrentPluginBundleVersionIdentifier.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier());
if (pluginBundleIdentifierToCurrentPluginBundleVersionIdentifier.remove(pluginBundleVersionIdentifier.getPluginBundleIdentifier()) == null) {
LOGGER.warn("Previous version of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
}
if (pluginBundleVersionIdentifierToPluginBundle.remove(currentVersion) == null) {
LOGGER.warn("Previous version (" + currentVersion + ") of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
}
for (PluginContext pluginContext : existingPluginBundle) {
Set<PluginContext> set = implementations.get(pluginContext.getPluginInterface());
set.remove(pluginContext);
}
if (existingPluginBundle.getPluginBundle().getInstalledVersion().getType() == SPluginBundleType.MAVEN) {
Path target = pluginsDir.resolve(currentVersion.getFileName());
Files.delete(target);
}
// for (PluginContext pluginContext : existingPluginBundle) {
// pluginChangeListener.pluginUninstalled(pluginContext);
// }
} catch (IOException e) {
LOGGER.error("", e);
}
Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
if (Files.exists(target)) {
throw new PluginException("This plugin has already been installed " + target.getFileName().toString());
}
Files.copy(jarFile, target);
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
Model model = null;
try (FileReader fileReader = new FileReader(pomFile.toFile())) {
model = mavenreader.read(fileReader);
}
DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
// TODO Skip, we should also check the version though
} else {
PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
// if (false) {
// VersionRange versionRange =
// VersionRange.createFromVersion(dependency.getVersion());
// String version =
// pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
// ArtifactVersion artifactVersion = new
// DefaultArtifactVersion(version);
// if (versionRange.containsVersion(artifactVersion)) {
// // OK
// } else {
// throw new Exception("Required dependency " +
// pluginBundleIdentifier + " is installed, but it's version
// (" + version + ") does not comply to the required version
// (" + dependency.getVersion() + ")");
// }
// } else {
LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
// }
} else {
if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
} else {
MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(model.getRepositories().get(0).getUrl(), dependency.getGroupId(), dependency.getArtifactId());
try {
Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader, depJarFile);
jarClassLoaders.add(jarClassLoader);
delegatingClassLoader.add(jarClassLoader);
} catch (Exception e) {
}
}
}
}
}
PluginBundle pluginBundle = null;
// Stage 1, load all plugins from the JAR file and initialize them
try {
pluginBundle = loadPluginsFromJar(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, delegatingClassLoader);
for (SPluginInformation sPluginInformation : plugins) {
if (sPluginInformation.isEnabled()) {
PluginContext pluginContext = pluginBundle.getPluginContext(sPluginInformation.getIdentifier());
pluginContext.getPlugin().init(pluginContext);
}
}
} catch (Exception e) {
Files.delete(target);
LOGGER.error("", e);
throw e;
}
// uninstalled
try {
long pluginBundleVersionId = pluginChangeListener.pluginBundleUpdated(pluginBundle);
for (SPluginInformation sPluginInformation : plugins) {
if (sPluginInformation.isEnabled()) {
PluginContext pluginContext = pluginBundle.getPluginContext(sPluginInformation.getIdentifier());
pluginChangeListener.pluginUpdated(pluginBundleVersionId, pluginContext, sPluginInformation);
}
}
return pluginBundle;
} catch (Exception e) {
uninstall(pluginBundleVersionIdentifier);
LOGGER.error("", e);
throw e;
}
}
use of org.bimserver.plugins.classloaders.FileJarClassLoader in project BIMserver by opensourceBIM.
the class PluginManager method loadPluginsFromJar.
public PluginBundle loadPluginsFromJar(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, Path file, SPluginBundle sPluginBundle, SPluginBundleVersion pluginBundleVersion, ClassLoader parentClassLoader) throws PluginException {
PluginBundleIdentifier pluginBundleIdentifier = pluginBundleVersionIdentifier.getPluginBundleIdentifier();
if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
throw new PluginException("Plugin " + pluginBundleIdentifier.getHumanReadable() + " already loaded (version " + pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion() + ")");
}
LOGGER.debug("Loading plugins from " + file.toString());
if (!Files.exists(file)) {
throw new PluginException("Not a file: " + file.toString());
}
FileJarClassLoader jarClassLoader = null;
try {
jarClassLoader = new FileJarClassLoader(this, parentClassLoader, file);
jarClassLoaders.add(jarClassLoader);
final JarClassLoader finalLoader = jarClassLoader;
InputStream pluginStream = jarClassLoader.getResourceAsStream("plugin/plugin.xml");
if (pluginStream == null) {
jarClassLoader.close();
throw new PluginException("No plugin/plugin.xml found in " + file.getFileName().toString());
}
PluginDescriptor pluginDescriptor = getPluginDescriptor(pluginStream);
if (pluginDescriptor == null) {
jarClassLoader.close();
throw new PluginException("No plugin descriptor could be created");
}
LOGGER.debug(pluginDescriptor.toString());
URI fileUri = file.toAbsolutePath().toUri();
URI jarUri = new URI("jar:" + fileUri.toString());
ResourceLoader resourceLoader = new ResourceLoader() {
@Override
public InputStream load(String name) {
return finalLoader.getResourceAsStream(name);
}
};
return loadPlugins(pluginBundleVersionIdentifier, resourceLoader, jarClassLoader, jarUri, file.toAbsolutePath().toString(), pluginDescriptor, PluginSourceType.JAR_FILE, new HashSet<org.bimserver.plugins.Dependency>(), sPluginBundle, pluginBundleVersion);
} catch (Exception e) {
if (jarClassLoader != null) {
try {
jarClassLoader.close();
} catch (IOException e1) {
LOGGER.error("", e1);
}
}
throw new PluginException(e);
}
}
use of org.bimserver.plugins.classloaders.FileJarClassLoader in project BIMserver by opensourceBIM.
the class PluginManager method loadDependencies.
// @SuppressWarnings("unused")
// private PluginBundle loadJavaScriptProject(Path projectRoot, Path packageFile, Path pluginFolder, PluginDescriptor pluginDescriptor) {
// ObjectNode packageModel;
// try {
// packageModel = OBJECT_MAPPER.readValue(packageFile.toFile(), ObjectNode.class);
// SPluginBundle sPluginBundle = new SPluginBundle();
//
// if (!packageModel.has("organization")) {
// throw new PluginException("package.json does not contain 'organization'");
// }
//
// sPluginBundle.setOrganization(packageModel.get("organization").asText());
// sPluginBundle.setName(packageModel.get("name").asText());
//
// SPluginBundleVersion sPluginBundleVersion = new SPluginBundleVersion();
// sPluginBundleVersion.setType(SPluginBundleType.GITHUB);
//
// if (!packageModel.has("organization")) {
// throw new PluginException("package.json does not contain 'groupId'");
// }
// sPluginBundleVersion.setGroupId(packageModel.get("groupId").asText());
//
// if (!packageModel.has("organization")) {
// throw new PluginException("package.json does not contain 'artifactId'");
// }
// sPluginBundleVersion.setArtifactId(packageModel.get("artifactId").asText());
//
// if (!packageModel.has("organization")) {
// throw new PluginException("package.json does not contain 'version'");
// }
// sPluginBundleVersion.setVersion(packageModel.get("version").asText());
//
// if (!packageModel.has("organization")) {
// throw new PluginException("package.json does not contain 'description'");
// }
// sPluginBundleVersion.setDescription(packageModel.get("description").asText());
//
// sPluginBundleVersion.setRepository("local");
// sPluginBundleVersion.setType(SPluginBundleType.LOCAL_DEV);
// sPluginBundleVersion.setMismatch(false); // TODO
//
// sPluginBundle.setInstalledVersion(sPluginBundleVersion);
//
// PluginBundleVersionIdentifier pluginBundleVersionIdentifier = new PluginBundleVersionIdentifier(new PluginBundleIdentifier(packageModel.get("groupId").asText(), packageModel.get("artifactId").asText()),
// packageModel.get("version").asText());
//
// ResourceLoader resourceLoader = new ResourceLoader() {
// @Override
// public InputStream load(String name) {
// try {
// return Files.newInputStream(pluginFolder.resolve(name));
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
// }
// };
//
// return loadPlugins(pluginBundleVersionIdentifier, resourceLoader, null, projectRoot.toUri(), null, pluginDescriptor, PluginSourceType.ECLIPSE_PROJECT, null, sPluginBundle, sPluginBundleVersion);
// } catch (JsonParseException e1) {
// e1.printStackTrace();
// } catch (JsonMappingException e1) {
// e1.printStackTrace();
// } catch (IOException e1) {
// e1.printStackTrace();
// } catch (PluginException e) {
// e.printStackTrace();
// }
// return null;
// }
private void loadDependencies(Path libFile, DelegatingClassLoader classLoader) throws FileNotFoundException, IOException {
if (libFile.getFileName().toString().toLowerCase().endsWith(".jar")) {
FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, classLoader, libFile);
jarClassLoaders.add(jarClassLoader);
classLoader.add(jarClassLoader);
}
}
use of org.bimserver.plugins.classloaders.FileJarClassLoader in project BIMserver by opensourceBIM.
the class PluginManager method install.
public PluginBundle install(MavenPluginBundle mavenPluginBundle, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
PluginBundleVersionIdentifier pluginBundleVersionIdentifier = mavenPluginBundle.getPluginVersionIdentifier();
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
Model model = null;
try (InputStream pomInputStream = mavenPluginBundle.getPomInputStream()) {
model = mavenreader.read(pomInputStream);
}
if (plugins == null) {
try (JarInputStream jarInputStream = new JarInputStream(mavenPluginBundle.getJarInputStream())) {
JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
while (nextJarEntry != null) {
if (nextJarEntry.getName().equals("plugin/plugin.xml")) {
// Install all plugins
PluginDescriptor pluginDescriptor = getPluginDescriptor(new FakeClosingInputStream(jarInputStream));
plugins = new ArrayList<>();
processPluginDescriptor(pluginDescriptor, plugins);
for (SPluginInformation info : plugins) {
info.setInstallForAllUsers(true);
info.setInstallForNewUsers(true);
}
break;
}
nextJarEntry = jarInputStream.getNextJarEntry();
}
}
}
DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
// TODO Skip, we should also check the version though
} else {
PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
if (strictDependencyChecking) {
VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
// String version =
// pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
ArtifactVersion artifactVersion = new DefaultArtifactVersion(mavenPluginBundle.getVersion());
if (versionRange.containsVersion(artifactVersion)) {
// OK
} else {
throw new Exception("Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + mavenPluginBundle.getVersion() + ") does not comply to the required version (" + dependency.getVersion() + ")");
}
} else {
LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
}
} else {
try {
MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());
Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader, depJarFile);
jarClassLoaders.add(jarClassLoader);
delegatingClassLoader.add(jarClassLoader);
} catch (Exception e) {
throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
}
}
}
}
Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
if (Files.exists(target)) {
throw new PluginException("This plugin has already been installed " + target.getFileName().toString());
}
Files.copy(mavenPluginBundle.getJarInputStream(), target);
return loadPlugin(pluginBundleVersionIdentifier, target, mavenPluginBundle.getPluginBundle(), mavenPluginBundle.getPluginBundleVersion(), plugins, delegatingClassLoader);
}
Aggregations