Search code examples
javajakarta-mailjavax.activation

dataContentHandler member variable being overwritten in javax.activation.DataHandler.getDataContentHandler?


The code below is for javax.activation.DataHandler.getDataContentHandler, 1.41, 07/05/14.

If the dataContentHandler member variable is null when the method is called, then it gets set by the 'if' clause at [1] (which is what happens in my program).

It then immediately gets overwritten in the if clause at [2].

Am I missing something, or is that unlikely to be the intended behaviour?

   private synchronized DataContentHandler getDataContentHandler() {

     // make sure the factory didn't change
     if (factory != oldFactory) {
         oldFactory = factory;
         factoryDCH = null;
         dataContentHandler = null;
         transferFlavors = emptyFlavors;
     }

     if (dataContentHandler != null)
         return dataContentHandler;

     String simpleMT = getBaseType();

     if (factoryDCH == null && factory != null)
         factoryDCH = factory.createDataContentHandler(simpleMT);

     if (factoryDCH != null)
         dataContentHandler = factoryDCH;

     if (dataContentHandler == null) { // [1]
         if (dataSource != null)
         dataContentHandler = getCommandMap().
                createDataContentHandler(simpleMT, dataSource);
         else
         dataContentHandler = getCommandMap().
                createDataContentHandler(simpleMT);
     }

     // getDataContentHandler always uses these 'wrapper' handlers
     // to make sure it returns SOMETHING meaningful...
     if (dataSource != null) // [2]
         dataContentHandler = new DataSourceDataContentHandler(
                                  dataContentHandler,
                          dataSource);
     else
         dataContentHandler = new ObjectDataContentHandler(
                          dataContentHandler,
                          object,
                          objectMimeType);
     return dataContentHandler;
     }

I


Solution

  • Am I missing something, or is that unlikely to be the intended behaviour?

    Looks like that is the intended behavior per the comment:

    // getDataContentHandler always uses these 'wrapper' handlers // to make sure it returns SOMETHING meaningful...

    If you look closely at the code, the 'dataContentHandler' is passed as an argument to 'new DataSourceDataContentHandler' and 'new ObjectDataContentHandler'. That is the 'wrapping' referred to in the comment.