[FB-Discuss] OS_OPEN_STREAM_EXCEPTION_PATH not working?

Paul Rowe prowe2 at yahoo.com
Fri Apr 16 22:33:01 EDT 2004


Hi All;

  I have a piece of code where
OS_OPEN_STREAM_EXCEPTION_PATH appears to be working
improperly.  It is reporting a stream may not be
closed ... which I cannot for the life of me figure
out.  I spent around 12 hours analyzing the Findbugs
code and I am still stumped as to why Findbugs may be
reporting this as an issue.  Here is the offending
method (more comments below it):

boolean sendPostRequest(URL url, String query, Event
[] batch)
    {
        boolean ret = true;

        HttpURLConnection connection =  null;
            	PrintWriter out = null;
		BufferedReader in = null;

	boolean done = false;
	for (int i = 0; i < 5; i++)
	{
		if (done)
			break;
        	try 
        	{
            EtgLogger.log(this,"dispatchEvent: opening
connection", EtgLogger.DEBUG);
            connection = (HttpURLConnection)
url.openConnection();
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setAllowUserInteraction(false);
           
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
            connection.setDoInput(true);
            connection.setDoOutput(true);


            EtgLogger.log(EuSmsSender.class, "writing
to  connection", EtgLogger.DEBUG);
            out = new
PrintWriter(connection.getOutputStream());  //
FINDBUGS MAY REPORT THIS DEPENDING ON THE CLOSE
POSITION IN THE FINALLY CLAUSE
            out.println(query);
            out.flush();
            out.close();
		out = null;


            EtgLogger.log(this,"dispatchEvent: writing
to  connection", EtgLogger.DEBUG);
            int responseCode =
connection.getResponseCode();
            EtgLogger.log(EuSmsSender.class,
"responseCode = " + responseCode, EtgLogger.DEBUG);

            EtgLogger.log(this,"dispatchEvent:
responseCode = " + responseCode, EtgLogger.DEBUG);


            if (responseCode ==
HttpURLConnection.HTTP_OK) 
            {
		if (i == 0)  //write successful message only first
time
		{
                
Event.updateBatchState(batch,EventOps.STATE_TX,"Sent
to SMS Gateway",EventOps.STATUS_SUCCESS);
                
Event.updateStateInDB(batch,source.getStatusSenderClass());
		}
            }
            else
            {
                EtgLogger.log(this,"dispatchEvent:
Unable to connect to " + 
			url + " .(http error code:" + 
			String.valueOf(responseCode) + ").",
EtgLogger.DEBUG);
                
		String errorMessage = "Unable to connect to " + url
+ 
		" .(http error code:" + String.valueOf(responseCode)
+ ").";
               
Event.updateBatchState(batch,EventOps.STATE_TX,errorMessage,EventOps.STATUS_FAILED);
               
//Event.updateStateInDB(batch,source.getStatusSenderClass());
		throw new Exception(errorMessage);
                //return false;
            }


            in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));  //
FINDBUGS REPORTS THIS LINE AS A PROBLEM, WHICH DOES
NOT MAKE SENSE.
            String inputLine;
            StringBuffer responseBuf = new
StringBuffer();
            while( (inputLine = in.readLine()) !=
null)
            {
                responseBuf.append(inputLine);
            }
            in.close();
		in = null;
            connection.disconnect();

            String response = responseBuf.toString();

            EtgLogger.log(this,"dispatchEvent: SMS
RESPONSE: " + response , EtgLogger.DEBUG);

            verifyGatewayResponse(batch,response);
		done = true;

        } 
        catch ( Throwable e)
        {
        	e.printStackTrace();
        	EtgLogger.log(this,"dispatchEvent",e,
EtgLogger.DEBUG);
               
Event.updateBatchState(batch,EventOps.STATE_ROUTING,e.getMessage(),EventOps.STATUS_FAILED);

                if (i == 4)
                {
                   
updateState(batch,Event.TRY_AGAIN_FLAG);
                   
Event.updateBatchState(batch,EventOps.STATE_ROUTING,"Unable
to send message. Will try later:
"+e.getMessage(),EventOps.STATUS_FAILED);
                }     
        	//ret = false;
        }
	finally
	{
		if (out != null)
		{
			try 
			{
			out.close();
			} catch (java.io.IOException ignore) {}
		}
		if (in != null)
		{
			try 
			{
				in.close();
			} catch (java.io.IOException ignore) {}
		}
	}

	} //for - trying 5 times

        EtgLogger.log(this,"dispatchEvent: returning
from dispatching wireless event", EtgLogger.DEBUG);
        return ret;
    }

I noticed that if I put in.close() before out.close(),
then findbugs will report the out instantiation as a
problem rather than the in instantiation.

I need to get the Dragon Book out.  The code is over
my head for now and very hard to debug.  I enabled all
debugging flags for most of the classes, but the
debugging doesn't make much sense to me. 

I have a feeling that this problem is related to the
null pointer deref problem I reported in February.




	
		
__________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html



More information about the Findbugs-discuss mailing list