Author: tyrell
Date: Tue Mar 18 00:20:32 2008
New Revision: 14945

Log:

Addressing the server startup related part of MASHUP-729

Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.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/MashupConstants.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/MashupConstants.java     
Tue Mar 18 00:20:32 2008
@@ -70,6 +70,7 @@
     public static final String USER_REGISTRY = "user_registry";
     public static final String MASHUP_MEDIA_TYPE = "wso2_mashup";
     public static final String EVERYONE_ROLE = "everyone";
+    public static final String CONTENT_MD5 = "content_md5";
 
     public static final String REGISTRY_MASHUP_PATH = "registryMashupPath";
     public static final String PROFILE_PATH = "profilePath";

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   
Tue Mar 18 00:20:32 2008
@@ -16,10 +16,9 @@
 package org.wso2.mashup.utils;
 
 import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.util.UUIDGenerator;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.util.UUIDGenerator;
 import org.apache.axis2.AxisFault;
-import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.addressing.EndpointReference;
 import org.apache.axis2.client.Options;
 import org.apache.axis2.client.ServiceClient;
@@ -28,6 +27,7 @@
 import org.apache.axis2.deployment.DeploymentEngine;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.Parameter;
+import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.rpc.client.RPCServiceClient;
 import org.apache.axis2.transport.http.HTTPConstants;
 import org.apache.axis2.transport.http.util.URIEncoderDecoder;
@@ -46,7 +46,6 @@
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.jdbc.JDBCRegistry;
-
 import org.wso2.utils.ServerConfiguration;
 import org.wso2.utils.security.CryptoUtil;
 import org.wso2.wsas.ServerConstants;
@@ -66,12 +65,17 @@
 import javax.xml.stream.XMLStreamException;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Properties;
@@ -452,7 +456,7 @@
             parameter = configuration.getParameter("Proxy");
             // The axis2.xml could have details of a proxy, and in that case 
that can cause problems for login hence
             // we temparorily take it off before login and reinsert it later on
-            if (parameter!= null) {
+            if (parameter != null) {
                 configuration.removeParameter(parameter);
             }
             defaultConfigurationContext.setAxisConfiguration(configuration);
@@ -504,7 +508,7 @@
             parameter = configuration.getParameter("Proxy");
             // The axis2.xml could have details of a proxy, and in that case 
that can cause problems for login hence
             // we temparorily take it off before login and reinsert it later on
-            if (parameter!= null) {
+            if (parameter != null) {
                 configuration.removeParameter(parameter);
             }
             defaultConfigurationContext.setAxisConfiguration(configuration);
@@ -562,7 +566,7 @@
             parameter = configuration.getParameter("Proxy");
             // The axis2.xml could have details of a proxy, and in that case 
that can cause problems for login hence
             // we temparorily take it off before login and reinsert it later on
-            if (parameter!= null) {
+            if (parameter != null) {
                 configuration.removeParameter(parameter);
             }
             defaultConfigurationContext.setAxisConfiguration(configuration);
@@ -688,7 +692,7 @@
     public static void setInitialSetupComplete(boolean status) {
         PersistenceManager pm = new PersistenceManager();
         pm.updateConfigurationProperty(MashupConstants.INITIAL_SETUP_COMPLETE,
-                                       String.valueOf(status));        
+                                       String.valueOf(status));
     }
 
     /**
@@ -749,4 +753,43 @@
         return serverUUID;
     }
 
+
+    public static String calculateFileMD5(File file) throws MashupFault {
+        String retMD5 = "";
+
+        MessageDigest digest = null;
+        try {
+            digest = MessageDigest.getInstance("MD5");
+
+            InputStream is = new FileInputStream(file);
+            byte[] buffer = new byte[8192];
+            int read = 0;
+            try {
+                while ((read = is.read(buffer)) > 0) {
+                    digest.update(buffer, 0, read);
+                }
+                byte[] md5sum = digest.digest();
+                BigInteger bigInt = new BigInteger(1, md5sum);
+                retMD5 = bigInt.toString(16);
+            }
+            catch (IOException e) {
+                throw new RuntimeException("Unable to process file for MD5", 
e);
+            }
+            finally {
+                try {
+                    is.close();
+                }
+                catch (IOException e) {
+                    throw new MashupFault("Unable to close input stream for 
MD5 calculation", e);
+                }
+            }
+        } catch (NoSuchAlgorithmException e) {
+            throw new MashupFault("Failed to initiate message digest to 
calculate MD5", e);
+        } catch (FileNotFoundException e) {
+            throw new MashupFault("File not found to calculate MD5", e);
+        }
+
+        return retMD5;
+    }
+
 }

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
   Tue Mar 18 00:20:32 2008
@@ -66,6 +66,7 @@
 import org.wso2.registry.Registry;
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
+import org.wso2.registry.Resource;
 import org.wso2.registry.ResourceImpl;
 import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.registry.jdbc.utils.RegistryDataSource;
@@ -272,7 +273,8 @@
                 }
 
                 JavaScriptEngine engine = new JavaScriptEngine(shortFileName);
-                Function destroy = (Function) 
service.getParameterValue(MashupConstants.MASHUP_DESTROY_FUNCTION);
+                Function destroy = (Function) service
+                        
.getParameterValue(MashupConstants.MASHUP_DESTROY_FUNCTION);
                 if (destroy != null) {
                     destroy.call(engine.getCx(), engine, engine, new 
Object[0]);
                 }
@@ -467,10 +469,10 @@
             }
             axisService.addSchema(schemaGenerator.getSchema());
 
-            // We are putting the service into the registry here
-
+            // We are adding/updating the registry with the service data here 
             String path =
                     populateRegistry(currentFile, 
axisService.getDocumentation(), name, false);
+
             //Storing the registry path as a service parameter, to be used at 
undeployment time
             //to remove the service from registry
             Parameter myRegistryPath = new 
Parameter(MashupConstants.REGISTRY_MASHUP_PATH, path);
@@ -500,9 +502,13 @@
             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(
@@ -510,37 +516,46 @@
             SecureRegistry secureRegistry =
                     RegistryUtils.createSecureRegistry(username, registry, 
realm);
 
-            // create a content resource
-            ResourceImpl resource1 = new ResourceImpl();
+            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);
+                }
 
-            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());
-            File resourcesDir = new File(currentFile.getFile().getParent(), 
DescriptionBuilder
-                    .getShortFileName(currentFile.getName()) + ".resources");
-            resource1.setProperty(JavaScriptEngineConstants.RESOURCES_FOLDER,
-                                  resourcesDir.getAbsolutePath());
+                String fileMD5 = MashupUtils.calculateFileMD5(file);
 
-            // store it in a non existence path
-            String path1 = "/mashups/" + username + "/" + name;
-            secureRegistry.put(path1, resource1);
+                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);
@@ -551,6 +566,42 @@
         }
     }
 
+    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,
@@ -801,7 +852,7 @@
             if 
(!registry.resourceExists(MashupConstants.QUERYSTORE_QUERY_PATH)) {
                 // add queries to generate user profiles
 
-                Collection queryCollection = registry.newCollection();         
        
+                Collection queryCollection = registry.newCollection();
                 registry.put(MashupConstants.QUERYSTORE_QUERY_PATH, 
queryCollection);
 
                 // All mashups query
@@ -819,7 +870,8 @@
                         registry,
                         MashupConstants.MY_MASHUPS_QUERY_PATH,
                         "SELECT PATH FROM ARTIFACTS WHERE STATE=100 AND 
AUTHOR=? AND MEDIA_TYPE='" +
-                                MashupConstants.MASHUP_MEDIA_TYPE + "' ORDER 
BY LAST_UPDATED_TIME DESC",
+                                MashupConstants.MASHUP_MEDIA_TYPE +
+                                "' ORDER BY LAST_UPDATED_TIME DESC",
                         RegistryConstants.RESOURCES_RESULT_TYPE
                 );
 
@@ -976,7 +1028,8 @@
                 ac.authorizeRole(MashupConstants.EVERYONE_ROLE, 
MashupConstants.ALL_MASHUPS_PATH,
                                  AccessControlConstants.READ);
                 ac.authorizeRole(MashupConstants.EVERYONE_ROLE,
-                                 MashupConstants.ALL_MASHUPS_QUERY_PATH, 
AccessControlConstants.READ);
+                                 MashupConstants.ALL_MASHUPS_QUERY_PATH,
+                                 AccessControlConstants.READ);
 
                 // Create the anonymous user profile.
                 RegistryUtils.createUser(secureRegistry, realm,
@@ -1015,7 +1068,8 @@
         }
     }
 
-    private String inferUserName(File file, UserRealm realm, String 
serviceName) throws MashupFault {
+    private String inferUserName(File file, UserRealm realm, String 
serviceName)
+            throws MashupFault {
         String username = file.getParentFile().getName();
         try {
             if (!realm.getUserStoreAdmin().isExistingUser(username)) {
@@ -1037,4 +1091,5 @@
         q.setPath(path);
         registry.put(path, q);
     }
+
 }
\ No newline at end of file

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

Reply via email to