KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > JahiaApplicationContextBaseService


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 18-JUN-2002, Jahia Solutions Sarl, Khue Nguyen: Initial version
37  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.services.applications;
44
45
46 import org.jahia.data.applications.ApplicationBean;
47 import org.jahia.data.applications.ApplicationContext;
48 import org.jahia.data.applications.ServletBean;
49 import org.jahia.data.webapps.Security_Role;
50 import org.jahia.data.webapps.Servlet_Element;
51 import org.jahia.data.webapps.Web_App_Xml;
52 import org.jahia.exceptions.JahiaException;
53 import org.jahia.exceptions.JahiaInitializationException;
54 import org.jahia.registries.ServicesRegistry;
55 import org.jahia.settings.SettingsBean;
56 import org.xml.sax.EntityResolver JavaDoc;
57
58 import javax.servlet.ServletContext JavaDoc;
59 import java.util.Vector JavaDoc;
60 import org.jahia.bin.Jahia;
61 import org.jahia.services.cache.CacheFactory;
62 import org.jahia.services.cache.Cache;
63
64
65 /**
66  * Application context manager service.
67  *
68  * @author Khue Nguyen <a HREF="mailto:khue@jahia.com">khue@jahia.com</a>
69  * @version 1.0
70  */

71 public class JahiaApplicationContextBaseService extends JahiaApplicationContextService {
72
73     /** logging */
74     private static org.apache.log4j.Logger logger =
75             org.apache.log4j.Logger.getLogger (JahiaApplicationContextBaseService.class);
76
77     private static final String JavaDoc REGISTRY_CACHE_NAME = "ApplicationContextCache";
78
79     /** the DTD Entity Resolver we use with local dtd files * */
80     private static EntityResolver JavaDoc mResolver;
81
82     /** The web.xml file * */
83     private static final String JavaDoc WEB_XML_FILE = "/WEB-INF/web.xml";
84
85     /** the instance * */
86     private static JahiaApplicationContextBaseService instance = null;
87
88     /** the cache of application context beans * */
89     private Cache mRegistry;
90
91     /** The Server ServerContext * */
92     private ServletContext JavaDoc mContext;
93
94
95     //--------------------------------------------------------------------------
96
/**
97      * constructor
98      */

99     protected JahiaApplicationContextBaseService () {
100         logger.debug ("***** Starting the Jahia Application Context Manager Base Service *****");
101     }
102
103
104     /**
105      * return the singleton instance
106      */

107     public static synchronized JahiaApplicationContextBaseService getInstance () {
108         if (instance == null) {
109             instance = new JahiaApplicationContextBaseService ();
110         }
111         return instance;
112     }
113
114     //--------------------------------------------------------------------------
115
/**
116      * Services initializations
117      *
118      * @param jSettings
119      */

120     public void init (SettingsBean jSettings)
121             throws JahiaInitializationException {
122         if (!isInitialized ()) {
123             mRegistry = CacheFactory.createCache(REGISTRY_CACHE_NAME);
124             mContext = Jahia.getStaticServletConfig().getServletContext();
125             mResolver = jSettings.getDtdEntityResolver ();
126             mIsServiceInitialized = true;
127         }
128     }
129
130     //--------------------------------------------------------------------------
131
/**
132      * Get an ApplicationContext for a given application id
133      *
134      * @param id , the application id
135      *
136      * @return the application context , null if not found
137      */

138     public ApplicationContext getApplicationContext (int id)
139             throws JahiaException {
140
141         logger.debug ("Requested application : [" + id + "]");
142
143         ApplicationBean appBean = ServicesRegistry.getInstance ()
144                 .getJahiaApplicationsManagerService ()
145                 .getApplication (id);
146         if (appBean == null) {
147             String JavaDoc errMsg = "Error getting application bean for application [" + id + "]";
148             logger.debug (errMsg);
149
150             throw new JahiaException (errMsg, errMsg,
151                     JahiaException.ERROR_SEVERITY,
152                     JahiaException.APPLICATION_ERROR);
153         }
154
155         return getApplicationContext (appBean.getContext ());
156     }
157
158     //--------------------------------------------------------------------------
159
/**
160      * Get an ApplicationContext for a given context
161      *
162      * @param context , the context
163      *
164      * @return the application context , null if not found
165      */

166     public ApplicationContext getApplicationContext (String JavaDoc context)
167             throws JahiaException {
168
169         logger.debug ("Requested for context : " + context);
170
171         if (context == null) {
172             return null;
173         }
174         ApplicationContext appContext = null;
175         synchronized (mRegistry) {
176             appContext = (ApplicationContext) mRegistry.get (context);
177             if (appContext == null) {
178                 // try to load from disk
179
appContext = loadContextInfoFromDisk (context);
180             }
181             if (appContext == null) {
182                 // create a fake Application Context to avoid loading from disk the next time.
183
appContext = new ApplicationContext (context);
184             }
185             mRegistry.put (context, appContext);
186         }
187         return appContext;
188     }
189
190     public void removeContextFromCache(String JavaDoc context) {
191         mRegistry.remove(context);
192     }
193
194     //--------------------------------------------------------------------------
195
/**
196      * Returns a ApplicationContext bean with the information loaded from the web.xml
197      * file of a given context
198      *
199      * @param context , the context
200      *
201      * @return an ApplicationContext or null on error
202      */

203     private ApplicationContext loadContextInfoFromDisk (String JavaDoc context) throws JahiaException {
204
205         ServletContext JavaDoc dispatchedContext = mContext.getContext (context);
206         if (dispatchedContext == null) {
207             logger.debug ("Error getting dispatch context [" +
208                     context + "]");
209
210             throw new JahiaException ("Can't access context " +
211                     context,
212                     "Error getting request context " + context,
213                     JahiaException.ERROR_SEVERITY,
214                     JahiaException.APPLICATION_ERROR);
215         }
216
217         String JavaDoc path = dispatchedContext.getRealPath (WEB_XML_FILE);
218         logger.debug ("dispatched context real Path : " + path);
219
220         // extract data from the web.xml file
221
ApplicationContext appContext = null;
222         Web_App_Xml webXmlDoc = new Web_App_Xml (path);
223         webXmlDoc.extractDocumentData ();
224
225         appContext = new ApplicationContext (context,
226                 webXmlDoc.getDisplayName (),
227                 webXmlDoc.getdesc (),
228                 new Vector JavaDoc (),
229                 webXmlDoc.getServletMappings (),
230                 new Vector JavaDoc (),
231                 webXmlDoc.getWelcomeFiles ());
232
233         Vector JavaDoc servlets = webXmlDoc.getServlets ();
234         Servlet_Element servlet = null;
235         ServletBean servletBean = null;
236         for (int i = 0; i < servlets.size (); i++) {
237             servlet = (Servlet_Element) servlets.get (i);
238             servletBean = new ServletBean (
239                     servlet.getType (),
240                     servlet.getDisplayName (),
241                     servlet.getName (),
242                     servlet.getSource (),
243                     context,
244                     servlet.getdesc ()
245             );
246             appContext.addServlet (servletBean);
247         }
248
249         Vector JavaDoc roles = webXmlDoc.getRoles ();
250         Security_Role role = null;
251         for (int i = 0; i < roles.size (); i++) {
252             role = (Security_Role) roles.get (i);
253             appContext.addRole (role.getName ());
254         }
255
256         return appContext;
257     }
258
259 }
260
Popular Tags