Search in sources :

Example 1 with DelegatingClassLoader

use of org.bimserver.plugins.classloaders.DelegatingClassLoader in project BIMserver by opensourceBIM.

the class PluginManager method loadDependencies.

private PublicFindClassClassLoader loadDependencies(Set<org.bimserver.plugins.Dependency> bimServerDependencies, Model model, PublicFindClassClassLoader previous) throws FileNotFoundException, IOException {
    List<org.apache.maven.model.Dependency> dependencies = model.getDependencies();
    Iterator<org.apache.maven.model.Dependency> it = dependencies.iterator();
    Path workspaceDir = Paths.get("..");
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("PluginBase/target/classes")));
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("Shared/target/classes")));
    while (it.hasNext()) {
        org.apache.maven.model.Dependency depend = it.next();
        try {
            if (depend.getGroupId().equals("org.opensourcebim") && (depend.getArtifactId().equals("shared") || depend.getArtifactId().equals("pluginbase"))) {
                // TODO we might want to check the version though
                continue;
            }
            if (depend.isOptional() || "test".equals(depend.getScope())) {
                continue;
            }
            Dependency dependency2 = new Dependency(new DefaultArtifact(depend.getGroupId() + ":" + depend.getArtifactId() + ":jar:" + depend.getVersion()), "compile");
            DelegatingClassLoader depDelLoader = new DelegatingClassLoader(previous);
            if (!dependency2.getArtifact().isSnapshot()) {
                if (dependency2.getArtifact().getFile() != null) {
                    bimServerDependencies.add(new org.bimserver.plugins.Dependency(dependency2.getArtifact().getFile().toPath()));
                    loadDependencies(dependency2.getArtifact().getFile().toPath(), depDelLoader);
                } else {
                    ArtifactRequest request = new ArtifactRequest();
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (ArtifactResolutionException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                // Snapshot projects linked in Eclipse
                ArtifactRequest request = new ArtifactRequest();
                if ((!"test".equals(dependency2.getScope()) && !dependency2.getArtifact().isSnapshot())) {
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getLocalRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (Exception e) {
                        LOGGER.info(dependency2.getArtifact().toString());
                        e.printStackTrace();
                    }
                // bimServerDependencies.add(new
                // org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                }
            }
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            descriptorRequest.setArtifact(dependency2.getArtifact());
            descriptorRequest.setRepositories(mavenPluginRepository.getRepositories());
            ArtifactDescriptorResult descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
            CollectRequest collectRequest = new CollectRequest();
            collectRequest.setRootArtifact(descriptorResult.getArtifact());
            collectRequest.setDependencies(descriptorResult.getDependencies());
            collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
            collectRequest.setRepositories(descriptorResult.getRepositories());
            DependencyNode node = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest).getRoot();
            DependencyRequest dependencyRequest = new DependencyRequest();
            dependencyRequest.setRoot(node);
            CollectResult collectResult = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest);
            PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
            // collectResult.getRoot().accept(new
            // ConsoleDependencyGraphDumper());
            collectResult.getRoot().accept(nlg);
            try {
                mavenPluginRepository.getSystem().resolveDependencies(mavenPluginRepository.getSession(), dependencyRequest);
            } catch (DependencyResolutionException e) {
            // Ignore
            }
            for (DependencyNode dependencyNode : nlg.getNodes()) {
                ArtifactRequest newRequest = new ArtifactRequest(dependencyNode);
                newRequest.setRepositories(mavenPluginRepository.getRepositories());
                ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), newRequest);
                Artifact artifact = resolveArtifact.getArtifact();
                Path jarFile = Paths.get(artifact.getFile().getAbsolutePath());
                loadDependencies(jarFile, depDelLoader);
                Artifact versionArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
                ArtifactRequest request = new ArtifactRequest();
                request.setArtifact(versionArtifact);
                request.setRepositories(mavenPluginRepository.getRepositories());
                // try {
                // ArtifactResult resolveArtifact =
                // mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(),
                // request);
                // File depPomFile =
                // resolveArtifact.getArtifact().getFile();
                // if (depPomFile != null) {
                // MavenXpp3Reader mavenreader = new MavenXpp3Reader();
                // Model depModel = null;
                // try (FileReader reader = new FileReader(depPomFile)) {
                // try {
                // depModel = mavenreader.read(reader);
                // } catch (XmlPullParserException e) {
                // e.printStackTrace();
                // }
                // }
                // previous = loadDependencies(bimServerDependencies,
                // depModel, previous);
                // } else {
                // LOGGER.info("Artifact not found " + versionArtifact);
                // }
                // } catch (ArtifactResolutionException e1) {
                // LOGGER.error(e1.getMessage());
                // }
                // EclipsePluginClassloader depLoader = new
                // EclipsePluginClassloader(depDelLoader, projectRoot);
                bimServerDependencies.add(new org.bimserver.plugins.Dependency(jarFile));
            }
            previous = depDelLoader;
        } catch (DependencyCollectionException e) {
            e.printStackTrace();
        } catch (ArtifactDescriptorException e2) {
            e2.printStackTrace();
        } catch (ArtifactResolutionException e) {
            e.printStackTrace();
        }
    }
    return previous;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) Path(java.nio.file.Path) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CollectResult(org.eclipse.aether.collection.CollectResult) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 2 with DelegatingClassLoader

use of org.bimserver.plugins.classloaders.DelegatingClassLoader 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);
    }
}
Also used : Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) ZipEntry(java.util.zip.ZipEntry) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) VersionRange(org.apache.maven.artifact.versioning.VersionRange) JarFile(java.util.jar.JarFile) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model)

Example 3 with DelegatingClassLoader

use of org.bimserver.plugins.classloaders.DelegatingClassLoader 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;
    }
}
Also used : Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) UserException(org.bimserver.shared.exceptions.UserException)

Example 4 with DelegatingClassLoader

use of org.bimserver.plugins.classloaders.DelegatingClassLoader in project BIMserver by opensourceBIM.

the class PluginManager method loadJavaProject.

public PluginBundle loadJavaProject(Path projectRoot, Path pomFile, Path pluginFolder, PluginDescriptor pluginDescriptor) throws PluginException, FileNotFoundException, IOException, XmlPullParserException {
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;
    try (FileReader reader = new FileReader(pomFile.toFile())) {
        model = mavenreader.read(reader);
    }
    PluginBundleVersionIdentifier pluginBundleVersionIdentifier = new PluginBundleVersionIdentifier(model.getGroupId(), model.getArtifactId(), model.getVersion());
    if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleVersionIdentifier.getPluginBundleIdentifier())) {
        throw new PluginException("Plugin " + pluginBundleVersionIdentifier.getPluginBundleIdentifier().getHumanReadable() + " already loaded (version " + pluginBundleIdentifierToPluginBundle.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier()).getPluginBundleVersion().getVersion() + ")");
    }
    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
    PublicFindClassClassLoader previous = new PublicFindClassClassLoader(getClass().getClassLoader()) {

        @Override
        public Class<?> findClass(String name) throws ClassNotFoundException {
            return null;
        }

        @Override
        public URL findResource(String name) {
            return null;
        }

        @Override
        public void dumpStructure(int indent) {
        }
    };
    Set<org.bimserver.plugins.Dependency> bimServerDependencies = new HashSet<>();
    pluginBundleVersionIdentifier = new PluginBundleVersionIdentifier(new PluginBundleIdentifier(model.getGroupId(), model.getArtifactId()), model.getVersion());
    previous = loadDependencies(bimServerDependencies, model, previous);
    delegatingClassLoader.add(previous);
    // Path libFolder = projectRoot.resolve("lib");
    // loadDependencies(libFolder, delegatingClassLoader);
    EclipsePluginClassloader pluginClassloader = new EclipsePluginClassloader(delegatingClassLoader, projectRoot);
    // pluginClassloader.dumpStructure(0);
    ResourceLoader resourceLoader = new ResourceLoader() {

        @Override
        public InputStream load(String name) {
            try {
                return Files.newInputStream(pluginFolder.resolve(name));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    SPluginBundle sPluginBundle = new SPluginBundle();
    sPluginBundle.setOrganization(model.getOrganization().getName());
    sPluginBundle.setName(model.getName());
    SPluginBundleVersion sPluginBundleVersion = createPluginBundleVersionFromMavenModel(model, true);
    Path icon = projectRoot.resolve("icon.png");
    if (Files.exists(icon)) {
        byte[] iconBytes = Files.readAllBytes(icon);
        sPluginBundleVersion.setIcon(iconBytes);
    }
    sPluginBundle.setInstalledVersion(sPluginBundleVersion);
    return loadPlugins(pluginBundleVersionIdentifier, resourceLoader, pluginClassloader, projectRoot.toUri(), projectRoot.resolve("target/classes").toString(), pluginDescriptor, PluginSourceType.ECLIPSE_PROJECT, bimServerDependencies, sPluginBundle, sPluginBundleVersion);
}
Also used : SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) PublicFindClassClassLoader(org.bimserver.plugins.classloaders.PublicFindClassClassLoader) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Dependency(org.eclipse.aether.graph.Dependency) IOException(java.io.IOException) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) EclipsePluginClassloader(org.bimserver.plugins.classloaders.EclipsePluginClassloader) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 5 with DelegatingClassLoader

use of org.bimserver.plugins.classloaders.DelegatingClassLoader 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);
}
Also used : Path(java.nio.file.Path) JarInputStream(java.util.jar.JarInputStream) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) PluginException(org.bimserver.shared.exceptions.PluginException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) VersionRange(org.apache.maven.artifact.versioning.VersionRange) JarEntry(java.util.jar.JarEntry) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream)

Aggregations

IOException (java.io.IOException)5 Path (java.nio.file.Path)5 DelegatingClassLoader (org.bimserver.plugins.classloaders.DelegatingClassLoader)5 PluginException (org.bimserver.shared.exceptions.PluginException)5 FileNotFoundException (java.io.FileNotFoundException)4 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)4 JAXBException (javax.xml.bind.JAXBException)4 Model (org.apache.maven.model.Model)4 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)4 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)4 ObjectIDMException (org.bimserver.plugins.objectidms.ObjectIDMException)4 ChannelConnectionException (org.bimserver.shared.ChannelConnectionException)4 ServiceException (org.bimserver.shared.exceptions.ServiceException)4 UserException (org.bimserver.shared.exceptions.UserException)4 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)4 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)4 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)4 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)4 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)4 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)3