View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/dom/DOMFactory.java,v 1.10 2006/02/19 07:46:49 hkollmann Exp $
3    * $Revision: 1.10 $
4    * $Date: 2006/02/19 07:46:49 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.dom;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.interfaces.IDOMFactory;
30  
31  import org.dbforms.util.ReflectionUtil;
32  
33  
34  /***
35   * abstract class to hide the implemtation details of the various dom
36   * implementations.
37   *
38   * @author Henner Kollmann
39   */
40  public class DOMFactory {
41     private static final ThreadLocal singlePerThread = new ThreadLocal();
42     private static Log               logCat = LogFactory.getLog(DOMFactory.class
43                                                                 .getName());
44     private static String            factoryClass = "org.dbforms.dom.DOMFactoryXALANImpl";
45  
46     /***
47      * Creates a new DOMFactory object.
48      */
49     private DOMFactory() {
50     }
51  
52     /***
53      * Get the thread singelton instance
54      *
55      * @return a DOMFactory instance per thread
56      */
57     public static IDOMFactory instance() {
58        IDOMFactory fact = (IDOMFactory) singlePerThread.get();
59        if (fact == null) {
60           try {
61              fact = (IDOMFactory) ReflectionUtil.newInstance(factoryClass);
62           } catch (Exception e) {
63              logCat.error("instance", e);
64           }
65  
66           if (fact == null) {
67              fact = new DOMFactoryXALANImpl();
68           }
69           singlePerThread.set(fact);
70        }
71        return fact;
72     }
73  
74     /***
75      * Sets another dom factory to use.
76      * Must be called before the first call to instance()!!
77      *
78      * @param string
79      */
80     public static void setFactoryClass(String string) {
81        singlePerThread.set(null);
82        factoryClass = string;
83     }
84     
85  }