use of io.apiman.manager.api.core.exceptions.InvalidPluginException 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.manager.api.core.exceptions.InvalidPluginException in project apiman by apiman.
the class PluginResourceImpl method create.
/**
* @see IPluginResource#create(io.apiman.manager.api.beans.plugins.NewPluginBean)
*/
@Override
public PluginBean create(NewPluginBean bean) throws PluginAlreadyExistsException, PluginNotFoundException, NotAuthorizedException {
securityContext.checkAdminPermissions();
PluginCoordinates coordinates = new PluginCoordinates(bean.getGroupId(), bean.getArtifactId(), bean.getVersion(), bean.getClassifier(), bean.getType());
boolean isSnapshot = PluginUtils.isSnapshot(coordinates);
if (isSnapshot) {
// $NON-NLS-1$
LOGGER.debug("Loading a snapshot version of plugin: " + coordinates);
}
boolean isUpgrade = isSnapshot || bean.isUpgrade();
Plugin plugin;
try {
plugin = pluginRegistry.loadPlugin(coordinates);
bean.setName(plugin.getName());
bean.setDescription(plugin.getDescription());
} catch (InvalidPluginException e) {
throw new PluginNotFoundException(coordinates.toString(), e);
}
PluginBean pluginBean = new PluginBean();
pluginBean.setGroupId(bean.getGroupId());
pluginBean.setArtifactId(bean.getArtifactId());
pluginBean.setVersion(bean.getVersion());
pluginBean.setClassifier(bean.getClassifier());
pluginBean.setType(bean.getType());
pluginBean.setName(bean.getName());
pluginBean.setDescription(bean.getDescription());
pluginBean.setCreatedBy(securityContext.getCurrentUser());
pluginBean.setCreatedOn(new Date());
try {
PluginBean existingPlugin = storage.getPlugin(bean.getGroupId(), bean.getArtifactId());
boolean hasExistingPlugin = existingPlugin != null && !existingPlugin.isDeleted();
boolean isUpdatePolicyDefs = false;
if (hasExistingPlugin && !isUpgrade) {
throw ExceptionFactory.pluginAlreadyExistsException();
} else if (hasExistingPlugin && isUpgrade) {
isUpdatePolicyDefs = true;
existingPlugin.setName(pluginBean.getName());
existingPlugin.setDescription(pluginBean.getDescription());
existingPlugin.setVersion(pluginBean.getVersion());
existingPlugin.setClassifier(pluginBean.getClassifier());
existingPlugin.setType(pluginBean.getType());
pluginBean.setId(existingPlugin.getId());
storage.updatePlugin(existingPlugin);
} else if (!hasExistingPlugin && existingPlugin != null) {
isUpdatePolicyDefs = true;
existingPlugin.setName(pluginBean.getName());
existingPlugin.setDescription(pluginBean.getDescription());
existingPlugin.setVersion(pluginBean.getVersion());
existingPlugin.setClassifier(pluginBean.getClassifier());
existingPlugin.setType(pluginBean.getType());
existingPlugin.setCreatedOn(new Date());
existingPlugin.setCreatedBy(securityContext.getCurrentUser());
existingPlugin.setDeleted(false);
pluginBean.setId(existingPlugin.getId());
storage.updatePlugin(existingPlugin);
} else {
if (bean.isUpgrade()) {
throw ExceptionFactory.pluginNotFoundException(0L);
}
storage.createPlugin(pluginBean);
}
// Process any contributed policy definitions.
List<URL> policyDefs = plugin.getPolicyDefinitions();
int createdPolicyDefCounter = 0;
int updatedPolicyDefCounter = 0;
for (URL url : policyDefs) {
PolicyDefinitionBean policyDef = (PolicyDefinitionBean) MAPPER.reader(PolicyDefinitionBean.class).readValue(url);
if (policyDef.getId() == null || policyDef.getId().trim().isEmpty()) {
// $NON-NLS-1$
throw ExceptionFactory.policyDefInvalidException(Messages.i18n.format("PluginResourceImpl.MissingPolicyDefId", policyDef.getName()));
}
policyDef.setPluginId(pluginBean.getId());
if (policyDef.getId() == null) {
policyDef.setId(BeanUtils.idFromName(policyDef.getName()));
} else {
policyDef.setId(BeanUtils.idFromName(policyDef.getId()));
}
if (policyDef.getFormType() == null) {
policyDef.setFormType(PolicyFormType.Default);
}
PolicyDefinitionBean existingPolicyDef = storage.getPolicyDefinition(policyDef.getId());
if (existingPolicyDef == null) {
storage.createPolicyDefinition(policyDef);
createdPolicyDefCounter++;
} else if (isUpdatePolicyDefs) {
existingPolicyDef.setName(policyDef.getName());
existingPolicyDef.setDescription(policyDef.getDescription());
existingPolicyDef.setIcon(policyDef.getIcon());
existingPolicyDef.getTemplates().clear();
existingPolicyDef.getTemplates().addAll(policyDef.getTemplates());
existingPolicyDef.setFormType(policyDef.getFormType());
existingPolicyDef.setForm(policyDef.getForm());
existingPolicyDef.setDeleted(false);
existingPolicyDef.setPolicyImpl(policyDef.getPolicyImpl());
storage.updatePolicyDefinition(existingPolicyDef);
updatedPolicyDefCounter++;
} else {
// $NON-NLS-1$
throw ExceptionFactory.policyDefInvalidException(Messages.i18n.format("PluginResourceImpl.DuplicatePolicyDef", policyDef.getId()));
}
}
LOGGER.info(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Created plugin mvn:%s:%s:%s", // $NON-NLS-1$
pluginBean.getGroupId(), // $NON-NLS-1$
pluginBean.getArtifactId(), pluginBean.getVersion()));
// $NON-NLS-1$
LOGGER.info(String.format("\tCreated %s policy definitions from plugin.", String.valueOf(createdPolicyDefCounter)));
if (isUpdatePolicyDefs) {
// $NON-NLS-1$
LOGGER.info(String.format("\tUpdated %s policy definitions from plugin.", String.valueOf(updatedPolicyDefCounter)));
}
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
return pluginBean;
}
Aggregations