blob: 497dde0e29ba2c5ce6858128e5da1dab39d13edc [file] [log] [blame]
/**
*
* 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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.preferences.ui.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
/**
* Class for each block of information inside a <code>/META-INF/MANIFEST.MF</code> configuration file.
* <br>
* The file will be loaded via <code>java.util.jar.Manifest</code> and utilities from <code>org.apache.felix.utils.manifest.*</code>.
*/
public class ManifestFileVerifier extends ConfigurationFileVerifier {
private final static String MANIFEST_MF_PATH = "/META-INF/MANIFEST.MF";
private final static String SYMBOLIC_NAME = "Bundle-SymbolicName";
private final static String REQUIRE_BUNDLE = "Require-Bundle";
private final static String IMPORT_PACKAGE = "Import-Package";
private final static String FACTORY_MODEL = "Factory-Model";
private final static String FRAGMENT_HOST = "Fragment-Host";
private final Attributes attributes;
private final String rowName;
private final String nodeName;
public ManifestFileVerifier(IProject project) {
super(project);
IPath relativeFilePath = project.getFile(MANIFEST_MF_PATH).getProjectRelativePath();
IPath locationFilePath = project.getFile(MANIFEST_MF_PATH).getLocation();
Attributes attributes = null;
if (project.exists(relativeFilePath)) {
try {
Manifest manifest = new Manifest(new FileInputStream(new File(locationFilePath.toOSString())));
attributes = manifest.getMainAttributes();
}
catch (IOException e) {
e.printStackTrace();
}
}
this.attributes = attributes;
String hint = "";
if ((getAttributeValue(FACTORY_MODEL) != null) && !getAttributeValue(FACTORY_MODEL).isEmpty()) {
hint = " - models: "+getAttributeValue(FACTORY_MODEL);
rowName = "Model manifest";
}
else {
rowName = "Product manifest";
}
nodeName = "TODO: "+project.getName()+MANIFEST_MF_PATH+hint;
}
/**
* @return the symbolic name without the possible postfix defining a <code>singleton</code>.
*/
public String getSymbolicName() {
return getAttributeValue(SYMBOLIC_NAME).replace(";singleton:=true", "");
}
/**
* @param fragmentHostName name of the requested bundle host
* @return true if the project is a fragment of the given bundle host
*/
public boolean isFragmentOfHost(String fragmentHostName) {
return
(getAttributeValue(FRAGMENT_HOST) != null) &&
(
// either starts with the hostname following with some more information
getAttributeValue(FRAGMENT_HOST).startsWith(fragmentHostName+";")
||
// or equals the hostname without any more information
getAttributeValue(FRAGMENT_HOST).equals(fragmentHostName)
);
}
protected Set<String> getImportPackage() {
return getAttributeValues(IMPORT_PACKAGE);
}
public Set<String> getRequireBundle() {
return getAttributeValues(REQUIRE_BUNDLE);
}
public Set<String> getFactoryModel() {
return getAttributeValues(FACTORY_MODEL);
}
/**
* Some attributes contain a set of single items, like <code>Import-Package</code>, <code>Require-Bundle</code>, and <code>Factory-Model</code>.
* @param attribute the name of the attribute to be loaded
* @return set of single items, which will be extracted via utilities from <code>org.apache.felix.utils.manifest.*</code>
*/
private Set<String> getAttributeValues(String attribute) {
TreeSet<String> values = new TreeSet<String>();
String value = getAttributeValue(attribute);
try {
Clause[] clauses = Parser.parseHeader(value);
for (Clause clause : clauses) {
values.add(clause.getName());
}
}
catch (IllegalArgumentException ia) {}
return values;
}
private String getAttributeValue(String attribute) {
if (attributes == null) {
return "";
}
else {
return attributes.getValue(attribute);
}
}
@Override
public String getName() {
return nodeName;
}
@Override
public String getRowName() {
return rowName;
}
}