KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > WebModuleDefinitionManager


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: WebModuleDefinitionManager.java,v 1.5 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.www;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import org.apache.commons.collections.map.LinkedMap;
30 import org.opensubsystems.core.error.OSSException;
31 import org.opensubsystems.core.util.ClassFactory;
32 import org.opensubsystems.core.util.GlobalConstants;
33 import org.opensubsystems.core.util.Log;
34
35 /**
36  * This manager is used for collecting all web client modules that are available
37  * in the system.
38  *
39  * @version $Id: WebModuleDefinitionManager.java,v 1.5 2007/01/07 06:14:09 bastafidli Exp $
40  * @author Julian Legeny
41  * @code.reviewer Miro Halas
42  * @code.reviewed 1.3 2006/06/07 08:44:06 jlegeny
43  */

44 public class WebModuleDefinitionManager
45 {
46    // Constants ////////////////////////////////////////////////////////////////
47

48    /**
49     * Lock used in synchronized sections.
50     */

51    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
52    
53    // Cached values ////////////////////////////////////////////////////////////
54

55    /**
56     * Logger for this class
57     */

58    private static Logger JavaDoc s_logger = Log.getInstance(WebModuleDefinitionManager.class);
59   
60    /**
61     * Reference to the instance actually in use.
62     */

63    private static WebModuleDefinitionManager s_defaultInstance;
64
65    // Attributes ///////////////////////////////////////////////////////////////
66

67    /**
68     * Map of all web modules. Key is a module name, value is
69     * WebModule object.
70     */

71    protected Map JavaDoc m_mapWebModules;
72    
73    // Constructors /////////////////////////////////////////////////////////////
74

75    /**
76     * Default constructor
77     */

78    public WebModuleDefinitionManager()
79    {
80       // This has to be sequenced hashmap to create the schemas in the correct order
81
m_mapWebModules = new LinkedMap();
82    }
83    
84    // Public methods ///////////////////////////////////////////////////////////
85

86    /**
87     * Get the default instance. This method is here to make the manager
88     * configurable. Once can specify in configuration file derived class to used
89     * instead of this one [WebModuleDefinitionManager.class]=new class to use.
90     *
91     * @return WebModuleDefinitionManager - default instance
92     * @throws OSSException - an error has occured
93     */

94    public static WebModuleDefinitionManager getInstance(
95    ) throws OSSException
96    {
97       if (s_defaultInstance == null)
98       {
99          synchronized (IMPL_LOCK)
100          {
101             if (s_defaultInstance == null)
102             {
103                setInstance((WebModuleDefinitionManager)
104                         ClassFactory.getInstance().createInstance(
105                                    WebModuleDefinitionManager.class,
106                                    WebModuleDefinitionManager.class));
107             }
108          }
109          
110       }
111       return s_defaultInstance;
112    }
113
114    /**
115     * Set default instance. This instance will be returned by getInstance
116     * method until it is changed.
117     *
118     * @param defaultInstance - new default instance
119     * @see #getInstance
120     */

121    public static void setInstance(
122       WebModuleDefinitionManager defaultInstance
123    )
124    {
125       if (GlobalConstants.ERROR_CHECKING)
126       {
127          assert defaultInstance != null : "Default instance cannot be null";
128       }
129       
130       synchronized (IMPL_LOCK)
131       {
132          s_defaultInstance = defaultInstance;
133          s_logger.fine("Default module definition manager is "
134                        + s_defaultInstance.getClass().getName());
135       }
136    }
137    
138    /**
139     * @return Map - Key is a web module name, value is ModuleDefinition object
140     * describing complete set information for this web module.
141     */

142    public Map JavaDoc getWebModuleDefinitions()
143    {
144       return m_mapWebModules;
145    }
146
147    /**
148     * Add new web module definition to the cache.
149     *
150     * @param moduleDefinition - web module definition to add.
151     */

152    public void add(
153       WebModule moduleDefinition
154    )
155    {
156       if (GlobalConstants.ERROR_CHECKING)
157       {
158          assert moduleDefinition != null : "Cannot add null definition.";
159       }
160       
161       WebModule existingDef;
162       
163       existingDef = (WebModule)m_mapWebModules.get(
164                                            moduleDefinition.getName());
165       if (existingDef == null)
166       {
167          m_mapWebModules.put(moduleDefinition.getName(), moduleDefinition);
168          s_logger.log(Level.FINEST, "Web module definition for "
169                       + moduleDefinition.getName()
170                       + " added to the module definition manager.");
171       }
172    }
173    
174    /**
175     * Get web module name from URL.
176     *
177     * @param strURL - URL the module name has to be found for
178     * @return - module name for specified URL
179     */

180    public String JavaDoc getModuleNameFromURL(
181       String JavaDoc strURL
182    )
183    {
184       String JavaDoc strModuleNameReturn = null;
185
186       if ((m_mapWebModules != null) && (!m_mapWebModules.isEmpty()))
187       {
188          Iterator JavaDoc itDefinitions;
189          WebModule moduleDefinition;
190          
191          for (itDefinitions = m_mapWebModules.values().iterator();
192              itDefinitions.hasNext();)
193          {
194             moduleDefinition = (WebModule)itDefinitions.next();
195             // if strURL starts with url separator then remove it from there
196
if (strURL.startsWith(String.valueOf(WebCommonConstants.URL_SEPARATOR)))
197             {
198                strURL = strURL.substring(WebCommonConstants.URL_SEPARATOR.length());
199             }
200             
201             // we want to do starts with (not equals) because there can be
202
// something like activetab or other attributes attached
203
if (strURL.startsWith(moduleDefinition.getURL()))
204             {
205                // set up active module name into the return parameter
206
strModuleNameReturn = moduleDefinition.getName();
207             }
208          }
209       }
210       
211       return strModuleNameReturn;
212    }
213 }
214
Popular Tags