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.SynapseXMLConfigurationFactory;
027: import org.apache.synapse.config.xml.XMLConfigConstants;
028: import org.apache.synapse.config.xml.endpoints.EndpointAbstractFactory;
029: import org.apache.synapse.config.xml.endpoints.EndpointFactory;
030: import org.apache.synapse.endpoints.*;
031: import org.apache.synapse.endpoints.dispatch.Dispatcher;
032: import org.apache.synapse.endpoints.dispatch.HttpSessionDispatcher;
033: import org.apache.synapse.endpoints.dispatch.SimpleClientSessionDispatcher;
034: import org.apache.synapse.endpoints.dispatch.SoapSessionDispatcher;
035: import org.apache.synapse.endpoints.utils.EndpointDefinition;
036: import org.wso2.esb.services.tos.*;
037:
038: import javax.xml.namespace.QName;
039: import java.util.*;
040:
041: /*
042: *
043: */
044:
045: public class EndpointAdmin extends AbstractESBAdmin {
046:
047: private static final Log log = LogFactory
048: .getLog(EndpointAdmin.class);
049:
050: public static final int ADDRESS_EP = 0;
051: public static final int FAILOVER_EP = 2;
052: public static final int WSDL_EP = 1;
053: public static final int LOADBALANCE_EP = 3;
054:
055: /**
056: * Gets the details about the endpoints as a EndpointData array
057: *
058: * @return Array of EndpointData representing the endpoints in the
059: * SynapseConfiguration
060: * @throws AxisFault if an error occured while getting the data from
061: * SynapseConfiguration
062: */
063: public EndpointMetaData[] endpointData() throws AxisFault {
064:
065: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
066: Map namedEndpointMap = synapseConfiguration
067: .getDefinedEndpoints();
068: Collection namedEndpointCollection = namedEndpointMap.values();
069:
070: List epList = new ArrayList();
071: for (Iterator itr = namedEndpointCollection.iterator(); itr
072: .hasNext();) {
073: Object ep = itr.next();
074: if (ep instanceof Endpoint) {
075: epList.add(ep);
076: }
077: }
078:
079: Collections.sort(epList, new Comparator() {
080: public int compare(Object o1, Object o2) {
081: return ((Endpoint) o1).getName().compareToIgnoreCase(
082: ((Endpoint) o2).getName());
083: }
084: });
085: return listToEndpointMetaDatas((Endpoint[]) epList
086: .toArray(new Endpoint[epList.size()]));
087:
088: }
089:
090: /**
091: * @param endpointName
092: * @throws AxisFault
093: */
094: public void enableStatistics(String endpointName) throws AxisFault {
095: try {
096: Endpoint ep = getSynapseConfiguration().getEndpoint(
097: endpointName.trim());
098: if (ep instanceof AddressEndpoint) {
099: ((AddressEndpoint) ep).getEndpoint()
100: .setStatisticsState(
101: SynapseConstants.STATISTICS_ON);
102: } else if (ep instanceof WSDLEndpoint) {
103: ((WSDLEndpoint) ep).getEndpoint().setStatisticsState(
104: SynapseConstants.STATISTICS_ON);
105: } else {
106: handleFault(log, "Selected endpoint : " + endpointName
107: + " does not support statistics", null);
108: }
109: log
110: .info("Statistics enabled on endpoint : "
111: + endpointName);
112: } catch (SynapseException syne) {
113: handleFault(log,
114: "Error enabling statistics for the endpoint : "
115: + endpointName, syne);
116: }
117: }
118:
119: /**
120: * @param endpointName
121: * @throws AxisFault
122: */
123: public void disableStatistics(String endpointName) throws AxisFault {
124: try {
125: Endpoint ep = getSynapseConfiguration().getEndpoint(
126: endpointName.trim());
127: if (ep instanceof AddressEndpoint) {
128: ((AddressEndpoint) ep).getEndpoint()
129: .setStatisticsState(
130: SynapseConstants.STATISTICS_OFF);
131: } else if (ep instanceof WSDLEndpoint) {
132: ((WSDLEndpoint) ep).getEndpoint().setStatisticsState(
133: SynapseConstants.STATISTICS_OFF);
134: } else {
135: handleFault(log, "Selected endpoint : " + endpointName
136: + " does not support statistics", null);
137: }
138: log.info("Statistics disabled on endpoint : "
139: + endpointName);
140: } catch (SynapseException syne) {
141: handleFault(log,
142: "Error disabling statistics for the endpoint : "
143: + endpointName, syne);
144: }
145: }
146:
147: public String[] getEndPointsNames() throws AxisFault {
148: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
149: Map namedEndpointMap = synapseConfiguration
150: .getDefinedEndpoints();
151: Collection namedEndpointCollection = namedEndpointMap.values();
152: return listToNames((Endpoint[]) namedEndpointCollection
153: .toArray(new Endpoint[namedEndpointCollection.size()]));
154: }
155:
156: private String[] listToNames(Endpoint[] eps) {
157: if (eps == null) {
158: return null;
159: } else {
160: String[] datas = new String[eps.length];
161: for (int i = 0; i < eps.length; i++) {
162: Endpoint ep = eps[i];
163: datas[i] = ep.getName();
164: }
165: return datas;
166: }
167: }
168:
169: private EndpointMetaData[] listToEndpointMetaDatas(Endpoint[] eps) {
170: if (eps == null) {
171: return null;
172: } else {
173: EndpointMetaData[] datas = new EndpointMetaData[eps.length];
174: for (int i = 0; i < eps.length; i++) {
175: Endpoint ep = eps[i];
176: if (ep instanceof AddressEndpoint) {
177: EndpointMetaData data = new EndpointMetaData();
178: data.setName(ep.getName());
179: data.setType(ADDRESS_EP);
180: EndpointDefinition def = ((AddressEndpoint) ep)
181: .getEndpoint();
182: if (def.getStatisticsState() == SynapseConstants.STATISTICS_ON) {
183: data.setEnableStatistics(true);
184: } else {
185: data.setEnableStatistics(false);
186: }
187: datas[i] = data;
188: }
189: if (ep instanceof WSDLEndpoint) {
190: EndpointMetaData data = new EndpointMetaData();
191: data.setName(ep.getName());
192: data.setType(WSDL_EP);
193: EndpointDefinition def = ((WSDLEndpoint) ep)
194: .getEndpoint();
195: if (def.getStatisticsState() == SynapseConstants.STATISTICS_ON) {
196: data.setEnableStatistics(true);
197: } else {
198: data.setEnableStatistics(false);
199: }
200: datas[i] = data;
201: } else if (ep instanceof FailoverEndpoint) {
202: EndpointMetaData data = new EndpointMetaData();
203: data.setName(ep.getName());
204: data.setType(FAILOVER_EP);
205: datas[i] = data;
206: } else if (ep instanceof LoadbalanceEndpoint) {
207: EndpointMetaData data = new EndpointMetaData();
208: data.setName(ep.getName());
209: data.setType(LOADBALANCE_EP);
210: datas[i] = data;
211: } else if (ep instanceof SALoadbalanceEndpoint) {
212: EndpointMetaData data = new EndpointMetaData();
213: data.setName(ep.getName());
214: data.setType(LOADBALANCE_EP);
215: datas[i] = data;
216: }
217: }
218: return datas;
219: }
220: }
221:
222: private EndpointData getEndpointData(Endpoint ep) {
223: if (ep == null) {
224: return null;
225: } else {
226: if (ep instanceof AddressEndpoint) {
227: EndpointDefinition def = ((AddressEndpoint) ep)
228: .getEndpoint();
229: AddressEndpointData epData = new AddressEndpointData();
230: epData
231: .setSuspendDurationOnFailure(((AddressEndpoint) ep)
232: .getSuspendOnFailDuration());
233: epData.setName(ep.getName());
234: epData.setEndpointDefinition(def);
235: return epData;
236: }
237: if (ep instanceof IndirectEndpoint) {
238: IndirectEndpointData data = new IndirectEndpointData();
239: data.setName(ep.getName());
240: data.setKey(((IndirectEndpoint) ep).getKey());
241: return data;
242: }
243: if (ep instanceof WSDLEndpoint) {
244: WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) ep;
245: EndpointDefinition def = (wsdlEndpoint).getEndpoint();
246: WSDLEndpointData epData = new WSDLEndpointData();
247: epData.setSuspendDurationOnFailure(wsdlEndpoint
248: .getSuspendOnFailDuration());
249: epData.setName(wsdlEndpoint.getName());
250: epData.setPort(wsdlEndpoint.getPortName());
251: epData.setService(wsdlEndpoint.getServiceName());
252: epData.setUri(wsdlEndpoint.getWsdlURI());
253: epData.setInLineWSDL(wsdlEndpoint.getWsdlDoc());
254: epData.setEndpointDefinition(def);
255: return epData;
256: } else if (ep instanceof FailoverEndpoint) {
257: FailOverEndpointData epData = new FailOverEndpointData();
258: epData.setName(ep.getName());
259: List list = ((FailoverEndpoint) ep).getEndpoints();
260: epData
261: .setEndpoints(listToEndpointDatas((Endpoint[]) list
262: .toArray(new Endpoint[list.size()])));
263: return epData;
264: } else if (ep instanceof LoadbalanceEndpoint) {
265: LoadBalanceEndpointData epData = new LoadBalanceEndpointData();
266: epData.setName(ep.getName());
267: List list = ((LoadbalanceEndpoint) ep).getEndpoints();
268: epData
269: .setEndpoints(listToEndpointDatas((Endpoint[]) list
270: .toArray(new Endpoint[list.size()])));
271: return epData;
272: } else if (ep instanceof SALoadbalanceEndpoint) {
273: LoadBalanceEndpointData epData = new LoadBalanceEndpointData();
274: epData.setName(ep.getName());
275: Dispatcher dispatcher = ((SALoadbalanceEndpoint) ep)
276: .getDispatcher();
277: if (dispatcher instanceof HttpSessionDispatcher) {
278: epData.setSessiontype("http");
279: } else if (dispatcher instanceof SimpleClientSessionDispatcher) {
280: epData.setSessiontype("simpleClientSession");
281: } else if (dispatcher instanceof SoapSessionDispatcher) {
282: epData.setSessiontype("soap");
283: }
284: List list = ((SALoadbalanceEndpoint) ep).getEndpoints();
285: epData
286: .setEndpoints(listToEndpointDatas((Endpoint[]) list
287: .toArray(new Endpoint[list.size()])));
288: return epData;
289: } else {
290: return null;
291: }
292: }
293: }
294:
295: private EndpointData[] listToEndpointDatas(Endpoint[] eps) {
296: if (eps == null) {
297: return null;
298: } else {
299: EndpointData[] datas = new EndpointData[eps.length];
300: for (int i = 0; i < eps.length; i++) {
301: Endpoint ep = eps[i];
302: if (ep instanceof AddressEndpoint) {
303: EndpointDefinition def = ((AddressEndpoint) ep)
304: .getEndpoint();
305: AddressEndpointData epData = new AddressEndpointData();
306: epData.setName(ep.getName());
307: epData
308: .setSuspendDurationOnFailure(((AddressEndpoint) ep)
309: .getSuspendOnFailDuration());
310: epData.setEndpointDefinition(def);
311: datas[i] = epData;
312: }
313: if (ep instanceof IndirectEndpoint) {
314: IndirectEndpointData data = new IndirectEndpointData();
315: data.setName(ep.getName());
316: data.setKey(((IndirectEndpoint) ep).getKey());
317: datas[i] = data;
318: }
319: if (ep instanceof WSDLEndpoint) {
320: WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) ep;
321: EndpointDefinition def = (wsdlEndpoint)
322: .getEndpoint();
323: WSDLEndpointData epData = new WSDLEndpointData();
324: epData.setSuspendDurationOnFailure(wsdlEndpoint
325: .getSuspendOnFailDuration());
326: epData.setName(wsdlEndpoint.getName());
327: epData.setPort(wsdlEndpoint.getPortName());
328: epData.setService(wsdlEndpoint.getServiceName());
329: epData.setUri(wsdlEndpoint.getWsdlURI());
330: epData.setInLineWSDL(wsdlEndpoint.getWsdlDoc());
331: epData.setEndpointDefinition(def);
332: datas[i] = epData;
333: } else if (ep instanceof FailoverEndpoint) {
334: FailOverEndpointData epData = new FailOverEndpointData();
335: epData.setName(ep.getName());
336: List list = ((FailoverEndpoint) ep).getEndpoints();
337: epData
338: .setEndpoints(listToEndpointDatas((Endpoint[]) list
339: .toArray(new Endpoint[list.size()])));
340: datas[i] = epData;
341: } else if (ep instanceof LoadbalanceEndpoint) {
342: LoadBalanceEndpointData epData = new LoadBalanceEndpointData();
343: epData.setName(ep.getName());
344: List list = ((LoadbalanceEndpoint) ep)
345: .getEndpoints();
346: epData
347: .setEndpoints(listToEndpointDatas((Endpoint[]) list
348: .toArray(new Endpoint[list.size()])));
349: datas[i] = epData;
350: } else if (ep instanceof SALoadbalanceEndpoint) {
351: LoadBalanceEndpointData epData = new LoadBalanceEndpointData();
352: epData.setName(ep.getName());
353: Dispatcher dispatcher = ((SALoadbalanceEndpoint) ep)
354: .getDispatcher();
355: if (dispatcher instanceof HttpSessionDispatcher) {
356: epData.setSessiontype("http");
357: } else if (dispatcher instanceof SimpleClientSessionDispatcher) {
358: epData.setSessiontype("simpleClientSession");
359: } else if (dispatcher instanceof SoapSessionDispatcher) {
360: epData.setSessiontype("soap");
361: }
362: List list = ((SALoadbalanceEndpoint) ep)
363: .getEndpoints();
364: epData
365: .setEndpoints(listToEndpointDatas((Endpoint[]) list
366: .toArray(new Endpoint[list.size()])));
367: datas[i] = epData;
368: }
369: }
370: return datas;
371: }
372: }
373:
374: public EndpointData convertToEndpointData(OMElement epElement)
375: throws AxisFault {
376: EndpointFactory fac = EndpointAbstractFactory
377: .getEndpointFactroy(epElement);
378: if (fac != null) {
379: Endpoint endpoint = fac.createEndpoint(epElement, true);
380: if (endpoint != null) {
381: return getEndpointData(endpoint);
382: } else {
383: handleFault(log,
384: "Unable to create an endpoint definition", null);
385: }
386: } else {
387: handleFault(log,
388: "Unable to access the endpoint factory instance",
389: null);
390: }
391: return null;
392: }
393:
394: /**
395: * Add an endpoint described by the given OMElement
396: *
397: * @param endpointElement - OMelement representing the endpoint that needs
398: * to be added
399: * @throws AxisFault if the element is not an endpoint or if an endpoint
400: * wiht the same name exists
401: */
402: public boolean addEndpoint(OMElement endpointElement)
403: throws AxisFault {
404:
405: if (endpointElement.getQName().getLocalPart().equals(
406: XMLConfigConstants.ENDPOINT_ELT.getLocalPart())) {
407:
408: String endpointName = endpointElement
409: .getAttributeValue(new QName("name"));
410: log.debug("Adding endpoint : " + endpointName
411: + " to the configuration");
412:
413: if (endpointName != null
414: && !("".equals(endpointName))
415: && getSynapseConfiguration().getLocalRegistry()
416: .get(endpointName.trim()) != null) {
417: handleFault(log, "The name '" + endpointName
418: + "' is already used within the configuration",
419: null);
420: } else {
421: SynapseXMLConfigurationFactory.defineEndpoint(
422: getSynapseConfiguration(), endpointElement);
423: }
424: log.info("Added endpoint : " + endpointName
425: + " to the configuration");
426: return true;
427: } else {
428: handleFault(
429: log,
430: "Unable to create endpoint. Invalid XML definition",
431: null);
432: }
433: return false;
434: }
435:
436: /**
437: * Save an existing endpoint from the given String representation of the XML
438: *
439: * @param endpointElement - String representing the XML which describes the
440: * Endpoint element
441: * @throws AxisFault if the endpoint does not exists in the
442: * SynapseConfiguration
443: */
444: public boolean saveEndpoint(OMElement endpointElement)
445: throws AxisFault {
446: if (endpointElement.getQName().getLocalPart().equals(
447: XMLConfigConstants.ENDPOINT_ELT.getLocalPart())) {
448:
449: String endpointName = endpointElement
450: .getAttributeValue(new QName("name"));
451: log.info("Updating the definition of the endpoint : "
452: + endpointName);
453:
454: if (endpointName != null
455: && !("".equals(endpointName))
456: && getSynapseConfiguration().getEndpoint(
457: endpointName.trim()) == null) {
458: handleFault(log, "The endpoint named '" + endpointName
459: + "' does not exist", null);
460: } else {
461: Endpoint endpoint = EndpointAbstractFactory
462: .getEndpointFactroy(endpointElement)
463: .createEndpoint(endpointElement, false);
464: getSynapseConfiguration().addEndpoint(
465: endpointName.trim(), endpoint);
466: }
467: log.info("Updated the definition of the endpoint : "
468: + endpointName);
469: return true;
470: } else {
471: handleFault(
472: log,
473: "Unable to update endpoint. Invalid XML definition",
474: null);
475: }
476: return false;
477: }
478:
479: /**
480: * Gets the endpoint element as an OMElement
481: *
482: * @param endpointName - name of the endpoint to be get
483: * @return OMElement representing the endpoint with the given endpoint name
484: */
485: public EndpointData getEndpoint(String endpointName)
486: throws AxisFault {
487: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
488: if (endpointName != null
489: && !("".equals(endpointName))
490: && synapseConfiguration
491: .getEndpoint(endpointName.trim()) != null) {
492: return getEndpointData(synapseConfiguration
493: .getEndpoint(endpointName.trim()));
494: } else {
495: handleFault(log, "The endpoint named '" + endpointName
496: + "' does not exist", null);
497: }
498: return null;
499: }
500:
501: /**
502: * Deletes the endpoint from the SynapseConfiguration
503: *
504: * @param endpointName - name of the endpoint to be deleted
505: * @throws AxisFault if the proxy service name given is not existent in the
506: * synapse configuration
507: */
508: public boolean deleteEndpoint(String endpointName) throws AxisFault {
509:
510: if (endpointName != null && !("".equals(endpointName))) {
511: log.info("Deleting endpoint : " + endpointName
512: + " from the configuration");
513: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
514: synapseConfiguration.removeEndpoint(endpointName.trim());
515: log.info("Endpoint : " + endpointName
516: + " removed from the configuration");
517: return true;
518: } else {
519: log
520: .warn("Invalid endpoint name for removal. Null or empty");
521: return false;
522: }
523: }
524: }
|