Author: deepal
Date: Thu Jan  3 21:43:57 2008
New Revision: 11826

Log:

Moved file import and export into a utility class

Added:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
Modified:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
 (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
 Thu Jan  3 21:43:57 2008
@@ -499,96 +499,6 @@
     }
 
     /**
-     * This method can be used to export a local file system into a running 
instance of a registry.
-     * Need to create a file object representing the local file and the need 
to tell where to add
-     * the resource in the registry.
-     *
-     * @param file : File representing local file system
-     * @param path : Where to put the file
-     * @throws RegistryException : if something went wrong
-     */
-    public void importToRegistry(File file, String path) throws 
RegistryException {
-        try {
-            if (file == null || path == null) {
-                throw new 
RegistryException(Messages.getMessage("file.valaue.null"));
-            }
-            processImport(file, path);
-        } catch (Exception e) {
-            throw new RegistryException(e.getMessage());
-        }
-    }
-
-    /**
-     * This method can be used to export registry instance or node in a 
registry to a local file
-     * system. When we call this method a file structure will be created to 
match the structure to
-     * map the structure in the registry
-     *
-     * @param toFile : File in the local file system
-     * @param path   : To which node export
-     * @throws RegistryException : If something went wrong
-     */
-    public void exportFromRegistry(File toFile, String path) throws 
RegistryException {
-        try {
-            processExport(path, toFile);
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new RegistryException(e.getMessage());
-        }
-    }
-
-    private void processImport(File file, String path) throws Exception {
-        String filePart = file.getName();
-        String resourcePath = path + "/" + filePart;
-        if (file.isDirectory()) {
-            File files[] = file.listFiles();
-            if (files.length > 0) {
-                for (File childFile : files) {
-                    processImport(childFile, resourcePath);
-                }
-            } else {
-                Resource resource = new Resource();
-                resource.setPath(resourcePath);
-                resource.setDirectory(true);
-                put(resourcePath, resource);
-            }
-        } else {
-            Resource resource = new Resource();
-            resource.setContent(new FileInputStream(file));
-            resource.setPath(resourcePath);
-            resource.setDirectory(false);
-            put(resourcePath, resource);
-        }
-    }
-
-    private void processExport(String resourcePath,
-                               File toFile) throws RegistryException {
-        Resource resource = get(resourcePath);
-        if (resource != null) {
-            if (resource.isDirectory()) {
-                File file = new File(toFile, resource.getPath());
-                file.mkdirs();
-                String childNodes[] = (String[]) resource.getContent();
-                for (String childNode : childNodes) {
-                    processExport(childNode, toFile);
-                }
-            } else {
-                File file = new File(toFile, resource.getPath());
-                try {
-                    file.createNewFile();
-                    FileOutputStream out = new FileOutputStream(file);
-                    out.write((byte[]) resource.getContent());
-                    out.flush();
-                    out.close();
-                } catch (IOException e) {
-                    throw new RegistryException(Messages.getMessage(
-                            "error.creating.file", resource.getPath()));
-                }
-
-            }
-        }
-    }
-
-    /**
      * This method will create a RequestOptions object adding  Authorization 
headers.
      *
      * @return RequestOptions : Created RequestOptions object

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/utils/RegistryClientUtils.java
  Thu Jan  3 21:43:57 2008
@@ -0,0 +1,128 @@
+/*
+ *  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.registry.utils;
+
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.Resource;
+import org.wso2.registry.Registry;
+import org.wso2.registry.i18n.Messages;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * This class is used to provide client side utilities when someone uses 
Remote registry
+ * If user want to import Registry to a local file system or export a local 
file system
+ * into a remote registry (s)he can use this utility class
+ */
+public class RegistryClientUtils {
+
+    /**
+     * This method can be used to import a local file system into a running 
instance of a registry.
+     * Need to create a file object representing the local file and the need 
to tell where to add
+     * the resource in the registry.
+     *
+     * @param file : File representing local file system
+     * @param path : Where to put the file
+     * @throws org.wso2.registry.RegistryException : if something went wrong
+     * @param registry : Registry instance
+     */
+    public static void importToRegistry(File file, String path , Registry 
registry) throws RegistryException {
+        try {
+            if (file == null || path == null) {
+                throw new 
RegistryException(Messages.getMessage("file.valaue.null"));
+            }
+            processImport(file, path ,registry);
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+    }
+
+    /**
+     * This method can be used to export registry instance or node in a 
registry to a local file
+     * system. When we call this method a file structure will be created to 
match the structure to
+     * map the structure in the registry
+     *
+     * @param toFile : File in the local file system
+     * @param path   : To which node export
+     * @throws RegistryException : If something went wrong
+     * @param registry : Registry instance
+     */
+    public void exportFromRegistry(File toFile, String path , Registry 
registry) throws RegistryException {
+        try {
+            processExport(path, toFile ,registry);
+        } catch (Exception e) {
+            throw new RegistryException(e.getMessage());
+        }
+    }
+
+    private static void processImport(File file, String path , Registry 
registry) throws Exception {
+        String filePart = file.getName();
+        String resourcePath = path + "/" + filePart;
+        if (file.isDirectory()) {
+            File files[] = file.listFiles();
+            if (files.length > 0) {
+                for (File childFile : files) {
+                    processImport(childFile, resourcePath ,registry);
+                }
+            } else {
+                Resource resource = new Resource();
+                resource.setPath(resourcePath);
+                resource.setDirectory(true);
+                registry.put(resourcePath, resource);
+            }
+        } else {
+            Resource resource = new Resource();
+            resource.setContent(new FileInputStream(file));
+            resource.setPath(resourcePath);
+            resource.setDirectory(false);
+            registry.put(resourcePath, resource);
+        }
+    }
+
+    private void processExport(String resourcePath,
+                               File toFile , Registry registry) throws 
RegistryException {
+        Resource resource = registry.get(resourcePath);
+        if (resource != null) {
+            if (resource.isDirectory()) {
+                File file = new File(toFile, resource.getPath());
+                file.mkdirs();
+                String childNodes[] = (String[]) resource.getContent();
+                for (String childNode : childNodes) {
+                    processExport(childNode, toFile , registry);
+                }
+            } else {
+                File file = new File(toFile, resource.getPath());
+                try {
+                    file.createNewFile();
+                    FileOutputStream out = new FileOutputStream(file);
+                    out.write((byte[]) resource.getContent());
+                    out.flush();
+                    out.close();
+                } catch (IOException e) {
+                    throw new RegistryException(Messages.getMessage(
+                            "error.creating.file", resource.getPath()));
+                }
+
+            }
+        }
+    }
+}

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

Reply via email to