[jaxb~v2:37f7f040] Merge fixes with trunk: 14348987, 16998431

  • From: snajper@...
  • To: commits@...
  • Subject: [jaxb~v2:37f7f040] Merge fixes with trunk: 14348987, 16998431
  • Date: Tue, 6 Aug 2013 12:21:55 +0000

Project:    jaxb
Repository: v2
Revision:   37f7f040138ca2a8cea3f5274af5a5c9fd2cc32c
Author:     snajper
Date:       2013-08-06 12:12:55 UTC
Link:       

Log Message:
------------
Merge fixes with trunk: 14348987, 16998431

14348987 - invalid absolute URL

16998431 - JAXB UNMARSHAL PERFORMANCE ISSUE



Revisions:
----------
37f7f040138ca2a8cea3f5274af5a5c9fd2cc32c


Modified Paths:
---------------
jaxb-ri/runtime/src/com/sun/xml/bind/Util.java
jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Loader.java
jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.java
jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.properties
jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/UnmarshallingContext.java
jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/XsiTypeLoader.java
jaxb-ri/runtime/src/com/sun/xml/bind/v2/util/EditDistance.java
jaxb-ri/xjc/src/com/sun/tools/xjc/reader/internalizer/AbstractReferenceFinderImpl.java


Diffs:
------
--- a/jaxb-ri/runtime/src/com/sun/xml/bind/Util.java
+++ b/jaxb-ri/runtime/src/com/sun/xml/bind/Util.java
@@ -45,7 +45,7 @@ import java.util.logging.Logger;
 /**
  * @author Kohsuke Kawaguchi
  */
-public abstract class Util {
+public final class Util {
     private Util() {}   // no instanciation
 
     /**--- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Loader.java
+++ b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Loader.java
@@ -109,11 +109,15 @@ public abstract class Loader {
 
     @SuppressWarnings({"StringEquality"})
     protected final void reportUnexpectedChildElement(TagName ea, boolean 
canRecover) throws SAXException {
-        if(canRecover && 
!UnmarshallingContext.getInstance().parent.hasEventHandler())
+        if (canRecover) {
             // this error happens particurly often (when input documents 
contain a lot of unexpected elements to be ignored),
             // so don't bother computing all the messages and etc if we know 
that
-            // there's no event handler to receive the error in the end. See 
#286 
-            return;
+            // there's no event handler to receive the error in the end. See 
#286
+            UnmarshallingContext context = 
UnmarshallingContext.getInstance();
+            if (!context.parent.hasEventHandler() // is somebody listening?
+                    || !context.shouldErrorBeReported()) // should we report 
error?
+                return;
+        }
         if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern())
             reportError(Messages.UNINTERNED_STRINGS.format(), canRecover );
         else--- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.java
+++ 
b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.java
@@ -55,6 +55,7 @@ enum Messages {
     UNRECOGNIZED_TYPE_NAME_MAYBE, // 2 args
     UNABLE_TO_CREATE_MAP, // 1 arg
     UNINTERNED_STRINGS, // no args
+    ERRORS_LIMIT_EXCEEDED, // no arg
     ;
 
     private static final ResourceBundle rb = 
ResourceBundle.getBundle(Messages.class.getName());--- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.properties
+++ 
b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/Messages.properties
@@ -63,3 +63,7 @@ UNABLE_TO_CREATE_MAP = \
 
 UNINTERNED_STRINGS = \
     Namespace URIs and local names to the unmarshaller needs to be interned.
+
+# user have to set 
Logger.getLogger("com.sun.xml.bind").setLevel(Level.FINEST)
+ERRORS_LIMIT_EXCEEDED = \
+    Errors limit exceeded. To receive all errors set 'com.sun.xml.bind' 
logger to FINEST level.
\ No newline at end of file--- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/UnmarshallingContext.java
+++ 
b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/UnmarshallingContext.java
@@ -50,6 +50,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBElement;
@@ -96,6 +98,7 @@ public final class UnmarshallingContext extends Coordinator
      */
     private final State root;
 
+    private static final Logger logger = 
Logger.getLogger(UnmarshallingContext.class.getName());
     /**
      * The currently active state.
      */
@@ -192,6 +195,14 @@ public final class UnmarshallingContext extends 
Coordinator
     public @Nullable ClassLoader classLoader;
 
     /**
+     * The variable introduced to avoid reporting n^10 similar errors.
+     * After error is reported counter is decremented. When it became 0 - 
errors should not be reported any more.
+     *
+     * volatile is required to ensure that concurrent threads will see 
changed value
+     */
+    private static volatile int errorsCounter = 10;
+
+    /**
      * State information for each element.
      */
     public final class State {
@@ -1249,5 +1260,28 @@ public final class UnmarshallingContext extends 
Coordinator
         return null;
     }
 
+    /**
+     * Based on current {@link Logger} {@link Level} and errorCounter value 
determines if error should be reported.
+     *
+     * If the method called and return true it is expected that error will 
be reported. And that's why
+     * errorCounter is automatically decremented during the check.
+     *
+     * NOT THREAD SAFE!!! In case of heave concurrency access several 
additional errors could be reported. It's not expected to be the
+     * problem. Otherwise add synchronization here.
+     *
+     * @return true in case if {@link Level#FINEST} is set OR we haven't 
exceed errors reporting limit.
+     */
+    public boolean shouldErrorBeReported() throws SAXException {
+        if (logger.isLoggable(Level.FINEST))
+            return true;
+
+        if (errorsCounter >= 0) {
+            --errorsCounter;
+            if (errorsCounter == 0)
+                handleEvent(new ValidationEventImpl(ValidationEvent.WARNING, 
Messages.ERRORS_LIMIT_EXCEEDED.format(),
+                        getLocator().getLocation(), null), true);
+        }
+        return errorsCounter >= 0;
+    }
 }
 --- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/XsiTypeLoader.java
+++ 
b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/runtime/unmarshaller/XsiTypeLoader.java
@@ -107,12 +107,15 @@ public class XsiTypeLoader extends Loader {
                     return defaultBeanInfo;
 
                 beanInfo = context.getJAXBContext().getGlobalType(type);
-                if(beanInfo==null) {
-                    String nearest = 
context.getJAXBContext().getNearestTypeName(type);
-                    if(nearest!=null)
-                        
reportError(Messages.UNRECOGNIZED_TYPE_NAME_MAYBE.format(type,nearest),true);
-                    else
-                        
reportError(Messages.UNRECOGNIZED_TYPE_NAME.format(type),true);
+                if(beanInfo==null) { // let's report an error
+                    if (context.parent.hasEventHandler() // is somebody 
listening?
+                            && context.shouldErrorBeReported()) { // should 
we report error?
+                        String nearest = 
context.getJAXBContext().getNearestTypeName(type);
+                        if(nearest!=null)
+                            
reportError(Messages.UNRECOGNIZED_TYPE_NAME_MAYBE.format(type,nearest),true);
+                        else
+                            
reportError(Messages.UNRECOGNIZED_TYPE_NAME.format(type),true);
+                    }
                 }
                 // TODO: resurrect the following check
         //                    else--- 
a/jaxb-ri/runtime/src/com/sun/xml/bind/v2/util/EditDistance.java
+++ b/jaxb-ri/runtime/src/com/sun/xml/bind/v2/util/EditDistance.java
@@ -40,8 +40,10 @@
 
 package com.sun.xml.bind.v2.util;
 
-import java.util.Collection;
+import java.util.AbstractMap;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.WeakHashMap;
 
 /**
  * Computes the string edit distance.
@@ -56,13 +58,29 @@ import java.util.Arrays;
 public class EditDistance {
 
     /**
+     * Weak results cache to avoid additional computations.
+     * Because of high complexity caching is required.
+     */
+    private static final WeakHashMap<AbstractMap.SimpleEntry<String,String>, 
Integer> CACHE = new WeakHashMap<AbstractMap.SimpleEntry<String, String>, 
Integer>();
+
+    /**
      * Computes the edit distance between two strings.
      * 
      * <p>
      * The complexity is O(nm) where n=a.length() and m=b.length().
      */
     public static int editDistance( String a, String b ) {
-        return new EditDistance(a,b).calc();
+        // let's check cache
+        AbstractMap.SimpleEntry<String,String> entry = new 
AbstractMap.SimpleEntry<String, String>(a, b); // using this class to avoid 
creation of my own which will handle PAIR of values
+        Integer result = null;
+        if (CACHE.containsKey(entry))
+            result = CACHE.get(entry); // looks like we have it
+
+        if (result == null) {
+            result = new EditDistance(a, b).calc();
+            CACHE.put(entry, result); // cache the result
+        }
+        return result;
     }
     
     /**--- 
a/jaxb-ri/xjc/src/com/sun/tools/xjc/reader/internalizer/AbstractReferenceFinderImpl.java
+++ 
b/jaxb-ri/xjc/src/com/sun/tools/xjc/reader/internalizer/AbstractReferenceFinderImpl.java
@@ -98,16 +98,21 @@ public abstract class AbstractReferenceFinderImpl extends 
XMLFilterImpl {
             // absolutize URL.
             String lsi = locator.getSystemId();
             String ref;
-            if (lsi.startsWith("jar:")) {
-                int bangIdx = lsi.indexOf('!');
-                if (bangIdx > 0) {
-                    ref = lsi.substring(0, bangIdx + 1)
-                            + new URI(lsi.substring(bangIdx + 
1)).resolve(new URI(relativeRef)).toString();
+            URI relRefURI = new URI(relativeRef);
+            if (relRefURI.isAbsolute())
+                ref = relativeRef;
+            else {
+                if (lsi.startsWith("jar:")) {
+                    int bangIdx = lsi.indexOf('!');
+                    if (bangIdx > 0) {
+                        ref = lsi.substring(0, bangIdx + 1)
+                                + new URI(lsi.substring(bangIdx + 
1)).resolve(new URI(relativeRef)).toString();
+                    } else {
+                        ref = relativeRef;
+                    }
                 } else {
-                    ref = relativeRef;
+                    ref = new URI(lsi).resolve(new 
URI(relativeRef)).toString();
                 }
-            } else {
-                ref = new URI(lsi).resolve(new URI(relativeRef)).toString();
             }
 
             // then parse this schema as well,
@@ -141,4 +146,4 @@ public abstract class AbstractReferenceFinderImpl extends 
XMLFilterImpl {
         super.setDocumentLocator(locator);
         this.locator = locator;
     }
-};
+}





[jaxb~v2:37f7f040] Merge fixes with trunk: 14348987, 16998431

snajper 08/06/2013
Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close