[jdom-interest] static data needs to be thread data for mult ithreaded applications

Nick Reeves Nick.Reeves at gtl.com
Fri Sep 22 05:40:29 PDT 2000


> Nick, do you want to submit a patch to Namespace that makes access to
> the statics sync'd?
> 

What's the procedure for this I can't find anything on the website.

I can't use CVS by the way as I'm behind a firewall.

The actual patch is trivial it's just a matter of add a synchronized block
in getNamespace
as marked with ++ below, and some suitable comments on synch policy.


    public static Namespace getNamespace(String prefix, String uri) {
        // Ensure proper naming
        String reason;
        if ((reason = Verifier.checkNamespacePrefix(prefix)) != null) {
            throw new IllegalNameException(prefix, "Namespace prefix",
reason);
        }
        if ((reason = Verifier.checkNamespaceURI(uri)) != null) {
            throw new IllegalNameException(uri, "Namespace URI", reason);
        }

        // Housekeeping
        if ((prefix == null) || (prefix.trim().equals(""))) {
            prefix = "";
        }
        if ((uri == null) || (uri.trim().equals(""))) {
            uri = "";
        }

        // Unless the "empty" Namespace (no prefix and no URI), require a
URI
        if ((!prefix.equals("")) && (uri.equals(""))) {
            throw new IllegalNameException("", "namespace",
                                           "Namespace URIs must be non-null
and non-empty Strings.");
        }

++	  synchronized (namespaces) {

        // Return existing namespace if found
        if (namespaces.containsKey(uri)) {
            return (Namespace)namespaces.get(uri);
        }

        // Ensure prefix uniqueness in non-default namespaces
        if (!prefix.equals("")) {
            int i = 0;
            String newPrefix = prefix;

            while (mappings.containsKey(newPrefix)) {
                newPrefix = newPrefix + i++;
            }
            prefix = newPrefix;

            // We really don't care to store all the default namespaces, so
            //   storing mappings here is OK
            mappings.put(prefix, uri);
        }

        // Finally, store and return
        Namespace ns = new Namespace(prefix, uri);
        namespaces.put(uri, ns);
        return ns;

++      }
    }



More information about the jdom-interest mailing list