You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by Ivan <xh...@gmail.com> on 2012/09/26 11:37:55 UTC

Use of new String(String value) constructor in CXF codes ?

While searching the codes, I saw that there are some cxf codes are using
new String(String value). like in StaxUtils, I am wondering is there any
special reason for this ?

public static Document read(XMLStreamReader reader, boolean recordLoc)
throws XMLStreamException {
        Document doc = DOMUtils.createDocument();
        if (reader.getLocation().getSystemId() != null) {
            try {
                doc.setDocumentURI(new
String(reader.getLocation().getSystemId()));
<-----------------------------------
            } catch (Exception e) {
                //ignore - probably not DOM level 3
            }
        }
        readDocElements(doc, doc, reader, true, recordLoc);
        return doc;
    }

Thanks

-- 
Ivan

Re: Use of new String(String value) constructor in CXF codes ?

Posted by Daniel Kulp <dk...@apache.org>.
The WSDLManager uses the systemId string that is passed in (and then recorded in the locations and such) as a key for a WeakHashMap to provide a cache for the WSDL objects and schemas and such.  However, that does mean we need to be a little careful with those location String objects to make sure nothing strongly holds onto those objects when they don't need to.   If things end up being held onto strongly, the caches will never clear and we end up with memory leaks.

In particular, the WSDL holds onto Schemas which are DOM's and would use this method to parse.  If the DOM comes from the schema in the WSDL, this would result in the system ID recorded below being the same as the WSDL and thus locking the wsdl in the cache.

In general, creating a copy of the string is relatively cheap.   Not creating a copy and risking an entire WSDL/DOM being left in memory is not so cheap.

Dan




On Sep 26, 2012, at 5:37 AM, Ivan <xh...@gmail.com> wrote:

> While searching the codes, I saw that there are some cxf codes are using
> new String(String value). like in StaxUtils, I am wondering is there any
> special reason for this ?
> 
> public static Document read(XMLStreamReader reader, boolean recordLoc)
> throws XMLStreamException {
>        Document doc = DOMUtils.createDocument();
>        if (reader.getLocation().getSystemId() != null) {
>            try {
>                doc.setDocumentURI(new
> String(reader.getLocation().getSystemId()));
> <-----------------------------------
>            } catch (Exception e) {
>                //ignore - probably not DOM level 3
>            }
>        }
>        readDocElements(doc, doc, reader, true, recordLoc);
>        return doc;
>    }
> 
> Thanks
> 
> -- 
> Ivan

-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com