Package org.apache.cocoon.environment

Examples of org.apache.cocoon.environment.Source


*/
public class ResourceExistsAction extends ComposerAction implements ThreadSafe {

    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception {
        String urlstring = parameters.getParameter("url",null);
        Source src = null;
        try {
            src = resolver.resolve(urlstring);
            src.getInputStream();
        } catch (Exception e) {
            getLogger().debug("ResourceExistsAction: exception: ",e);
            return null;
        } finally {
            if (src != null) src.recycle();
        }
        return EMPTY_MAP;
    }
View Full Code Here


            long key = HashUtil.hash("CA(" +
                                     this.rootElement.prefix +
                                     ':' +
                                     this.rootElement.name +
                                     '<' + this.rootElement.namespace + ">)");
            Source current;
            for (int i = 0; i < this.parts.size(); i++) {
                final Part part = (Part)this.parts.get(i);
                current = part.source;
                if (current.getLastModified() == 0) {
                        return 0;
                } else {
                    if (part.element == null) {
                        key += HashUtil.hash("P=" +
                                         part.stripRootElement + ':' +
                                         current.getSystemId() + ';');
                    } else {
                        key += HashUtil.hash("P=" +
                                         part.element.prefix +
                                         ':' +
                                         part.element.name +
                                         '<' + part.element.namespace + ">:" +
                                         part.stripRootElement + ':' +
                                         current.getSystemId() + ';');
                    }
                }
            }
            return key;
        } catch (Exception e) {
View Full Code Here

     *         component is currently not cacheable.
     */
    public CacheValidity generateValidity() {
        try {
            AggregatedCacheValidity v = new AggregatedCacheValidity();
            Source current;
            for (int i = 0; i < this.parts.size(); i++) {
                current = ((Part)this.parts.get(i)).source;
                if (current.getLastModified() == 0) {
                        return null;
                } else {
                    v.add(new TimeStampCacheValidity(current.getLastModified()));
                }
            }
            return v;
        } catch (Exception e) {
            getLogger().error("ContentAggregator: could not generateKey", e);
View Full Code Here

        if (httpResponse == null || httpRequest == null || httpContext == null) {
            throw new ProcessingException("JSPReader can be used only in a Servlet/JSP environment");
        }

        JSPEngine engine = null;
        Source src = null;
        try {
            // KP: A hacky way of source resolving.
            // Why context:// protocol returns not a string in URL format,
            // but a system-dependent path with 'file:' prefix?
            String contextDir = new File(httpContext.getRealPath("/")).toURL().toExternalForm();
            src = this.resolver.resolve(this.source);
            String url = src.getSystemId();
            if(url.startsWith(contextDir)) {
                // File is located under contextDir, using relative file name
                url = url.substring(contextDir.length());
            }
            if (url.startsWith("file:")) {
                // we need a relative path
                url = url.substring(5);
            }

            getLogger().debug("JSPReader executing JSP:" + url);

            engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE);
            byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext);

            byte[] buffer = new byte[8192];
            int length = -1;

            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            while ((length = bais.read(buffer)) > -1) {
                out.write(buffer, 0, length);
            }
            bais.close();
            bais = null;
            out.flush();
            //
        } catch (ServletException e) {
            getLogger().debug("ServletException in JSPReader.generate()", e);
            getLogger().debug("Embedded ServletException JSPReader.generate()", e.getRootCause());
            throw new ProcessingException("ServletException in JSPReader.generate()",e.getRootCause());
        } catch (IOException e) {
            getLogger().debug("IOException in JSPReader.generate()", e);
            throw new ProcessingException("IOException JSPReader.generate()",e);
        } catch (ProcessingException e) {
            throw e;
        } catch (Exception e) {
            getLogger().debug("Exception in JSPReader.generate()", e);
            throw new ProcessingException("Exception JSPReader.generate()",e);
        } finally {
            if (src != null) src.recycle();
            if (engine != null) this.manager.release(engine);
        }
    }
View Full Code Here

        Logicsheet logicsheet = (Logicsheet)logicsheetCache.get(CACHE_PREFIX + logicsheetLocation);
        if (logicsheet == null) {
            // Logicsheet is reusable (across multiple XSPs) object,
            // and it is resolved via urlResolver, and not via per-request
            // temporary resolver.
            Source inputSource = this.urlResolver.resolve(logicsheetLocation);

            // Resolver (local) could not be used as it is temporary
            // (per-request) object, yet Logicsheet is being cached and reused
            // across multiple requests. "Global" url-factory-based resolver
            // passed to the Logicsheet.
View Full Code Here

            throw new ConfigurationException("The form descriptor is not set!");
        }

        synchronized (XSPFormValidatorHelper.configurations) {
            conf = (ConfigurationHelper) XSPFormValidatorHelper.configurations.get(descriptor);
            Source source = null;
            SourceHandler sourceHandler = null;
            try {
                sourceHandler = (SourceHandler) manager.lookup(SourceHandler.ROLE);
                source = sourceHandler.getSource(null, descriptor);

                if (conf == null || ( reloadable && conf.lastModified < source.getLastModified())) {
                    logger.debug("XSPFormValidatorHelper.getConfiguration: (Re)Loading " + descriptor);

                    if (conf == null)
                    conf = new ConfigurationHelper();

                    SAXConfigurationHandler builder = new SAXConfigurationHandler();
                    source.toSAX(builder);

                    conf.lastModified = source.getLastModified();
                    conf.configuration = builder.getConfiguration();

                    XSPFormValidatorHelper.cacheConfiguration(descriptor, conf);
                } else {
                    logger.debug("XSPFormValidatorHelper.getConfiguration: Using cached configuration for " + descriptor);
                }
            } catch (Exception e) {
                logger.error("XSPFormValidatorHelper.getConfiguration: Could not configure Database mapping environment", e);
                throw new ConfigurationException("Error trying to load configurations for resource: " + source.getSystemId());
            } finally {
                if (source != null) source.recycle();
                manager.release(sourceHandler);
            }
        }

        return conf.configuration;
View Full Code Here

                XMLResourceBundleFactory.ConfigurationKeys.ROOT_DIRECTORY,
                "location"
            );

        debug("catalog location:" + location);
        Source source = resolver.resolve(location);
        try {
            String systemId = source.getSystemId();
            if (!systemId.startsWith(FILE)) {
                throw new ResourceNotFoundException(
                    systemId + " does not denote a directory"
                );
            }
            debug("catalog directory:" + systemId);
            dirConf.setValue(systemId);
            configuration.addChild(dirConf);
        } finally {
            source.recycle();
        }

        // Pass created configuration object to the factory
        factory.configure(configuration);
        debug("configured");
View Full Code Here

                                  String markupLanguageName,
                                  String programmingLanguageName,
                                  SourceResolver resolver)
        throws Exception {

        final Source source = resolver.resolve(fileName);
        final String id = source.getSystemId();

        ProgrammingLanguage programmingLanguage = null;
        MarkupLanguage markupLanguage = null;
        try {
            // Create file name for the program generated from the provided source.
            final String normalizedName = getNormalizedName(id);

            // Ensure no 2 requests for the same file overlap
            Program program = null;
            CompiledComponent programInstance = null;

            // Attempt to load program object from cache
            try {
                programInstance = (CompiledComponent)this.cache.select(normalizedName);
            } catch (Exception e) {
                getLogger().debug("The instance was not accessible from the internal cache. Proceeding.");
            }

            if (programInstance == null && this.preload) {
                // Preloading: Load program if its source/[object file] is available
                try {
                    markupLanguage =
                            (MarkupLanguage)this.markupSelector.select(markupLanguageName);
                    programmingLanguage =
                            (ProgrammingLanguage)this.languageSelector.select(programmingLanguageName);
                    programmingLanguage.setLanguageName(programmingLanguageName);
                    program = programmingLanguage.load(normalizedName,
                            this.workDir, markupLanguage.getEncoding());

                    this.cache.addGenerator(newManager, normalizedName, program);
                    programInstance = (CompiledComponent)this.cache.select(normalizedName);
                } catch (Exception e) {
                    getLogger().debug("The program was not preloaded");
                }
            }

            /*
             * FIXME: It's the program (not the instance) that must
             * be queried for changes!!!
             */
            if (programInstance != null && this.autoReload) {
                // Autoreloading: Unload program if its source is modified
                long lastModified = source.getLastModified();
                if (lastModified == 0 || programInstance.modifiedSince(lastModified)) {
                    // Release the component.
                    release(programInstance);
                    programInstance = null;

                    // Unload program
                    if (programmingLanguage == null) {
                        programmingLanguage =
                                (ProgrammingLanguage)this.languageSelector.select(programmingLanguageName);
                        programmingLanguage.setLanguageName(programmingLanguageName);
                    }

                    programmingLanguage.unload(program, normalizedName, this.workDir);
                    this.cache.removeGenerator(normalizedName);
                    program = null;
                }
            }

            // Not preloaded or just unloaded: (re)create.
            if (programInstance == null) {
                if (programmingLanguage == null) {
                    programmingLanguage =
                            (ProgrammingLanguage)this.languageSelector.select(programmingLanguageName);
                    programmingLanguage.setLanguageName(programmingLanguageName);
                }
                if (markupLanguage == null) {
                    markupLanguage =
                            (MarkupLanguage)this.markupSelector.select(markupLanguageName);
                }
                programInstance = this.createResource(newManager,
                        source, normalizedName, markupLanguage,
                        programmingLanguage, resolver);
            }

            return programInstance;
        } finally {
            source.recycle();
            this.markupSelector.release(markupLanguage);
            this.languageSelector.release(programmingLanguage);
        }
    }
View Full Code Here

                getLogger().debug("Base URI: null");
            else
                getLogger().debug("Base URI: " + current_xmlbase_uri.getSystemId());
        }

        Source url = null;
        String suffix;
        try {
            int index = href.indexOf('#');
            if (index < 0) {
                if(current_xmlbase_uri == null)
                    url = this.resolver.resolve(href);
                else
                    url = this.resolver.resolve(current_xmlbase_uri.getSystemId() + href);
                suffix = "";
            } else {
                if(current_xmlbase_uri == null)
                    url = this.resolver.resolve(href.substring(0,index));
                else
                    url = this.resolver.resolve(current_xmlbase_uri.getSystemId() + href.substring(0,index));
                suffix = href.substring(index+1);
            }
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("URL: "+url+"\nSuffix: "+suffix);
            }

            if (parse.equals("text")) {
                getLogger().debug("Parse type is text");
                InputStream input = url.getInputStream();
                Reader reader = new BufferedReader(new InputStreamReader(input));
                int read;
                char ary[] = new char[1024];
                if (reader != null) {
                    while ((read = reader.read(ary)) != -1) {
                        super.characters(ary,0,read);
                    }
                    reader.close();
                }
            } else if (parse.equals("xml")) {
                getLogger().debug("Parse type is XML");
                Parser parser = null;
                try {
                    parser = (Parser)manager.lookup(Parser.ROLE);

                    InputSource input = url.getInputSource();

                    if (suffix.startsWith("xpointer(") && suffix.endsWith(")")) {
                        String xpath = suffix.substring(9,suffix.length()-1);
                        getLogger().debug("XPath is "+xpath);
                        Document document = parser.parseDocument(input);
                        NodeList list = processor.selectNodeList(document,xpath);
                        DOMStreamer streamer = new DOMStreamer(super.contentHandler,super.lexicalHandler);
                        int length = list.getLength();
                        for (int i=0; i<length; i++) {
                            streamer.stream(list.item(i));
                        }
                    } else {
                        IncludeXMLConsumer xinclude_handler = new IncludeXMLConsumer(super.contentHandler,super.lexicalHandler);
                        parser.parse(input, xinclude_handler);
                    }
                } catch(SAXException e) {
                    getLogger().error("Error in processXIncludeElement", e);
                    throw e;
                } catch(ProcessingException e) {
                    getLogger().error("Error in processXIncludeElement", e);
                    throw e;
                } catch(MalformedURLException e) {
                    getLogger().error("Error in processXIncludeElement", e);
                    throw e;
                } catch(IOException e) {
                    getLogger().error("Error in processXIncludeElement", e);
                    throw e;
                } catch(ComponentException e) {
                    getLogger().error("Error in processXIncludeElement", e);
                    throw new SAXException(e);
                } finally {
                    this.manager.release(parser);
                }
            }
        } finally {
            if (url != null) url.recycle();
        }
    }
View Full Code Here

            throw new ProcessingException("HttpServletRequest or HttpServletResponse or ServletContext object not available");
        }

        JSPEngine engine = null;
        Parser parser = null;
        Source src = null;
        try {
            src = this.resolver.resolve(this.source);
            String url = src.getSystemId();
            // Guarantee src parameter is a file
            if (!url.startsWith("file:/"))
                throw new IOException("Protocol not supported: " + url);

            url = url.substring(5);
            getLogger().debug("JspGenerator executing JSP:" + url);

            engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE);
            byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext);

            // explicitly specify bytestream encoding
            InputSource input = new InputSource(new ByteArrayInputStream(bytes));
            input.setEncoding("utf-8");

            // pipe the results into the parser
            parser = (Parser)this.manager.lookup(Parser.ROLE);
            parser.parse(input, this.xmlConsumer);
        } catch (ServletException e) {
            getLogger().debug("ServletException in JspGenerator.generate()", e);
            getLogger().debug("Embedded ServletException JspGenerator.generate()", e.getRootCause());
            throw new ProcessingException("ServletException in JspGenerator.generate()",e.getRootCause());
        } catch (SAXException e) {
            getLogger().debug("SAXException JspGenerator.generate()", e);
            getLogger().debug("Embedded SAXException JspGenerator.generate()", e.getException());
            throw new ProcessingException("SAXException JspGenerator.generate()",e.getException());
        } catch (IOException e) {
            getLogger().debug("IOException in JspGenerator.generate()", e);
            throw new ProcessingException("IOException JspGenerator.generate()",e);
        } catch (ProcessingException e) {
            throw e;
        } catch (Exception e) {
            getLogger().debug("Exception in JspGenerator.generate()", e);
            throw new ProcessingException("Exception JspGenerator.generate()",e);
        } finally {
            if (src != null) src.recycle();
            this.manager.release(parser);
            this.manager.release(engine);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.cocoon.environment.Source

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.