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.OMElement;
020: import org.apache.axis2.AxisFault;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.synapse.SynapseConstants;
024: import org.apache.synapse.SynapseException;
025: import org.apache.synapse.Mediator;
026: import org.apache.synapse.ManagedLifecycle;
027: import org.apache.synapse.config.SynapseConfiguration;
028: import org.apache.synapse.config.xml.MediatorFactoryFinder;
029: import org.apache.synapse.config.xml.MediatorSerializerFinder;
030: import org.apache.synapse.config.xml.SynapseXMLConfigurationFactory;
031: import org.apache.synapse.config.xml.XMLConfigConstants;
032: import org.apache.synapse.mediators.base.SequenceMediator;
033: import org.wso2.esb.services.tos.SequenceData;
034:
035: import javax.xml.namespace.QName;
036: import java.util.*;
037:
038: /**
039: * This is a POJO for the Sequence admin service
040: */
041: public class SequenceAdmin extends AbstractESBAdmin {
042:
043: private static final Log log = LogFactory
044: .getLog(SequenceAdmin.class);
045:
046: /**
047: * Gets the details about the sequences as a SequenceData array
048: *
049: * @return Array of SequenceData representing the Sequences in the
050: * SynapseConfiguration
051: * @throws AxisFault if an error occured while getting the data from
052: * SynapseConfiguration
053: */
054: public SequenceData[] sequenceData() throws AxisFault {
055: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
056: Map definedsequencesMap = synapseConfiguration
057: .getDefinedSequences();
058: Collection definedsequenceValues = definedsequencesMap.values();
059: ArrayList sequenceDataList = new ArrayList();
060: for (Iterator ite = definedsequenceValues.iterator(); ite
061: .hasNext();) {
062: Object obj = ite.next();
063: /*
064: instanceof will have SequenceMediator || GolableConfigurations
065: */
066: if (obj instanceof SequenceMediator) {
067: SequenceMediator seqMediator = (SequenceMediator) obj;
068: SequenceData data = new SequenceData();
069: data.setName(seqMediator.getName());
070: if (seqMediator.getStatisticsState() == SynapseConstants.STATISTICS_ON) {
071: data.setEnableStatistics(true);
072: } else {
073: data.setEnableStatistics(false);
074: }
075: if (seqMediator.getTraceState() == SynapseConstants.TRACING_ON) {
076: data.setEnableTracing(true);
077: } else {
078: data.setEnableTracing(false);
079: }
080: sequenceDataList.add(data);
081: }
082: }
083:
084: Collections.sort(sequenceDataList, new Comparator() {
085: public int compare(Object o1, Object o2) {
086: return ((SequenceData) o1).getName()
087: .compareToIgnoreCase(
088: ((SequenceData) o2).getName());
089: }
090: });
091:
092: return (SequenceData[]) sequenceDataList
093: .toArray(new SequenceData[sequenceDataList.size()]);
094: }
095:
096: /**
097: * Deletes the sequence with the given name from SynapseConfiguration
098: *
099: * @param sequenceName - Name of the sequence to delete
100: * @throws AxisFault if the Sequence described by the given name doesnt
101: * exists in the Synapse Configuration
102: */
103: public void deleteSequence(String sequenceName) throws AxisFault {
104: try {
105: log.debug("Deleting sequence : " + sequenceName);
106: if ("main".equals(sequenceName)
107: || "fault".equals(sequenceName)) {
108: handleFault(log, "Unable to delete sequence : "
109: + sequenceName, null);
110: } else {
111: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
112: synapseConfiguration.removeSequence(sequenceName);
113: log.info("Deleted sequence : " + sequenceName
114: + " from the configuration");
115: }
116: } catch (SynapseException syne) {
117: handleFault(log, "Error removing sequence : "
118: + sequenceName, syne);
119: }
120: }
121:
122: /**
123: * todo - docs
124: * @param sequenceName
125: * @throws AxisFault
126: */
127: public void enableStatistics(String sequenceName) throws AxisFault {
128: try {
129: ((SequenceMediator) getSynapseConfiguration().getSequence(
130: sequenceName))
131: .setStatisticsState(SynapseConstants.STATISTICS_ON);
132: log
133: .info("Enabled statistics on sequence : "
134: + sequenceName);
135: } catch (SynapseException syne) {
136: handleFault(log,
137: "Unable to enable statistics for the sequence "
138: + sequenceName, syne);
139: }
140: }
141:
142: /**
143: * todo - docs
144: * @param sequenceName
145: * @throws AxisFault
146: */
147: public void disableStatistics(String sequenceName) throws AxisFault {
148: try {
149: ((SequenceMediator) getSynapseConfiguration().getSequence(
150: sequenceName))
151: .setStatisticsState(SynapseConstants.STATISTICS_OFF);
152: log.info("Disabled statistics on sequence : "
153: + sequenceName);
154: } catch (SynapseException syne) {
155: handleFault(log,
156: "Unable to disable statistics for the sequence "
157: + sequenceName, syne);
158: }
159: }
160:
161: /**
162: *
163: * @param sequenceName
164: * @throws AxisFault
165: */
166: public void enableTracing(String sequenceName) throws AxisFault {
167:
168: try {
169: ((SequenceMediator) getSynapseConfiguration().getSequence(
170: sequenceName))
171: .setTraceState(SynapseConstants.TRACING_ON);
172: log.info("Enabled tracing on sequence : " + sequenceName);
173: } catch (SynapseException syne) {
174: handleFault(log,
175: "Unable to enable tracing for the sequence "
176: + sequenceName, syne);
177: }
178: }
179:
180: /**
181: *
182: * @param sequenceName
183: * @throws AxisFault
184: */
185: public void disableTracing(String sequenceName) throws AxisFault {
186:
187: try {
188: ((SequenceMediator) getSynapseConfiguration().getSequence(
189: sequenceName))
190: .setTraceState(SynapseConstants.TRACING_OFF);
191: log.info("Disabled tracing on sequence : " + sequenceName);
192: } catch (SynapseException syne) {
193: handleFault(log,
194: "Unable to disable tracing for the sequence "
195: + sequenceName, syne);
196: }
197: }
198:
199: /**
200: * Add a sequence into the synapseConfiguration
201: *
202: * @param sequenceElement - Sequence object to be added as an OMElement
203: * @throws AxisFault if a sequence exists with the same name or if the
204: * element provided is not a Sequence element
205: */
206: public void addSequence(OMElement sequenceElement) throws AxisFault {
207:
208: try {
209: if (sequenceElement.getFirstElement().getQName()
210: .getLocalPart().equals(
211: XMLConfigConstants.SEQUENCE_ELT
212: .getLocalPart())) {
213: String sequenceName = sequenceElement.getFirstElement()
214: .getAttributeValue(new QName("name"));
215: SynapseConfiguration config = getSynapseConfiguration();
216: log.info("Adding sequence : " + sequenceName
217: + " to the configuration");
218: if (config.getLocalRegistry().get(sequenceName) != null) {
219: handleFault(
220: log,
221: "The name '"
222: + sequenceName
223: + "' is already used within the configuration",
224: null);
225: } else {
226: SynapseXMLConfigurationFactory.defineSequence(
227: config, sequenceElement.getFirstElement());
228: log.info("Added sequence : " + sequenceName
229: + " to the configuration");
230:
231: Mediator seq = config.getSequence(sequenceName);
232: if (seq != null && seq instanceof ManagedLifecycle) {
233: ((ManagedLifecycle) seq)
234: .init(getSynapseEnvironment());
235: }
236: }
237: } else {
238: handleFault(log,
239: "Error adding sequence. Invalid definition",
240: null);
241: }
242: } catch (SynapseException syne) {
243: handleFault(log, "Error adding sequence", syne);
244: }
245: }
246:
247: /**
248: * Returns the OMelement representation of the sequence given by sequence
249: * name
250: *
251: * @param sequenceName - name of the sequence to get
252: * @return OMElement representing the SequenceMediator of the given sequence
253: * name
254: * @throws AxisFault if any error occured while getting the data from the
255: * SynapseConfiguration
256: */
257: public OMElement getSequence(String sequenceName) throws AxisFault {
258: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
259: try {
260: if (synapseConfiguration.getSequence(sequenceName) != null) {
261: return MediatorSerializerFinder.getInstance()
262: .getSerializer(
263: synapseConfiguration
264: .getSequence(sequenceName))
265: .serializeMediator(
266: null,
267: synapseConfiguration
268: .getSequence(sequenceName));
269: } else {
270: handleFault(log, "Sequence with the name "
271: + sequenceName + " does not exist", null);
272: }
273: } catch (SynapseException syne) {
274: handleFault(log, "Unable to get the sequence : "
275: + sequenceName, syne);
276: }
277: return null;
278: }
279:
280: /**
281: * Returns the OMElement representation of the main mediator sequence
282: *
283: * @return OMElement representing the main mediator sequence
284: * @throws AxisFault if an error occurs while retrieving the main mediator from Synapse
285: */
286: public OMElement getMainSequence() throws AxisFault {
287: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
288: try {
289: if (synapseConfiguration.getMainSequence() != null) {
290: return MediatorSerializerFinder.getInstance()
291: .getSerializer(
292: synapseConfiguration.getMainSequence())
293: .serializeMediator(null,
294: synapseConfiguration.getMainSequence());
295: } else {
296: handleFault(log, "The main mediator sequence "
297: + "does not exist in the configuration", null);
298: }
299: } catch (SynapseException syne) {
300: handleFault(
301: log,
302: "Unable to get or serialize the main mediator sequence",
303: syne);
304: }
305: return null;
306: }
307:
308: /**
309: * Saves the sequence described with the sequence string
310: *
311: * @param sequenceElement - String representing the XML describing the
312: * Sequence
313: * @throws AxisFault if the sequence name already exists or if the string
314: * doesnt represent a Sequence element
315: */
316: public void saveSequence(OMElement sequenceElement)
317: throws AxisFault {
318: try {
319: sequenceElement = sequenceElement
320: .getFirstChildWithName(XMLConfigConstants.SEQUENCE_ELT);
321: if (sequenceElement != null) {
322: String sequenceName = sequenceElement
323: .getAttributeValue(new QName("name"));
324: SynapseConfiguration config = getSynapseConfiguration();
325: log.debug("Saving sequence : " + sequenceName);
326: if (config.getSequence(sequenceName) == null) {
327: handleFault(log, "Unable to sa ve sequence "
328: + sequenceName + ". Does not exist", null);
329: } else {
330: config.addSequence(sequenceName,
331: MediatorFactoryFinder.getInstance()
332: .getMediator(sequenceElement));
333: log.debug("Saved sequence : " + sequenceName
334: + " to the configuration");
335:
336: Mediator seq = config.getSequence(sequenceName);
337: if (seq != null && seq instanceof ManagedLifecycle) {
338: ((ManagedLifecycle) seq)
339: .init(getSynapseEnvironment());
340: }
341: }
342: } else {
343: handleFault(log,
344: "Unable to save sequence. Invalid definition",
345: null);
346: }
347: } catch (SynapseException syne) {
348: handleFault(log, "Unable to save the sequence", syne);
349: }
350: }
351:
352: }
|