@Nonnull
  @SuppressFBWarnings ("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
  @MustBeLocked (ELockType.WRITE)
  private ESuccess _writeToFile ()
  {
    // Build the filename to write to
    final String sFilename = m_aFilenameProvider.get ();
    if (sFilename == null)
    {
      // We're not operating on a file! Required for testing
      if (!isSilentMode ())
        if (LOGGER.isWarnEnabled ())
          LOGGER.warn ("The DAO of class " + getClass ().getName () + " cannot write to a file");
      return ESuccess.FAILURE;
    }

    // Check for a filename change before writing
    if (!sFilename.equals (m_sPreviousFilename))
    {
      onFilenameChange (m_sPreviousFilename, sFilename);
      m_sPreviousFilename = sFilename;
    }

    if (isDebugLogging ())
      if (LOGGER.isInfoEnabled ())
        LOGGER.info ("Trying to write DAO file '" + sFilename + "'");

    File aFile = null;
    IMicroDocument aDoc = null;
    try
    {
      // Get the file handle
      aFile = getSafeFile (sFilename, EMode.WRITE);

      m_aStatsCounterWriteTotal.increment ();
      final StopWatch aSW = StopWatch.createdStarted ();

      // Create XML document to write
      aDoc = createWriteData ();
      if (aDoc == null)
        throw new DAOException ("Failed to create data to write to file");

      // Generic modification
      modifyWriteData (aDoc);

      // Perform optional stuff like backup etc. Must be done BEFORE the output
      // stream is opened!
      beforeWriteToFile (sFilename, aFile);

      // Get the output stream
      final OutputStream aOS = FileHelper.getOutputStream (aFile);
      if (aOS == null)
      {
        // Happens, when another application has the file open!
        // Logger warning already emitted
        throw new DAOException ("Failed to open output stream");
      }

      // Write to file (closes the OS)
      final IXMLWriterSettings aXWS = getXMLWriterSettings ();
      if (MicroWriter.writeToStream (aDoc, aOS, aXWS).isFailure ())
        throw new DAOException ("Failed to write DAO XML data to file");

      m_aStatsCounterWriteTimer.addTime (aSW.stopAndGetMillis ());
      m_aStatsCounterWriteSuccess.increment ();
      m_nWriteCount++;
      m_aLastWriteDT = PDTFactory.getCurrentLocalDateTime ();
      return ESuccess.SUCCESS;
    }
    catch (final Exception ex)
    {
      final String sErrorFilename = aFile != null ? aFile.getAbsolutePath () : sFilename;

      if (LOGGER.isErrorEnabled ())
        LOGGER.error ("The DAO of class " +
                      getClass ().getName () +
                      " failed to write the DAO data to '" +
                      sErrorFilename +
                      "'",
                      ex);

      triggerExceptionHandlersWrite (ex, sErrorFilename, aDoc);
      m_aStatsCounterWriteExceptions.increment ();
      return ESuccess.FAILURE;
    }
  }
@ Nonnull @ SuppressFBWarnings ( "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" ) @ MustBeLocked ( ELockType . WRITE ) private ESuccess _writeToFile ( ) { final String sFilename = m_aFilenameProvider . get ( ) ; if ( sFilename == null ) { if ( ! isSilentMode ( ) ) if ( LOGGER . isWarnEnabled ( ) ) LOGGER . warn ( "The DAO of class " + getClass ( ) . getName ( ) + " cannot write to a file" ) ; return ESuccess . FAILURE ; } if ( ! sFilename . equals ( m_sPreviousFilename ) ) { onFilenameChange ( m_sPreviousFilename , sFilename ) ; m_sPreviousFilename = sFilename ; } if ( isDebugLogging ( ) ) if ( LOGGER . isInfoEnabled ( ) ) LOGGER . info ( "Trying to write DAO file '" + sFilename + "'" ) ; File aFile = null ; IMicroDocument aDoc = null ; try { aFile = getSafeFile ( sFilename , EMode . WRITE ) ; m_aStatsCounterWriteTotal . increment ( ) ; final StopWatch aSW = StopWatch . createdStarted ( ) ; aDoc = createWriteData ( ) ; if ( aDoc == null ) throw new DAOException ( "Failed to create data to write to file" ) ; modifyWriteData ( aDoc ) ; beforeWriteToFile ( sFilename , aFile ) ; final OutputStream aOS = FileHelper . getOutputStream ( aFile ) ; if ( aOS == null ) { throw new DAOException ( "Failed to open output stream" ) ; } final IXMLWriterSettings aXWS = getXMLWriterSettings ( ) ; if ( MicroWriter . writeToStream ( aDoc , aOS , aXWS ) . isFailure ( ) ) throw new DAOException ( "Failed to write DAO XML data to file" ) ; m_aStatsCounterWriteTimer . addTime ( aSW . stopAndGetMillis ( ) ) ; m_aStatsCounterWriteSuccess . increment ( ) ; m_nWriteCount ++ ; m_aLastWriteDT = PDTFactory . getCurrentLocalDateTime ( ) ; return ESuccess . SUCCESS ; } catch ( final Exception ex ) { final String sErrorFilename = aFile != null ? aFile . getAbsolutePath ( ) : sFilename ; if ( LOGGER . isErrorEnabled ( ) ) LOGGER . error ( "The DAO of class " + getClass ( ) . getName ( ) + " failed to write the DAO data to '" + sErrorFilename + "'" , ex ) ; triggerExceptionHandlersWrite ( ex , sErrorFilename , aDoc ) ; m_aStatsCounterWriteExceptions . increment ( ) ; return ESuccess . FAILURE ; } }
The main method for writing the new data to a file. This method may only be
called within a write lock!

@return {@link ESuccess} and never <code>null</code>.