Search in sources :

Example 1 with PluginBundleIdentifier

use of org.bimserver.plugins.PluginBundleIdentifier in project BIMserver by opensourceBIM.

the class GetInstalledPluginBundles method execute.

@Override
public List<SPluginBundle> execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException, ServerException {
    List<SPluginBundle> result = Collections.synchronizedList(new ArrayList<>());
    bimserverVersion = new DefaultArtifactVersion(bimServer.getVersionChecker().getLocalVersion().getFullString());
    GitHubPluginRepository repository = new GitHubPluginRepository(bimServer.getMavenPluginRepository(), bimServer.getServerSettingsCache().getServerSettings().getServiceRepositoryUrl());
    Map<PluginBundleIdentifier, PluginLocation<?>> repositoryKnownLocation = new HashMap<>();
    for (PluginLocation<?> pluginLocation : repository.listPluginLocations()) {
        repositoryKnownLocation.put(pluginLocation.getPluginIdentifier(), pluginLocation);
    }
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 32, 1L, TimeUnit.HOURS, new ArrayBlockingQueue<>(100));
    for (PluginBundle currentlyInstalledPluginBundle : bimServer.getPluginBundleManager().getPluginBundles()) {
        SPluginBundleVersion installedVersion = currentlyInstalledPluginBundle.getPluginBundleVersion();
        for (PluginBundleVersion pluginBundleVersion : getDatabaseSession().getAll(PluginBundleVersion.class)) {
            if (pluginBundleVersion.getArtifactId().equals(installedVersion.getArtifactId()) && pluginBundleVersion.getGroupId().equals(installedVersion.getGroupId()) && pluginBundleVersion.getVersion().equals(installedVersion.getVersion())) {
                installedVersion.setOid(pluginBundleVersion.getOid());
            }
        }
        PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(installedVersion.getGroupId(), installedVersion.getArtifactId());
        PluginLocation<?> pluginLocation = repositoryKnownLocation.get(pluginBundleIdentifier);
        threadPoolExecutor.submit(new Runnable() {

            @Override
            public void run() {
                SPluginBundle sPluginBundle = processPluginLocation(pluginLocation, strictVersionChecking, bimserverVersion);
                if (sPluginBundle == null) {
                    // No versions found on repository
                    sPluginBundle = currentlyInstalledPluginBundle.getPluginBundle();
                }
                boolean found = false;
                for (SPluginBundleVersion sPluginBundleVersion : sPluginBundle.getAvailableVersions()) {
                    if (sPluginBundleVersion.getVersion().equals(currentlyInstalledPluginBundle.getPluginBundleVersion().getVersion())) {
                        found = true;
                    }
                }
                if (!found) {
                    // Add the currently installed version if it's not in the list of available plugins
                    sPluginBundle.getAvailableVersions().add(currentlyInstalledPluginBundle.getPluginBundleVersion());
                }
                sPluginBundle.setInstalledVersion(installedVersion);
                Collections.sort(sPluginBundle.getAvailableVersions(), new Comparator<SPluginBundleVersion>() {

                    private List<Integer> split(String in) {
                        List<Integer> result = new ArrayList<>();
                        for (String s : in.split("\\.")) {
                            if (s.endsWith("-SNAPSHOT")) {
                                s = s.substring(0, s.length() - 9);
                            }
                            result.add(Integer.parseInt(s));
                        }
                        return result;
                    }

                    @Override
                    public int compare(SPluginBundleVersion o1, SPluginBundleVersion o2) {
                        // Ideally we would not depend on a specific versioning scheme, but alas
                        String v1 = o1.getVersion();
                        String v2 = o2.getVersion();
                        if (v1.contains(".") && v2.contains(".")) {
                            List<Integer> v1s = split(v1);
                            List<Integer> v2s = split(v2);
                            for (int i = 0; i < v1s.size(); i++) {
                                if (v1s.get(i) == v2s.get(i)) {
                                // Continue
                                } else {
                                    return v2s.get(i) - v1s.get(i);
                                }
                            }
                        } else {
                            // Fall back to string based compare
                            return v1.compareTo(v2);
                        }
                        return 0;
                    }
                });
                result.add(sPluginBundle);
            }
        });
    }
    threadPoolExecutor.shutdown();
    try {
        threadPoolExecutor.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Collections.sort(result, new Comparator<SPluginBundle>() {

        @Override
        public int compare(SPluginBundle o1, SPluginBundle o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    return result;
}
Also used : SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) PluginBundle(org.bimserver.plugins.PluginBundle) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) PluginLocation(org.bimserver.plugins.PluginLocation) PluginBundleVersion(org.bimserver.models.store.PluginBundleVersion) SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) GitHubPluginRepository(org.bimserver.plugins.GitHubPluginRepository) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) PluginBundleIdentifier(org.bimserver.plugins.PluginBundleIdentifier) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)1 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)1 SPluginBundleVersion (org.bimserver.interfaces.objects.SPluginBundleVersion)1 PluginBundleVersion (org.bimserver.models.store.PluginBundleVersion)1 GitHubPluginRepository (org.bimserver.plugins.GitHubPluginRepository)1 PluginBundle (org.bimserver.plugins.PluginBundle)1 PluginBundleIdentifier (org.bimserver.plugins.PluginBundleIdentifier)1 PluginLocation (org.bimserver.plugins.PluginLocation)1