org.wso2.carbon.registry.synchronization
Class Utils

java.lang.Object
  extended by org.wso2.carbon.registry.synchronization.Utils

public class Utils
extends Object

Some utility methods used by the synchronization operations.


Field Summary
static int RADIX
           
 
Method Summary
static void addResource(String path)
          This method add the resources which are not added to commit
static void cleanEmbeddedRegistry()
          Method to clean the embedded registry instance, after the synchronization operation.
static List<String> cleanUpDirectory(File directory, List<String> filesToPreserve)
          Method to determine the files that are required to be cleaned up from meta information directory and preserve only the given list of files.
static boolean confirmDelete(File file, File metaFile, UserInputCallback callback)
          This method will obtain the consent from the user to delete the specified file and meta file (corresponding to the file), if the user agrees to.
static boolean contentChanged(File file)
          Determines whether the content of the given file has changed.
static void copy(File source, File destination)
          copying the contents of one file to another.
static org.apache.axiom.om.OMElement createDefaultMetaFile(boolean isCollection, String path, String username)
          Method to generate the XML content of a meta file.
static void createMetaFile(String fileName, org.apache.axiom.om.OMElement metaData)
          This method creates the file that store the meta data of the current directory or file.
static String decodeFilename(String path)
          This method will obtain the decoded representation of the given encoded resource path.
static boolean deleteFile(File file)
          This method deletes the specified file from the filesystem.
static String encodeResourceName(String resourceName)
          This method will obtain the encoded representation of the given resource name.
static boolean fileContentChanged(File file)
           
static byte[] getBytesFromFile(File file)
          Returns the contents of the file in a byte array.
static String getMD5(byte[] content)
          Method to obtain the MD5 hash value for the given content.
static String getMD5(File file)
          Method to obtain the MD5 hash value for the given file.
static org.apache.axiom.om.OMElement getMetaOMElement(String filePath)
          Method to obtain the XML representation of the meta information corresponding to the given file.
static org.apache.axiom.om.OMElement getOMElementFromMetaFile(String metaFilePath)
          Method to obtain the XML representation of the data contained in a meta file.
static String getPath(String url)
          Method to extract the resource path from the given URL.
static String getRegistryUrl(String url)
          Method to extract the URL of the remote registry instance from the given URL.
static org.apache.axiom.om.OMElement readMetaElement(XMLStreamReader xmlReader)
          This method reads the xml stream up to the children and return the meta element.
static boolean resourceUpdated(String metaFilePath, org.apache.axiom.om.OMElement metaElement)
          This method checks whether the resource updated or not in the Registry from the last checkout/update
static void setResourcesDelete(String dirPath)
          This method mark the resources as delete if it no longer exists in the file system
static org.apache.axiom.om.OMElement updateDefaultAddMetaFile(org.apache.axiom.om.OMElement root, String path, String username, boolean isDirectory)
          Method to update the XML content of a meta file.
static void updateMetaFile(String fileName, org.apache.axiom.om.OMElement metaData)
          This method update the file that store the meta data of the current directory or file.
static void writeMetaElement(XMLStreamWriter xmlWriter, org.apache.axiom.om.OMElement metaElement)
          This method writes the meta element to the xml stream up to the children.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RADIX

public static final int RADIX
See Also:
Constant Field Values
Method Detail

writeMetaElement

public static void writeMetaElement(XMLStreamWriter xmlWriter,
                                    org.apache.axiom.om.OMElement metaElement)
                             throws XMLStreamException
This method writes the meta element to the xml stream up to the children.

Parameters:
xmlWriter - xml writer
metaElement - meta element to write
Throws:
XMLStreamException - if the operation failed

readMetaElement

public static org.apache.axiom.om.OMElement readMetaElement(XMLStreamReader xmlReader)
                                                     throws SynchronizationException,
                                                            XMLStreamException
This method reads the xml stream up to the children and return the meta element.

Parameters:
xmlReader - the xml reader.
Returns:
the meta element.
Throws:
SynchronizationException - if the provided XML is invalid.
XMLStreamException - if XML parsing failed.

createMetaFile

public static void createMetaFile(String fileName,
                                  org.apache.axiom.om.OMElement metaData)
                           throws SynchronizationException
This method creates the file that store the meta data of the current directory or file.

Parameters:
fileName - the name of the file.
metaData - the meta data element.
Throws:
SynchronizationException - if the operation failed.

updateMetaFile

public static void updateMetaFile(String fileName,
                                  org.apache.axiom.om.OMElement metaData)
                           throws SynchronizationException
This method update the file that store the meta data of the current directory or file.

Parameters:
fileName - the name of the file.
metaData - the meta data element.
Throws:
SynchronizationException - if the operation failed.

resourceUpdated

public static boolean resourceUpdated(String metaFilePath,
                                      org.apache.axiom.om.OMElement metaElement)
This method checks whether the resource updated or not in the Registry from the last checkout/update

Parameters:
metaFilePath - Resource metadata file location in the file system
metaElement - Metadata of Registry resource
Returns:
Whether the resource updated or not in the Registry from the last checkout/update

getBytesFromFile

public static byte[] getBytesFromFile(File file)
                               throws SynchronizationException
Returns the contents of the file in a byte array.

Parameters:
file - the file the to read
Returns:
the content of the file
Throws:
SynchronizationException - if the operation failed.

getMetaOMElement

public static org.apache.axiom.om.OMElement getMetaOMElement(String filePath)
                                                      throws SynchronizationException
Method to obtain the XML representation of the meta information corresponding to the given file. This method will first determine the path of the meta file for the given file-path, and then obtain the corresponding XML representation using the getOMElementFromMetaFile(String) method.

Parameters:
filePath - the path to the file of which the meta information is required.
Returns:
An OM element containing the meta information.
Throws:
SynchronizationException - if the operation failed.

getOMElementFromMetaFile

public static org.apache.axiom.om.OMElement getOMElementFromMetaFile(String metaFilePath)
                                                              throws SynchronizationException
Method to obtain the XML representation of the data contained in a meta file.

Parameters:
metaFilePath - the path to the meta file.
Returns:
An OM element containing the meta information.
Throws:
SynchronizationException - if the operation failed.

createDefaultMetaFile

public static org.apache.axiom.om.OMElement createDefaultMetaFile(boolean isCollection,
                                                                  String path,
                                                                  String username)
                                                           throws SynchronizationException
Method to generate the XML content of a meta file. This method will require information about the resource and details of who created it.

Parameters:
isCollection - whether the resource at the given path is a collection.
path - the path of the resource for which the meta information is generated.
username - the username of the creator.
Returns:
the meta information as an OM element.
Throws:
SynchronizationException - if the operation failed.

updateDefaultAddMetaFile

public static org.apache.axiom.om.OMElement updateDefaultAddMetaFile(org.apache.axiom.om.OMElement root,
                                                                     String path,
                                                                     String username,
                                                                     boolean isDirectory)
                                                              throws SynchronizationException
Method to update the XML content of a meta file. This method will require information about the resource and details of who created it.

Parameters:
root - metadata OMElement
path - the path of the resource for which the meta information is generated.
username - the username of the creator.
Returns:
the meta information as an OM element.
Throws:
SynchronizationException - if the operation failed.

copy

public static void copy(File source,
                        File destination)
                 throws SynchronizationException
copying the contents of one file to another.

Parameters:
source - source
destination - destination
Throws:
SynchronizationException - throws if the operation failed.

getRegistryUrl

public static String getRegistryUrl(String url)
Method to extract the URL of the remote registry instance from the given URL.

Parameters:
url - aggregate URL containing a concatenation of the registry URL and the resource path that is capable of referencing a remote resource. This url will contain only the resource path if the resource was local to the given registry instance.
Returns:
the URL of the remote instance, or null if the instance was local.

getPath

public static String getPath(String url)
Method to extract the resource path from the given URL.

Parameters:
url - aggregate URL containing a concatenation of the registry URL and the resource path that is capable of referencing a remote resource. This url will contain only the resource path if the resource was local to the given registry instance.
Returns:
the path of the resource on the registry.

encodeResourceName

public static String encodeResourceName(String resourceName)
                                 throws SynchronizationException
This method will obtain the encoded representation of the given resource name.

Parameters:
resourceName - the name of the resource.
Returns:
the encoded name.
Throws:
SynchronizationException - if the operation failed.
See Also:
URLEncoder

decodeFilename

public static String decodeFilename(String path)
                             throws SynchronizationException
This method will obtain the decoded representation of the given encoded resource path.

Parameters:
path - the encoded path of the resource.
Returns:
the decoded path.
Throws:
SynchronizationException - if the operation failed.
See Also:
URLDecoder

confirmDelete

public static boolean confirmDelete(File file,
                                    File metaFile,
                                    UserInputCallback callback)
                             throws SynchronizationException
This method will obtain the consent from the user to delete the specified file and meta file (corresponding to the file), if the user agrees to.

Parameters:
file - the file or directory to delete.
metaFile - the meta file corresponding to the file or directory to delete.
callback - the callback which is used to obtain the user's consent. If this parameter is null, the file and the meta file will be deleted irrespective of the user's choice.
Returns:
whether the operation succeeded or not.
Throws:
SynchronizationException - if an error occurred during the operation.

deleteFile

public static boolean deleteFile(File file)
This method deletes the specified file from the filesystem. If the given file-path corresponds to a directory, the directory and everything under it (child files and directories) will be recursively deleted in the process.

Parameters:
file - the file or directory to delete.
Returns:
true if the operation succeeded or false if it failed.

cleanUpDirectory

public static List<String> cleanUpDirectory(File directory,
                                            List<String> filesToPreserve)
Method to determine the files that are required to be cleaned up from meta information directory and preserve only the given list of files. This method will determine all meta files within the given directory that have no corresponding physical files on the filesystem. This is used after a check-in operation has been propagated to ensure that the filesystem will always remain consistent.

Parameters:
directory - the meta information directory that requires cleaning up.
filesToPreserve - the list of files to preserve.
Returns:
the list of files to be cleaned.

getMD5

public static String getMD5(byte[] content)
Method to obtain the MD5 hash value for the given content.

Parameters:
content - the content as an array of bytes.
Returns:
the MD5 hash of the content.

getMD5

public static String getMD5(File file)
                     throws SynchronizationException
Method to obtain the MD5 hash value for the given file.

Parameters:
file - the content file.
Returns:
the MD5 hash of the content.
Throws:
SynchronizationException

contentChanged

public static boolean contentChanged(File file)
                              throws SynchronizationException
Determines whether the content of the given file has changed. If the given file is a directory, this method will recursively test each file under this directory to determine whether the content of any child, or grand child has changed.

The change in content is determined using MD5 hash values written to the meta files during a check-out or update. If the MD5 hash value of the given file was not found in its meta file, this operation will assume that a change has been made, irrespective of whether the content of the file changed or not.

Parameters:
file - the file to test for changes.
Returns:
true if the content has changed, or false if not.
Throws:
SynchronizationException - if an error occurred during the operation.

fileContentChanged

public static boolean fileContentChanged(File file)
                                  throws SynchronizationException
Throws:
SynchronizationException

cleanEmbeddedRegistry

public static void cleanEmbeddedRegistry()
Method to clean the embedded registry instance, after the synchronization operation. This method should only be invoked if the synchronization happens at a client that terminates soon after the execution of the synchronization operation, to prevent loss of activity logs.


addResource

public static void addResource(String path)
                        throws SynchronizationException
This method add the resources which are not added to commit

Parameters:
path -
Throws:
SynchronizationException

setResourcesDelete

public static void setResourcesDelete(String dirPath)
                               throws SynchronizationException
This method mark the resources as delete if it no longer exists in the file system

Parameters:
dirPath - Parent directory of the deleted file/s
Throws:
SynchronizationException - If the operation failed


Copyright © 2013 WSO2 Inc. All Rights Reserved.