Index: CastorRegistryService.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/CastorRegistryService.java,v retrieving revision 1.34 diff -u -r1.34 CastorRegistryService.java --- CastorRegistryService.java 3 Aug 2003 22:36:35 -0000 1.34 +++ CastorRegistryService.java 18 Nov 2003 08:42:46 -0000 @@ -158,8 +158,11 @@ /** the output format for pretty printing when saving registries */ private OutputFormat format = null; - /** the base regsitry directory */ + /** the base registry directory */ private String directory = null; + + /** the temp directory */ + private String tmpdirectory = null; /** the extension for registry files */ private String extension = null; @@ -366,12 +369,14 @@ try { directory = serviceConf.getString("directory"); + tmpdirectory = serviceConf.getString("tmpdirectory"); mapFile = serviceConf.getString("mapping", DEFAULT_MAPPING); extension = serviceConf.getString("extension", DEFAULT_EXTENSION); refreshRate = serviceConf.getInt("refreshRate", DEFAULT_REFRESH); mapFile = TurbineServlet.getRealPath(mapFile); directory = TurbineServlet.getRealPath(directory); + tmpdirectory = TurbineServlet.getRealPath(tmpdirectory); } catch (Throwable t) { @@ -665,37 +670,47 @@ */ public void saveFragment(String file) { - OutputStreamWriter writer = null; - String encoding = new String("UTF-8"); - RegistryFragment fragment = (RegistryFragment) fragments.get(file); - - if (fragment != null) - { - try - { - writer = new OutputStreamWriter(new FileOutputStream(file), encoding); - format.setEncoding(encoding); - Serializer serializer = new XMLSerializer(writer, format); - Marshaller marshaller = new Marshaller(serializer.asDocumentHandler()); - marshaller.setMapping(this.mapping); - marshaller.marshal(fragment); - } - catch (Throwable t) - { - logger.error("RegistryService: Could not marshal: " + file, t); - } - finally - { - try - { - writer.close(); - } - catch (Exception e) - { - logger.error("Exception", e); // At least log the exception. - } - } - } + OutputStreamWriter writer = null; + String encoding = new String("UTF-8"); + RegistryFragment fragment = (RegistryFragment) fragments.get(file); + // A temporary file to move after write ok + File tmpFile = null; + File originalFile = new File(file); + + if (fragment != null) + { + try + { + // First we write in the tmpdirectory, in case tomcat isn't properly shutdowned + tmpFile = new File(tmpdirectory,originalFile.getName()); + writer = new OutputStreamWriter(new FileOutputStream(tmpFile), encoding); + format.setEncoding(encoding); + Serializer serializer = new XMLSerializer(writer, format); + Marshaller marshaller = new Marshaller(serializer.asDocumentHandler()); + marshaller.setMapping(this.mapping); + marshaller.marshal(fragment); + } + catch (Throwable t) + { + logger.error("RegistryService: Could not marshal: " + file, t); + } + finally + { + try + { + writer.close(); + // Then move it back to the good directory + originalFile.delete(); + // Here we pray that the thread isn't interrupted between those two System calls... + // Anyway, user can still find it under its tmp and move it back to conf + tmpFile.renameTo(originalFile); + } + catch (Exception e) + { + logger.error("Exception", e); // At least log the exception. + } + } + } } /** @@ -930,4 +945,4 @@ } } -} +} \ No newline at end of file Index: JetspeedResources.properties =================================================================== RCS file: /home/cvs/jakarta-jetspeed/webapp/WEB-INF/conf/JetspeedResources.properties,v retrieving revision 1.111 diff -u -r1.111 JetspeedResources.properties --- JetspeedResources.properties 30 Oct 2003 01:53:17 -0000 1.111 +++ JetspeedResources.properties 18 Nov 2003 08:43:41 -0000 @@ -318,6 +318,7 @@ ######################################### services.Registry.classname=org.apache.jetspeed.services.registry.CastorRegistryService services.Registry.directory=/WEB-INF/conf/ +services.Registry.tmpdirectory=/WEB-INF/tmp/ services.Registry.mapping=/WEB-INF/conf/registry.xml services.Registry.default.Portlet=local-portlets services.Registry.default.PortletControl=local-managers