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.config.SynapseConfiguration;
026: import org.apache.synapse.config.xml.ProxyServiceFactory;
027: import org.apache.synapse.config.xml.ProxyServiceSerializer;
028: import org.apache.synapse.config.xml.XMLConfigConstants;
029: import org.apache.synapse.core.axis2.ProxyService;
030: import org.wso2.esb.services.tos.ProxyData;
031:
032: import javax.xml.namespace.QName;
033: import java.util.*;
034:
035: /**
036: * This is a POJO for the Proxy Service admin service
037: */
038: public class ProxyServiceAdmin extends AbstractESBAdmin {
039:
040: private static Log log = LogFactory.getLog(ProxyServiceAdmin.class);
041:
042: /**
043: * Get all the proxy services wrapped in the ProxyData objects as a list for the
044: * admin console to view the details of them
045: *
046: * @return Array of ProxyData objects in the SynapseConfiguraiton
047: *
048: * @throws AxisFault in case of a failure in getting proxies
049: */
050: public ProxyData[] proxyData() throws AxisFault {
051: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
052: Collection proxyCollection = synapseConfiguration
053: .getProxyServices();
054: ArrayList proxyServiceList = new ArrayList(1);
055: for (Iterator ite = proxyCollection.iterator(); ite.hasNext();) {
056: Object obj = ite.next();
057: if (obj instanceof ProxyService) {
058: ProxyService proxy = (ProxyService) obj;
059: ProxyData data = new ProxyData();
060: data.setName(proxy.getName());
061:
062: if (proxy.getTargetInSequence() != null
063: || proxy.getTargetOutSequence() != null) {
064: data.setTarget(proxy.getTargetInSequence());
065: } else {
066: data.setTarget(proxy.getTargetEndpoint());
067: }
068: data.setRunning(proxy.isRunning());
069: if (proxy.getStatisticsState() == SynapseConstants.STATISTICS_ON) {
070: data.setEnableStatistics(true);
071: } else {
072: data.setEnableStatistics(false);
073: }
074: if (proxy.getTraceState() == SynapseConstants.TRACING_ON) {
075: data.setEnableTracing(true);
076: } else {
077: data.setEnableTracing(false);
078: }
079: if (proxy.getWsdlURI() != null
080: || proxy.getWSDLKey() != null
081: || proxy.getInLineWSDL() != null) {
082: data.setWsdlAvailabel(true);
083: } else {
084: data.setWsdlAvailabel(false);
085: }
086: if (proxy.getServiceLevelPolicies().isEmpty()) {
087: data.setPolicyAvailabel(false);
088: } else {
089: data.setPolicyAvailabel(true);
090: }
091: proxyServiceList.add(data);
092: }
093: }
094:
095: Collections.sort(proxyServiceList, new Comparator() {
096: public int compare(Object o1, Object o2) {
097: return ((ProxyData) o1).getName().compareToIgnoreCase(
098: ((ProxyData) o2).getName());
099: }
100: });
101:
102: return (ProxyData[]) proxyServiceList
103: .toArray(new ProxyData[proxyServiceList.size()]);
104: }
105:
106: /**
107: * Enables statistics for the specified proxy service
108: *
109: * @param proxyName
110: * name of the proxy service name of which the statistics need to be enabled
111: *
112: * @throws AxisFault in case of a failure in enabling statistics
113: */
114: public void enableStatistics(String proxyName) throws AxisFault {
115:
116: try {
117: getSynapseConfiguration().getProxyService(proxyName)
118: .setStatisticsState(SynapseConstants.STATISTICS_ON);
119: log.info("Enabled statistics on proxy service : "
120: + proxyName);
121: } catch (SynapseException syne) {
122: handleFault(log,
123: "Unable to enable statistics for proxy service "
124: + proxyName, syne);
125: }
126: }
127:
128: /**
129: * Disables statistics for the specified proxy servivce
130: *
131: * @param proxyName
132: * name of the proxy service of which statistics need to be disabled
133: *
134: * @throws AxisFault in case of a failure in disabling statistics
135: */
136: public void disableStatistics(String proxyName) throws AxisFault {
137:
138: try {
139: getSynapseConfiguration()
140: .getProxyService(proxyName)
141: .setStatisticsState(SynapseConstants.STATISTICS_OFF);
142: log.info("Disabled statistics on proxy service : "
143: + proxyName);
144: } catch (SynapseException syne) {
145: handleFault(log,
146: "Unable to disable statistics for proxy service "
147: + proxyName, syne);
148: }
149: }
150:
151: /**
152: * Enables tracing for the specified proxy service
153: *
154: * @param proxyName
155: * name of the the proxy service of which tracing needs to be enabled
156: *
157: * @throws AxisFault in case of a failure in enabling tracing
158: */
159: public void enableTracing(String proxyName) throws AxisFault {
160:
161: try {
162: getSynapseConfiguration().getProxyService(proxyName)
163: .setTraceState(SynapseConstants.TRACING_ON);
164: log.info("Enabled tracing on proxy service : " + proxyName);
165: } catch (SynapseException syne) {
166: handleFault(log,
167: "Unable to enable tracing for proxy service "
168: + proxyName, syne);
169: }
170: }
171:
172: /**
173: * Disables tracing for the specified proxy service
174: *
175: * @param proxyName
176: * name of the proxy service of which tracing needs to be disabled
177: *
178: * @throws AxisFault in case of a failure in disabling tracing
179: */
180: public void disableTracing(String proxyName) throws AxisFault {
181:
182: try {
183: getSynapseConfiguration().getProxyService(proxyName)
184: .setTraceState(SynapseConstants.TRACING_OFF);
185: log
186: .info("Disabled tracing on proxy service : "
187: + proxyName);
188: } catch (SynapseException syne) {
189: handleFault(log,
190: "Unable to disable tracing for proxy service "
191: + proxyName, syne);
192: }
193: }
194:
195: /**
196: * Get the proxy service configuration as an OMElement
197: *
198: * @param proxyName
199: * name of the proxy service which needs the configuraiton
200: *
201: * @return OMElement representing the proxy servicve configuration
202: *
203: * @throws AxisFault in case of failure in getting the configuration
204: */
205: public OMElement getProxyServiceElement(String proxyName)
206: throws AxisFault {
207: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
208: try {
209: if (synapseConfiguration.getProxyService(proxyName) != null) {
210: return ProxyServiceSerializer
211: .serializeProxy(null, synapseConfiguration
212: .getProxyService(proxyName));
213: } else {
214: handleFault(log, "A proxy service named : " + proxyName
215: + " does not exist", null);
216: }
217: } catch (SynapseException syne) {
218: handleFault(log,
219: "Unable to get the proxy service definition for : "
220: + proxyName, syne);
221: }
222: return null;
223: }
224:
225: /**
226: * Add a proxy service described by the given OMElement
227: *
228: * @param proxyServiceElement
229: * configuraiton of the proxy service which needs to be added
230: *
231: * @throws AxisFault if the element is not an proxy service or if a proxy service with the
232: * same name exists
233: */
234: public void addProxyService(OMElement proxyServiceElement)
235: throws AxisFault {
236: try {
237: if (proxyServiceElement.getQName().getLocalPart().equals(
238: XMLConfigConstants.PROXY_ELT.getLocalPart())) {
239: String proxyName = proxyServiceElement
240: .getAttributeValue(new QName("name"));
241: if (getSynapseConfiguration()
242: .getProxyService(proxyName) != null) {
243: handleFault(log, "A proxy service named : "
244: + proxyName + " already exists", null);
245:
246: } else {
247: ProxyService proxy = ProxyServiceFactory
248: .createProxy(proxyServiceElement);
249:
250: try {
251: proxy.buildAxisService(
252: getSynapseConfiguration(),
253: getAxisConfig());
254:
255: log.info("Added proxy service : " + proxyName);
256: if (!proxy.isStartOnLoad()) {
257: proxy.stop(getSynapseConfiguration());
258: }
259: getSynapseConfiguration().addProxyService(
260: proxy.getName(), proxy);
261:
262: } catch (AxisFault af) {
263: log.error("Unable to add Proxy service : "
264: + proxy.getName(), af);
265: if (getAxisConfig().getService(proxy.getName()) != null) {
266: try {
267: getAxisConfig().removeService(
268: proxy.getName());
269: } catch (Exception ignore) {
270: }
271: }
272: throw new AxisFault(
273: "Unable to add Proxy service : "
274: + proxy.getName(), af);
275: }
276: }
277: } else {
278: handleFault(
279: log,
280: "Unable to add proxy service. Invalid definition",
281: null);
282: }
283: } catch (SynapseException syne) {
284: handleFault(log,
285: "Unable to add proxy service. Invalid definition",
286: syne);
287: }
288: }
289:
290: /**
291: * Alter and saves the proxy service to the SynapseConfiguration as specified by the
292: * given OMElement configuration
293: *
294: * @param proxyServiceElement
295: * configuration of the proxy service which needs to be altered
296: *
297: * @throws AxisFault if the service not present or the configuration is wrong or
298: * in case of a failure in building the axis service
299: */
300: public void saveProxyService(OMElement proxyServiceElement)
301: throws AxisFault {
302:
303: try {
304: if (proxyServiceElement.getQName().getLocalPart().equals(
305: XMLConfigConstants.PROXY_ELT.getLocalPart())) {
306:
307: String proxyName = proxyServiceElement
308: .getAttributeValue(new QName("name"));
309:
310: ProxyService currentProxy = getSynapseConfiguration()
311: .getProxyService(proxyName);
312: boolean wasRunning = false;
313: if (currentProxy == null) {
314: handleFault(log, "Unable to save proxy service : "
315: + proxyName + ". Does not exist", null);
316:
317: } else {
318: wasRunning = currentProxy.isRunning();
319: log.debug("Deleting existing proxy service : "
320: + proxyName);
321: deleteProxyService(proxyName);
322:
323: try {
324: log
325: .debug("Adding proxy service : "
326: + proxyName);
327: addProxyService(proxyServiceElement);
328: log.info("Added added proxy service : "
329: + proxyName);
330:
331: if (!wasRunning
332: && getSynapseConfiguration()
333: .getProxyService(proxyName)
334: .isRunning()) {
335: getSynapseConfiguration().getProxyService(
336: proxyName).stop(
337: getSynapseConfiguration());
338: } else if (wasRunning
339: && !getSynapseConfiguration()
340: .getProxyService(proxyName)
341: .isRunning()) {
342: getSynapseConfiguration().getProxyService(
343: proxyName).start(
344: getSynapseConfiguration());
345: }
346: } catch (Exception e) {
347:
348: log
349: .error("Unable to save changes made for the proxy service : "
350: + proxyName
351: + ". Restoring the existing proxy..");
352: getSynapseConfiguration().addProxyService(
353: proxyName, currentProxy);
354: currentProxy.buildAxisService(
355: getSynapseConfiguration(),
356: getAxisConfig());
357: if (!wasRunning) {
358: currentProxy
359: .stop(getSynapseConfiguration());
360: } else {
361: currentProxy
362: .start(getSynapseConfiguration());
363: }
364:
365: handleFault(
366: log,
367: "Unable to save changes made for the proxy service : "
368: + proxyName
369: + ". Restored the existing proxy...",
370: e);
371: }
372: }
373:
374: } else {
375: handleFault(
376: log,
377: "Unable to save proxy service. Invalid definition",
378: null);
379: }
380:
381: } catch (SynapseException syne) {
382: handleFault(log,
383: "Unable to save proxy service. Invalid definition",
384: syne);
385: }
386: }
387:
388: /**
389: * Deletes a proxy service from the synapse configuration
390: *
391: * @param proxyServiceName
392: * name of the proxy service which needs to be deleted
393: *
394: * @throws AxisFault if the proxy service name given is not existent in the
395: * synapse configuration
396: */
397: public void deleteProxyService(String proxyServiceName)
398: throws AxisFault {
399: try {
400: log.debug("Deleting proxy service : " + proxyServiceName);
401: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
402: synapseConfiguration.removeProxyService(proxyServiceName);
403: log
404: .info("Proxy service : " + proxyServiceName
405: + " deleted");
406: } catch (SynapseException syne) {
407: handleFault(log, "Unable to delete proxy service : "
408: + proxyServiceName, syne);
409: }
410: }
411:
412: /**
413: * Get the availabel transport names from the AxisConfiguration
414: *
415: * @return String array of availabel transport names
416: *
417: * @throws AxisFault if there is an error
418: */
419: public String[] getAvailabelTransports() throws AxisFault {
420: Object[] transports = getAxisConfig().getTransportsIn()
421: .keySet().toArray();
422: String[] ret = new String[transports.length];
423: for (int i = 0; i < transports.length; i++) {
424: ret[i] = (String) transports[i];
425: }
426: return ret;
427: }
428:
429: /**
430: * Get the availabel sequences from the SynapseConfiguration
431: *
432: * @return String array of availabel sequence names
433: *
434: * @throws AxisFault if there is an error
435: */
436: public String[] getAvailabelSequences() throws AxisFault {
437: Object[] sequences = getSynapseConfiguration()
438: .getDefinedSequences().keySet().toArray();
439: String[] ret = new String[sequences.length];
440: for (int i = 0; i < sequences.length; i++) {
441: ret[i] = (String) sequences[i];
442: }
443: return ret;
444: }
445:
446: /**
447: * Get the availabel endpoints from the SynapseConfiguration
448: *
449: * @return String array of availabel endpoint names
450: *
451: * @throws AxisFault if there is an error
452: */
453: public String[] getAvailabelEndpoints() throws AxisFault {
454: Object[] endpoints = getSynapseConfiguration()
455: .getDefinedEndpoints().keySet().toArray();
456: String[] ret = new String[endpoints.length];
457: for (int i = 0; i < endpoints.length; i++) {
458: ret[i] = (String) endpoints[i];
459: }
460: return ret;
461: }
462:
463: /**
464: * Starts the service specified by the name
465: *
466: * @param proxyServiceName
467: * name of the proxy service which needs to be started
468: *
469: * @throws AxisFault incase of a failure in starting the service
470: */
471: public void startProxyService(String proxyServiceName)
472: throws AxisFault {
473: log.debug("Starting/Re-starting proxy service : "
474: + proxyServiceName);
475: getSynapseConfiguration().getProxyService(proxyServiceName)
476: .start(getSynapseConfiguration());
477: log.info("Started/Re-started proxy service : "
478: + proxyServiceName);
479: }
480:
481: /**
482: * Stops the service specified by the name
483: *
484: * @param proxyServiceName
485: * name of the proxy service which needs to be stoped
486: *
487: * @throws AxisFault in case of a failure in stopping the service
488: */
489: public void stopProxyService(String proxyServiceName)
490: throws AxisFault {
491: log.debug("Stopping proxy service : " + proxyServiceName);
492: getSynapseConfiguration().getProxyService(proxyServiceName)
493: .stop(getSynapseConfiguration());
494: log.info("Stopped proxy service : " + proxyServiceName);
495: }
496: }
|