use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.
the class TemplateDeployer method restoreSynapseArtifact.
@Override
public void restoreSynapseArtifact(String artifactName) {
if (log.isDebugEnabled()) {
log.debug("Restoring the Template with name : " + artifactName + " : Started");
}
try {
Template st = getSynapseConfiguration().getEndpointTemplate(artifactName);
if (st != null) {
TemplateSerializer ts = new TemplateSerializer();
OMElement stElem = ts.serializeEndpointTemplate(st, null);
if (st.getFileName() != null) {
String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.TEMPLATES_DIR + File.separator + st.getFileName();
writeToFile(stElem, fileName);
if (log.isDebugEnabled()) {
log.debug("Restoring the Endpoint Template with name : " + artifactName + " : Completed");
}
log.info("Template named '" + artifactName + "' has been restored");
}
} else {
TemplateMediator mt = getSynapseConfiguration().getSequenceTemplate(artifactName);
if (mt != null) {
TemplateMediatorSerializer ts = new TemplateMediatorSerializer();
OMElement stElem = ts.serializeMediator(null, mt);
if (mt.getFileName() != null) {
String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.TEMPLATES_DIR + File.separator + st.getFileName();
writeToFile(stElem, fileName);
if (log.isDebugEnabled()) {
log.debug("Restoring the Sequence Template with name : " + artifactName + " : Completed");
}
log.info("Template named '" + artifactName + "' has been restored");
}
} else {
handleSynapseArtifactDeploymentError("Couldn't restore the Template named '" + artifactName + "', filename cannot be found");
}
}
} catch (Exception e) {
handleSynapseArtifactDeploymentError("Restoring of the Template named '" + artifactName + "' has failed", e);
}
}
use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.
the class TemplateDeployer method updateSynapseArtifact.
@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
if (log.isDebugEnabled()) {
log.debug("Template update from file : " + fileName + " has started");
}
try {
OMElement element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
if (element != null) {
org.apache.synapse.config.xml.endpoints.TemplateFactory templateFactory = new org.apache.synapse.config.xml.endpoints.TemplateFactory();
Template tm = templateFactory.createEndpointTemplate(artifactConfig, properties);
tm.setFileName(new File(fileName).getName());
if (log.isDebugEnabled()) {
log.debug("Endpoint Template named '" + tm.getName() + "' has been built from the file " + fileName);
}
Template existingSt = getSynapseConfiguration().getEndpointTemplate(existingArtifactName);
if (existingArtifactName.equals(tm.getName())) {
getSynapseConfiguration().updateEndpointTemplate(tm.getName(), tm);
} else {
getSynapseConfiguration().addEndpointTemplate(tm.getName(), tm);
getSynapseConfiguration().removeEndpointTemplate(existingSt.getName());
log.info("Template: " + existingArtifactName + " has been undeployed");
}
log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
return tm.getName();
} else {
element = artifactConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sequence"));
if (element != null) {
Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(artifactConfig, properties);
if (mediator instanceof TemplateMediator) {
TemplateMediator tm = (TemplateMediator) mediator;
tm.setFileName((new File(fileName)).getName());
if (log.isDebugEnabled()) {
log.debug("Sequence Template named '" + tm.getName() + "' has been built from the file " + fileName);
}
tm.init(getSynapseEnvironment());
if (log.isDebugEnabled()) {
log.debug("Initialized the Template : " + tm.getName());
}
TemplateMediator existingSt = getSynapseConfiguration().getSequenceTemplate(existingArtifactName);
if (existingArtifactName.equals(tm.getName())) {
getSynapseConfiguration().updateSequenceTemplate(tm.getName(), tm);
} else {
getSynapseConfiguration().addSequenceTemplate(tm.getName(), tm);
getSynapseConfiguration().removeSequenceTemplate(existingSt.getName());
log.info("Template: " + existingArtifactName + " has been undeployed");
}
existingSt.destroy();
log.info("Template: " + tm.getName() + " has been updated from the file: " + fileName);
return tm.getName();
}
}
}
} catch (DeploymentException e) {
handleSynapseArtifactDeploymentError("Error while updating the Template from the " + "file: " + fileName);
}
return null;
}
use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.
the class SynapseConfiguration method getEndpointTemplate.
public Template getEndpointTemplate(String key) {
Object o = getEntry(key);
if (o instanceof Template) {
return (Template) o;
}
Entry entry = null;
if (o == null) {
entry = new Entry(key);
entry.setType(Entry.REMOTE_ENTRY);
} else {
Object object = localRegistry.get(key);
if (object instanceof Entry) {
entry = (Entry) object;
}
}
assertEntryNull(entry, key);
// noinspection ConstantConditions
if (entry.getMapper() == null) {
entry.setMapper(new XMLToTemplateMapper());
}
if (entry.getType() == Entry.REMOTE_ENTRY) {
if (registry != null) {
o = registry.getResource(entry, getProperties());
if (o != null && o instanceof Template) {
localRegistry.put(key, entry);
return (Template) o;
} else if (o instanceof OMNode) {
Template m = new TemplateFactory().createEndpointTemplate((OMElement) o, properties);
if (m != null) {
entry.setValue(m);
return m;
}
}
}
} else {
Object value = entry.getValue();
if (value instanceof OMNode) {
Object object = entry.getMapper().getObjectFromOMNode((OMNode) value, getProperties());
if (object instanceof Template) {
entry.setValue(object);
return (Template) object;
}
}
}
// load from available libraries
Template templateFromLib = LibDeployerUtils.getLibArtifact(synapseLibraries, key, Template.class);
if (templateFromLib != null) {
return templateFromLib;
}
return null;
}
use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.
the class SynapseConfiguration method getEndpointTemplates.
/**
* Returns the map of defined synapse endpoint templates in the configuration excluding the
* fetched sequences from remote registry.
*
* @return Map of Templates defined in the local configuration
*/
public Map<String, Template> getEndpointTemplates() {
Map<String, Template> definedTemplates = new HashMap<String, Template>();
synchronized (this) {
for (Object o : localRegistry.values()) {
if (o instanceof Template) {
Template template = (Template) o;
definedTemplates.put(template.getName(), template);
}
}
}
return definedTemplates;
}
use of org.apache.synapse.endpoints.Template in project wso2-synapse by wso2.
the class MultiXMLConfigurationBuilder method createTemplates.
private static void createTemplates(SynapseConfiguration synapseConfig, String rootDirPath, Properties properties) {
File templatesDir = new File(rootDirPath, TEMPLATES_DIR);
if (templatesDir.exists()) {
if (log.isDebugEnabled()) {
log.debug("Loading template from : " + templatesDir.getPath());
}
Iterator templates = FileUtils.iterateFiles(templatesDir, extensions, false);
while (templates.hasNext()) {
File file = (File) templates.next();
try {
OMElement document = getOMElement(file);
OMElement element = document.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "sequence"));
if (element != null) {
TemplateMediator mediator = (TemplateMediator) SynapseXMLConfigurationFactory.defineMediatorTemplate(synapseConfig, document, properties);
if (mediator != null) {
mediator.setFileName(file.getName());
synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(), mediator.getName());
}
} else {
element = document.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint"));
if (element != null) {
Template endpointTemplate = SynapseXMLConfigurationFactory.defineEndpointTemplate(synapseConfig, document, properties);
if (endpointTemplate != null) {
endpointTemplate.setFileName(file.getName());
synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(), endpointTemplate.getName());
}
}
}
} catch (Exception e) {
String msg = "Template configuration cannot be built from : " + file.getName();
handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_TEMPLATES, msg, e);
}
}
}
}
Aggregations