/** * Create new ConfigLoader using supplied filename and config. */ public ConfigLoader(String configFileName, Config config) throws ServletException { this.config = config; this.configFileName = configFileName; if (config.getServletContext().getRealPath(configFileName) != null) { this.configFile = new File(config.getServletContext().getRealPath(configFileName)); } state = loadConfig(); }
public DefaultFactory(Config config) { super(config); configFileName = config.getServletContext().getInitParameter("sitemesh.configfile"); if (configFileName == null) { configFileName = DEFAULT_CONFIG_FILENAME; } // configFilePath is null if loaded from war file String initParamConfigFile = config.getConfigFile(); if(initParamConfigFile != null) { configFileName = initParamConfigFile; } String configFilePath = config.getServletContext().getRealPath(configFileName); if (configFilePath != null) { // disable config auto reloading for .war files configFile = new File(configFilePath); } loadConfig(); }
private void loadExcludes() throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputStream is = null; if (excludesFile == null) { is = config.getServletContext().getResourceAsStream(excludesFileName); } else if (excludesFile.exists() && excludesFile.canRead()) { is = excludesFile.toURL().openStream(); } if (is == null){ throw new IllegalStateException("Cannot load excludes configuration file \"" + excludesFileName + "\" as specified in \"sitemesh.xml\" or \"sitemesh-default.xml\""); } Document document = builder.parse(is); Element root = document.getDocumentElement(); NodeList sections = root.getChildNodes(); // Loop through child elements of root node looking for the <excludes> block for (int i = 0; i < sections.getLength(); i++) { if (sections.item(i) instanceof Element) { Element curr = (Element)sections.item(i); if ("excludes".equalsIgnoreCase(curr.getTagName())) { loadExcludeUrls(curr.getChildNodes()); } } } }
private Element loadSitemeshXML() throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputStream is = null; if (configFile == null) { is = config.getServletContext().getResourceAsStream(configFileName); } else if (configFile.exists() && configFile.canRead()) { is = configFile.toURL().openStream(); } if (is == null){ // load the default sitemesh configuration is = getClass().getClassLoader().getResourceAsStream("com/opensymphony/module/sitemesh/factory/sitemesh-default.xml"); } if (is == null){ // load the default sitemesh configuration using another classloader is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/opensymphony/module/sitemesh/factory/sitemesh-default.xml"); } if (is == null){ throw new IllegalStateException("Cannot load default configuration from jar"); } if (configFile != null) configLastModified = configFile.lastModified(); Document doc = builder.parse(is); Element root = doc.getDocumentElement(); // Verify root element if (!"sitemesh".equalsIgnoreCase(root.getTagName())) { throw new FactoryException("Root element of sitemesh configuration file not <sitemesh>", null); } return root; }
document = builder.parse(configFile); } else { document = builder.parse(config.getServletContext().getResourceAsStream(configFileName));
/** * Entry-point for obtaining singleton instance of Factory. The default factory class * that will be instantiated can be overridden with the environment * entry <code>sitemesh.factory</code>. */ public static Factory getInstance(Config config) { Factory instance = (Factory)config.getServletContext().getAttribute(SITEMESH_FACTORY); if (instance == null) { String factoryClass = getEnvEntry("sitemesh.factory", "com.opensymphony.module.sitemesh.factory.DefaultFactory"); try { Class cls = ClassLoaderUtil.loadClass(factoryClass, config.getClass()); Constructor con = cls.getConstructor(new Class[] { Config.class }); instance = (Factory)con.newInstance(new Config[] { config }); config.getServletContext().setAttribute(SITEMESH_FACTORY, instance); } catch (InvocationTargetException e) { throw new FactoryException("Cannot construct Factory : " + factoryClass, e.getTargetException()); } catch (Exception e) { throw new FactoryException("Cannot construct Factory : " + factoryClass, e); } } instance.refresh(); return instance; }
resourcePath = config.getServletContext().getResource('/' + name); String filePath = config.getServletContext().getRealPath(name);
public Decorator getDecorator(HttpServletRequest request, Page page) { try { Decorator result = null; final Decorator d = super.getDecorator(request, page); String path = modifyPath(d.getPage(), getExt(request.getHeader("Accept-Language"))); File decFile = new File(config.getServletContext().getRealPath(path)); if (decFile.isFile()) { result = new DefaultDecorator(d.getName(), path, null) { public String getInitParameter(String paramName) { return d.getInitParameter(paramName); } }; } return result == null ? super.getDecorator(request, page) : result; } catch (NullPointerException e) { return super.getDecorator(request, page); } }
public Decorator getDecorator(HttpServletRequest request, Page page) { try { Decorator result = null; final Decorator d = super.getDecorator(request, page); String path = modifyPath(d.getPage(), getExt(request.getHeader("User-Agent"))); File decFile = new File(config.getServletContext().getRealPath(path)); if (decFile.isFile()) { result = new DefaultDecorator(d.getName(), path, null) { public String getInitParameter(String paramName) { return d.getInitParameter(paramName); } }; } return result == null ? super.getDecorator(request, page) : result; } catch (NullPointerException e) { return super.getDecorator(request, page); } }