View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/DbFormsConfigRegistry.java,v 1.7 2006/01/25 20:05:32 hkollmann Exp $
3    * $Revision: 1.7 $
4    * $Date: 2006/01/25 20:05:32 $
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.config;
25  
26  import javax.servlet.ServletContext;
27  
28  
29  
30  /***
31   * Registry for DbFormsConfig classes
32   *
33   * @author Luca Fossato
34   *
35   */
36  public class DbFormsConfigRegistry {
37     /*** unique instance for this class */
38  
39  	private static DbFormsConfigRegistry instance = null;
40  
41     /*** servlet config */
42     private ServletContext servletContext = null;
43  
44     /***
45      * Protected constructor
46      */
47     protected DbFormsConfigRegistry() {
48     }
49  
50     /***
51      * Get the instance of DatabaseEventFactory class.
52      *
53      * @return the instance of DatabaseEventFactory class
54      */
55     public static synchronized DbFormsConfigRegistry instance() {
56        if (instance == null) {
57           instance = new DbFormsConfigRegistry();
58        }
59  
60        return instance;
61     }
62  
63  
64     /***
65      * Sets the servletContext attribute of the DbFormsConfigRegistry object
66      *
67      * @param servletContext The new servletConfig value
68      */
69     public void setServletContext(ServletContext servletContext) {
70        this.servletContext = servletContext;
71     }
72  
73  
74     /***
75      * Look up the default DbFormsConfig object stored into the registry.
76      *
77      * @return Description of the Return Value
78      *
79      * @exception Exception if the lookup operation fails
80      */
81     public DbFormsConfig lookup() throws Exception {
82        return lookup(DbFormsConfig.CONFIG);
83     }
84  
85  
86     /***
87      * Register the input DbFormsConfig object into the registry as the default
88      * config object.
89      *
90      * @param config the DbFormsConfig object
91      */
92     public void register(DbFormsConfig config) {
93        register(DbFormsConfig.CONFIG, config);
94     }
95  
96  
97     /***
98      * Look up a DbFormsConfig object stored into the registry.
99      *
100     * @param name the DbFormsConfig name previously used to store the config
101     *        object into the registry.
102     *
103     * @return Description of the Return Value
104     *
105     * @exception Exception if the lookup operation fails
106     */
107    private DbFormsConfig lookup(String name) throws Exception {
108       DbFormsConfig config = null;
109 
110       if (servletContext != null) {
111          config = (DbFormsConfig) servletContext.getAttribute(name);
112       } else {
113          throw new Exception("cannot lookup a config object with the name ["
114                              + name + "]");
115       }
116 
117       return config;
118    }
119 
120 
121    /***
122     * Register a DbFormsConfig object into the registry
123     *
124     * @param name the DbFormsConfig name used as the registry key
125     * @param config the DbFormsConfig object
126     */
127    private void register(String        name,
128                          DbFormsConfig config) {
129       if (servletContext != null) {
130          servletContext.setAttribute(name, config);
131       }
132    }
133 }