| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.preferences.ui.utils; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.util.Collection; |
| import java.util.Map; |
| import java.util.Properties; |
| import java.util.Set; |
| import java.util.TreeMap; |
| import java.util.TreeSet; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.osbp.preferences.AItemDescribed; |
| |
| public class ApplicationProjectInspector { |
| |
| private static final String WIZARD_CONFIGURATION_FILE = "model.wizard"; |
| private static final String PRODUCT_POSTFIX = ".product"; |
| |
| private final IProject applicationProject; |
| private final String applicationFileName; |
| private ProductFilePluginsVerifier productPlugins = null; |
| private ProductFileConfigurationsVerifier productConfigurations = null; |
| private ManifestFileVerifier productManifest = null; |
| Map<String,ManifestFileVerifier> manifestVerifiers = null; |
| |
| private static String extractProjectName(IProject project) { |
| String name = project.getName(); |
| if (name.endsWith(PRODUCT_POSTFIX)) { |
| name = name.substring(0, name.length()-PRODUCT_POSTFIX.length()); |
| } |
| if (name.contains(".")) { |
| name = name.substring(name.lastIndexOf(".")+1); |
| } |
| return name; |
| } |
| |
| public ApplicationProjectInspector(IProject productProject) { |
| this(productProject, extractProjectName(productProject)); |
| } |
| |
| public ApplicationProjectInspector(IProject applicationProject, String applicationFileName) { |
| this.applicationProject = applicationProject; |
| this.applicationFileName = applicationFileName; |
| } |
| |
| public IProject getApplicationProject() { |
| return applicationProject; |
| } |
| |
| public boolean isApplicationProject() { |
| initialize(); |
| if (productManifest.getFactoryModel().size() > 0) { |
| return true; |
| } |
| for (ManifestFileVerifier verifier : manifestVerifiers.values()) { |
| if (verifier.getFactoryModel().size() > 0) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| public boolean isTemplateFragmentProject() { |
| initialize(); |
| return productManifest.isFragmentOfHost("org.eclipse.osbp.wizard.ui"); |
| } |
| |
| public ProductFilePluginsVerifier getProductPlugins() { |
| initialize(); |
| return productPlugins; |
| } |
| |
| public AItemDescribed getProductConfigurations() { |
| initialize(); |
| return productConfigurations; |
| } |
| |
| public ManifestFileVerifier getProductManifest() { |
| initialize(); |
| return productManifest; |
| } |
| |
| public Collection<ManifestFileVerifier> getManifestVerifiers() { |
| initialize(); |
| return manifestVerifiers.values(); |
| } |
| |
| private void initialize() { |
| if (manifestVerifiers == null) { |
| productPlugins = new ProductFilePluginsVerifier(applicationProject, applicationFileName); |
| productConfigurations = new ProductFileConfigurationsVerifier(applicationProject, applicationFileName); |
| productManifest = new ManifestFileVerifier(applicationProject); |
| manifestVerifiers = new TreeMap<>(); |
| getPluginBundleManifests(manifestVerifiers, productPlugins, productConfigurations); |
| getRequireBundleManifests(manifestVerifiers, productManifest); |
| } |
| } |
| |
| /** |
| * @param verifiers the target map, which will contain all relevant verifiers verifying specific manifest files |
| * @param productVerifiers a list product file verifiers, which will be inspected for required model bundles, which will be inspected themselves recursively |
| */ |
| private void getPluginBundleManifests(Map<String,ManifestFileVerifier> verifiers, ProductFileVerifier... productVerifiers) { |
| Set<String> requireBundle = null; |
| for (ProductFileVerifier verifier : productVerifiers) { |
| if (requireBundle == null) { |
| requireBundle = new TreeSet<String>(verifier.getPluginBundles().keySet()); |
| } |
| else { |
| requireBundle.addAll(verifier.getPluginBundles().keySet()); |
| } |
| } |
| for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| if (project.isOpen()) { |
| ManifestFileVerifier manifest = new ManifestFileVerifier(project); |
| if (manifest.getFactoryModel().size() > 0) { |
| if (requireBundle.contains(manifest.getSymbolicName())) { |
| if (!verifiers.containsKey(manifest.getSymbolicName())) { |
| verifiers.put(manifest.getSymbolicName(), manifest); |
| getRequireBundleManifests(verifiers, manifest); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| /** |
| * @param verifiers the target map, which will contain all relevant verifiers verifying specific manifest files |
| * @param rootManifest a single model bundle manifest verifier, who will be inspected for required model bundles, which will be inspected themselves recursively |
| */ |
| private void getRequireBundleManifests(Map<String,ManifestFileVerifier> verifiers, ManifestFileVerifier rootManifest) { |
| Set<String> requireBundle = rootManifest.getRequireBundle(); |
| for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| if (project.isOpen()) { |
| ManifestFileVerifier manifest = new ManifestFileVerifier(project); |
| if (manifest.getFactoryModel().size() > 0) { |
| String symbolicName = manifest.getSymbolicName(); |
| if (requireBundle.contains(symbolicName)) { |
| if (!verifiers.containsKey(symbolicName)) { |
| verifiers.put(symbolicName, manifest); |
| getRequireBundleManifests(verifiers, manifest); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| /** |
| * @param findProject the project to be found |
| * @return true if the project is contained by the application project |
| */ |
| public boolean contains(IProject findProject) { |
| if (applicationProject.equals(findProject)) { |
| return true; |
| } |
| initialize(); |
| for (ManifestFileVerifier verifier : manifestVerifiers.values()) { |
| if (verifier.getProject().equals(findProject)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| /** |
| * @param findProject the project to be found |
| * @return true if the project is contained by the application project |
| */ |
| public IProject getProject(String projectPattern) { |
| if (applicationProject.getName().endsWith(projectPattern)) { |
| return applicationProject; |
| } |
| initialize(); |
| for (ManifestFileVerifier verifier : manifestVerifiers.values()) { |
| if (verifier.getProject().getName().endsWith(projectPattern)) { |
| return verifier.getProject(); |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * @return true if the wizard configuration file exists |
| */ |
| public boolean hasBeenGeneratedViaWizard() { |
| return applicationProject.findMember(WIZARD_CONFIGURATION_FILE) != null; |
| } |
| |
| /** |
| * @return true if the wizard configuration file exists |
| */ |
| public Properties loadWizardConfigurationProperties() { |
| Properties properties = new Properties(); |
| InputStream input = null; |
| try { |
| //getClass().getClassLoader().getResourceAsStream("config.properties"); |
| input = ((IFile)applicationProject.findMember(WIZARD_CONFIGURATION_FILE)).getContents(); |
| // load a properties file |
| properties.load(input); |
| } |
| catch (IOException | CoreException ex) { |
| ex.printStackTrace(); |
| } finally { |
| if (input != null) { |
| try { |
| input.close(); |
| } |
| catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return properties; |
| } |
| } |