001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.wso2.esb.services;
018:
019: import org.apache.axiom.om.OMAttribute;
020: import org.apache.axiom.om.OMElement;
021: import org.apache.axiom.om.xpath.AXIOMXPath;
022: import org.apache.axis2.AxisFault;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.synapse.Startup;
026: import org.apache.synapse.SynapseConstants;
027: import org.apache.synapse.SynapseException;
028: import org.apache.synapse.config.SynapseConfiguration;
029: import org.apache.synapse.config.xml.StartupFinder;
030: import org.apache.synapse.core.SynapseEnvironment;
031: import org.apache.synapse.startup.quartz.SimpleQuartz;
032: import org.apache.synapse.startup.quartz.SimpleQuartzFactory;
033: import org.jaxen.JaxenException;
034: import org.wso2.esb.services.tos.TaskData;
035:
036: import javax.xml.namespace.QName;
037: import java.util.ArrayList;
038: import java.util.Collections;
039: import java.util.Comparator;
040: import java.util.List;
041:
042: /**
043: *
044: */
045: public class TaskAdmin extends AbstractESBAdmin {
046:
047: private Log log = LogFactory.getLog(TaskAdmin.class);
048:
049: /**
050: * Gives the list of Tasks in the SynapseConfiguration as a TaskData transfer objects
051: *
052: * @return Array of TaskData objects
053: * @throws AxisFault if any error occured in reading the contents from the
054: * SynapseConfiguration
055: */
056: public TaskData[] taskData() throws AxisFault {
057: SynapseConfiguration synCfg = getSynapseConfiguration();
058: ArrayList<TaskData> taskData = new ArrayList<TaskData>();
059: // for the moment we only have tasks
060: for (Object obj : synCfg.getStartups()) {
061: if (obj instanceof SimpleQuartz) {
062: TaskData td = new TaskData();
063: SimpleQuartz q = (SimpleQuartz) obj;
064: td.setName(q.getName());
065: td.setClazz(q.getJobClass());
066: td.setEnableStatistics(false);
067: td.setEnableTracing(false);
068: taskData.add(td);
069: }
070: }
071:
072: Collections.sort(taskData, new Comparator() {
073: public int compare(Object o1, Object o2) {
074: return ((TaskData) o1).getName().compareToIgnoreCase(
075: ((TaskData) o2).getName());
076: }
077: });
078:
079: return taskData.toArray(new TaskData[taskData.size()]);
080: }
081:
082: /**
083: * Deletes the task with the given name from SynapseConfiguration
084: *
085: * @param taskName - Name of the task to delete
086: * @throws AxisFault if the Task described by the given name doesnt
087: * exists in the Synapse Configuration
088: */
089: public void deleteTask(String taskName) throws AxisFault {
090: try {
091: log.debug("Deleting task : " + taskName);
092:
093: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
094: Startup st = synapseConfiguration.getStartup(taskName);
095: if (st != null) {
096: st.destroy();
097: }
098: synapseConfiguration.removeStartup(taskName);
099: log.info("Deleted task : " + taskName
100: + " from the configuration");
101: } catch (SynapseException syne) {
102: handleFault(log, "Error removing sequence : " + taskName,
103: syne);
104: }
105: }
106:
107: /**
108: * Returns the OMelement representation of the task given by task name
109: *
110: * @param taskName - name of the sequence to get
111: * @return OMElement representing the Task of the given task name
112: * @throws AxisFault if any error occured while getting the data from the
113: * SynapseConfiguration
114: */
115: public OMElement getTask(String taskName) throws AxisFault {
116: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
117: try {
118: if (synapseConfiguration.getStartup(taskName) != null) {
119: OMElement taskElement = StartupFinder.getInstance()
120: .serializeStartup(
121: null,
122: synapseConfiguration
123: .getStartup(taskName));
124:
125: AXIOMXPath xPathLast = new AXIOMXPath("//syn:property");
126: xPathLast.addNamespace("syn",
127: SynapseConstants.SYNAPSE_NAMESPACE);
128: List<OMElement> selectNodes = xPathLast
129: .selectNodes(taskElement);
130:
131: for (OMElement selectedNode : selectNodes) {
132: OMAttribute attributeValue = selectedNode
133: .getAttribute(new QName("value"));
134: if (attributeValue == null) {
135: OMElement firstElement = selectedNode
136: .getFirstElement();
137: selectedNode.setText(firstElement.toString());
138: firstElement.detach();
139: }
140:
141: }
142:
143: return taskElement;
144: } else {
145: handleFault(log, "Task with the name " + taskName
146: + " does not exist", null);
147: }
148: } catch (SynapseException syne) {
149: handleFault(log, "Unable to get the task : " + taskName,
150: syne);
151: } catch (JaxenException e) {
152: handleFault(log, "Unable to get the task : " + taskName, e);
153: }
154: return null;
155: }
156:
157: public void addTask(OMElement taskElement) throws AxisFault {
158:
159: SimpleQuartzFactory fac = new SimpleQuartzFactory();
160: Startup st = fac.createStartup(taskElement.getFirstElement());
161: getSynapseConfiguration().addStartup(st);
162: Object o = getAxisConfig().getParameter(
163: SynapseConstants.SYNAPSE_ENV).getValue();
164: if (o instanceof SynapseEnvironment) {
165: st.init((SynapseEnvironment) o);
166: } else {
167: handleFault(
168: log,
169: "Can not initialize the task. Task is added but not scheduled.",
170: null);
171: }
172: }
173:
174: public void editTask(OMElement taskElement) throws AxisFault {
175:
176: SimpleQuartzFactory fac = new SimpleQuartzFactory();
177: Startup st = fac.createStartup(taskElement.getFirstElement());
178: deleteTask(st.getName());
179: getSynapseConfiguration().addStartup(st);
180: Object o = getAxisConfig().getParameter(
181: SynapseConstants.SYNAPSE_ENV).getValue();
182: if (o instanceof SynapseEnvironment) {
183: st.init((SynapseEnvironment) o);
184: } else {
185: handleFault(
186: log,
187: "Can not initialize the task. Task is added but not scheduled.",
188: null);
189: }
190: }
191: }
|