1 package com.ctc.wstx.compat;
2
3 import java.util.logging.Logger;
4
5 import javax.xml.namespace.QName;
6
7 /**
8  * Helper class used to solve [WSTX-174]: some older AppServers were
9  * shipped with incompatible version of QName class, which is missing
10  * the 3 argument constructor. To address this, we'll use bit of
11  * ClassLoader hacker to gracefully (?) downgrade to using 2 arg
12  * alternatives if necessary.
13  *<p>
14  * Note: choice of java.util.logging logging is only based on the
15  * fact that it is guaranteed to be present (we have JDK 1.4 baseline
16  * requirement) so that we do not add external dependencies.
17  * It is not a recommendation for using JUL per se; most users would
18  * do well to just use slf4j or log4j directly instead.
19  *
20  * @author Tatu Saloranta
21  * 
22  * @since 3.2.8
23  */

24 public final class QNameCreator
25 {
26     /**
27      * Creator object that creates QNames using proper 3-arg constructor.
28      * If dynamic class loading fails
29      */

30     private final static Helper _helper;
31     static {
32         Helper h = null;
33         try {
34             // Not sure where it'll fail, constructor or create...
35             Helper h0 = new Helper();
36             /*QName n =*/ h0.create("elem""http://dummy""ns");
37             h = h0;
38         } catch (Throwable t) {
39             String msg = "Could not construct QNameCreator.Helper; assume 3-arg QName constructor not available and use 2-arg method instead. Problem: "+t.getMessage();
40             try {
41                 Logger.getLogger("com.ctc.wstx.compat.QNameCreator").warning(msg);
42             } catch (Throwable t2) { // just in case JUL craps out...
43                 System.err.println("ERROR: failed to log error using Logger (problem "+t.getMessage()+"), original problem: "+msg);
44             }
45         }
46         _helper = h;
47     }
48
49     public static QName create(String uri, String localName, String prefix)
50     {
51         if (_helper == null) { // can't use 3-arg constructor; but 2-arg will be there
52             return new QName(uri, localName);
53         }
54         return _helper.create(uri, localName, prefix);
55     }
56
57     /**
58      * Helper class used to encapsulate calls to the missing method.
59      */

60     private final static class Helper
61     {
62         public Helper() { }
63         
64         public QName create(String localName, String nsURI, String prefix)
65         {
66             return new QName(localName, nsURI, prefix);
67         }
68     }
69 }
70
71