use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.
the class CloneMediator method init.
public void init(SynapseEnvironment se) {
synapseEnv = se;
for (Target target : targets) {
ManagedLifecycle seq = target.getSequence();
if (seq != null) {
seq.init(se);
} else if (target.getSequenceRef() != null) {
SequenceMediator targetSequence = (SequenceMediator) se.getSynapseConfiguration().getSequence(target.getSequenceRef());
if (targetSequence == null || targetSequence.isDynamic()) {
se.addUnavailableArtifactRef(target.getSequenceRef());
}
}
Endpoint endpoint = target.getEndpoint();
if (endpoint != null) {
endpoint.init(se);
}
}
}
use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.
the class QuartzTaskManager method schedule.
@Override
public boolean schedule(TaskDescription taskDescription) {
assertInitialized();
assertStarted();
if (taskDescription == null) {
throw new SynapseTaskException("Task Description cannot be found", logger);
}
Trigger trigger;
JobDetail jobDetail;
synchronized (lock) {
if (triggerFactory == null) {
throw new SynapseTaskException("TriggerFactory cannot be found", logger);
}
if (jobDetailFactory == null) {
throw new SynapseTaskException("JobDetailFactory cannot be found", logger);
}
trigger = triggerFactory.createTrigger(taskDescription);
if (trigger == null) {
throw new SynapseTaskException("Trigger cannot be created from : " + taskDescription, logger);
}
jobDetail = jobDetailFactory.createJobDetail(taskDescription, taskDescription.getResources(), SimpleQuartzJob.class);
if (jobDetail == null) {
throw new SynapseTaskException("JobDetail cannot be created from : " + taskDescription + " and job class " + taskDescription.getTaskImplClassName(), logger);
}
}
Object clsInstance = taskDescription.getResource(TaskDescription.INSTANCE);
if (clsInstance == null) {
String className = (String) taskDescription.getProperty(TaskDescription.CLASSNAME);
try {
clsInstance = Class.forName(className).newInstance();
if (clsInstance instanceof ManagedLifecycle) {
Object se = properties.get(TaskConstants.SYNAPSE_ENV);
if (!(se instanceof SynapseEnvironment)) {
return false;
}
((ManagedLifecycle) clsInstance).init((SynapseEnvironment) se);
}
for (Object property : taskDescription.getXmlProperties()) {
OMElement prop = (OMElement) property;
logger.debug("Found Property : " + prop.toString());
PropertyHelper.setStaticProperty(prop, clsInstance);
}
} catch (ClassNotFoundException e) {
logger.error("Could not schedule task[" + name + "].", e);
return false;
} catch (InstantiationException e) {
logger.error("Could not schedule task[" + name + "].", e);
return false;
} catch (IllegalAccessException e) {
logger.error("Could not schedule task[" + name + "].", e);
return false;
}
}
if (!(clsInstance instanceof Task)) {
logger.error("Could not schedule task[" + name + "]. Cannot load class " + "org.apache.synapse.startup.quartz.SimpleQuartzJob");
return false;
}
jobDetail.getJobDataMap().put(TaskDescription.INSTANCE, clsInstance);
jobDetail.getJobDataMap().put(TaskDescription.CLASSNAME, clsInstance.getClass().toString());
jobDetail.getJobDataMap().put(TaskConstants.SYNAPSE_ENV, getProperty(TaskConstants.SYNAPSE_ENV));
try {
if (logger.isDebugEnabled()) {
logger.debug("scheduling job : " + jobDetail + " with trigger " + trigger);
}
if (taskDescription.getCount() != 0 && !isTaskRunning(jobDetail.getKey())) {
try {
synchronized (lock) {
scheduler.scheduleJob(jobDetail, trigger);
}
} catch (ObjectAlreadyExistsException e) {
logger.warn("did not schedule the job : " + jobDetail + ". the job is already running.");
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("did not schedule the job : " + jobDetail + ". count is zero.");
}
}
} catch (SchedulerException e) {
throw new SynapseTaskException("Error scheduling job : " + jobDetail + " with trigger " + trigger);
}
logger.info("Scheduled task [" + taskDescription.getName() + "::" + taskDescription.getTaskGroup() + "]");
return true;
}
use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.
the class SimpleQuartzJob method execute.
public void execute(JobExecutionContext ctx) throws JobExecutionException {
String jobName = ctx.getJobDetail().getKey().getName();
if (log.isDebugEnabled()) {
log.debug("Executing task : " + jobName);
}
JobDataMap jdm = ctx.getMergedJobDataMap();
String jobClassName = (String) jdm.get(CLASSNAME);
if (jobClassName == null) {
handleException("No " + CLASSNAME + " in JobDetails");
}
boolean initRequired = false;
Task task = (Task) jdm.get(TaskDescription.INSTANCE);
if (task == null) {
initRequired = true;
}
SynapseEnvironment se = (SynapseEnvironment) jdm.get("SynapseEnvironment");
if (initRequired) {
try {
task = (Task) getClass().getClassLoader().loadClass(jobClassName).newInstance();
} catch (Exception e) {
handleException("Cannot instantiate task : " + jobClassName, e);
}
Set properties = (Set) jdm.get(PROPERTIES);
for (Object property : properties) {
OMElement prop = (OMElement) property;
log.debug("Found Property : " + prop.toString());
PropertyHelper.setStaticProperty(prop, task);
}
// 1. Initialize
if (task instanceof ManagedLifecycle && se != null) {
((ManagedLifecycle) task).init(se);
}
}
// 2. Execute
if (se != null && task != null && se.isInitialized()) {
task.execute();
}
if (initRequired) {
// 3. Destroy
if (task instanceof ManagedLifecycle && se != null) {
((ManagedLifecycle) task).destroy();
}
}
}
use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.
the class AbstractListMediator method init.
/**
* Initialize child mediators recursively
* @param se synapse environment
*/
public void init(SynapseEnvironment se) {
if (log.isDebugEnabled()) {
log.debug("Initializing child mediators of mediator : " + getType());
}
for (int i = 0; i < mediators.size(); i++) {
Mediator mediator = mediators.get(i);
mediator.setMediatorPosition(i);
if (mediator instanceof ManagedLifecycle) {
((ManagedLifecycle) mediator).init(se);
}
if (mediator.isContentAware()) {
if (log.isDebugEnabled()) {
log.debug(mediator.getType() + " is content aware, setting sequence <" + getType() + "> as content aware");
}
sequenceContentAware = true;
}
}
}
use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.
the class IndirectEndpoint method reLoadAndInitEndpoint.
/**
* Reload as needed , either from registry , local entries or predefined endpoints
* @param cc ConfigurationContext
*/
private synchronized void reLoadAndInitEndpoint(ConfigurationContext cc) {
Parameter parameter = cc.getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_CONFIG);
Parameter synEnvParameter = cc.getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
if (parameter.getValue() instanceof SynapseConfiguration && synEnvParameter.getValue() instanceof SynapseEnvironment) {
SynapseConfiguration synCfg = (SynapseConfiguration) parameter.getValue();
SynapseEnvironment synapseEnvironment = (SynapseEnvironment) synEnvParameter.getValue();
boolean reLoad = (realEndpoint == null);
if (!reLoad) {
Entry entry = synCfg.getEntryDefinition(key);
if (entry != null && entry.isDynamic()) {
if (!entry.isCached() || entry.isExpired()) {
reLoad = true;
}
} else {
// If the endpoint is static we should reload it from the Synapse config
reLoad = true;
}
}
if (reLoad) {
if (log.isDebugEnabled()) {
log.debug("Loading real endpoint with key : " + key);
}
realEndpoint = synCfg.getEndpoint(key);
if (realEndpoint != null && !realEndpoint.isInitialized()) {
realEndpoint.init(synapseEnvironment);
}
} else {
Endpoint epr = synCfg.getEndpoint(key);
if (epr != realEndpoint) {
realEndpoint = epr;
if (realEndpoint != null && !realEndpoint.isInitialized() && realEndpoint instanceof ManagedLifecycle) {
realEndpoint.init(synapseEnvironment);
}
}
}
}
}
Aggregations