directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r612867 - /directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java
Date Thu, 17 Jan 2008 16:58:57 GMT
Author: pamarcelot
Date: Thu Jan 17 08:58:50 2008
New Revision: 612867

URL: http://svn.apache.org/viewvc?rev=612867&view=rev
Log:
Improved the way projects are saved and loaded. Now using a temporary file, in case something
goes wrong when saving to the real file.

Modified:
    directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java

Modified: directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java?rev=612867&r1=612866&r2=612867&view=diff
==============================================================================
--- directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java
(original)
+++ directory/studio/trunk/studio-schemaeditor/src/main/java/org/apache/directory/studio/schemaeditor/PluginUtils.java
Thu Jan 17 08:58:50 2008
@@ -32,6 +32,7 @@
 import java.util.HashMap;
 import java.util.List;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.directory.studio.connection.core.Connection;
 import org.apache.directory.studio.connection.core.ConnectionCorePlugin;
 import org.apache.directory.studio.schemaeditor.controller.ProjectsHandler;
@@ -134,10 +135,10 @@
 
 
     /**
-     * Gets the Projects (where is stored information about the loaded Projects).
+     * Gets the projects file (where is stored information about the loaded projects).
      *
      * @return
-     *      the Projects File
+     *      the projects File
      */
     private static File getProjectsFile()
     {
@@ -146,16 +147,30 @@
 
 
     /**
+     * Gets the temporary projects file.
+     *
+     * @return
+     *      the temporary projects file
+     */
+    private static File getTempProjectsFile()
+    {
+        return Activator.getDefault().getStateLocation().append( "projects-temp.xml" ).toFile();
//$NON-NLS-1$
+    }
+
+
+    /**
      * Loads the projects saved in the Projects File.
      */
     public static void loadProjects()
     {
         ProjectsHandler projectsHandler = Activator.getDefault().getProjectsHandler();
         File projectsFile = getProjectsFile();
+        boolean loadFailed = false;
+        Project[] projects = null;
 
+        // We try to load the projects file
         if ( projectsFile.exists() )
         {
-            Project[] projects = null;
             try
             {
                 projects = ProjectsImporter.getProjects( new FileInputStream( projectsFile
), projectsFile
@@ -163,20 +178,60 @@
             }
             catch ( ProjectsImportException e )
             {
-                reportError( "An error occured when loading the projects.", e, "Projects
Loading Error",
-                    "An error occured when loading the projects." );
-                return;
+                loadFailed = true;
             }
             catch ( FileNotFoundException e )
             {
-                reportError( "An error occured when loading the projects.", e, "Projects
Loading Error",
-                    "An error occured when loading the projects." );
-                return;
+                loadFailed = true;
             }
 
-            for ( Project project : projects )
+            if ( !loadFailed )
+            {
+                // If everything went fine, we add the projects 
+                for ( Project project : projects )
+                {
+                    projectsHandler.addProject( project );
+                }
+            }
+            else
             {
-                projectsHandler.addProject( project );
+                // If something went wrong, we try to load the temp projects file
+                File tempProjectsFile = getTempProjectsFile();
+
+                if ( tempProjectsFile.exists() )
+                {
+                    try
+                    {
+                        projects = ProjectsImporter.getProjects( new FileInputStream( tempProjectsFile
), projectsFile
+                            .getAbsolutePath() );
+
+                        loadFailed = false;
+                    }
+                    catch ( ProjectsImportException e )
+                    {
+                        reportError( "An error occured when loading the projects.", e, "Projects
Loading Error",
+                            "An error occured when loading the projects." );
+                        return;
+                    }
+                    catch ( FileNotFoundException e )
+                    {
+                        reportError( "An error occured when loading the projects.", e, "Projects
Loading Error",
+                            "An error occured when loading the projects." );
+                        return;
+                    }
+
+                    // We add the projects 
+                    for ( Project project : projects )
+                    {
+                        projectsHandler.addProject( project );
+                    }
+                }
+                else
+                {
+                    reportError( "An error occured when loading the projects.", null, "Projects
Loading Error",
+                        "An error occured when loading the projects." );
+                }
+
             }
         }
     }
@@ -189,17 +244,38 @@
     {
         try
         {
+            // Saving the projects to the temp projects file
             OutputFormat outformat = OutputFormat.createPrettyPrint();
             outformat.setEncoding( "UTF-8" );
-            XMLWriter writer = new XMLWriter( new FileOutputStream( getProjectsFile() ),
outformat );
+            XMLWriter writer = new XMLWriter( new FileOutputStream( getTempProjectsFile()
), outformat );
             writer.write( ProjectsExporter.toDocument( Activator.getDefault().getProjectsHandler().getProjects()
                 .toArray( new Project[0] ) ) );
             writer.flush();
+
+            // Copying the temp projects file to the final location
+            String content = FileUtils.readFileToString( getTempProjectsFile(), "UTF-8" );
+            FileUtils.writeStringToFile( getProjectsFile(), content, "UTF-8" );
         }
         catch ( IOException e )
         {
-            reportError( "An error occured when saving the projects.", e, "Projects Saving
Error",
-                "An error occured when saving the projects." );
+            // If an error occurs when saving to the temp projects file or
+            // when copying the temp projects file to the final location,
+            // we try to save the projects directly to the final location.
+            try
+            {
+                OutputFormat outformat = OutputFormat.createPrettyPrint();
+                outformat.setEncoding( "UTF-8" );
+                XMLWriter writer = new XMLWriter( new FileOutputStream( getProjectsFile()
), outformat );
+                writer.write( ProjectsExporter.toDocument( Activator.getDefault().getProjectsHandler().getProjects()
+                    .toArray( new Project[0] ) ) );
+                writer.flush();
+            }
+            catch ( IOException e2 )
+            {
+                // If another error occur, we display an error
+                reportError( "An error occured when saving the projects.", e2, "Projects
Saving Error",
+                    "An error occured when saving the projects." );
+            }
         }
     }
 



Mime
View raw message