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.OMAbstractFactory;
020: import org.apache.axiom.om.OMElement;
021: import org.apache.axiom.om.OMFactory;
022: import org.apache.axiom.om.OMNamespace;
023: import org.apache.axis2.AxisFault;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.synapse.Mediator;
027: import org.apache.synapse.config.xml.ClassMediatorSerializer;
028: import org.apache.synapse.config.xml.MediatorFactoryFinder;
029: import org.apache.synapse.config.xml.POJOCommandMediatorSerializer;
030: import org.apache.synapse.config.xml.XMLConfigConstants;
031: import org.apache.synapse.mediators.ListMediator;
032: import org.apache.synapse.mediators.ext.ClassMediator;
033: import org.apache.synapse.mediators.ext.POJOCommandMediator;
034:
035: import javax.xml.namespace.QName;
036: import java.lang.reflect.Field;
037: import java.lang.reflect.Method;
038: import java.util.ArrayList;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042:
043: /*
044: * This is a POJO for the Mediator admin service
045: */
046:
047: public class MediatorAdmin extends AbstractESBAdmin {
048:
049: private Log log = LogFactory.getLog(MediatorAdmin.class);
050:
051: public String[] showMediators() throws AxisFault {
052:
053: Mediator mainMediator = getSynapseConfiguration()
054: .getMainSequence();
055:
056: List mediatorList = ((ListMediator) mainMediator).getList();
057: return parseList(mediatorList);
058:
059: }
060:
061: private String[] parseList(List list) {
062: ArrayList topLevelMediatorList = new ArrayList();
063: for (Iterator ite = list.iterator(); ite.hasNext();) {
064: Object obj = ite.next();
065:
066: if (obj instanceof Mediator) {
067: //output
068: topLevelMediatorList.add(((Mediator) obj).getType());
069:
070: }
071: }
072: return (String[]) topLevelMediatorList
073: .toArray(new String[topLevelMediatorList.size()]);
074: }
075:
076: public String[] getAllMediators() throws AxisFault {
077: MediatorFactoryFinder finder = MediatorFactoryFinder
078: .getInstance();
079:
080: Map factoryMap = finder.getFactoryMap();
081:
082: QName tagNames[] = (QName[]) factoryMap.keySet().toArray(
083: new QName[factoryMap.size()]);
084: ArrayList mediatorNames = new ArrayList();
085: for (int i = 0; i < tagNames.length; i++) {
086: mediatorNames.add(tagNames[i].getLocalPart());
087: }
088:
089: return (String[]) mediatorNames
090: .toArray(new String[mediatorNames.size()]);
091: }
092:
093: public OMElement loadClass(String className) throws AxisFault {
094: ClassMediator med = new ClassMediator();
095: OMFactory fac = OMAbstractFactory.getOMFactory();
096: OMNamespace nullNS = fac.createOMNamespace("", "");
097: OMNamespace synNS = fac.createOMNamespace(
098: XMLConfigConstants.SYNAPSE_NAMESPACE, "syn");
099: try {
100: Class clazz = Class.forName(className);
101: Object o = clazz.newInstance();
102: if (o instanceof Mediator) {
103: med.setMediator((Mediator) o);
104: }
105: Method[] methods = clazz.getMethods();
106: Field[] fields = clazz.getDeclaredFields();
107: for (int i = 0; i < methods.length; i++) {
108: String methodName = methods[i].getName();
109: if (methodName.startsWith("set")
110: && !"setTraceState".equals(methodName)) {
111: // OMElement propElem = fac.createOMElement("property", synNS);
112: for (int j = 0; j < fields.length; j++) {
113: if (fields[j].getName().equalsIgnoreCase(
114: methodName.substring(3))) {
115: // propElem.addAttribute("name", fields[j].getName(), nullNS);
116: med.addProperty(fields[j].getName(), "");
117: break;
118: }
119: }
120: // propElem.addAttribute("value", "", nullNS);
121: // med.addProperty(propElem);
122: }
123: }
124: } catch (ClassNotFoundException e) {
125: throw new AxisFault("Class " + className
126: + " not found in the path", e);
127: } catch (IllegalAccessException e) {
128: throw new AxisFault("Unable to access the class: "
129: + className);
130: } catch (InstantiationException e) {
131: throw new AxisFault("Unable to instatiate the class: "
132: + className);
133: }
134: return (new ClassMediatorSerializer()).serializeMediator(null,
135: med);
136: }
137:
138: public OMElement loadCommand(String className) throws AxisFault {
139: POJOCommandMediator med = new POJOCommandMediator();
140: OMFactory fac = OMAbstractFactory.getOMFactory();
141: OMNamespace nullNS = fac.createOMNamespace("", "");
142: OMNamespace synNS = fac.createOMNamespace(
143: XMLConfigConstants.SYNAPSE_NAMESPACE, "syn");
144: try {
145: Class clazz = Class.forName(className);
146: med.setCommand(clazz);
147:
148: Object o = clazz.newInstance();
149: clazz.getMethod("execute", new Class[] {});
150:
151: Method[] methods = clazz.getMethods();
152: Field[] fields = clazz.getDeclaredFields();
153: for (int i = 0; i < methods.length; i++) {
154: String methodName = methods[i].getName();
155: if (methodName.startsWith("set")
156: && !"setTraceState".equals(methodName)) {
157: OMElement propElem = fac.createOMElement(
158: "property", synNS);
159: for (int j = 0; j < fields.length; j++) {
160: if (fields[j].getName().equalsIgnoreCase(
161: methodName.substring(3))) {
162: // propElem.addAttribute("name", fields[j].getName(), nullNS);
163: med.addStaticSetterProperty(fields[j]
164: .getName(), "");
165: break;
166: }
167: }
168: // propElem.addAttribute("value", "", nullNS);
169: // med.addProperty(propElem);
170: }
171: }
172: } catch (ClassNotFoundException e) {
173: throw new AxisFault("Class " + className
174: + " not found in the path", e);
175: } catch (IllegalAccessException e) {
176: throw new AxisFault("Unable to access the class: "
177: + className);
178: } catch (InstantiationException e) {
179: throw new AxisFault("Unable to instatiate the class: "
180: + className);
181: } catch (NoSuchMethodException e) {
182: throw new AxisFault(
183: "Unable to find the command execution method: execute()");
184: }
185: return (new POJOCommandMediatorSerializer()).serializeMediator(
186: null, med);
187: }
188: }
|