public String generatePoolsConfigIfClassSet ( ) { if ( poolsConfigDocumentGenerator == null ) { return null ; } Document document = poolsConfigDocumentGenerator . generatePoolsDocument ( ) ; if ( document == null ) { LOG . warn ( "generatePoolsConfig: Did not generate a valid pools xml file" ) ; return null ; } // Write the content into a temporary xml file and rename to the // expected file. File tempXmlFile ; try { TransformerFactory transformerFactory = TransformerFactory . newInstance ( ) ; transformerFactory . setAttribute ( "indent-number" , new Integer ( 2 ) ) ; Transformer transformer = transformerFactory . newTransformer ( ) ; transformer . setOutputProperty ( "{http://xml.apache.org/xslt}indent-amount" , "2" ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; DOMSource source = new DOMSource ( document ) ; tempXmlFile = File . createTempFile ( "tmpPoolsConfig" , "xml" ) ; if ( LOG . isDebugEnabled ( ) ) { StreamResult stdoutResult = new StreamResult ( System . out ) ; transformer . transform ( source , stdoutResult ) ; } StreamResult result = new StreamResult ( tempXmlFile ) ; transformer . transform ( source , result ) ; String md5 = org . apache . commons . codec . digest . DigestUtils . md5Hex ( new FileInputStream ( tempXmlFile ) ) ; File destXmlFile = new File ( conf . getPoolsConfigFile ( ) ) ; boolean success = tempXmlFile . renameTo ( destXmlFile ) ; LOG . info ( "generatePoolConfig: Renamed generated file " + tempXmlFile . getAbsolutePath ( ) + " to " + destXmlFile . getAbsolutePath ( ) + " returned " + success + " with md5sum " + md5 ) ; return md5 ; } catch ( TransformerConfigurationException e ) { LOG . warn ( "generatePoolConfig: Failed to write file" , e ) ; } catch ( IOException e ) { LOG . warn ( "generatePoolConfig: Failed to write file" , e ) ; } catch ( TransformerException e ) { LOG . warn ( "generatePoolConfig: Failed to write file" , e ) ; } return null ; }
Generate the new pools configuration using the configuration generator . The generated configuration is written to a temporary file and then atomically renamed to the specified destination file . This function may be called concurrently and it is safe to do so because of the atomic rename to the destination file .