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.*;
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.SynapseException;
024: import org.apache.synapse.config.Entry;
025: import org.apache.synapse.config.SynapseConfiguration;
026: import org.apache.synapse.config.xml.EntrySerializer;
027: import org.apache.synapse.config.xml.XMLConfigConstants;
028: import org.apache.synapse.endpoints.Endpoint;
029: import org.apache.synapse.mediators.base.SequenceMediator;
030: import org.wso2.esb.services.tos.EntryData;
031:
032: import javax.xml.namespace.QName;
033: import java.net.MalformedURLException;
034: import java.net.URL;
035: import java.util.*;
036:
037: /**
038: * This is a POJO for Entry based administration service
039: */
040: public class EntriesAdmin extends AbstractESBAdmin {
041:
042: private static final Log log = LogFactory
043: .getLog(EntriesAdmin.class);
044:
045: public EntryData[] entryData() throws AxisFault {
046: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
047: Map gloabalEntiesMap = synapseConfiguration.getDefinedEntries();
048: ArrayList globalEntryList = new ArrayList();
049: for (Iterator ite = gloabalEntiesMap.entrySet().iterator(); ite
050: .hasNext();) {
051: EntryData data = new EntryData();
052: Map.Entry entry = (Map.Entry) ite.next();
053: String key = (String) entry.getKey();
054: if (entry.getValue() instanceof Entry) {
055: Entry value = (Entry) entry.getValue();
056: data.setName(key);
057: switch (value.getType()) {
058: case Entry.REMOTE_ENTRY:
059: data.setType("Registry Key");
060: break;
061: case Entry.INLINE_TEXT:
062: data.setType("Inline Text");
063: break;
064: case Entry.INLINE_XML:
065: data.setType("Inline XML");
066: break;
067: case Entry.URL_SRC:
068: data.setType("Source URL");
069: break;
070: default:
071: data.setType("Unknown");
072: break;
073: }
074: if (value.getValue() instanceof String) {
075: String s = (String) value.getValue();
076: if (value.getType() == Entry.INLINE_TEXT
077: && s.length() > 40) {
078: data.setValue(s.substring(0, 40) + "...");
079: } else {
080: data.setValue(s);
081: }
082: } else if (value.getType() == Entry.URL_SRC) {
083: String s = value.getSrc().toString();
084: if (s.length() > 40) {
085: data.setValue(s.substring(0, 40) + "...");
086: } else {
087: data.setValue(s);
088: }
089: } else if (value.getType() == Entry.REMOTE_ENTRY)
090: data.setValue(value.getKey());
091: else
092: data.setValue("'XML Fragment'");
093: globalEntryList.add(data);
094: }
095: }
096:
097: Collections.sort(globalEntryList, new Comparator() {
098: public int compare(Object o1, Object o2) {
099: return ((EntryData) o1).getName().compareToIgnoreCase(
100: ((EntryData) o2).getName());
101: }
102: });
103:
104: return (EntryData[]) globalEntryList
105: .toArray(new EntryData[globalEntryList.size()]);
106: }
107:
108: /**
109: * Add a entry into the synapseConfiguration
110: *
111: * @param elem - Entry object to be added as an OMElement
112: * @throws AxisFault if a Entry exists with the same name or if the
113: * element provided is not a Entry element
114: */
115: public boolean addEntry(OMElement elem) throws AxisFault {
116: try {
117: if (elem.getQName().getLocalPart().equals(
118: XMLConfigConstants.ENTRY_ELT.getLocalPart())) {
119:
120: String entryName = elem.getAttributeValue(new QName(
121: "name"));
122:
123: log
124: .debug("Adding local entry with name : "
125: + entryName);
126: if (getSynapseConfiguration().getLocalRegistry()
127: .containsKey(entryName)) {
128: handleFault(
129: log,
130: "The name '"
131: + entryName
132: + "' is already used within the configuration",
133: null);
134: } else {
135: OMAttribute name = elem.getAttribute(new QName(
136: XMLConfigConstants.NULL_NAMESPACE, "name"));
137: if (name == null) {
138: handleFault(
139: log,
140: "Unable to add local entry. Cannot find the name",
141: null);
142:
143: } else {
144: Entry entry = new Entry();
145: entry.setKey(name.getAttributeValue());
146: String value = elem
147: .getAttributeValue(new QName(
148: XMLConfigConstants.NULL_NAMESPACE,
149: "value"));
150: String src = elem.getAttributeValue(new QName(
151: XMLConfigConstants.NULL_NAMESPACE,
152: "src"));
153: String key = elem.getAttributeValue(new QName(
154: XMLConfigConstants.NULL_NAMESPACE,
155: "key"));
156: int type = Integer
157: .parseInt(elem
158: .getAttributeValue(new QName(
159: XMLConfigConstants.NULL_NAMESPACE,
160: "type")));
161: OMElement content = elem.getFirstElement();
162:
163: if (type == Entry.URL_SRC) {
164: entry.setType(Entry.URL_SRC);
165: try {
166: entry.setSrc(new URL(src));
167: } catch (MalformedURLException e) {
168: handleFault(log, "Invalid URL : '"
169: + src + "'", e);
170: }
171: } else if (type == Entry.REMOTE_ENTRY) {
172: entry.setType(Entry.REMOTE_ENTRY);
173: entry.setKey(key);
174: } else if (type == Entry.INLINE_XML) {
175: entry.setType(Entry.INLINE_XML);
176: entry.setValue(content);
177: } else if (type == Entry.INLINE_TEXT) {
178: entry.setType(Entry.INLINE_TEXT);
179: entry.setValue(elem.getText());
180: } else {
181: handleFault(
182: log,
183: "Unable to add local entry. Invalid definition",
184: null);
185: }
186: getSynapseConfiguration().addEntry(entryName,
187: entry);
188: }
189: }
190: log.info("Local registry entry : " + entryName
191: + " added to the configuration");
192: return true;
193: } else {
194: handleFault(log,
195: "Error adding local entry. Invalid definition",
196: null);
197: }
198: } catch (SynapseException syne) {
199: handleFault(log, "Unable to add local entry ", syne);
200: }
201: return false;
202: }
203:
204: /**
205: * Saves the entry described with the OMElement representing the entry
206: *
207: * @param elem - OMElement representing the entry
208: * @throws AxisFault if the entry name already exists or if the Element
209: * doesnt represent a entry element
210: */
211: public boolean saveEntry(OMElement elem) throws AxisFault {
212:
213: Entry entry = new Entry();
214: if (elem == null) {
215: handleFault(log,
216: "Unable to save local entry. Null definition", null);
217: }
218: OMAttribute name = elem.getAttribute(new QName(
219: XMLConfigConstants.NULL_NAMESPACE, "name"));
220: String nameValue = null;
221: if (name != null) {
222: if (name.getAttributeValue() != null) {
223: nameValue = name.getAttributeValue().trim();
224: }
225: }
226: if (nameValue != null) {
227: log.debug("Saving local entry with name : " + nameValue);
228:
229: if (getSynapseConfiguration().getEntry(nameValue) == null) {
230: handleFault(log,
231: "Unable to update local entry. Non existent",
232: null);
233: } else {
234: entry.setKey(nameValue);
235: String value = elem.getAttributeValue(new QName(
236: XMLConfigConstants.NULL_NAMESPACE, "value"));
237: String src = elem.getAttributeValue(new QName(
238: XMLConfigConstants.NULL_NAMESPACE, "src"));
239: String key = elem.getAttributeValue(new QName(
240: XMLConfigConstants.NULL_NAMESPACE, "key"));
241: int type = Integer.parseInt(elem
242: .getAttributeValue(new QName(
243: XMLConfigConstants.NULL_NAMESPACE,
244: "type")));
245: OMElement content = elem.getFirstElement();
246:
247: if (type == Entry.URL_SRC) {
248: entry.setType(Entry.URL_SRC);
249: try {
250: entry.setSrc(new URL(src));
251: } catch (MalformedURLException e) {
252: handleFault(log, "Invalid URL : '" + src
253: + "' used in local entry definition : "
254: + nameValue, e);
255: }
256: } else if (type == Entry.REMOTE_ENTRY) {
257: entry.setType(Entry.REMOTE_ENTRY);
258: entry.setKey(key);
259: } else if (type == Entry.INLINE_XML) {
260: entry.setType(Entry.INLINE_XML);
261: entry.setValue(content);
262: } else if (type == Entry.INLINE_TEXT) {
263: entry.setType(Entry.INLINE_TEXT);
264: entry.setValue(elem.getText());
265: } else {
266: handleFault(log, "Unable to add local entry : "
267: + nameValue + ". Unknown entry type", null);
268: }
269: getSynapseConfiguration().addEntry(
270: name.getAttributeValue(), entry);
271: log.info("Added local entry : " + nameValue
272: + " into the configuration");
273: }
274: }
275: return true;
276: }
277:
278: /**
279: * Returns the OMelement representation of the entry given by sequence
280: * name
281: *
282: * @param entryName - name of the entry to get
283: * @return OMElement representing the entryMediator of the given entry
284: * name
285: * @throws AxisFault if any error occured while getting the data from the
286: * SynapseConfiguration
287: */
288: public OMElement getEntry(String entryName) throws AxisFault {
289: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
290:
291: if (synapseConfiguration.getEntry(entryName) != null) {
292: OMElement elem = EntrySerializer.serializeEntry(
293: synapseConfiguration.getEntryDefinition(entryName),
294: null);
295: OMFactory fac = elem.getOMFactory();
296: OMNamespace nullNS = fac.createOMNamespace(
297: XMLConfigConstants.NULL_NAMESPACE, "ns3");
298: elem.declareNamespace(nullNS);
299: if (elem.getAttribute(new QName(
300: XMLConfigConstants.NULL_NAMESPACE, "key")) != null) {
301: if (elem.getAttribute(new QName(
302: XMLConfigConstants.NULL_NAMESPACE, "src")) != null) {
303: elem.addAttribute("type", Integer
304: .toString(Entry.URL_SRC), nullNS);
305: } else if (elem.getFirstOMChild() instanceof OMText) {
306: elem.addAttribute("type", Integer
307: .toString(Entry.INLINE_TEXT), nullNS);
308: } else if (elem.getFirstOMChild() instanceof OMElement) {
309: elem.addAttribute("type", Integer
310: .toString(Entry.INLINE_XML), nullNS);
311: }
312: return elem;
313: } else {
314: handleFault(log,
315: "Unable to fetch local entry. Key missing",
316: null);
317: }
318: } else {
319: handleFault(log, "Entry with the key '" + entryName
320: + "' does not exist", null);
321: }
322: return null;
323: }
324:
325: /**
326: * Deletes the entry with the given name from SynapseConfiguration
327: *
328: * @param entryName - Name of the entry to delete
329: * @throws AxisFault if the entry described by the given name doesnt
330: * exists in the Synapse Configuration
331: */
332: public boolean deleteEntry(String entryName) throws AxisFault {
333: try {
334: log.debug("Deleting local entry with name : " + entryName);
335: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
336: synapseConfiguration.removeEntry(entryName);
337: log.info("Deleted local entry named : " + entryName);
338: return true;
339: } catch (SynapseException syne) {
340: handleFault(log, "Unable to delete the local entry : "
341: + entryName, syne);
342: }
343: return false;
344: }
345:
346: /**
347: * Returns an String array of the entry names present in the synapse configuration
348: *
349: * @return String array of entry names
350: * @throws AxisFault if an error occurs in getting the synapse configuration
351: */
352: public String[] getEntryNames() throws AxisFault {
353: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
354: Map gloabalEntriesMap = synapseConfiguration.getLocalRegistry();
355: List propKeys = new ArrayList();
356: Iterator values = gloabalEntriesMap.values().iterator();
357: while (values.hasNext()) {
358: Object entryValue = values.next();
359: if (entryValue instanceof Entry) {
360: propKeys.add(((Entry) entryValue).getKey());
361: }
362: }
363: return (String[]) propKeys.toArray(new String[propKeys.size()]);
364: }
365:
366: public String getEntryNamesString() throws AxisFault {
367: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
368: Map gloabalEntriesMap = synapseConfiguration.getLocalRegistry();
369: List sequenceList = new ArrayList();
370: List endpointList = new ArrayList();
371: List entryList = new ArrayList();
372: StringBuffer entrySb = new StringBuffer();
373: StringBuffer endpointSb = new StringBuffer();
374: StringBuffer sequenceSb = new StringBuffer();
375: Iterator values = gloabalEntriesMap.values().iterator();
376: while (values.hasNext()) {
377: Object entryValue = values.next();
378: if (entryValue instanceof Endpoint) {
379: Endpoint endpoint = (Endpoint) entryValue;
380: String name = endpoint.getName();
381: if (name != null) {
382: endpointList.add(name);
383: }
384: } else if (entryValue instanceof SequenceMediator) {
385: SequenceMediator sequenceMediator = (SequenceMediator) entryValue;
386: String name = sequenceMediator.getName();
387: if (name != null) {
388: sequenceList.add(name);
389: }
390: } else if (entryValue instanceof Entry) {
391: Entry entry = (Entry) entryValue;
392: if (!entry.isDynamic() && !entry.isRemote()) { // only care pre-defined local entries
393: entryList.add(entry.getKey());
394: }
395: }
396: }
397:
398: if (!sequenceList.isEmpty()) {
399:
400: Collections.sort(sequenceList, new Comparator() {
401: public int compare(Object o1, Object o2) {
402: return ((String) o1)
403: .compareToIgnoreCase((String) o2);
404: }
405: });
406: for (Iterator it = sequenceList.iterator(); it.hasNext();) {
407: String name = (String) it.next();
408: if (name != null) {
409: sequenceSb.append("[Sequence]-" + name + " ");
410: }
411: }
412: }
413:
414: if (!entryList.isEmpty()) {
415:
416: Collections.sort(entryList, new Comparator() {
417: public int compare(Object o1, Object o2) {
418: return ((String) o1)
419: .compareToIgnoreCase((String) o2);
420: }
421: });
422: for (Iterator it = entryList.iterator(); it.hasNext();) {
423: String name = (String) it.next();
424: if (name != null) {
425: entrySb.append("[Entry]-" + name + " ");
426: }
427: }
428: }
429:
430: if (!endpointList.isEmpty()) {
431:
432: Collections.sort(endpointList, new Comparator() {
433: public int compare(Object o1, Object o2) {
434: return ((String) o1)
435: .compareToIgnoreCase((String) o2);
436: }
437: });
438: for (Iterator it = endpointList.iterator(); it.hasNext();) {
439: String name = (String) it.next();
440: if (name != null) {
441: endpointSb.append("[Enpoint]-" + name + " ");
442: }
443: }
444: }
445: return endpointSb.toString() + entrySb.toString()
446: + sequenceSb.toString();
447: }
448: }
|