|
|
@ -13,6 +13,10 @@ |
|
|
|
package ro.fortsoft.pf4j; |
|
|
|
package ro.fortsoft.pf4j; |
|
|
|
|
|
|
|
|
|
|
|
import com.github.zafarkhaja.semver.Version; |
|
|
|
import com.github.zafarkhaja.semver.Version; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import ro.fortsoft.pf4j.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileNotFoundException; |
|
|
@ -20,11 +24,6 @@ import java.io.IOException; |
|
|
|
import java.util.jar.Attributes; |
|
|
|
import java.util.jar.Attributes; |
|
|
|
import java.util.jar.Manifest; |
|
|
|
import java.util.jar.Manifest; |
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import ro.fortsoft.pf4j.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Read the plugin descriptor from the manifest file. |
|
|
|
* Read the plugin descriptor from the manifest file. |
|
|
|
* |
|
|
|
* |
|
|
@ -42,74 +41,87 @@ public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public PluginDescriptor find(File pluginRepository) throws PluginException { |
|
|
|
public PluginDescriptor find(File pluginRepository) throws PluginException { |
|
|
|
// TODO it's ok with first classes directory? Another idea is to specify in PluginClasspath the folder.
|
|
|
|
Manifest manifest = readManifest(pluginRepository); |
|
|
|
String classes = pluginClasspath.getClassesDirectories().get(0); |
|
|
|
|
|
|
|
|
|
|
|
PluginDescriptor pluginDescriptor = createPluginDescriptor(manifest); |
|
|
|
|
|
|
|
validatePluginDescriptor(pluginDescriptor); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return pluginDescriptor; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected Manifest readManifest(File pluginRepository) throws PluginException { |
|
|
|
|
|
|
|
// TODO it's ok with first classes directory? Another idea is to specify in PluginClasspath the folder.
|
|
|
|
|
|
|
|
String classes = pluginClasspath.getClassesDirectories().get(0); |
|
|
|
File manifestFile = new File(pluginRepository, classes + "/META-INF/MANIFEST.MF"); |
|
|
|
File manifestFile = new File(pluginRepository, classes + "/META-INF/MANIFEST.MF"); |
|
|
|
log.debug("Lookup plugin descriptor in '{}'", manifestFile); |
|
|
|
log.debug("Lookup plugin descriptor in '{}'", manifestFile); |
|
|
|
if (!manifestFile.exists()) { |
|
|
|
if (!manifestFile.exists()) { |
|
|
|
throw new PluginException("Cannot find '" + manifestFile + "' file"); |
|
|
|
throw new PluginException("Cannot find '" + manifestFile + "' file"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
FileInputStream input = null; |
|
|
|
FileInputStream input = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
input = new FileInputStream(manifestFile); |
|
|
|
input = new FileInputStream(manifestFile); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
// not happening
|
|
|
|
// not happening
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Manifest manifest = null; |
|
|
|
Manifest manifest = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
manifest = new Manifest(input); |
|
|
|
manifest = new Manifest(input); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new PluginException(e.getMessage(), e); |
|
|
|
throw new PluginException(e.getMessage(), e); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
try { |
|
|
|
input.close(); |
|
|
|
input.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new PluginException(e.getMessage(), e); |
|
|
|
throw new PluginException(e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return manifest; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected PluginDescriptor createPluginDescriptor(Manifest manifest) { |
|
|
|
PluginDescriptor pluginDescriptor = new PluginDescriptor(); |
|
|
|
PluginDescriptor pluginDescriptor = new PluginDescriptor(); |
|
|
|
|
|
|
|
|
|
|
|
// TODO validate !!!
|
|
|
|
// TODO validate !!!
|
|
|
|
Attributes attrs = manifest.getMainAttributes(); |
|
|
|
Attributes attributes = manifest.getMainAttributes(); |
|
|
|
String id = attrs.getValue("Plugin-Id"); |
|
|
|
String id = attributes.getValue("Plugin-Id"); |
|
|
|
if (StringUtils.isEmpty(id)) { |
|
|
|
|
|
|
|
throw new PluginException("Plugin-Id cannot be empty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pluginDescriptor.setPluginId(id); |
|
|
|
pluginDescriptor.setPluginId(id); |
|
|
|
|
|
|
|
|
|
|
|
String description = attrs.getValue("Plugin-Description"); |
|
|
|
String description = attributes.getValue("Plugin-Description"); |
|
|
|
if (StringUtils.isEmpty(description)) { |
|
|
|
if (StringUtils.isEmpty(description)) { |
|
|
|
pluginDescriptor.setPluginDescription(""); |
|
|
|
pluginDescriptor.setPluginDescription(""); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
pluginDescriptor.setPluginDescription(description); |
|
|
|
pluginDescriptor.setPluginDescription(description); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String clazz = attrs.getValue("Plugin-Class"); |
|
|
|
String clazz = attributes.getValue("Plugin-Class"); |
|
|
|
if (StringUtils.isEmpty(clazz)) { |
|
|
|
|
|
|
|
throw new PluginException("Plugin-Class cannot be empty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pluginDescriptor.setPluginClass(clazz); |
|
|
|
pluginDescriptor.setPluginClass(clazz); |
|
|
|
|
|
|
|
|
|
|
|
String version = attrs.getValue("Plugin-Version"); |
|
|
|
String version = attributes.getValue("Plugin-Version"); |
|
|
|
if (StringUtils.isEmpty(version)) { |
|
|
|
|
|
|
|
throw new PluginException("Plugin-Version cannot be empty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pluginDescriptor.setPluginVersion(Version.valueOf(version)); |
|
|
|
pluginDescriptor.setPluginVersion(Version.valueOf(version)); |
|
|
|
|
|
|
|
|
|
|
|
String provider = attrs.getValue("Plugin-Provider"); |
|
|
|
String provider = attributes.getValue("Plugin-Provider"); |
|
|
|
pluginDescriptor.setProvider(provider); |
|
|
|
pluginDescriptor.setProvider(provider); |
|
|
|
String dependencies = attrs.getValue("Plugin-Dependencies"); |
|
|
|
String dependencies = attributes.getValue("Plugin-Dependencies"); |
|
|
|
pluginDescriptor.setDependencies(dependencies); |
|
|
|
pluginDescriptor.setDependencies(dependencies); |
|
|
|
|
|
|
|
|
|
|
|
String requires = attrs.getValue("Plugin-Requires"); |
|
|
|
String requires = attributes.getValue("Plugin-Requires"); |
|
|
|
if (StringUtils.isNotEmpty(requires)) { |
|
|
|
if (StringUtils.isNotEmpty(requires)) { |
|
|
|
pluginDescriptor.setRequires(requires); |
|
|
|
pluginDescriptor.setRequires(requires); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return pluginDescriptor; |
|
|
|
return pluginDescriptor; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void validatePluginDescriptor(PluginDescriptor pluginDescriptor) throws PluginException { |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(pluginDescriptor.getPluginId())) { |
|
|
|
|
|
|
|
throw new PluginException("Plugin-Id cannot be empty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (StringUtils.isEmpty(pluginDescriptor.getPluginClass())) { |
|
|
|
|
|
|
|
throw new PluginException("Plugin-Class cannot be empty"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|