use of io.apiman.common.plugin.PluginClassLoader in project apiman by apiman.
the class DefaultPluginRegistry method createPluginClassLoader.
/**
* Creates a plugin classloader for the given plugin file.
*/
protected PluginClassLoader createPluginClassLoader(final File pluginFile) throws IOException {
return new PluginClassLoader(pluginFile, Thread.currentThread().getContextClassLoader()) {
@Override
protected File createWorkDir(File pluginArtifactFile) throws IOException {
// $NON-NLS-1$
File workDir = new File(pluginFile.getParentFile(), ".work");
workDir.mkdirs();
return workDir;
}
};
}
use of io.apiman.common.plugin.PluginClassLoader in project apiman by apiman.
the class PluginResourceImpl method getPolicyForm.
/**
* @see IPluginResource#getPolicyForm(java.lang.Long, java.lang.String)
*/
@Override
public String getPolicyForm(Long pluginId, String policyDefId) throws PluginNotFoundException, PluginResourceNotFoundException, PolicyDefinitionNotFoundException {
// No permission check is needed
PluginBean pbean;
PolicyDefinitionBean pdBean;
try {
pbean = storage.getPlugin(pluginId);
if (pbean == null) {
throw ExceptionFactory.pluginNotFoundException(pluginId);
}
pdBean = storage.getPolicyDefinition(policyDefId);
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
PluginCoordinates coordinates = new PluginCoordinates(pbean.getGroupId(), pbean.getArtifactId(), pbean.getVersion(), pbean.getClassifier(), pbean.getType());
try {
if (pdBean == null) {
throw ExceptionFactory.policyDefNotFoundException(policyDefId);
}
if (pdBean.getPluginId() == null || !pdBean.getPluginId().equals(pbean.getId())) {
throw ExceptionFactory.pluginNotFoundException(pluginId);
}
if (pdBean.getFormType() == PolicyFormType.JsonSchema && pdBean.getForm() != null) {
String formPath = pdBean.getForm();
if (!formPath.startsWith("/")) {
// $NON-NLS-1$
// $NON-NLS-1$
formPath = "META-INF/apiman/policyDefs/" + formPath;
} else {
formPath = formPath.substring(1);
}
Plugin plugin = pluginRegistry.loadPlugin(coordinates);
PluginClassLoader loader = plugin.getLoader();
InputStream resource = null;
try {
resource = loader.getResourceAsStream(formPath);
if (resource == null) {
throw ExceptionFactory.pluginResourceNotFoundException(formPath, coordinates);
}
StringWriter writer = new StringWriter();
IOUtils.copy(resource, writer);
return writer.toString();
} finally {
IOUtils.closeQuietly(resource);
}
} else {
throw ExceptionFactory.pluginResourceNotFoundException(null, coordinates);
}
} catch (AbstractRestException e) {
throw e;
} catch (Throwable t) {
throw new SystemErrorException(t);
}
}
use of io.apiman.common.plugin.PluginClassLoader in project apiman by apiman.
the class PolicyFactoryImpl method doLoadFromPlugin.
/**
* Loads a policy from a plugin.
* @param policyImpl
* @param handler
*/
private void doLoadFromPlugin(final String policyImpl, final IAsyncResultHandler<IPolicy> handler) {
PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(policyImpl);
if (coordinates == null) {
handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl)));
return;
}
int ssidx = policyImpl.indexOf('/');
if (ssidx == -1) {
handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl)));
return;
}
final String classname = policyImpl.substring(ssidx + 1);
this.pluginRegistry.loadPlugin(coordinates, new IAsyncResultHandler<Plugin>() {
@Override
public void handle(IAsyncResult<Plugin> result) {
if (result.isSuccess()) {
IPolicy rval;
Plugin plugin = result.getResult();
PluginClassLoader pluginClassLoader = plugin.getLoader();
ClassLoader oldCtxLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(pluginClassLoader);
Class<?> c = pluginClassLoader.loadClass(classname);
rval = (IPolicy) c.newInstance();
} catch (Exception e) {
handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, e)));
return;
} finally {
Thread.currentThread().setContextClassLoader(oldCtxLoader);
}
policyCache.put(policyImpl, rval);
handler.handle(AsyncResultImpl.create(rval));
} else {
handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, result.getError())));
}
}
});
}
use of io.apiman.common.plugin.PluginClassLoader in project apiman by apiman.
the class AbstractPluginRegistry method loadPlugin.
/**
* @see io.apiman.manager.api.core.IPluginRegistry#loadPlugin(io.apiman.common.plugin.PluginCoordinates)
*/
@Override
public Plugin loadPlugin(PluginCoordinates coordinates) throws InvalidPluginException {
boolean isSnapshot = PluginUtils.isSnapshot(coordinates);
synchronized (mutex) {
if (pluginCache.containsKey(coordinates)) {
Plugin cachedPlugin = pluginCache.get(coordinates);
if (isSnapshot) {
pluginCache.remove(coordinates);
try {
cachedPlugin.getLoader().close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
return cachedPlugin;
}
}
String pluginRelativePath = PluginUtils.getPluginRelativePath(coordinates);
File pluginDir = new File(pluginsDir, pluginRelativePath);
if (!pluginDir.exists()) {
pluginDir.mkdirs();
}
// $NON-NLS-1$
File pluginFile = new File(pluginDir, "plugin." + coordinates.getType());
// Clean up stale files in the case of a snapshot.
if (pluginFile.exists() && isSnapshot) {
try {
FileUtils.deleteDirectory(pluginFile.getParentFile());
pluginFile.getParentFile().mkdirs();
} catch (IOException e) {
e.printStackTrace();
}
}
// Doesn't exist (or it's a snapshot)? Better download it.
if (!pluginFile.exists()) {
downloadPlugin(pluginFile, coordinates);
}
// Still doesn't exist? That's a failure.
if (!pluginFile.exists()) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.PluginNotFound"));
}
PluginClassLoader pluginClassLoader;
try {
pluginClassLoader = createPluginClassLoader(pluginFile);
} catch (IOException e) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
}
URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
if (specFile == null) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
}
try {
PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
pluginCache.put(coordinates, plugin);
return plugin;
} catch (Exception e) {
// $NON-NLS-1$
throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.FailedToReadSpecFile", PluginUtils.PLUGIN_SPEC_PATH), e);
}
}
}
use of io.apiman.common.plugin.PluginClassLoader in project apiman by apiman.
the class DefaultPluginRegistry method readPluginFile.
/**
* Reads the plugin into an object. This method will fail if the plugin is not valid.
* This could happen if the file is not a java archive, or if the plugin spec file is
* missing from the archive, etc.
*/
protected Plugin readPluginFile(PluginCoordinates coordinates, File pluginFile) throws Exception {
try {
PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginFile);
URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
if (specFile == null) {
// $NON-NLS-1$
throw new Exception(Messages.i18n.format("DefaultPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
} else {
PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
// $NON-NLS-1$
LOGGER.info("Read apiman plugin: {0}", spec);
return plugin;
}
} catch (Exception e) {
// $NON-NLS-1$
throw new Exception(Messages.i18n.format("DefaultPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
}
}
Aggregations