Author: chathura
Date: Mon Dec 17 23:10:21 2007
New Revision: 11327

Log:


Implemented the support for create data sources at start up using the 
RegistryConfiguration.



Modified:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/DataBaseConfiguration.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfiguration.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
   trunk/registry/modules/core/src/main/resources/registry.xml

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
  Mon Dec 17 23:10:21 2007
@@ -31,6 +31,9 @@
     public static final String INMEM_REGISTRY_TYPE = "InMemoryRegistry";
     public static final String STATUS_MESSAGE_NAME = "edit_status";
 
+    public static final String REGISTRY_CONFIG_PATH = "registry.config.path";
+    public static final String REGISTRY_CONFIG = "registry.config";
+
     /**
      * Built in user names
      */

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/DataBaseConfiguration.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/DataBaseConfiguration.java
       (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/DataBaseConfiguration.java
       Mon Dec 17 23:10:21 2007
@@ -24,12 +24,20 @@
  */
 public class DataBaseConfiguration {
 
+    private String dataSourceName;
     private String dbUrl;
     private String userName;
     private String passWord;
     private String driverName;
     private String configName;
 
+    public String getDataSourceName() {
+        return dataSourceName;
+    }
+
+    public void setDataSourceName(String dataSourceName) {
+        this.dataSourceName = dataSourceName;
+    }
 
     public String getDbUrl() {
         return dbUrl;

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfiguration.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfiguration.java
       (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfiguration.java
       Mon Dec 17 23:10:21 2007
@@ -36,6 +36,7 @@
 
 public class RegistryConfiguration {
 
+    private DataBaseConfiguration deafultDataBaseConfiguration = null;
     private Map dbconfgigMap = new HashMap();
 
     /**
@@ -69,28 +70,46 @@
                 while (dbconfgs.hasNext()) {
                     OMElement dbconfig = (OMElement) dbconfgs.next();
                     DataBaseConfiguration dataBaseConfiguration = new 
DataBaseConfiguration();
+
                     String dbName = dbconfig.getAttributeValue(new 
QName("name"));
                     if (dbName == null) {
                         throw new 
RegistryException(Messages.getMessage("dbname.null"));
                     }
                     dataBaseConfiguration.setConfigName(dbName);
-                    OMElement userName = dbconfig.getFirstChildWithName(new 
QName("userName"));
-                    if (userName != null) {
-                        dataBaseConfiguration.setUserName(userName.getText());
-                    }
-                    OMElement password = dbconfig.getFirstChildWithName(new 
QName("password"));
-                    if (password != null) {
-                        dataBaseConfiguration.setPassWord(password.getText());
-                    }
-                    OMElement url = dbconfig.getFirstChildWithName(new 
QName("url"));
-                    if (url != null) {
-                        dataBaseConfiguration.setDbUrl(url.getText());
-                    }
-                    OMElement driverName = dbconfig.getFirstChildWithName(new 
QName("driverName"));
-                    if (driverName != null) {
-                        
dataBaseConfiguration.setDriverName(driverName.getText());
+
+                    OMElement dataSource = dbconfig.getFirstChildWithName(new 
QName("dataSource"));
+                    if (dataSource != null) {
+                        
dataBaseConfiguration.setDataSourceName(dataSource.getText());
+
+                    } else {
+
+                        OMElement userName = 
dbconfig.getFirstChildWithName(new QName("userName"));
+                        if (userName != null) {
+                            
dataBaseConfiguration.setUserName(userName.getText());
+                        }
+
+                        OMElement password = 
dbconfig.getFirstChildWithName(new QName("password"));
+                        if (password != null) {
+                            
dataBaseConfiguration.setPassWord(password.getText());
+                        }
+
+                        OMElement url = dbconfig.getFirstChildWithName(new 
QName("url"));
+                        if (url != null) {
+                            dataBaseConfiguration.setDbUrl(url.getText());
+                        }
+
+                        OMElement driverName = 
dbconfig.getFirstChildWithName(new QName("driverName"));
+                        if (driverName != null) {
+                            
dataBaseConfiguration.setDriverName(driverName.getText());
+                        }
                     }
+
                     dbconfgigMap.put(dbName, dataBaseConfiguration);
+
+                    String defaultConfig = dbconfig.getAttributeValue(new 
QName("default"));
+                    if (defaultConfig != null && 
defaultConfig.equalsIgnoreCase("true")) {
+                        setDeafultDataBaseConfiguration(dataBaseConfiguration);
+                    }
                 }
             }
 
@@ -134,4 +153,12 @@
         }
         return root;
     }
+
+    public DataBaseConfiguration getDeafultDataBaseConfiguration() {
+        return deafultDataBaseConfiguration;
+    }
+
+    public void setDeafultDataBaseConfiguration(DataBaseConfiguration 
deafultDataBaseConfiguration) {
+        this.deafultDataBaseConfiguration = deafultDataBaseConfiguration;
+    }
 }

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
    (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
    Mon Dec 17 23:10:21 2007
@@ -25,8 +25,12 @@
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.Resource;
+import org.wso2.registry.config.RegistryConfiguration;
+import org.wso2.registry.config.DataBaseConfiguration;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.InMemoryJDBCRegistry;
+import org.wso2.registry.jdbc.JDBCRegistry;
+import org.wso2.registry.jdbc.utils.RegistryDataSource;
 import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
 import org.wso2.registry.jdbc.realm.RegistryRealm;
 import org.wso2.registry.secure.SecureRegistry;
@@ -36,6 +40,10 @@
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.sql.DataSource;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import java.io.IOException;
 
 /** Servlet for providing REST API for the registry. */
@@ -50,30 +58,85 @@
 
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
+
         RegistryRealm registryRealm;
         Registry coreRegistry = null;
+
         try {
-            registryRealm = new InMemoryRegistryRealm();
-            String coreRegistryTypeParam =
-                    
config.getInitParameter(RegistryConstants.REGISTRY_TYPE_PARAMTER);
-            if (coreRegistryTypeParam == null ||
-                
coreRegistryTypeParam.equals(RegistryConstants.JDBC_REGISTRY_TYPE)) {
+
+            // read the registry.xml file from the configured location. if not 
configured, read the
+            // default registry.xml file bundled with the webapp.
+            String configPath = 
config.getInitParameter(RegistryConstants.REGISTRY_CONFIG_PATH);
+            RegistryConfiguration registryConfiguration = new 
RegistryConfiguration();
+            registryConfiguration.populateRegistryConfig(configPath);
+            config.getServletContext().
+                    setAttribute(RegistryConstants.REGISTRY_CONFIG, 
registryConfiguration);
+
+            DataBaseConfiguration dbConfiguration =
+                    registryConfiguration.getDeafultDataBaseConfiguration();
+
+            if (dbConfiguration.getConfigName().equals("in-memory")) {
+                registryRealm = new InMemoryRegistryRealm();
                 coreRegistry = new InMemoryJDBCRegistry(registryRealm);
-            } else if 
(coreRegistryTypeParam.equals(RegistryConstants.REMOTE_REGISTRY_TYPE)) {
-                // todo: initialize core registry with a remote registry 
instance.
+            } else {
+
+                DataSource dataSource = null;
+
+                if (dbConfiguration.getDataSourceName() != null &&
+                        dbConfiguration.getDataSourceName().length() > 0) {
+
+                    try {
+                        Context context = new InitialContext();
+                        dataSource =
+                                (DataSource) 
context.lookup(dbConfiguration.getDataSourceName());
+
+                    } catch (NamingException e) {
+
+                        String msg = "Could not find the specified data source 
named: " +
+                                dbConfiguration.getDataSourceName() + ". 
Caused by: " +
+                                e.getMessage();
+                        log.fatal(msg, e);
+                        throw new ServletException(msg);
+                    }
+
+                } else {
+
+                    dataSource = new RegistryDataSource(
+                            dbConfiguration.getDbUrl(),
+                            dbConfiguration.getDriverName(),
+                            dbConfiguration.getUserName(),
+                            dbConfiguration.getPassWord());
+                }
+
+                registryRealm = new RegistryRealm(dataSource);
+                coreRegistry = new JDBCRegistry(dataSource, registryRealm);
             }
+
             // create a system registry and put it in the context
             SecureRegistry systemRegistry =
                     new SecureRegistry(RegistryConstants.SYSTEM_USER,
-                                       RegistryConstants.SYSTEM_PASSWORD,
-                                       coreRegistry,
-                                       registryRealm);
+                            RegistryConstants.SYSTEM_PASSWORD,
+                            coreRegistry,
+                            registryRealm);
+
             
config.getServletContext().setAttribute(RegistryConstants.REGISTRY, 
coreRegistry);
             config.getServletContext()
                     .setAttribute(RegistryConstants.REGISTRY_REALM, 
registryRealm);
             System.getProperties().put(RegistryConstants.REGISTRY, 
coreRegistry);
             System.getProperties().put(RegistryConstants.SYSTEM_REGISTRY, 
systemRegistry);
 
+
+            //String coreRegistryTypeParam =
+            //        
config.getInitParameter(RegistryConstants.REGISTRY_TYPE_PARAMTER);
+            //
+            //if (coreRegistryTypeParam == null ||
+            //        
coreRegistryTypeParam.equals(RegistryConstants.JDBC_REGISTRY_TYPE)) {
+            //
+            //
+            //} else if 
(coreRegistryTypeParam.equals(RegistryConstants.REMOTE_REGISTRY_TYPE)) {
+            //    // todo: initialize core registry with a remote registry 
instance.
+            //}
+
         } catch (RegistryException e) {
             e.printStackTrace();
             throw new ServletException(e.getMessage());
@@ -109,8 +172,8 @@
             String path = fileElement.getPath();
             registry.put(path, fileElement);
             
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
-                                              "Resource " + path +
-                                              " is sucessfully added to the 
registry.");
+                    "Resource " + path +
+                            " is sucessfully added to the registry.");
             response.setContentType("text/html");
             request.getRequestDispatcher(ConsoleURLMapper.RESOURCES_JSP)
                     .forward(request, response);
@@ -119,7 +182,7 @@
 
             e.printStackTrace();
             
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
-                                              e.getMessage());
+                    e.getMessage());
             response.setContentType("text/html");
             request.getRequestDispatcher(ConsoleURLMapper.RESOURCES_JSP)
                     .forward(request, response);
@@ -196,7 +259,7 @@
             return true;
         }
         String path = uri.substring((contextRoot + 
RegistryConstants.TAG_REGISTRY).length(),
-                                    uri.length());
+                uri.length());
         path = path + query;
 
         try {

Modified: trunk/registry/modules/core/src/main/resources/registry.xml
==============================================================================
--- trunk/registry/modules/core/src/main/resources/registry.xml (original)
+++ trunk/registry/modules/core/src/main/resources/registry.xml Mon Dec 17 
23:10:21 2007
@@ -1,8 +1,17 @@
 <wso2regsitry>
+    
+    <dbconfig name="in-memory" default="true">
+    </dbconfig>
+
     <dbconfig name="DB1">
         <url>url</url>
         <userName>userName</userName>
         <password>password</password>
         <driverName>driverName</driverName>
     </dbconfig>
+
+    <dbconfig name="DB1">
+        <dataSource>dataSource</dataSource>
+    </dbconfig>
+
 </wso2regsitry>
\ No newline at end of file

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to