[esb-java-dev] svn commit r682 - in esb/java/trunk/modules/core/src: main/java/org/wso2/esb main/java/org/wso2/esb/persistence/dao main/java/org/wso2/esb/persistence/dataobject main/java/org/wso2/esb/registry main/java/org/wso2/esb/services main/java/org/wso2/esb/transport/jetty test/resources

svn at wso2.com svn at wso2.com
Fri Feb 2 05:03:09 PST 2007


Author: chathura
Date: Fri Feb  2 05:02:42 2007
New Revision: 682

Added:
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dao/RegistryEntryDAO.java
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dataobject/RegistryEntryDO.java
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistryEntry.java
   esb/java/trunk/modules/core/src/test/resources/hibernate.cfg.xml
Modified:
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/Constants.java
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/services/RegistryAdmin.java
   esb/java/trunk/modules/core/src/main/java/org/wso2/esb/transport/jetty/JettyServer.java
Log:
Implemented the ESBRegistry with support for persisting registry entry metadata and adding and updating registry resources.

Modified: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/Constants.java
==============================================================================
--- esb/java/trunk/modules/core/src/main/java/org/wso2/esb/Constants.java	(original)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/Constants.java	Fri Feb  2 05:02:42 2007
@@ -15,6 +15,8 @@
  */
 
 package org.wso2.esb;
+
+import java.net.URI;
 /*
  *
  */
@@ -34,6 +36,16 @@
     public static String ESB_WEB_XML_KEY = "ESB_WEB_XML_KEY";
     public static final String WELCOME_PAGE = "INDEX_HTML_FILE";
 
+    // Constants for ESB registry
+
+    public static final int LOCAL_HOST_REGISTRY = 100;
+    public static final int REMOTE_HOST_REGISTRY = 101;
+    public static final int REGISTRY_MODE = LOCAL_HOST_REGISTRY;
+    public static final String LOCAL_REGISTRY_ROOT = "registry/"; // this will be overwritten if localRegistry parameter is set
+    public static final String REGISTRY_FILE = "file";
+    public static final String REGISTRY_FOLDER = "folder";
+    public static final URI folder = URI.create("http://wso2.org/projects/esb/registry/types/folder");
+
     // Constants for ESBSendMediator
 
     public static final String ESBSEND_ELEMENT = "send";

Added: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dao/RegistryEntryDAO.java
==============================================================================
--- (empty file)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dao/RegistryEntryDAO.java	Fri Feb  2 05:02:42 2007
@@ -0,0 +1,85 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.wso2.esb.persistence.dao;
+
+import org.wso2.esb.util.HibernateConfig;
+import org.wso2.esb.persistence.dataobject.RegistryEntryDO;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.Criteria;
+import org.hibernate.criterion.Expression;
+
+public class RegistryEntryDAO extends BaseDAO {
+
+    public RegistryEntryDAO(HibernateConfig hbConfig) {
+        super(hbConfig);
+    }
+
+    public void addRegistryEntry(RegistryEntryDO registryEntryDO) {
+        if (registryEntryDO != null && registryEntryDO.getRegistryKey() != null) {
+            super.create(registryEntryDO);
+        } else {
+            throw new RuntimeException("Invalid registry entry.");
+        }
+    }
+
+    public void updateRegistryEntry(RegistryEntryDO registryEntryDO) {
+        if (registryEntryDO != null && registryEntryDO.getRegistryKey() != null) {
+            RegistryEntryDO storedEntry = getRegistryEntry(registryEntryDO.getRegistryKey());
+            if (storedEntry != null) {
+                storedEntry.setExpiryTime(registryEntryDO.getExpiryTime());
+                super.update(storedEntry);
+            }
+        } else {
+            throw new RuntimeException("Invalid registry entry.");
+        }
+    }
+
+    public void saveOrUpdateRegistryEntry(RegistryEntryDO registryEntryDO) {
+        if (registryEntryDO != null && registryEntryDO.getRegistryKey() != null) {
+            RegistryEntryDO storedEntry = getRegistryEntry(registryEntryDO.getRegistryKey());
+            if (storedEntry != null) {
+                storedEntry.setExpiryTime(registryEntryDO.getExpiryTime());
+                super.update(storedEntry);
+            } else {
+                super.create(registryEntryDO);
+            }
+        } else {
+            throw new RuntimeException("Invalid registry entry.");
+        }
+    }
+
+    public RegistryEntryDO getRegistryEntry(String key) {
+
+        Session session = hbConfig.currentSession();
+        Transaction tx = session.beginTransaction();
+
+        Criteria criteria = session.createCriteria(RegistryEntryDO.class);
+        criteria.add(Expression.eq("registryKey", key.trim()));
+
+        RegistryEntryDO registryEntryDO = (RegistryEntryDO) criteria.uniqueResult();
+
+        session.flush();
+        tx.commit();
+        hbConfig.closeSession();
+
+        return registryEntryDO;
+    }
+}

Added: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dataobject/RegistryEntryDO.java
==============================================================================
--- (empty file)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/persistence/dataobject/RegistryEntryDO.java	Fri Feb  2 05:02:42 2007
@@ -0,0 +1,42 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.wso2.esb.persistence.dataobject;
+
+public class RegistryEntryDO extends BaseDO {
+
+    private String registryKey;
+    private Long expiryTime;
+
+    public String getRegistryKey() {
+        return registryKey;
+    }
+
+    public void setRegistryKey(String registryKey) {
+        this.registryKey = registryKey;
+    }
+
+    public Long getExpiryTime() {
+        return expiryTime;
+    }
+
+    public void setExpiryTime(Long expiryTime) {
+        this.expiryTime = expiryTime;
+    }
+}

Added: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java
==============================================================================
--- (empty file)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistry.java	Fri Feb  2 05:02:42 2007
@@ -0,0 +1,455 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.wso2.esb.registry;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.synapse.registry.RegistryEntry;
+import org.apache.synapse.registry.AbstractRegistry;
+import org.apache.synapse.registry.Registry;
+import org.apache.synapse.SynapseException;
+import org.wso2.esb.Constants;
+import org.wso2.esb.persistence.dataobject.RegistryEntryDO;
+import org.wso2.esb.persistence.PersistenceManager;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import java.net.*;
+import java.io.*;
+import java.util.ArrayList;
+
+/**
+ * Registry implementation for ESB. This assumes that registry resources can be accessed using file
+ * system or http. Registry meta data are accessed using web services.
+ */
+public class ESBRegistry extends AbstractRegistry implements Registry {
+
+    private static final Log log = LogFactory.getLog(ESBRegistry.class);
+
+    private static final int MAX_KEYS = 200;
+
+    private String localRegistry = null;
+    private String metaDataService = null;
+    private int registryType = Constants.LOCAL_HOST_REGISTRY;
+
+    public OMNode lookup(String key) {
+
+        log.info("==> Repository fetch of resource with key : " + key);
+        try {
+            URL url = new URL(getRoot() + key);
+            URLConnection urlc = url.openConnection();
+
+            XMLStreamReader parser = XMLInputFactory.newInstance().
+                    createXMLStreamReader(urlc.getInputStream());
+            StAXOMBuilder builder = new StAXOMBuilder(parser);
+            return builder.getDocumentElement();
+
+        } catch (MalformedURLException e) {
+            handleException("Invalid URL reference " + getRoot() + key, e);
+        } catch (IOException e) {
+            handleException("IO Error reading from URL " + getRoot() + key, e);
+        } catch (XMLStreamException e) {
+            handleException("XML Error reading from URL " + getRoot() + key, e);
+        }
+        return null;
+    }
+
+    public RegistryEntry getRegistryEntry(String key) {
+
+        // get information from the actual resource
+        ESBRegistryEntry entry = new ESBRegistryEntry();
+
+        try {
+            URL url = new URL(getRoot() + key);
+            URLConnection urlc = url.openConnection();
+
+            entry.setKey(key);
+            entry.setName(url.getFile());
+            entry.setType(new URI(urlc.getContentType()));
+            entry.setDescription("Resource at : " + url.toString());
+            entry.setLastModified(urlc.getLastModified());
+            entry.setVersion(urlc.getLastModified());
+            if (urlc.getExpiration() > 0) {
+                entry.setCachableDuration(
+                        urlc.getExpiration() - System.currentTimeMillis());
+            } else {
+                entry.setCachableDuration(getCachableDuration());
+            }
+
+        } catch (MalformedURLException e) {
+            handleException("Invalid URL reference " + getRoot() + key, e);
+        } catch (IOException e) {
+            handleException("IO Error reading from URL " + getRoot() + key, e);
+        } catch (URISyntaxException e) {
+            handleException("URI Syntax error reading from URL " + getRoot() + key, e);
+        }
+
+        // get information from the database
+        PersistenceManager persistenceManager = new PersistenceManager();
+        RegistryEntryDO registryEntryDO = persistenceManager.getRegistryEntry(key);
+
+        if (registryEntryDO != null) {
+
+            if (registryEntryDO.getExpiryTime() != null) {
+                entry.setCachableDuration(registryEntryDO.getExpiryTime().longValue());
+            } else {
+                entry.setCachableDuration(0);
+            }
+        }
+
+        return entry;
+    }
+
+    /**
+     * Updates the metadata of the given registry entry
+     *
+     * @param entry RegistryEntry containing the new metadata
+     */
+    public void updateRegistryEntry(RegistryEntry entry) {
+
+        RegistryEntryDO registryEntryDO = new RegistryEntryDO();
+        registryEntryDO.setRegistryKey(entry.getKey());
+        registryEntryDO.setExpiryTime(new Long(entry.getCachableDuration()));
+
+        PersistenceManager persistenceManager = new PersistenceManager();
+        persistenceManager.saveOrUpdateRegistryEntry(registryEntryDO);
+    }
+
+    /**
+     * Updates the registry resource pointed by the given key.
+     *
+     * @param key Key of the resource to be updated
+     * @param value New value of the resource
+     * @throws Exception
+     */
+    public void updateResource(String key, OMElement value) throws Exception {
+        if (registryType == Constants.LOCAL_HOST_REGISTRY) {
+            File file = new File(localRegistry + key);
+            if (file.exists()) {
+                OutputStream out = new FileOutputStream(file);
+                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
+                writer.write(value.toString());
+                writer.flush();
+                writer.close();
+            }
+        } else {
+            throw new RuntimeException("Remote registry is not supported.");
+        }
+    }
+
+    /**
+     * Adds a new resource to the registry.
+     *
+     * @param parentName Key of the parent of the new resource
+     * @param resourceName Name of the new resource
+     * @param isLeaf Specifies whether the new resource is a leaf or not. In a file system based
+     * registry, leaf is a file and non-leaf is a folder.
+     * @throws Exception
+     */
+    public void addResource(String parentName, String resourceName, boolean isLeaf) throws Exception {
+
+        if (registryType == Constants.LOCAL_HOST_REGISTRY) {
+
+            if (isLeaf) {
+                createFile(parentName, resourceName);
+            } else {
+                createFolder(parentName, resourceName);
+            }
+        }
+    }
+
+    private void createFolder(String parentName, String newFolderName) throws Exception {
+
+        /*
+        search for parent. if found, create the new folder in it. this depends on whether we are
+        using a remote file system or not.
+        add entry 'parentName/newFolderName' as key to the database.
+        */
+
+        if (registryType == Constants.LOCAL_HOST_REGISTRY) {
+            File parent = new File(localRegistry + parentName);
+            if (parent.exists()) {
+
+                File newEntry = new File(parent, newFolderName);
+                newEntry.mkdir();
+
+                // update meta data to the database
+                // note that we are using the registry key part (path without the local registry
+                // root as the key in db
+                // update the db only if we have some thing else to write other than the key
+
+                //RegistryEntryDO registryEntryDO = new RegistryEntryDO();
+                //registryEntryDO.setRegistryKey(parentName + "/" + newFolderName);
+                //
+                //PersistenceManager persistenceManager = new PersistenceManager();
+                //persistenceManager.addRegistryEntry(registryEntryDO);
+
+            } else {
+                throw new SynapseException("Parent folder: " + parentName + " does not exists.");
+            }
+        }
+    }
+
+    private void createFile(String parentName, String newFileName) throws Exception {
+
+        /*
+        search for parent. if found, create the new folder in it. this depends on whether we are
+        using a remote file system or not.
+        */
+
+        if (registryType == Constants.LOCAL_HOST_REGISTRY) {
+            File parent = new File(localRegistry + parentName);
+            if (parent.exists()) {
+
+                File newFile = new File(parent, newFileName);
+                newFile.createNewFile();
+
+                //if (content != null) {
+                //    OutputStream out = new FileOutputStream(newFile);
+                //    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
+                //    writer.write(content);
+                //    writer.flush();
+                //    writer.close();
+                //}
+
+                // update meta data to the database
+                // note that we are using the registry key part (path without the local registry
+                // root as the key in db
+                // update the db only if we have some thing else to write other than the key
+
+                //RegistryEntryDO registryEntryDO = new RegistryEntryDO();
+                //registryEntryDO.setRegistryKey(parentName + "/" + newFolderName);
+                //
+                //PersistenceManager persistenceManager = new PersistenceManager();
+                //persistenceManager.addRegistryEntry(registryEntryDO);
+
+            } else {
+                throw new SynapseException("Parent folder: " + parentName + " does not exists.");
+            }
+        }
+    }
+
+    /**
+     * Configure the ESB registry using registry parameters.
+     *
+     * root: file:directory -   registry is on local host
+     *                          directory is used to access metadata
+     *
+     * root: http/https:location -  has to specify one of the following settings
+     *                  localRegistry - location of the local registry
+     *                  metadataService - url of the service to access metadata
+     * If none of above parameters are given "registry" folder is taken as the local registry.
+     *
+     * @param name
+     * @param value
+     */
+    public void addConfigProperty(String name, String value) {
+
+        if (localRegistry == null) {
+            // registry root should always end with "/"
+            if (Constants.LOCAL_REGISTRY_ROOT.endsWith("/")) {
+                localRegistry = Constants.LOCAL_REGISTRY_ROOT;
+            } else {
+                localRegistry = Constants.LOCAL_REGISTRY_ROOT + "/";
+            }
+        }
+
+        if(name.equals("root")) {
+
+            // root should always end with '/'
+            // therefore, property keys do not have to begin with '/', which could be misleading
+            try {
+                URL url = new URL(value);
+                if(url.getProtocol().equals("file") || url.getProtocol().equals("http")
+                        || url.getProtocol().equals("https")) {
+                    if(!value.endsWith("/")) {
+                        value = value + "/";
+                    }
+
+                    if (url.getProtocol().equals("file")) {
+                        registryType = Constants.LOCAL_HOST_REGISTRY;
+
+                        if (url.getPath().endsWith("/")) {
+                            localRegistry = url.getPath();
+                        } else {
+                            localRegistry = url.getPath() + "/";
+                        }
+                    }
+                }
+            } catch (MalformedURLException e) {
+                // don't set the root if this is not a valid URL
+                throw new RuntimeException("Registry root should be a valid URL.");
+            }
+        }
+
+        if (name.equals("localRegistry")) {
+            registryType = Constants.LOCAL_HOST_REGISTRY;
+
+            // registry root always ends with "/"
+            if (!value.endsWith("/")) {
+                value = value + "/";
+            }
+            localRegistry = value;
+
+        }
+
+        if (name.equals("matadataService")) {
+            registryType = Constants.REMOTE_HOST_REGISTRY;
+            metaDataService = value;
+        }
+
+        super.addConfigProperty(name, value);
+    }
+
+    public String getRoot() {
+        String root = (String) properties.get("root");
+        if (root == null) {
+            return "";
+        } else {
+            return root;
+        }
+    }
+
+    public long getCachableDuration() {
+        String cachableDuration = (String) properties.get("cachableDuration");
+        return cachableDuration == null ? 1500 : Long.parseLong(cachableDuration);
+    }
+
+    /**
+     * Gives the children of the given entry. If the registry is in the same host get the children
+     * (subfolders and files) using the file system. If the registry is in a remote host, get
+     * children using a WS call. Give null or registry entry with "" as the key to list the children
+     * of the root.
+     *
+     * @param entry
+     * @return children of the given entry
+     */
+    public RegistryEntry[] getChildren(RegistryEntry entry) {
+
+        String registryRoot = localRegistry;
+
+        if(entry == null) {
+            // give the children of the root
+            // null or key = "" stands for root
+
+            ESBRegistryEntry urlEntry = new ESBRegistryEntry();
+            urlEntry.setKey("");
+            entry = urlEntry;
+        }
+
+        if (registryType == Constants.LOCAL_HOST_REGISTRY) {
+
+            // registry is in the local file system. access it directly.
+
+            File file = new File(registryRoot + entry.getKey());
+            if(file.isDirectory() == false) {
+                return null;
+            }
+
+            try {
+
+                String[] children = file.list();
+                RegistryEntry[] entries = new RegistryEntry[children.length];
+                for (int i = 0; i < children.length; i ++) {
+
+                    ESBRegistryEntry registryEntry = new ESBRegistryEntry();
+                    if(entry.getKey().equals("")) {
+                        // user asking for the children of the root
+                        registryEntry.setKey(children[i]);
+                    } else {
+                        if(entry.getKey().endsWith("/")) {
+                            registryEntry.setKey(entry.getKey() + children[i]);
+                        } else {
+                            registryEntry.setKey(entry.getKey() + "/" + children[i]);
+                        }
+                    }
+
+                    // set if the registry entry is a file or a folder
+                    File entryFile = new File(registryRoot + registryEntry.getKey());
+                    if (entryFile.isDirectory()) {
+                        registryEntry.setType(Constants.folder);
+                    }
+
+                    entries[i] = registryEntry;
+                }
+
+                return entries;
+
+            } catch(Exception e) {
+                throw new SynapseException("Error in reading the URL.");
+            }
+
+
+        } else if (registryType == Constants.REMOTE_HOST_REGISTRY) {
+
+        }
+
+        return null;
+    }
+
+    public RegistryEntry[] getDescendants(RegistryEntry entry) {
+
+        ArrayList list = new ArrayList();
+        RegistryEntry[] entries = getChildren(entry);
+        if(entries != null) {
+            for(int i=0; i<entries.length; i++) {
+
+                if(list.size() > MAX_KEYS) {
+                    break;
+                }
+
+                fillDescendants(entries[i], list);
+            }
+        }
+
+        RegistryEntry[] descendants = new RegistryEntry[list.size()];
+        for(int i=0; i<list.size(); i++) {
+            descendants[i] = (RegistryEntry) list.get(i);
+        }
+
+        return descendants;
+    }
+
+    private void fillDescendants(RegistryEntry parent, ArrayList list) {
+
+        RegistryEntry[] entries = getChildren(parent);
+        if(entries != null) {
+            for(int i=0; i<entries.length; i++) {
+
+                if(list.size() > MAX_KEYS) {
+                    break;
+                }
+
+                fillDescendants(entries[i], list);
+            }
+        } else {
+            list.add(parent);
+        }
+    }
+
+    private void handleException(String msg, Exception e) {
+        log.error(msg, e);
+        throw new SynapseException(msg, e);
+    }
+}

Added: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistryEntry.java
==============================================================================
--- (empty file)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/registry/ESBRegistryEntry.java	Fri Feb  2 05:02:42 2007
@@ -0,0 +1,116 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.wso2.esb.registry;
+
+import org.apache.synapse.registry.RegistryEntry;
+
+import java.net.URI;
+import java.util.Date;
+
+public class ESBRegistryEntry implements RegistryEntry {
+
+    private String key = null;
+    private String name = null;
+    private long version = Long.MIN_VALUE;
+    private URI type = null;
+    private String description;
+    private long created;
+    private long lastModified;
+    private long cachableDuration;
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public long getVersion() {
+        return version;
+    }
+
+    public void setVersion(long version) {
+        this.version = version;
+    }
+
+    public URI getType() {
+        return type;
+    }
+
+    public void setType(URI type) {
+        this.type = type;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public long getCreated() {
+        return created;
+    }
+
+    public void setCreated(long created) {
+        this.created = created;
+    }
+
+    public long getLastModified() {
+        return lastModified;
+    }
+
+    public void setLastModified(long lastModified) {
+        this.lastModified = lastModified;
+    }
+
+    public long getCachableDuration() {
+        return cachableDuration;
+    }
+
+    public void setCachableDuration(long cachableDuration) {
+        this.cachableDuration = cachableDuration;
+    }
+
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append("RegistryEntry {")
+                .append(" Key : " + key)
+                .append(" Name : " + name)
+                .append(" Ver : " + version)
+                .append(" Type : " + type)
+                .append(" Desc : " + description)
+                .append(" Created : " + new Date(created))
+                .append(" Modified : " + new Date(lastModified))
+                .append(" Cacheable for : " + (cachableDuration / 1000) + "sec")
+                .append("}");
+        return sb.toString();
+    }
+}

Modified: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/services/RegistryAdmin.java
==============================================================================
--- esb/java/trunk/modules/core/src/main/java/org/wso2/esb/services/RegistryAdmin.java	(original)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/services/RegistryAdmin.java	Fri Feb  2 05:02:42 2007
@@ -23,8 +23,17 @@
 import org.apache.synapse.config.SynapseConfiguration;
 import org.apache.synapse.registry.Registry;
 import org.apache.synapse.registry.RegistryEntry;
+import org.apache.synapse.registry.url.SimpleURLRegistry;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMNode;
 import org.wso2.esb.services.utils.RegistryData;
+import org.wso2.esb.registry.ESBRegistry;
+import org.wso2.esb.registry.ESBRegistryEntry;
+import org.wso2.esb.Constants;
 
+import javax.xml.namespace.QName;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -34,6 +43,19 @@
 
     private static final Log log = LogFactory.getLog(RegistryAdmin.class);
 
+    public String getDefaultRegistryName() throws AxisFault {
+        Registry reg = getDefaultRegistry();
+        String regClass = reg.getProviderClass();
+
+        if (ESBRegistry.class.getName().equals(regClass)) {
+            return "ESB Registry";
+        } else if (SimpleURLRegistry.class.getName().equals(regClass)) {
+            return "Default Synapse Registry";
+        } else {
+            return "Unknown Registry";
+        }
+    }
+
     public RegistryData[] registryData() throws AxisFault {
         SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
         Map regMap = synapseConfiguration.getRegistries();
@@ -75,4 +97,327 @@
 
         return keys;
     }
+
+    /**
+     * Returns the children of the given registry entry.
+     *
+     * @param key
+     *
+     * @return
+     * <children>
+     * <regEntry><key>key</key><type>file/folder</type></regEntry>
+     * ...
+     * </children>
+     *
+     * @throws AxisFault
+     */
+    public OMElement getChildren(String key) throws AxisFault {
+
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMElement children = fac.createOMElement("children", null);
+
+        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
+        Map regMap = synapseConfiguration.getRegistries();
+
+        for (Iterator iter = regMap.values().iterator(); iter.hasNext();) {
+            Registry reg = (Registry) iter.next();
+
+            try {
+                ESBRegistryEntry entry = new ESBRegistryEntry();
+                entry.setKey(key);
+                RegistryEntry[] entries = reg.getChildren(entry);
+                for(int i=0; i<entries.length; i++) {
+                    OMElement entryElement = fac.createOMElement("regEntry", null);
+
+                    OMElement keyElement = fac.createOMElement("key", null);
+                    keyElement.setText(entries[i].getKey());
+                    entryElement.addChild(keyElement);
+
+                    OMElement typeElement = fac.createOMElement("type", null);
+                    if (entries[i].getType() != null && entries[i].getType() == Constants.folder) {
+                        typeElement.setText("folder");
+                    } else {
+                        typeElement.setText("file");
+                    }
+                    entryElement.addChild(typeElement);
+
+                    children.addChild(entryElement);
+                }
+            } catch (SynapseException e) {
+                log.error("Couldn't get descendants from registry: " + reg.getRegistryName());
+            }
+        }
+
+        return children;
+    }
+
+    /**
+     * Gives the registry entry meta data (not the actual registry entry)
+     *
+     * @param key
+     * @return
+     * @throws AxisFault
+     */
+    public OMElement getRegistryEntry(String key) throws AxisFault {
+
+        Registry reg = getDefaultRegistry();
+
+        if (reg != null) {
+            RegistryEntry entry = reg.getRegistryEntry(key);
+
+            // initialize all values to defaults.
+            String sExpiryTime = "0";
+
+            if (entry != null) {
+
+                // registry entry found. update values from that.
+
+                Long expiryTime = new Long(entry.getCachableDuration());
+                sExpiryTime = expiryTime.toString();
+            }
+
+            OMFactory fac = OMAbstractFactory.getOMFactory();
+            OMElement regEntry = fac.createOMElement("regEntry", null);
+
+            OMElement keyElement = fac.createOMElement("key", null);
+            keyElement.setText(key);
+            regEntry.addChild(keyElement);
+
+            OMElement expElement = fac.createOMElement("expiryTime", null);
+            expElement.setText(sExpiryTime);
+            regEntry.addChild(expElement);
+
+            return regEntry;
+
+        } else {
+            throw new AxisFault("No registries found.");
+        }
+    }
+
+    /**
+     * Updates the registry entry data. Actual registry entry has to created either by uploading a
+     * file or creating a new folder before calling this method.
+     *
+     * @param entryElement
+     * @throws AxisFault
+     */
+    public void updateRegistryEntry(OMElement entryElement) throws AxisFault {
+
+        Registry reg = getDefaultRegistry();
+
+        if (reg != null) {
+
+            ESBRegistryEntry entry = new ESBRegistryEntry();
+
+            OMElement keyElement = entryElement.getFirstChildWithName(new QName("key"));
+            if (keyElement != null) {
+                entry.setKey(keyElement.getText());
+            } else {
+                throw new AxisFault("All registry entries should have a key.");
+            }
+
+            OMElement expiryTime = entryElement.getFirstChildWithName(new QName("expiryTime"));
+            if (expiryTime != null) {
+                long time = new Long(expiryTime.getText()).longValue();
+                entry.setCachableDuration(time);
+            }
+
+            ESBRegistry esbRegistry = (ESBRegistry) reg;
+            esbRegistry.updateRegistryEntry(entry);
+
+        } else {
+            throw new AxisFault("No registries found.");
+        }
+    }
+
+    /**
+     * Creates new folder in the given parent folder.
+     *
+     * @param entryElement
+     * <folder><folderName>new folder name</folderName><parent>parent folder name</parent></folder>
+     *
+     * @return OMElement
+     * <folder><folderPath>full new folder name<folderPath><status>successfull/failed</status><folder>
+     *
+     * @throws AxisFault
+     */
+    public OMElement createFolder(OMElement entryElement) throws AxisFault {
+
+        Registry reg = getDefaultRegistry();
+
+        if (reg != null) {
+
+            String newFolderName;
+            String parentName;
+
+            OMElement entryNameElement = entryElement.getFirstChildWithName(new QName("folderName"));
+            OMElement parentNameElement = entryElement.getFirstChildWithName(new QName("parent"));
+
+            if (entryNameElement != null && parentNameElement != null) {
+                newFolderName = entryNameElement.getText();
+                parentName = parentNameElement.getText();
+            } else {
+                throw new AxisFault("Invalid input data format.");
+            }
+
+            ESBRegistry esbRegistry = (ESBRegistry) reg;
+
+            try {
+                esbRegistry.addResource(parentName, newFolderName, false);
+                //esbRegistry.createFolder(parentName, newFolderName);
+
+                OMFactory fac = OMAbstractFactory.getOMFactory();
+                OMElement rFolder = fac.createOMElement("folder", null);
+
+                OMElement path = fac.createOMElement("folderPath", null);
+                path.setText(parentName + "/" + newFolderName);
+                rFolder.addChild(path);
+
+                OMElement status = fac.createOMElement("status", null);
+                status.setText("successfull");
+                rFolder.addChild(status);
+
+                return rFolder;
+
+            } catch (Exception e) {
+                throw new AxisFault("Couldn't add new entry: " + newFolderName);
+            }
+
+        } else {
+            throw new AxisFault("No registries found.");
+        }
+    }
+
+    /**
+     * Adds a new file to the registry.
+     *
+     * @param fileElement
+     * <file><fileName>file name</fileName><parent>parent key</parent><content>xml content</content></file>
+     * if <content> element is absent, creates an empty file.
+     *
+     * @return OMElement
+     * <file><filePath>full file path</filePath><status>ok/failed</status></file>
+     */
+    public OMElement createFile(OMElement fileElement) throws AxisFault {
+        Registry reg = getDefaultRegistry();
+
+        if (reg != null) {
+
+            String newFileName;
+            String parentName;
+            String content = null;
+
+            OMElement entryNameElement = fileElement.getFirstChildWithName(new QName("fileName"));
+            OMElement parentNameElement = fileElement.getFirstChildWithName(new QName("parent"));
+
+            OMElement contentWrapper = fileElement.getFirstChildWithName(new QName("content"));
+            OMElement contentElement = null;
+            if (contentWrapper != null) {
+                contentElement = contentWrapper.getFirstElement();
+            }
+
+            if (entryNameElement != null && parentNameElement != null) {
+                newFileName = entryNameElement.getText();
+                parentName = parentNameElement.getText();
+                if (contentElement != null) {
+                    content = contentElement.toString();
+                }
+            } else {
+                throw new AxisFault("Invalid input data format.");
+            }
+
+            ESBRegistry esbRegistry = (ESBRegistry) reg;
+
+            try {
+                esbRegistry.addResource(parentName, newFileName, true);
+                esbRegistry.updateResource(parentName + "/" + newFileName, contentElement);
+                //esbRegistry.createFile(parentName, newFileName, content);
+
+                OMFactory fac = OMAbstractFactory.getOMFactory();
+                OMElement rFile = fac.createOMElement("file", null);
+
+                OMElement filePath = fac.createOMElement("filePath", null);
+                filePath.setText(parentName + "/" + newFileName);
+                rFile.addChild(filePath);
+
+                OMElement status = fac.createOMElement("status", null);
+                status.setText("ok");
+                rFile.addChild(status);
+
+                return rFile;
+                
+            } catch (Exception e) {
+                throw new AxisFault("Couldn't create new file: " + newFileName);
+            }
+
+        } else {
+            throw new AxisFault("No registries found.");
+        }
+    }
+
+    public OMElement getRegistryEntryValue(String key) throws AxisFault {
+
+        // check if the request resource is a file or a folder
+        // if it is a folder return a error message
+        // if not look up the resource and append it to the value part
+
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMElement entryElement = fac.createOMElement("regEntry", null);
+
+        Registry reg = getDefaultRegistry();
+
+        try {
+            OMNode resourceNode = reg.lookup(key);
+
+            OMElement keyElement = fac.createOMElement("key", null);
+            keyElement.setText(key);
+            entryElement.addChild(keyElement);
+
+            OMElement valueElement = fac.createOMElement("value", null);
+            valueElement.addChild(resourceNode);
+            entryElement.addChild(valueElement);
+        } catch(Exception e) {
+            // add an error message to the body
+
+            OMElement errElement = fac.createOMElement("error", null);
+            errElement.setText("Couldn't read from the registry entry.");
+            entryElement.addChild(errElement);
+        }
+
+        return entryElement;
+    }
+
+    /**
+     * Updates the registry entry file.
+     *
+     * @param regEntryElement
+     * <regEntry><key>registry key</key><value>xml contents of the file</value></regEntry>
+     */
+    public void updateRegistryEntryValue(OMElement regEntryElement) throws AxisFault {
+
+        OMElement keyElement = regEntryElement.getFirstChildWithName(new QName("key"));
+        OMElement valueWrapper = regEntryElement.getFirstChildWithName(new QName("value"));
+        if (valueWrapper != null) {
+            OMElement valueElement = valueWrapper.getFirstElement();
+            if (keyElement != null && valueElement != null) {
+                String key = keyElement.getText();
+
+                ESBRegistry reg = (ESBRegistry) getDefaultRegistry();
+
+                try {
+                    reg.updateResource(key, valueElement);
+                    //reg.updateRegistryEntryValue(key, valueElement);
+                } catch (Exception e) {
+                    throw new AxisFault("Couldn't write to file: " + key);
+                }
+            }
+        }
+
+    }
+
+    private Registry getDefaultRegistry() throws AxisFault {
+
+        SynapseConfiguration synapseConfiguration = getSynapseConfiguration();
+        return synapseConfiguration.getRegistry("DEFAULT");
+    }
 }

Modified: esb/java/trunk/modules/core/src/main/java/org/wso2/esb/transport/jetty/JettyServer.java
==============================================================================
--- esb/java/trunk/modules/core/src/main/java/org/wso2/esb/transport/jetty/JettyServer.java	(original)
+++ esb/java/trunk/modules/core/src/main/java/org/wso2/esb/transport/jetty/JettyServer.java	Fri Feb  2 05:02:42 2007
@@ -109,12 +109,18 @@
             requestLog.setLogTimeZone("GMT");
             requestLogHandler.setRequestLog(requestLog);
 
+            ContextHandler registryCtx = new ContextHandler();
+            registryCtx.setContextPath("/registry");
+            ResourceHandler registryResourceHandler = new ResourceHandler();
+            registryResourceHandler.setResourceBase("/home/chathura/temp");
+            registryCtx.addHandler(registryResourceHandler);
+
             httpContext.setHandlers(new Handler[]{
-                soapCtx, restCtx, contextRootSoap, contextRootRest});
+                soapCtx, restCtx, contextRootSoap, contextRootRest, registryCtx});
             httpsContext.setHandlers(new Handler[]{
-               contextRootSoap, contextRootRest, soapCtx, restCtx});
+               contextRootSoap, contextRootRest, soapCtx, restCtx, registryCtx});
             adminContext.setHandlers(new Handler[] {
-                stylesCtx, adminCtx, docsCtx, soapCtx, contextRootSoap});
+                stylesCtx, adminCtx, docsCtx, soapCtx, contextRootSoap, registryCtx});
 
             httpHandlers.setHandlers(
                     new Handler[]{httpContext, new ServerDefaultHandler(), requestLogHandler});

Added: esb/java/trunk/modules/core/src/test/resources/hibernate.cfg.xml
==============================================================================
--- (empty file)
+++ esb/java/trunk/modules/core/src/test/resources/hibernate.cfg.xml	Fri Feb  2 05:02:42 2007
@@ -0,0 +1,34 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!DOCTYPE hibernate-configuration PUBLIC
+        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+<hibernate-configuration>
+
+    <session-factory>
+        <!-- Settings for a Derby Network Server database. -->
+        <property name="connection.url">
+            jdbc:derby://localhost:1527//home/chathura/temp/db/db1/DB2</property>
+        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
+        <property name="connection.username">wso2esb</property>
+        <property name="connection.password">wso2esb</property>
+        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
+        <!--<property name="hibernate.hbm2ddl.auto">create</property>-->
+
+
+        <!-- Use the Hibernate built-in pool -->
+        <property name="connection.pool_size">5</property>
+
+        <!-- Use EHCache but not the query cache.
+        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
+        <property name="cache.use_query_cache">false</property>
+        <property name="cache.use_minimal_puts">false</property>
+        -->
+
+        <!-- Print SQL to stdout -->
+        <property name="show_sql">true</property>
+
+        <!-- mapping files -->
+        <mapping resource="reg-meta-data.hbm.xml"/>
+
+    </session-factory>
+</hibernate-configuration>
\ No newline at end of file




More information about the Esb-java-dev mailing list