Author: keith
Date: Mon Mar 24 23:24:44 2008
New Revision: 15056

Log:

Refractoring codebase so that we reuse the JSDeployer logic in the DBDeployer



Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/deployers/DBDeployer.java
   trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
   
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/deployers/DBDeployer.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/deployers/DBDeployer.java    
    (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/deployers/DBDeployer.java    
    Mon Mar 24 23:24:44 2008
@@ -30,27 +30,14 @@
 import org.apache.commons.logging.LogFactory;
 import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.mashup.MashupConstants;
-import org.wso2.mashup.MashupFault;
 import org.wso2.mashup.utils.MashupUtils;
-import org.wso2.mashup.webapp.utils.RegistryUtils;
 import org.wso2.registry.RegistryConstants;
-import org.wso2.registry.RegistryException;
-import org.wso2.registry.ResourceImpl;
-import org.wso2.registry.jdbc.JDBCRegistry;
-import org.wso2.registry.secure.SecureRegistry;
-import org.wso2.registry.users.AccessControlAdmin;
 import org.wso2.registry.users.UserRealm;
-import org.wso2.registry.users.UserStoreException;
-import org.wso2.registry.users.accesscontrol.AccessControlConstants;
 import org.wso2.ws.dataservice.DBConstants;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
 import java.util.ArrayList;
 
 public class DBDeployer extends org.wso2.ws.dataservice.DBDeployer {
@@ -110,11 +97,12 @@
             if (serviceStatus.startsWith("Error:")) {
                 
axisConfig.getFaultyServices().put(deploymentFileData.getFile().getAbsolutePath(),
                                                    serviceStatus);
-                populateRegistry(deploymentFileData,
-                                 "This Service is Faulty. Reason : " + 
serviceStatus,
-                                 DescriptionBuilder.getShortFileName(
-                                         deploymentFileData.getName()),
-                                 true);
+                MashupUtils.populateRegistry(configCtx, deploymentFileData,
+                                             "This Service is Faulty. Reason : 
" + serviceStatus,
+                                             
DescriptionBuilder.getShortFileName(
+                                                     
deploymentFileData.getName()),
+                                             true, 
MashupConstants.DATA_SERVICE_MEDIA_TYPE,
+                                             
DBConstants.DB_SERVICE_CONFIG_FILE);
 
 
             }
@@ -134,7 +122,7 @@
                 RegistryConstants.REGISTRY_REALM);
         // Infer the username (The owner of this service)
         File file = currentFile.getFile();
-        String username = inferUserName(file, realm, name);
+        String username = MashupUtils.inferUserName(file, realm, name);
 
         //create a service group per service.
         // ServiceGroup name and axisservice name will be of the form 
username-serviceName
@@ -165,127 +153,4 @@
         axisService.addParameter(resourceFolderParameter);
         return serviceList;
     }
-
-    private String inferUserName(File file, UserRealm realm, String 
serviceName)
-            throws MashupFault {
-        String username = file.getParentFile().getName();
-        try {
-            if (!realm.getUserStoreAdmin().isExistingUser(username)) {
-                throw new MashupFault("No user with the username " + username +
-                        " exists in the system. Cannot deploy " + serviceName);
-            }
-        } catch (UserStoreException e) {
-            throw new MashupFault(e);
-        }
-        return username;
-    }
-
-    private String populateRegistry(DeploymentFileData currentFile, String 
documentation,
-                                    String name, boolean faulty)
-            throws DeploymentException {
-
-        try {
-            UserRealm realm = (UserRealm) 
configCtx.getAxisConfiguration().getParameterValue(
-                    RegistryConstants.REGISTRY_REALM);
-            File file = currentFile.getFile();
-
-            // Infer the username (The owner of this service)
-            String username = inferUserName(file, realm, name);
-
-            // Assembling the path for this service
-            String path1 = "/mashups/" + username + "/" + name;
-
-            // Get the realm and the registry
-            JDBCRegistry registry =
-                    (JDBCRegistry) 
configCtx.getAxisConfiguration().getParameterValue(
-                            RegistryConstants.REGISTRY);
-            SecureRegistry secureRegistry =
-                    RegistryUtils.createSecureRegistry(username, registry, 
realm);
-
-            ResourceImpl resource1;
-
-            //todo: Workaround to figure out whether the resource is deleted. 
Remove when registry implements undelete/rollback.
-            boolean resourceExists = false;
-            try {
-                secureRegistry.get(path1);
-                resourceExists = true;
-            } catch (RegistryException e) {
-                //just swallowing the exception
-            }
-
-            if (resourceExists) {
-                //Getting the existing latest version from the registry
-                resource1 = (ResourceImpl) secureRegistry.get(path1);
-
-                String regMD5 = "";
-                try {
-                    regMD5 = 
resource1.getProperty(MashupConstants.CONTENT_MD5);
-                } catch (Exception e) {
-                    log.warn(e);
-                }
-
-                String fileMD5 = MashupUtils.calculateFileMD5(file);
-
-                if (!regMD5.equals(fileMD5)) {
-                    // The content of the service has changed, updating the 
registry
-                    addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
-                                  secureRegistry);
-                }
-
-            } else {
-                // A brand new service, adding to the registry
-                addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
-                              secureRegistry);
-            }
-
-            AccessControlAdmin ac = realm.getAccessControlAdmin();
-            ac.authorizeUser(username, path1, AccessControlConstants.EDIT);
-            ac.authorizeUser(username, path1, AccessControlConstants.DELETE);
-
-            return path1;
-        } catch (RegistryException e) {
-            throw new DeploymentException(e);
-        } catch (IOException e) {
-            throw new DeploymentException(e);
-        } catch (UserStoreException e) {
-            throw new DeploymentException(e);
-        }
-    }
-
-    private void addToRegistry(DeploymentFileData currentFile, String 
documentation, boolean faulty,
-                               File file, String username, String path1,
-                               SecureRegistry secureRegistry)
-            throws IOException, RegistryException {
-        ResourceImpl resource1;// create a content resource
-        resource1 = new ResourceImpl();
-
-        resource1.setAuthorUserName(username);
-        FileInputStream fis = new FileInputStream(file);
-        FileChannel fc = fis.getChannel();
-        byte[] data =
-                new byte[(int) (fc.size())];   // fc.size returns the size of 
the file which backs the channel
-        ByteBuffer bb = ByteBuffer.wrap(data);
-        fc.read(bb);
-        resource1.setContent(data);
-        resource1.setDescription(documentation);
-        resource1.setMediaType(MashupConstants.DATA_SERVICE_MEDIA_TYPE);
-        if (faulty) {
-            resource1.setProperty(MashupConstants.FAULTY_MASHUP, "true");
-        }
-
-        //Setting the service js file path and resources directory path
-        resource1.setProperty(DBConstants.DB_SERVICE_CONFIG_FILE, 
file.getAbsolutePath());
-
-        //Setting the MD5 of the content
-        resource1.setProperty(MashupConstants.CONTENT_MD5, 
MashupUtils.calculateFileMD5(file));
-
-        File resourcesDir = new File(currentFile.getFile().getParent(), 
DescriptionBuilder
-                .getShortFileName(currentFile.getName()) + ".resources");
-        resource1.setProperty(JavaScriptEngineConstants.RESOURCES_FOLDER,
-                              resourcesDir.getAbsolutePath());
-
-        // store it in a non existence path
-        secureRegistry.put(path1, resource1);
-    }
-
 }

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
Mon Mar 24 23:24:44 2008
@@ -25,6 +25,9 @@
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.ConfigurationContextFactory;
 import org.apache.axis2.deployment.DeploymentEngine;
+import org.apache.axis2.deployment.DeploymentException;
+import org.apache.axis2.deployment.DescriptionBuilder;
+import org.apache.axis2.deployment.repository.util.DeploymentFileData;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.Parameter;
 import org.apache.axis2.engine.AxisConfiguration;
@@ -43,8 +46,15 @@
 import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.mashup.MashupConstants;
 import org.wso2.mashup.MashupFault;
+import org.wso2.mashup.webapp.utils.RegistryUtils;
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
+import org.wso2.registry.ResourceImpl;
+import org.wso2.registry.secure.SecureRegistry;
+import org.wso2.registry.users.UserRealm;
+import org.wso2.registry.users.AccessControlAdmin;
+import org.wso2.registry.users.UserStoreException;
+import org.wso2.registry.users.accesscontrol.AccessControlConstants;
 import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.utils.ServerConfiguration;
 import org.wso2.utils.security.CryptoUtil;
@@ -52,6 +62,7 @@
 import org.wso2.wsas.ServerManager;
 import org.wso2.wsas.persistence.PersistenceManager;
 import org.wso2.wsas.persistence.dataobject.ServiceUserDO;
+import org.wso2.ws.dataservice.DBConstants;
 
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanException;
@@ -79,6 +90,8 @@
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Properties;
+import java.nio.channels.FileChannel;
+import java.nio.ByteBuffer;
 
 public class MashupUtils {
 
@@ -792,4 +805,126 @@
         return retMD5;
     }
 
+    public static String populateRegistry(ConfigurationContext configCtx, 
DeploymentFileData currentFile, String documentation,
+                                    String name, boolean faulty, String 
mediaType, String serviceType)
+            throws DeploymentException {
+
+        try {
+            UserRealm realm = (UserRealm) 
configCtx.getAxisConfiguration().getParameterValue(
+                    RegistryConstants.REGISTRY_REALM);
+            File file = currentFile.getFile();
+
+            // Infer the username (The owner of this service)
+            String username = inferUserName(file, realm, name);
+
+            // Assembling the path for this service
+            String path1 = "/mashups/" + username + "/" + name;
+
+            // Get the realm and the registry
+            JDBCRegistry registry =
+                    (JDBCRegistry) 
configCtx.getAxisConfiguration().getParameterValue(
+                            RegistryConstants.REGISTRY);
+            SecureRegistry secureRegistry =
+                    RegistryUtils.createSecureRegistry(username, registry, 
realm);
+
+            ResourceImpl resource1;
+
+            //todo: Workaround to figure out whether the resource is deleted. 
Remove when registry implements undelete/rollback.
+            boolean resourceExists = false;
+            try {
+                secureRegistry.get(path1);
+                resourceExists = true;
+            } catch (RegistryException e) {
+                //just swallowing the exception
+            }
+
+            if (resourceExists) {
+                //Getting the existing latest version from the registry
+                resource1 = (ResourceImpl) secureRegistry.get(path1);
+
+                String regMD5 = "";
+                try {
+                    regMD5 = 
resource1.getProperty(MashupConstants.CONTENT_MD5);
+                } catch (Exception e) {
+                    log.warn(e);
+                }
+
+                String fileMD5 = MashupUtils.calculateFileMD5(file);
+
+                if (!regMD5.equals(fileMD5)) {
+                    // The content of the service has changed, updating the 
registry
+                    addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
+                                  secureRegistry, mediaType, serviceType);
+                }
+
+            } else {
+                // A brand new service, adding to the registry
+                addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
+                              secureRegistry, mediaType, serviceType);
+            }
+
+            AccessControlAdmin ac = realm.getAccessControlAdmin();
+            ac.authorizeUser(username, path1, AccessControlConstants.EDIT);
+            ac.authorizeUser(username, path1, AccessControlConstants.DELETE);
+
+            return path1;
+        } catch (RegistryException e) {
+            throw new DeploymentException(e);
+        } catch (IOException e) {
+            throw new DeploymentException(e);
+        } catch (UserStoreException e) {
+            throw new DeploymentException(e);
+        }
+    }
+
+    private static void addToRegistry(DeploymentFileData currentFile, String 
documentation, boolean faulty,
+                               File file, String username, String path1,
+                               SecureRegistry secureRegistry, String 
mediaType, String serviceType)
+            throws IOException, RegistryException {
+        ResourceImpl resource1;// create a content resource
+        resource1 = new ResourceImpl();
+
+        resource1.setAuthorUserName(username);
+        FileInputStream fis = new FileInputStream(file);
+        FileChannel fc = fis.getChannel();
+        byte[] data =
+                new byte[(int) (fc.size())];   // fc.size returns the size of 
the file which backs the channel
+        ByteBuffer bb = ByteBuffer.wrap(data);
+        fc.read(bb);
+        resource1.setContent(data);
+        resource1.setDescription(documentation);
+        resource1.setMediaType(mediaType);
+        if (faulty) {
+            resource1.setProperty(MashupConstants.FAULTY_MASHUP, "true");
+        }
+
+        //Setting the service js file path and resources directory path
+        resource1.setProperty(serviceType, file.getAbsolutePath());
+
+        //Setting the MD5 of the content
+        resource1.setProperty(MashupConstants.CONTENT_MD5, 
MashupUtils.calculateFileMD5(file));
+
+        File resourcesDir = new File(currentFile.getFile().getParent(), 
DescriptionBuilder
+                .getShortFileName(currentFile.getName()) + ".resources");
+        resource1.setProperty(JavaScriptEngineConstants.RESOURCES_FOLDER,
+                              resourcesDir.getAbsolutePath());
+
+        // store it in a non existence path
+        secureRegistry.put(path1, resource1);
+    }
+
+    public static String inferUserName(File file, UserRealm realm, String 
serviceName)
+            throws MashupFault {
+        String username = file.getParentFile().getName();
+        try {
+            if (!realm.getUserStoreAdmin().isExistingUser(username)) {
+                throw new MashupFault("No user with the username " + username +
+                        " exists in the system. Cannot deploy " + serviceName);
+            }
+        } catch (UserStoreException e) {
+            throw new MashupFault(e);
+        }
+        return username;
+    }
+
 }

Modified: 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
==============================================================================
--- 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
   (original)
+++ 
trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java
   Mon Mar 24 23:24:44 2008
@@ -205,10 +205,10 @@
             if (serviceStatus.startsWith("Error:")) {
                 
axisConfig.getFaultyServices().put(deploymentFileData.getFile().getAbsolutePath(),
                                                    serviceStatus);
-                populateRegistry(deploymentFileData,
+                MashupUtils.populateRegistry(configCtx, deploymentFileData,
                                  "This Service is Faulty. Reason : " + 
serviceStatus,
                                  
DescriptionBuilder.getShortFileName(deploymentFileData.getName()),
-                                 true);
+                                 true, MashupConstants.MASHUP_MEDIA_TYPE, 
JavaScriptEngineConstants.SERVICE_JS);
 
             }
         }
@@ -383,7 +383,7 @@
             UserRealm realm = (UserRealm) 
configCtx.getAxisConfiguration().getParameterValue(
                     RegistryConstants.REGISTRY_REALM);
             // Infer the username (The owner of this service)
-            String username = inferUserName(file, realm, name);
+            String username = MashupUtils.inferUserName(file, realm, name);
 
             //create a service group per service.
             // ServiceGroup name and axisservice name will be of the form 
username-serviceName
@@ -471,7 +471,10 @@
 
             // We are adding/updating the registry with the service data here 
             String path =
-                    populateRegistry(currentFile, 
axisService.getDocumentation(), name, false);
+                    MashupUtils.populateRegistry(configCtx, currentFile,
+                                                 
axisService.getDocumentation(), name, false,
+                                                 
MashupConstants.MASHUP_MEDIA_TYPE,
+                                                 
JavaScriptEngineConstants.SERVICE_JS);
 
             //Storing the registry path as a service parameter, to be used at 
undeployment time
             //to remove the service from registry
@@ -494,114 +497,6 @@
         }
     }
 
-    private String populateRegistry(DeploymentFileData currentFile, String 
documentation,
-                                    String name, boolean faulty)
-            throws DeploymentException {
-
-        try {
-            UserRealm realm = (UserRealm) 
configCtx.getAxisConfiguration().getParameterValue(
-                    RegistryConstants.REGISTRY_REALM);
-            File file = currentFile.getFile();
-
-            // Infer the username (The owner of this service)
-            String username = inferUserName(file, realm, name);
-
-            // Assembling the path for this service
-            String path1 = "/mashups/" + username + "/" + name;
-
-            // Get the realm and the registry
-            JDBCRegistry registry =
-                    (JDBCRegistry) 
configCtx.getAxisConfiguration().getParameterValue(
-                            RegistryConstants.REGISTRY);
-            SecureRegistry secureRegistry =
-                    RegistryUtils.createSecureRegistry(username, registry, 
realm);
-
-            ResourceImpl resource1;
-
-            //todo: Workaround to figure out whether the resource is deleted. 
Remove when registry implements undelete/rollback.
-            boolean resourceExists = false;
-            try {
-                secureRegistry.get(path1);
-                resourceExists = true;
-            } catch (RegistryException e) {
-                //just swallowing the exception
-            }
-
-            if (resourceExists) {
-                //Getting the existing latest version from the registry
-                resource1 = (ResourceImpl) secureRegistry.get(path1);
-
-                String regMD5 = "";
-                try {
-                    regMD5 = 
resource1.getProperty(MashupConstants.CONTENT_MD5);
-                } catch (Exception e) {
-                    log.warn(e);
-                }
-
-                String fileMD5 = MashupUtils.calculateFileMD5(file);
-
-                if (!regMD5.equals(fileMD5)) {
-                    // The content of the service has changed, updating the 
registry
-                    addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
-                                  secureRegistry);
-                }
-
-            } else {
-                // A brand new service, adding to the registry
-                addToRegistry(currentFile, documentation, faulty, file, 
username, path1,
-                              secureRegistry);
-            }
-
-            AccessControlAdmin ac = realm.getAccessControlAdmin();
-            ac.authorizeUser(username, path1, AccessControlConstants.EDIT);
-            ac.authorizeUser(username, path1, AccessControlConstants.DELETE);
-
-            return path1;
-        } catch (RegistryException e) {
-            throw new DeploymentException(e);
-        } catch (IOException e) {
-            throw new DeploymentException(e);
-        } catch (UserStoreException e) {
-            throw new DeploymentException(e);
-        }
-    }
-
-    private void addToRegistry(DeploymentFileData currentFile, String 
documentation, boolean faulty,
-                               File file, String username, String path1,
-                               SecureRegistry secureRegistry)
-            throws IOException, RegistryException {
-        ResourceImpl resource1;// create a content resource
-        resource1 = new ResourceImpl();
-
-        resource1.setAuthorUserName(username);
-        FileInputStream fis = new FileInputStream(file);
-        FileChannel fc = fis.getChannel();
-        byte[] data =
-                new byte[(int) (fc.size())];   // fc.size returns the size of 
the file which backs the channel
-        ByteBuffer bb = ByteBuffer.wrap(data);
-        fc.read(bb);
-        resource1.setContent(data);
-        resource1.setDescription(documentation);
-        resource1.setMediaType(MashupConstants.MASHUP_MEDIA_TYPE);
-        if (faulty) {
-            resource1.setProperty(MashupConstants.FAULTY_MASHUP, "true");
-        }
-
-        //Setting the service js file path and resources directory path
-        resource1.setProperty(JavaScriptEngineConstants.SERVICE_JS, 
file.getAbsolutePath());
-
-        //Setting the MD5 of the content
-        resource1.setProperty(MashupConstants.CONTENT_MD5, 
MashupUtils.calculateFileMD5(file));
-
-        File resourcesDir = new File(currentFile.getFile().getParent(), 
DescriptionBuilder
-                .getShortFileName(currentFile.getName()) + ".resources");
-        resource1.setProperty(JavaScriptEngineConstants.RESOURCES_FOLDER,
-                              resourcesDir.getAbsolutePath());
-
-        // store it in a non existence path
-        secureRegistry.put(path1, resource1);
-    }
-
     //processing the a function present in the deploying javascript file.
     private void processOperation(JavaScriptEngine engine, AxisService 
axisService, String method,
                                   Function function, SchemaGenerator 
schemaGenerator,
@@ -1074,20 +969,6 @@
         }
     }
 
-    private String inferUserName(File file, UserRealm realm, String 
serviceName)
-            throws MashupFault {
-        String username = file.getParentFile().getName();
-        try {
-            if (!realm.getUserStoreAdmin().isExistingUser(username)) {
-                throw new MashupFault("No user with the username " + username +
-                        " exists in the system. Cannot deploy " + serviceName);
-            }
-        } catch (UserStoreException e) {
-            throw new MashupFault(e);
-        }
-        return username;
-    }
-
     private static void addQuery(Registry registry, String path, String sql, 
String type)
             throws RegistryException {
         ResourceImpl q = new ResourceImpl();

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to