1 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 ; 57 58 import javax.servlet.ServletContext ; 59 import java.util.Vector ; 60 import org.jahia.bin.Jahia; 61 import org.jahia.services.cache.CacheFactory; 62 import org.jahia.services.cache.Cache; 63 64 65 71 public class JahiaApplicationContextBaseService extends JahiaApplicationContextService { 72 73 74 private static org.apache.log4j.Logger logger = 75 org.apache.log4j.Logger.getLogger (JahiaApplicationContextBaseService.class); 76 77 private static final String REGISTRY_CACHE_NAME = "ApplicationContextCache"; 78 79 80 private static EntityResolver mResolver; 81 82 83 private static final String WEB_XML_FILE = "/WEB-INF/web.xml"; 84 85 86 private static JahiaApplicationContextBaseService instance = null; 87 88 89 private Cache mRegistry; 90 91 92 private ServletContext mContext; 93 94 95 99 protected JahiaApplicationContextBaseService () { 100 logger.debug ("***** Starting the Jahia Application Context Manager Base Service *****"); 101 } 102 103 104 107 public static synchronized JahiaApplicationContextBaseService getInstance () { 108 if (instance == null) { 109 instance = new JahiaApplicationContextBaseService (); 110 } 111 return instance; 112 } 113 114 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 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 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 166 public ApplicationContext getApplicationContext (String 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 appContext = loadContextInfoFromDisk (context); 180 } 181 if (appContext == null) { 182 appContext = new ApplicationContext (context); 184 } 185 mRegistry.put (context, appContext); 186 } 187 return appContext; 188 } 189 190 public void removeContextFromCache(String context) { 191 mRegistry.remove(context); 192 } 193 194 203 private ApplicationContext loadContextInfoFromDisk (String context) throws JahiaException { 204 205 ServletContext 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 path = dispatchedContext.getRealPath (WEB_XML_FILE); 218 logger.debug ("dispatched context real Path : " + path); 219 220 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 (), 229 webXmlDoc.getServletMappings (), 230 new Vector (), 231 webXmlDoc.getWelcomeFiles ()); 232 233 Vector 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 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 |