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.SynapseConfiguration;
025: import org.apache.synapse.registry.Registry;
026: import org.apache.synapse.registry.RegistryEntry;
027: import org.apache.synapse.registry.url.SimpleURLRegistry;
028: import org.wso2.esb.ServiceBusConstants;
029: import org.wso2.esb.registry.ESBRegistry;
030: import org.wso2.esb.registry.ESBRegistryEntry;
031:
032: import javax.xml.namespace.QName;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035:
036: public class RegistryAdmin extends AbstractESBAdmin {
037:
038: private static final Log log = LogFactory
039: .getLog(RegistryAdmin.class);
040:
041: public String getDefaultRegistryName() throws AxisFault {
042: Registry reg = getDefaultRegistry();
043: if (reg == null) {
044: return "Registry is not defined";
045: }
046:
047: String regClass = reg.getProviderClass();
048:
049: if (ESBRegistry.class.getName().equals(regClass)) {
050: return "ESB Registry";
051: } else if (SimpleURLRegistry.class.getName().equals(regClass)) {
052: return "Default Synapse Registry";
053: } else {
054: return "Unknown Registry";
055: }
056: }
057:
058: public String[] getDescendants() throws AxisFault {
059:
060: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
061: Registry reg = synapseConfiguration.getRegistry();
062:
063: ArrayList keyList = new ArrayList();
064:
065: try {
066: RegistryEntry[] entries = reg.getDescendants(null);
067: for (int i = 0; i < entries.length; i++) {
068: keyList.add(entries[i].getKey());
069: }
070: } catch (SynapseException e) {
071: handleFault(log, "Couldn't get descendants from registry",
072: e);
073: }
074:
075: String[] keys = new String[keyList.size()];
076: for (int i = 0; i < keyList.size(); i++) {
077: keys[i] = (String) keyList.get(i);
078: }
079:
080: return keys;
081: }
082:
083: /**
084: * Returns the children of the given registry entry.
085: *
086: * @param key
087: *
088: * @return
089: * <children>
090: * <regEntry><key>key</key><type>file/folder</type></regEntry>
091: * ...
092: * </children>
093: *
094: * @throws AxisFault
095: */
096: public OMElement getChildren(String key) throws AxisFault {
097:
098: OMFactory fac = OMAbstractFactory.getOMFactory();
099: OMElement children = fac.createOMElement("children", null);
100:
101: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
102: Registry reg = synapseConfiguration.getRegistry();
103:
104: try {
105: ESBRegistryEntry entry = new ESBRegistryEntry();
106: // key = null ==> list the root level childrens
107: if (key == null) {
108: key = "";
109: }
110: entry.setKey(key);
111: RegistryEntry[] entries = reg.getChildren(entry);
112: if (entries != null) {
113: for (int i = 0; i < entries.length; i++) {
114: OMElement entryElement = fac.createOMElement(
115: "regEntry", null);
116:
117: OMElement keyElement = fac.createOMElement("key",
118: null);
119: keyElement.setText(entries[i].getKey());
120: entryElement.addChild(keyElement);
121:
122: OMElement typeElement = fac.createOMElement("type",
123: null);
124: if (entries[i].getType() != null
125: && entries[i].getType() == ServiceBusConstants.folder) {
126: typeElement.setText("folder");
127: } else {
128: typeElement.setText("file");
129: }
130: entryElement.addChild(typeElement);
131:
132: children.addChild(entryElement);
133: }
134: } else {
135: handleFault(
136: log,
137: "Selected entry is a leaf level entry. "
138: + "No child entries are available for leaf entries",
139: null);
140: }
141: } catch (SynapseException e) {
142: handleFault(log,
143: "Couldn't get child elements from registry", e);
144: }
145: return children;
146: }
147:
148: /**
149: * Gives the registry entry meta data (not the actual registry entry)
150: *
151: * @param key
152: * @return
153: * @throws AxisFault
154: */
155: public OMElement getRegistryEntry(String key) throws AxisFault {
156:
157: Registry reg = getDefaultRegistry();
158:
159: if (reg != null) {
160: RegistryEntry entry = reg.getRegistryEntry(key);
161:
162: // initialize all values to defaults.
163: String sExpiryTime = "0";
164:
165: if (entry != null && entry.getCachableDuration() > 0) {
166: // registry entry found. update values from that.
167: // we are exposing cachable duration to outside in seconds and it is kept internally in milliseconds
168: Long expiryTime = new Long(
169: entry.getCachableDuration() / 1000);
170: sExpiryTime = expiryTime.toString();
171: }
172:
173: OMFactory fac = OMAbstractFactory.getOMFactory();
174: OMElement regEntry = fac.createOMElement("regEntry", null);
175:
176: OMElement keyElement = fac.createOMElement("key", null);
177: keyElement.setText(key);
178: regEntry.addChild(keyElement);
179:
180: OMElement expElement = fac.createOMElement("expiryTime",
181: null);
182: expElement.setText(sExpiryTime);
183: regEntry.addChild(expElement);
184:
185: return regEntry;
186:
187: } else {
188: handleFault(log, "No registry found", null);
189: return null;
190: }
191: }
192:
193: /**
194: * Updates the registry entry data. Actual registry entry has to created either by uploading a
195: * file or creating a new folder before calling this method.
196: *
197: * @param entryElement
198: * @throws AxisFault
199: */
200: public void updateRegistryEntry(OMElement entryElement)
201: throws AxisFault {
202:
203: Registry reg = getDefaultRegistry();
204:
205: if (reg != null) {
206:
207: ESBRegistryEntry entry = new ESBRegistryEntry();
208:
209: OMElement keyElement = entryElement
210: .getFirstChildWithName(new QName("key"));
211: if (keyElement != null) {
212: entry.setKey(keyElement.getText());
213: } else {
214: handleFault(log,
215: "All registry entries should have a key", null);
216: }
217:
218: log.debug("Updating registry entry with key : "
219: + keyElement.getText());
220: OMElement expiryTime = entryElement
221: .getFirstChildWithName(new QName("expiryTime"));
222: if (expiryTime != null) {
223: long time = 0;
224: try {
225: time = new Long(expiryTime.getText()).longValue();
226: } catch (NumberFormatException e) {
227: handleFault(
228: log,
229: "Cachable duration should be a valid number",
230: e);
231: }
232: // we are exposing cachable duration to outside in seconds and it is kept internally in milliseconds
233: entry.setCachableDuration(time * 1000);
234: }
235:
236: ESBRegistry esbRegistry = (ESBRegistry) reg;
237: esbRegistry.updateRegistryEntry(entry);
238: log.info("Updated registry entry with key : "
239: + keyElement.getText());
240:
241: } else {
242: handleFault(log, "No registry found", null);
243: }
244: }
245:
246: /**
247: * Creates new folder in the given parent folder.
248: *
249: * @param entryElement
250: * <folder><folderName>new folder name</folderName><parent>parent folder name</parent></folder>
251: *
252: * @return OMElement
253: * <folder>
254: * <folderPath>full new folder name<folderPath><status>successfull/failed</status>
255: * </folder>
256: *
257: * @throws AxisFault
258: */
259: public OMElement createFolder(OMElement entryElement)
260: throws AxisFault {
261:
262: Registry reg = getDefaultRegistry();
263:
264: if (reg != null) {
265:
266: String newFolderName = null;
267: String parentName = null;
268:
269: OMElement entryNameElement = entryElement
270: .getFirstChildWithName(new QName("folderName"));
271: OMElement parentNameElement = entryElement
272: .getFirstChildWithName(new QName("parent"));
273:
274: if (entryNameElement != null && parentNameElement != null) {
275: newFolderName = entryNameElement.getText();
276: parentName = parentNameElement.getText();
277: } else {
278: handleFault(
279: log,
280: "Invalid input data. Null entry name or parent",
281: null);
282: }
283:
284: log.debug("Creating folder : " + newFolderName
285: + " under : " + parentName);
286: ESBRegistry esbRegistry = (ESBRegistry) reg;
287:
288: try {
289: esbRegistry.addResource(parentName, newFolderName,
290: false);
291: //esbRegistry.createFolder(parentName, newFolderName);
292:
293: OMFactory fac = OMAbstractFactory.getOMFactory();
294: OMElement rFolder = fac.createOMElement("folder", null);
295:
296: OMElement path = fac
297: .createOMElement("folderPath", null);
298: path.setText(parentName + ESBRegistry.URL_SEPARATOR
299: + newFolderName);
300: rFolder.addChild(path);
301:
302: OMElement status = fac.createOMElement("status", null);
303: status.setText("successfull");
304: rFolder.addChild(status);
305:
306: log.info("Created folder : " + newFolderName
307: + " under : " + parentName);
308: return rFolder;
309:
310: } catch (Exception e) {
311: handleFault(log, "Couldn't add new Registry entry: "
312: + newFolderName, e);
313: }
314:
315: } else {
316: handleFault(log, "No registry found", null);
317: }
318: return null;
319: }
320:
321: /**
322: * Adds a new file to the registry.
323: *
324: * @param fileElement
325: * <file>
326: * <fileName>file name</fileName><parent>parent key</parent><content>xml content</content>
327: * </file>
328: * if <content> element is absent, creates an empty file.
329: *
330: * @return OMElement
331: * <file><filePath>full file path</filePath><status>ok/failed</status></file>
332: */
333: public OMElement createFile(OMElement fileElement) throws AxisFault {
334: Registry reg = getDefaultRegistry();
335:
336: if (reg != null) {
337:
338: String newFileName = null;
339: String parentName = null;
340: String content = null;
341:
342: OMElement entryNameElement = null;
343: OMElement parentNameElement = null;
344: OMElement contentWrapper = null;
345: try {
346: entryNameElement = fileElement
347: .getFirstChildWithName(new QName("fileName"));
348: parentNameElement = fileElement
349: .getFirstChildWithName(new QName("parent"));
350: contentWrapper = fileElement
351: .getFirstChildWithName(new QName("content"));
352: } catch (Exception e) {
353: handleFault(log, "Invalid data. Content should be XML",
354: e);
355: }
356:
357: log.debug("Creating file : " + newFileName + " under : "
358: + parentName);
359:
360: OMElement contentElement = null;
361: if (contentWrapper != null) {
362: contentElement = contentWrapper.getFirstElement();
363: if (contentElement == null) {
364: //if the user has specified a <content> tag, he should provide a valid xml in it
365: handleFault(log,
366: "Invalid data. Content should be XML", null);
367: }
368: }
369:
370: if (entryNameElement != null && parentNameElement != null
371: && contentElement != null) {
372: newFileName = entryNameElement.getText();
373: parentName = parentNameElement.getText();
374: } else {
375: handleFault(log,
376: "Unable to create resource. Resource name, content and parent"
377: + " directory must be specified", null);
378: }
379:
380: ESBRegistry esbRegistry = (ESBRegistry) reg;
381:
382: try {
383: esbRegistry.addResource(parentName, newFileName, true);
384: esbRegistry.updateResource(parentName
385: + ESBRegistry.URL_SEPARATOR + newFileName,
386: contentElement);
387: //esbRegistry.createFile(parentName, newFileName, content);
388:
389: OMFactory fac = OMAbstractFactory.getOMFactory();
390: OMElement rFile = fac.createOMElement("file", null);
391:
392: OMElement filePath = fac.createOMElement("filePath",
393: null);
394: filePath.setText(parentName + ESBRegistry.URL_SEPARATOR
395: + newFileName);
396: rFile.addChild(filePath);
397:
398: OMElement status = fac.createOMElement("status", null);
399: status.setText("ok");
400: rFile.addChild(status);
401:
402: log.info("Created file : " + newFileName + " under : "
403: + parentName);
404: return rFile;
405:
406: } catch (Exception e) {
407: handleFault(log, "Couldn't create new file: "
408: + newFileName + " under : " + parentName, e);
409: }
410:
411: } else {
412: handleFault(log, "No registry found", null);
413: }
414: return null;
415: }
416:
417: public void deleteRegistryResource(String key) throws AxisFault {
418:
419: log.debug("Removing entry with key : " + key);
420: ESBRegistry esbRegistry = (ESBRegistry) getDefaultRegistry();
421: esbRegistry.removeResource(key);
422: log.info("Entry with key : " + key
423: + " removed from the registry");
424: }
425:
426: public OMElement getRegistryEntryValue(String key) throws AxisFault {
427:
428: // check if the request resource is a file or a folder
429: // if it is a folder return a error message
430: // if not look up the resource and append it to the value part
431:
432: OMFactory fac = OMAbstractFactory.getOMFactory();
433: OMElement entryElement = fac.createOMElement("regEntry", null);
434:
435: Registry reg = getDefaultRegistry();
436:
437: try {
438: OMNode resourceNode = reg.lookup(key);
439:
440: OMElement keyElement = fac.createOMElement("key", null);
441: keyElement.setText(key);
442: entryElement.addChild(keyElement);
443:
444: OMElement valueElement = fac.createOMElement("value", null);
445: valueElement.addChild(resourceNode);
446: entryElement.addChild(valueElement);
447: } catch (Exception e) {
448: // add an error message to the body
449: OMElement errElement = fac.createOMElement("error", null);
450: errElement
451: .setText("Couldn't read from the registry entry.");
452: entryElement.addChild(errElement);
453: }
454:
455: return entryElement;
456: }
457:
458: /**
459: * Updates the registry entry file.
460: *
461: * @param regEntryElement
462: * <regEntry><key>registry key</key><value>xml contents of the file</value></regEntry>
463: */
464: public void updateRegistryEntryValue(OMElement regEntryElement)
465: throws AxisFault {
466:
467: OMElement keyElement = null;
468: OMElement valueWrapper = null;
469: try {
470: keyElement = regEntryElement
471: .getFirstChildWithName(new QName("key"));
472: valueWrapper = regEntryElement
473: .getFirstChildWithName(new QName("value"));
474: } catch (OMException e) {
475: handleFault(log, "Error updating entry. Invalid data", e);
476: }
477:
478: if (valueWrapper != null) {
479: OMElement valueElement = null;
480: try {
481: valueElement = valueWrapper.getFirstElement();
482: } catch (Exception e) {
483: handleFault(log,
484: "Error updating entry. Content should be XML",
485: e);
486: }
487: if (keyElement != null && valueElement != null) {
488: String key = keyElement.getText();
489:
490: log.debug("Updating registry entry for key : " + key);
491: ESBRegistry reg = (ESBRegistry) getDefaultRegistry();
492:
493: try {
494: reg.updateResource(key, valueElement);
495: //reg.updateRegistryEntryValue(key, valueElement);
496: log
497: .debug("Updated registry entry for key : "
498: + key);
499: } catch (Exception e) {
500: handleFault(log,
501: "Error updating entry. Couldn't write to file: "
502: + key, e);
503: }
504: }
505: }
506:
507: }
508:
509: public void clearCachedEntries(String key) throws AxisFault {
510: getSynapseConfiguration().clearCachedEntry(key);
511: Iterator itr = getSynapseConfiguration().getCachedEntries()
512: .keySet().iterator();
513: while (itr.hasNext()) {
514: Object o = itr.next();
515: if (o instanceof String) {
516: String cachedEntryKey = o.toString();
517: if (cachedEntryKey.startsWith(key + "/")) {
518: getSynapseConfiguration().clearCachedEntry(
519: cachedEntryKey);
520: }
521: }
522: }
523: }
524:
525: public void clearCache() throws AxisFault {
526: getSynapseConfiguration().clearCache();
527: }
528:
529: private Registry getDefaultRegistry() throws AxisFault {
530:
531: SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
532: return synapseConfiguration.getRegistry();
533: }
534: }
|