/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ode.jbi.msgmap;

import java.util.WeakHashMap;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public abstract class BaseXmlMapper {
    protected Log __log = LogFactory.getLog(getClass());

    private DocumentBuilderFactory _dbf;

    private TransformerFactory _transformerFactory;

    /** Cache of the parsed messages. */
    private static WeakHashMap<Source, Document> __parsed = new WeakHashMap<Source, Document>();

    protected BaseXmlMapper() {
        _transformerFactory = TransformerFactory.newInstance();
        _dbf = DocumentBuilderFactory.newInstance();
    }

    protected Element parse(Source content) throws MessageTranslationException {
        Document parsed = __parsed.get(content);
        if (parsed != null)
            return parsed.getDocumentElement();

        Transformer txer = null;
        try {
            txer = _transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            String errmsg = "Transformer configuration error!";
            __log.fatal(errmsg, e);
            throw new Error(errmsg, e);
        }

        try {
            DOMResult domresult = new DOMResult();
            txer.transform(content, domresult);
            parsed = (Document) domresult.getNode();
            __parsed.put(content, parsed);
            return parsed.getDocumentElement();
        } catch (TransformerException e) {
            throw new MessageTranslationException("Transformer error!", e);
        }
    }

    protected Document newDocument() {
        try {
            return _dbf.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException e) {
            String errmsg = "Parser configuration error!";
            __log.fatal(errmsg, e);
            throw new Error(errmsg, e);
        }
    }

}

@@ -835,24 +835,76 @@
 bi.msgmap;%0A%0A
+import java.util.Collections;%0Aimport java.util.Map;%0A
 import java.
@@ -917,24 +917,24 @@
 eakHashMap;%0A
-
 %0Aimport java
@@ -1696,24 +1696,16 @@
  static 
-WeakHash
 Map%3CSour
@@ -1729,16 +1729,44 @@
 arsed = 
+Collections.synchronizedMap(
 new Weak
@@ -1792,16 +1792,17 @@
 ument%3E()
+)
 ;%0A%0A    p
@@ -2033,16 +2033,215 @@
 ption %7B%0A
+        %0A        // Check for the message in the cache. note that we are using a synchronized map here,%0A        // so that we are thread safe, although it is possible for the parse to happen twice. %0A
         
@@ -2778,24 +2778,24 @@
 domresult);%0A
-
             
@@ -2831,24 +2831,67 @@
 .getNode();%0A
+            // Again, synchronized map... %0A
             

Fix dangerous use of unsynchronzied cache.