vgritsenko 2003/02/01 16:46:21 Modified: src/java/org/apache/cocoon Main.java Added: src/java/org/apache/cocoon/bean CocoonBean.java src/java/org/apache/cocoon/bean/destination Destination.java DestinationNotFoundException.java FileDestination.java NullDestination.java ThreadedDestination.java Log: Bug #15748: Refactor Main java into Main and CocoonBean Revision Changes Path 1.32 +63 -760 xml-cocoon2/src/java/org/apache/cocoon/Main.java Index: Main.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/Main.java,v retrieving revision 1.31 retrieving revision 1.32 diff -u -r1.31 -r1.32 --- Main.java 1 Feb 2003 23:57:20 -0000 1.31 +++ Main.java 2 Feb 2003 00:46:20 -0000 1.32 @@ -54,59 +54,28 @@ import org.apache.avalon.excalibur.cli.CLOption; import org.apache.avalon.excalibur.cli.CLOptionDescriptor; import org.apache.avalon.excalibur.cli.CLUtil; -import org.apache.avalon.excalibur.logger.DefaultLogKitManager; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; -import org.apache.avalon.framework.context.DefaultContext; -import org.apache.avalon.framework.logger.LogKitLogger; -import org.apache.cocoon.environment.Environment; -import org.apache.cocoon.environment.commandline.CommandlineContext; -import org.apache.cocoon.environment.commandline.FileSavingEnvironment; -import org.apache.cocoon.environment.commandline.LinkSamplingEnvironment; -import org.apache.cocoon.util.IOUtils; -import org.apache.cocoon.util.MIMEUtils; +import org.apache.cocoon.Constants; import org.apache.cocoon.util.NetUtils; -import org.apache.cocoon.ProcessingException; -import org.apache.cocoon.components.notification.SimpleNotifyingBean; -import org.apache.cocoon.components.notification.DefaultNotifyingBuilder; -import org.apache.cocoon.components.notification.Notifier; -import org.apache.log.Hierarchy; -import org.apache.log.Logger; -import org.apache.log.Priority; import java.io.BufferedReader; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FileReader; -import java.io.FileWriter; -import java.io.PrintWriter; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.TreeMap; +import java.util.ArrayList; + +import org.apache.cocoon.bean.CocoonBean; +import org.apache.cocoon.bean.destination.FileDestination; /** - * Command line entry point. + * Command line entry point. Parses command line, create Cocoon bean and invokes it + * with file destination. * * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a> * @version CVS $Id$ */ public class Main { - /** The <code>Logger</code> */ - private static Logger log = null; - protected static final int HELP_OPT = 'h'; protected static final int VERSION_OPT = 'v'; protected static final int VERBOSE_OPT = 'V'; @@ -124,9 +93,6 @@ protected static final int BROKEN_LINK_FILE = 'b'; protected static final int PRECOMPILE_OPT = 'P'; - protected static final String DEFAULT_USER_AGENT = Constants.COMPLETE_NAME; - protected static final String DEFAULT_ACCEPT = "text/html, */*"; - protected static final CLOptionDescriptor [] OPTIONS = new CLOptionDescriptor [] { new CLOptionDescriptor("brokenLinkFile", CLOptionDescriptor.ARGUMENT_REQUIRED, @@ -196,8 +162,6 @@ + " file (default is ${contextDir}/cocoon.xconf)") }; - protected static boolean verbose = false; - /** * <code>processFile</code> method. * @@ -235,20 +199,21 @@ String destDir = Constants.DEFAULT_DEST_DIR; String contextDir = Constants.DEFAULT_CONTEXT_DIR; String configFile = null; - File brokenLinkFile = null; + String brokenLinkFile = null; String workDir = Constants.DEFAULT_WORK_DIR; List targets = new ArrayList(); - CLArgsParser parser = new CLArgsParser(args, OPTIONS); String logKit = null; String logger = null; String logLevel = "DEBUG"; + String agentOptions = null; + String acceptOptions = null; boolean precompileOnly = false; - String userAgent = DEFAULT_USER_AGENT; - String accept = DEFAULT_ACCEPT; boolean followLinks = true; + boolean verbose = false; System.out.print("\nSetup..."); - + + CLArgsParser parser = new CLArgsParser(args, OPTIONS); List clOptions = parser.getArguments(); int size = clOptions.size(); @@ -269,11 +234,11 @@ break; case Main.VERSION_OPT: - printVersion(); + printVersion(CocoonBean.getVersion()); break; case Main.VERBOSE_OPT: - Main.verbose = true; + verbose = true; break; case Main.DEST_DIR_OPT: @@ -305,11 +270,11 @@ break; case Main.AGENT_OPT: - userAgent = option.getArgument(); + agentOptions = option.getArgument(); break; case Main.ACCEPT_OPT: - accept = option.getArgument(); + acceptOptions = option.getArgument(); break; case Main.URI_FILE: @@ -322,134 +287,81 @@ break; case Main.BROKEN_LINK_FILE: - brokenLinkFile = new File(option.getArgument()); + brokenLinkFile = option.getArgument(); break; } } - final Priority priority = Priority.getPriorityForName(logLevel); - Hierarchy.getDefaultHierarchy().setDefaultPriority(priority); - log = Hierarchy.getDefaultHierarchy().getLoggerFor(""); - + CocoonBean cocoon = new CocoonBean(workDir, contextDir, configFile); + cocoon.setLogKit(logKit); + cocoon.setLogger(logger); + cocoon.setLogLevel(logLevel); + if (agentOptions != null) { + cocoon.setAgentOptions(agentOptions); + } + if (acceptOptions != null) { + cocoon.setAcceptOptions(acceptOptions); + } + cocoon.setBrokenLinkFile(brokenLinkFile); + cocoon.setPrecompileOnly(precompileOnly); + cocoon.setFollowLinks(followLinks); + cocoon.setVerbose(verbose); + if (destDir.equals("")) { String error = "Careful, you must specify a destination dir when using the -d/--destDir argument"; - log.fatalError(error); + cocoon.getLogger().fatalError(error); System.out.println(error); System.exit(1); } - if (contextDir.equals("")) { + if (cocoon.getContextDir().equals("")) { String error = "Careful, you must specify a configuration file when using the -c/--contextDir argument"; - log.error(error); + cocoon.getLogger().fatalError(error); System.out.println(error); System.exit(1); } - if (workDir.equals("")) { + if (cocoon.getWorkDir().equals("")) { String error = "Careful, you must specify a destination dir when using the -w/--workDir argument"; - log.error(error); + cocoon.getLogger().fatalError(error); System.out.println(error); System.exit(1); } - if (targets.size() == 0 && !precompileOnly) { + if (targets.size() == 0 && !cocoon.isPrecompileOnly()) { String error = "Please, specify at least one starting URI."; - log.error(error); + cocoon.getLogger().fatalError(error); System.out.println(error); System.exit(1); } - try { - File dest = null; - if (!precompileOnly) { - dest = getDir(destDir, "destination"); - } - File work = getDir(workDir, "working"); - File context = getDir(contextDir, "context"); - File conf = null; - - if (null == configFile) { - conf = getConfigurationFile(context); - } else { - conf = new File(configFile); - if(!conf.exists()) - conf = new File(context, configFile); - } + cocoon.initialize(); + System.out.println(" done."); - DefaultContext appContext = new DefaultContext(); - appContext.put(Constants.CONTEXT_CLASS_LOADER, Main.class.getClassLoader()); - CommandlineContext clContext = new CommandlineContext(contextDir); - clContext.enableLogging(new LogKitLogger(log)); - appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, clContext); - DefaultLogKitManager logKitManager = null; - if (logKit != null) { - final FileInputStream fis = new FileInputStream(logKit); - final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); - final Configuration logKitConf = builder.build(fis); - logKitManager = new DefaultLogKitManager(Hierarchy.getDefaultHierarchy()); - logKitManager.setLogger(log); - final DefaultContext subcontext = new DefaultContext(appContext); - subcontext.put("context-root", contextDir); - logKitManager.contextualize(subcontext); - logKitManager.configure(logKitConf); - if (logger != null) { - log = logKitManager.getLogger(logger); - } else { - log = logKitManager.getLogger("cocoon"); - } - } else { - logKitManager = new DefaultLogKitManager(Hierarchy.getDefaultHierarchy()); - logKitManager.setLogger(log); - } - - appContext.put(Constants.CONTEXT_CLASSPATH, getClassPath(contextDir)); - appContext.put(Constants.CONTEXT_WORK_DIR, work); - appContext.put(Constants.CONTEXT_UPLOAD_DIR, contextDir + "upload-dir"); - File cacheDir = getDir(workDir + File.separator + "cache-dir", "cache"); - appContext.put(Constants.CONTEXT_CACHE_DIR, cacheDir); - appContext.put(Constants.CONTEXT_CONFIG_URL, conf.toURL()); - Cocoon c = new Cocoon(); - - c.enableLogging(new LogKitLogger(log)); - c.contextualize(appContext); - c.setLogKitManager(logKitManager); - c.initialize(); - Main main = new Main(c, context, dest, brokenLinkFile); - - main.userAgent = userAgent; - main.accept = accept; - main.followLinks = followLinks; - - System.out.println(" done."); - - System.out.print("Initializing..."); - main.warmup(); - - System.out.println(" ready, let's go :-)"); - if (main.process(targets, precompileOnly) == 0) { - main.recursivelyPrecompile(context, context); - } - - System.out.print("\ndisposing..."); - c.dispose(); - - System.out.print(" done.\n"); - log.info("Done"); - - } catch (Exception e) { - log.fatalError("Exception caught ", e); - System.exit(1); - } + System.out.print("Initializing..."); + cocoon.warmup(); + + System.out.println(" ready, let's go :-)"); + cocoon.process(targets, new FileDestination(destDir)); + + System.out.print("\ndisposing..."); + cocoon.dispose(); + + System.out.println(" done."); System.exit(0); } - /** Print the version string and exit */ - private static void printVersion() { - System.out.println(Constants.VERSION); + /** + * Print the version string and exit + */ + private static void printVersion(String version) { + System.out.println(version); System.exit(0); } - /** Print the usage message and exit */ + /** + * Print the usage message and exit + */ private static void printUsage() { String lSep = System.getProperty("line.separator"); StringBuffer msg = new StringBuffer(); @@ -464,613 +376,4 @@ System.out.println(msg.toString()); System.exit(0); } - - /** - * Check for a configuration file in a specific directory. - * - * @param dir a <code>File</code> where to look for configuration files - * @return a <code>File</code> representing the configuration - * @exception IOException if an error occurs - */ - private static File getConfigurationFile(File dir) throws IOException { - - if (log.isDebugEnabled()) { - log.debug("Trying configuration file at: " + dir + File.separator + Constants.DEFAULT_CONF_FILE); - } - File f = new File(dir, Constants.DEFAULT_CONF_FILE); - if (f.canRead()) { - return f; - } - - if (log.isDebugEnabled()) { - log.debug("Trying configuration file at: " + System.getProperty("user.dir") + File.separator + Constants.DEFAULT_CONF_FILE); - } - f = new File(System.getProperty("user.dir"), Constants.DEFAULT_CONF_FILE); - if (f.canRead()) { - return f; - } - - if (log.isDebugEnabled()) { - log.debug("Trying configuration file at: /usr/local/etc/" + Constants.DEFAULT_CONF_FILE); - } - f = new File("/usr/local/etc/", Constants.DEFAULT_CONF_FILE); - if (f.canRead()) { - return f; - } - - log.error("Could not find the configuration file."); - throw new FileNotFoundException("The configuration file could not be found."); - } - - /** - * Get a <code>File</code> representing a directory. - * - * @param dir a <code>String</code> with a directory name - * @param type a <code>String</code> describing the type of directory - * @return a <code>File</code> value - * @exception IOException if an error occurs - */ - private static File getDir(String dir, String type) throws IOException { - - if (log.isDebugEnabled()) { - log.debug("Getting handle to " + type + " directory '" + dir + "'"); - } - File d = new File(dir); - - if (!d.exists()) { - if (!d.mkdirs()) { - log.error("Error creating " + type + " directory '" + d + "'"); - throw new IOException("Error creating " + type + " directory '" + d + "'"); - } - } - - if (!d.isDirectory()) { - log.error("'" + d + "' is not a directory."); - throw new IOException("'" + d + "' is not a directory."); - } - - if (!(d.canRead() && d.canWrite())) { - log.error("Directory '" + d + "' is not readable/writable"); - throw new IOException("Directory '" + d + "' is not readable/writable"); - } - - return d; - } - - // ----------------------------------------------------------------------- - - private Cocoon cocoon; - private File destDir; - private File context; - private PrintWriter brokenLinkWriter; - private Map attributes; - private HashMap empty; - private Map allProcessedLinks; - private Map allTranslatedLinks; - private String userAgent; - private String accept; - private boolean followLinks; - - /** - * Creates the Main class - * @param cocoon a <code>Cocoon</code> instance - * @param context a <code>File</code> for the context directory - * @param destDir a <code>File</code> for the destination directory - */ - public Main(Cocoon cocoon, File context, File destDir, File brokenLinks) { - this.cocoon = cocoon; - this.context = context; - this.destDir = destDir; - if (brokenLinks != null) { - try { - this.brokenLinkWriter = new PrintWriter(new FileWriter(brokenLinks), true); - } catch (IOException ioe) { - log.error("File does not exist: " + brokenLinks.getAbsolutePath()); - } - } - this.attributes = new HashMap(); - this.empty = new HashMap(); - this.allProcessedLinks = new HashMap(); - this.allTranslatedLinks = new HashMap(); - } - - /** - * Warms up the engine by accessing the root. - * @exception Exception if an error occurs - */ - public void warmup() throws Exception { - //log.info(" [Cocoon might need to compile the sitemaps, this might take a while]"); - cocoon.generateSitemap(new LinkSamplingEnvironment("/", context, attributes, null, - new LogKitLogger(log))); - } - - /** - * Process the URI list and process them all independently. - * @param uris a <code>Collection</code> of URIs - * @param precompileOnly a <code>boolean</code> denoting to process XSP only - * @return an <code>int</code> value with the number of links processed - * @exception Exception if an error occurs - */ - private int process(Collection uris, boolean precompileOnly) throws Exception { - - int nCount = 0; - ArrayList links = new java.util.ArrayList(); - Iterator i = uris.iterator(); - String next; - while (i.hasNext()) { - next = (String)i.next(); - if (!links.contains(next)) { - links.add(next); - } - } - while (links.size() > 0) { - String url = (String)links.get(0); - - try { - if (allProcessedLinks.get(url) == null) { - if (precompileOnly) { - this.processXSP(url); - } else if (this.followLinks) { - i = processURI(url).iterator(); - while (i.hasNext()) { - next = (String)i.next(); - if (!links.contains(next)) { - links.add(next); - } - } - } else { - processURI(url); - } - } - } catch (ResourceNotFoundException rnfe) { - printBroken (url, rnfe.getMessage()); - } - - links.remove(url); - nCount++; - - if (log.isInfoEnabled()) { - log.info(" Memory used: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); - log.info(" Processed, Translated & Left: " + allProcessedLinks.size() + ", " + allTranslatedLinks.size() + ", " + links.size()); - } - } - - return nCount; - } - - /** - * Recurse the directory hierarchy and process the XSP's. - * @param contextDir a <code>File</code> value for the context directory - * @param file a <code>File</code> value for a single XSP file or a directory to scan recursively - */ - private void recursivelyPrecompile(File contextDir, File file) { - if (file.isDirectory()) { - String entries[] = file.list(); - for (int i = 0; i < entries.length; i++) { - recursivelyPrecompile(contextDir, new File(file, entries[i])); - } - } else if (file.getName().toLowerCase().endsWith(".xmap")) { - try { - this.processXMAP(IOUtils.getContextFilePath(contextDir.getCanonicalPath(),file.getCanonicalPath())); - } catch (Exception e){ - //Ignore for now. - } - } else if (file.getName().toLowerCase().endsWith(".xsp")) { - try { - this.processXSP(IOUtils.getContextFilePath(contextDir.getCanonicalPath(),file.getCanonicalPath())); - } catch (Exception e){ - //Ignore for now. - } - } - } - - /** - * Process a single XSP file - * - * @param uri a <code>String</code> pointing to an xsp URI - * @exception Exception if an error occurs - */ - private void processXSP(String uri) throws Exception { - String markupLanguage = "xsp"; - String programmingLanguage = "java"; - Environment env = new LinkSamplingEnvironment("/", context, attributes, null, - new LogKitLogger(log)); - cocoon.precompile(uri, env, markupLanguage, programmingLanguage); - } - - /** - * Process a single XMAP file - * - * @param uri a <code>String</code> pointing to an xmap URI - * @exception Exception if an error occurs - */ - private void processXMAP(String uri) throws Exception { - String markupLanguage = "sitemap"; - String programmingLanguage = "java"; - Environment env = new LinkSamplingEnvironment("/", context, attributes, null, - new LogKitLogger(log)); - cocoon.precompile(uri, env, markupLanguage, programmingLanguage); - } - - /** - * Processes the given URI and return all links. The algorithm is the following: - * - * <ul> - * <li>file name for the URI is generated. URI MIME type is checked for - * consistency with the URI and, if the extension is inconsistent - * or absent, the file name is changed</li> - * <li>the link view of the given URI is called and the file names for linked - * resources are generated and stored.</li> - * <li>for each link, absolute file name is translated to relative path.</li> - * <li>after the complete list of links is translated, the link-translating - * view of the resource is called to obtain a link-translated version - * of the resource with the given link map</li> - * <li>list of absolute URI is returned, for every URI which is not yet - * present in list of all translated URIs</li> - * </ul> - * @param uri a <code>String</code> URI to process - * @return a <code>Collection</code> containing all links found - * @exception Exception if an error occurs - */ - private Collection processURI(String uri) throws Exception { - System.out.print(" * "); - - // Get parameters, deparameterized URI and path from URI - final TreeMap parameters = new TreeMap(); - final String deparameterizedURI = NetUtils.deparameterize(uri, parameters); - final String path = NetUtils.getPath(uri); - final String suri = NetUtils.parameterize(deparameterizedURI, parameters); - parameters.put("user-agent", userAgent); - parameters.put("accept", accept); - - // Get file name from URI (without path) - String pageURI = deparameterizedURI; - if (pageURI.indexOf("/") != -1) { - pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1); - if (pageURI.length() == 0) { - pageURI = "./"; - } - } - - String filename = (String)allTranslatedLinks.get(suri); - if (filename == null) { - filename = mangle(suri); - final String type = getType(deparameterizedURI, parameters); - final String ext = NetUtils.getExtension(filename); - final String defaultExt = MIMEUtils.getDefaultExtension(type); - if ((ext == null) || (!ext.equals(defaultExt))) { - filename += defaultExt; - } - allTranslatedLinks.put(suri, filename); - } - // Store processed URI list to avoid eternal loop - allProcessedLinks.put(suri, filename); - - if ("".equals(filename)) { - return new ArrayList(); - } - - // Process links - final ArrayList absoluteLinks = new ArrayList(); - final HashMap translatedLinks = new HashMap(); - final Iterator i = this.getLinks(deparameterizedURI, parameters).iterator(); - - while (i.hasNext()) { - String link = (String) i.next(); - // Fix relative links starting with "?" - String relativeLink = link; - if (relativeLink.startsWith("?")) { - relativeLink = pageURI + relativeLink; - } - - String absoluteLink = NetUtils.normalize(NetUtils.absolutize(path, relativeLink)); - { - final TreeMap p = new TreeMap(); - absoluteLink = NetUtils.parameterize(NetUtils.deparameterize(absoluteLink, p), p); - } - String translatedAbsoluteLink = (String)allTranslatedLinks.get(absoluteLink); - if (translatedAbsoluteLink == null) { - try { - translatedAbsoluteLink = this.translateURI(absoluteLink); - log.info(" Link translated: " + absoluteLink); - allTranslatedLinks.put(absoluteLink, translatedAbsoluteLink); - absoluteLinks.add(absoluteLink); - } catch (ProcessingException pe) { - printBroken(absoluteLink, pe.getMessage()); - } - } - - // I have to add also broken links to the absolute links - // to be able to generate the "broken link" page - absoluteLinks.add(absoluteLink); - final String translatedRelativeLink = NetUtils.relativize(path, translatedAbsoluteLink); - translatedLinks.put(link, translatedRelativeLink); - } - - printInfo("["+translatedLinks.size()+"] "); - - try { - // Process URI - File file = IOUtils.createFile(destDir, NetUtils.decodePath(filename)); - OutputStream output = new BufferedOutputStream(new FileOutputStream(file)); - - try{ - getPage(deparameterizedURI, parameters, translatedLinks, output); - printlnInfo(uri); // (can also output type returned by getPage) - } catch(ProcessingException pe) { - printBroken(filename, DefaultNotifyingBuilder.getRootCause(pe).getMessage()); - output.close(); - resourceUnavailable(file, uri); - } finally { - try { - if (output != null) - output.close(); - } catch(IOException ioex) { - log.warn(ioex.toString()); - } - } - } catch (Exception rnfe) { - System.out.println("XXX" + rnfe); - log.warn("Could not process URI: " + deparameterizedURI); - if (verbose) System.out.println("Could not process URI: " + deparameterizedURI); - } - - return absoluteLinks; - } - - /** - * Translate an URI into a file name. - * - * @param uri a <code>String</code> value to map - * @return a <code>String</code> vlaue for the file - * @exception Exception if an error occurs - */ - private String translateURI(String uri) throws Exception { - if (null == uri || "".equals(uri)) { - log.warn("translate empty uri"); - if (verbose) System.out.println("translate empty uri"); - return ""; - } - HashMap parameters = new HashMap(); - parameters.put("user-agent", userAgent); - parameters.put("accept", accept); - String deparameterizedURI = NetUtils.deparameterize(uri, parameters); - - String filename = mangle(uri); - String type = getType(deparameterizedURI, parameters); - String ext = NetUtils.getExtension(filename); - String defaultExt = MIMEUtils.getDefaultExtension(type); - if ((ext == null) || (!ext.equals(defaultExt))) { - filename += defaultExt; - } - - return filename; - } - - /** - * Generate a <code>resourceUnavailable</code> message. - * - * @param file being unavailable - * @exception IOException if an error occurs - */ - private void resourceUnavailable(File file, String uri) throws IOException { - SimpleNotifyingBean n = new SimpleNotifyingBean(this); - n.setType("resource-not-found"); - n.setTitle("Resource not Found"); - n.setSource("Cocoon commandline (Main.java)"); - n.setMessage("Page Not Available."); - n.setDescription("The requested resource couldn't be found."); - n.addExtraDescription("request-uri", uri); - n.addExtraDescription("missing-file", file.toString()); - - PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file))); - Notifier.notify(n, out, "text/html"); - out.flush(); - out.close(); - } - - /** - * Mangle a URI. - * - * @param uri a URI to mangle - * @return a mangled URI - */ - private String mangle(String uri) { - if (log.isDebugEnabled()) { - log.debug("mangle(\"" + uri + "\")"); - } - if (uri.charAt(uri.length() - 1) == '/') { - uri += Constants.INDEX_URI; - } - uri = uri.replace('"', '\''); - uri = uri.replace('?', '_'); - uri = uri.replace(':', '_'); - if (log.isDebugEnabled()) { - log.debug(uri); - } - return uri; - } - - /** - * Samples an URI for its links. - * - * @param deparameterizedURI a <code>String</code> value of an URI to start sampling from - * @param parameters a <code>Map</code> value containing request parameters - * @return a <code>Collection</code> of links - * @exception Exception if an error occurs - */ - protected Collection getLinks(String deparameterizedURI, Map parameters) throws Exception { - LinkSamplingEnvironment env = new LinkSamplingEnvironment(deparameterizedURI, - context, - attributes, - parameters, - new LogKitLogger(log)); - processLenient(env); - return env.getLinks(); - } - - /** - * Processes an URI for its content. - * - * @param deparameterizedURI a <code>String</code> value of an URI to start sampling from - * @param parameters a <code>Map</code> value containing request parameters - * @param links a <code>Map</code> value - * @param stream an <code>OutputStream</code> to write the content to - * @return a <code>String</code> value for the content - * @exception Exception if an error occurs - */ - protected String getPage(String deparameterizedURI, Map parameters, Map links, OutputStream stream) throws Exception { - FileSavingEnvironment env = new FileSavingEnvironment(deparameterizedURI, - context, - attributes, - parameters, - links, - stream, - new LogKitLogger(log)); - // Here Cocoon can throw an exception if there are errors in processing the page - cocoon.process(env); - // if we get here, the page was created :-) - return env.getContentType(); - } - - /** Class <code>NullOutputStream</code> here. */ - static class NullOutputStream extends OutputStream - { - public void write(int b) throws IOException { } - public void write(byte b[]) throws IOException { } - public void write(byte b[], int off, int len) throws IOException { } - } - - /** - * Analyze the type of content for an URI. - * - * @param deparameterizedURI a <code>String</code> value to analyze - * @param parameters a <code>Map</code> value for the request - * @return a <code>String</code> value denoting the type of content - * @exception Exception if an error occurs - */ - protected String getType(String deparameterizedURI, Map parameters) throws Exception { - FileSavingEnvironment env = new FileSavingEnvironment(deparameterizedURI, - context, - attributes, - parameters, - empty, - new NullOutputStream(), - new LogKitLogger(log)); - processLenient(env); - return env.getContentType(); - } - - /** - * Try to process something but don't throw a ProcessingException. - * - * @param env the <code>Environment</code> to process - * @return boolean true if no error were cast, false otherwise - * @exception Exception if an error occurs, except RNFE - */ - private boolean processLenient(Environment env) throws Exception { - try{ - cocoon.process(env); - } catch(ProcessingException pe) { - return false; - } - - return true; - } - - /** - * Print the message of a broken link. - * - * @param url the <code>URL</code> of the broken link - * @param cause of the broken link - */ - private void printBroken(String url, String cause) { - int screenWidth = 67; - int causeWidth = screenWidth - 6; - - printlnInfo(""); - printlnInfo("-> [broken page] " + url +" <- "); - printlnInfo(""); - printInfo(" "); - - int causeLength = cause.length(), currentStart = -causeWidth, currentEnd = 0 ; - do { - currentStart += causeWidth; - currentEnd += causeWidth; - - if (currentEnd>causeLength) { - currentEnd=causeLength; - } - - printlnInfo(cause.substring(currentStart, currentEnd) ); - printInfo(" "); - } while(currentEnd < causeLength); - - printlnInfo(""); - - if (null != this.brokenLinkWriter) { - this.brokenLinkWriter.println(url); - } - } - - /** - * Print an info message. - * - * @param message the message to print - */ - private void printlnInfo (String message) { - log.info(message); - if (verbose) System.out.println(message); - } - - /** - * Print an info message. - * - * @param message the message to print - */ - private void printInfo (String message) { - log.info(message); - if (verbose) System.out.print(message); - } - - /** - * This builds the important ClassPath used by this class. It - * does so in a neutral way. - * It iterates in alphabetical order through every file in the - * lib directory and adds it to the classpath. - * - * Also, we add the files to the ClassLoader for the Cocoon system. - * In order to protect ourselves from skitzofrantic classloaders, - * we need to work with a known one. - * - * @param context The context path - * @return a <code>String</code> value - */ - protected static String getClassPath(final String context) { - StringBuffer buildClassPath = new StringBuffer(); - String classDir = context + "/WEB-INF/classes"; - File root = new File(context + "/WEB-INF/lib"); - - buildClassPath.append(classDir); - - if (root.isDirectory()) { - File[] libraries = root.listFiles(); - Arrays.sort(libraries); - for (int i = 0; i < libraries.length; i++) { - buildClassPath.append(File.pathSeparatorChar) - .append(IOUtils.getFullFilename(libraries[i])); - } - } - - buildClassPath.append(File.pathSeparatorChar) - .append(System.getProperty("java.class.path")); - -// buildClassPath.append(File.pathSeparatorChar) -// .append(getExtraClassPath(context)); - - if (log.isDebugEnabled()) { - log.debug("Context classpath: " + buildClassPath.toString()); - } - return buildClassPath.toString(); - } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/CocoonBean.java Index: CocoonBean.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean; import org.apache.cocoon.util.NetUtils; import org.apache.cocoon.util.IOUtils; import org.apache.cocoon.util.MIMEUtils; import org.apache.cocoon.Constants; import org.apache.cocoon.Cocoon; import org.apache.cocoon.ResourceNotFoundException; import org.apache.cocoon.Main; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.components.notification.SimpleNotifyingBean; import org.apache.cocoon.components.notification.Notifier; import org.apache.cocoon.components.notification.DefaultNotifyingBuilder; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.environment.commandline.CommandlineContext; import org.apache.cocoon.environment.commandline.LinkSamplingEnvironment; import org.apache.cocoon.environment.commandline.FileSavingEnvironment; import org.apache.cocoon.bean.destination.Destination; import org.apache.avalon.excalibur.logger.DefaultLogKitManager; import org.apache.avalon.framework.context.DefaultContext; import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.logger.LogKitLogger; import org.apache.log.Priority; import org.apache.log.Hierarchy; import org.apache.log.Logger; import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileInputStream; import java.io.FileWriter; import java.io.OutputStream; import java.io.PrintStream; import java.util.Map; import java.util.HashMap; import java.util.TreeMap; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Arrays; import java.util.List; /** * The Cocoon Bean simplifies usage of the Cocoon object. Allows to create, configure Cocoon * instance and process requests, one by one or multiple with link traversal. * * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a> * @version CVS $Id: CocoonBean.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ */ public class CocoonBean { protected static final String DEFAULT_USER_AGENT = Constants.COMPLETE_NAME; protected static final String DEFAULT_ACCEPT = "text/html, */*"; // User Supplied Parameters private String contextDir = null; private String configFile = null; private String brokenLinkFileName = null; private String workDir = null; private String logKit = null; private String logger = null; private String userAgent = DEFAULT_USER_AGENT; private String accept = DEFAULT_ACCEPT; private boolean followLinks = true; private boolean precompileOnly = false; private boolean confirmExtension = true; // Objects used alongside User Supplied Parameters private File context; private File work; private File conf; private PrintWriter brokenLinkWriter; // Internal Objects private Cocoon cocoon; private Destination dest; private static Logger log; private Map attributes; private HashMap empty; private Map allProcessedLinks; private Map allTranslatedLinks; private boolean initialized; private boolean verbose; // // CONSTRUCTORS AND INITIALISATION METHODS // public CocoonBean(String workingDir, String contextDir) throws IOException { this(workingDir, contextDir, null); } public CocoonBean(String workingDir, String contextDir, String configFile) throws IOException { this.contextDir = contextDir; this.workDir = workingDir; this.configFile = configFile; setLogLevel("DEBUG"); this.context = getDir(this.contextDir, "context"); this.work = getDir(workDir, "working"); this.conf = null; if (null == this.configFile) { this.conf = getConfigurationFile(context); } else { this.conf = new File(this.configFile); if (!conf.exists()) { this.conf = new File(context, this.configFile); } } } public void initialize() throws Exception { try { DefaultContext appContext = new DefaultContext(); appContext.put(Constants.CONTEXT_CLASS_LOADER, Main.class.getClassLoader()); CommandlineContext clContext = new CommandlineContext(contextDir); clContext.enableLogging(new LogKitLogger(log)); appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, clContext); DefaultLogKitManager logKitManager = null; if (logKit != null) { final FileInputStream fis = new FileInputStream(logKit); final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); final Configuration logKitConf = builder.build(fis); logKitManager = new DefaultLogKitManager(Hierarchy.getDefaultHierarchy()); logKitManager.setLogger(log); final DefaultContext subcontext = new DefaultContext(appContext); subcontext.put("context-root", contextDir); logKitManager.contextualize(subcontext); logKitManager.configure(logKitConf); if (logger != null) { log = logKitManager.getLogger(logger); } else { log = logKitManager.getLogger("cocoon"); } } else { logKitManager = new DefaultLogKitManager(Hierarchy.getDefaultHierarchy()); logKitManager.setLogger(log); } appContext.put(Constants.CONTEXT_CLASSPATH, getClassPath(contextDir)); appContext.put(Constants.CONTEXT_WORK_DIR, work); appContext.put(Constants.CONTEXT_UPLOAD_DIR, contextDir + "upload-dir"); File cacheDir = getDir(workDir + File.separator + "cache-dir", "cache"); appContext.put(Constants.CONTEXT_CACHE_DIR, cacheDir); appContext.put(Constants.CONTEXT_CONFIG_URL, conf.toURL()); cocoon = new Cocoon(); cocoon.enableLogging(new LogKitLogger(log)); cocoon.contextualize(appContext); cocoon.setLogKitManager(logKitManager); cocoon.initialize(); if (brokenLinkFileName != null) { try { this.brokenLinkWriter = new PrintWriter(new FileWriter(new File(brokenLinkFileName)), true); } catch (IOException ioe) { log.error("File does not exist: " + brokenLinkFileName); } } } catch (Exception e) { log.fatalError("Exception caught", e); throw e; } initialized = true; } /** * Check for a configuration file in a specific directory. * * @param dir a <code>File</code> where to look for configuration files * @return a <code>File</code> representing the configuration * @exception IOException if an error occurs */ private static File getConfigurationFile(File dir) throws IOException { if (log.isDebugEnabled()) { log.debug("Trying configuration file at: " + dir + File.separator + Constants.DEFAULT_CONF_FILE); } File f = new File(dir, Constants.DEFAULT_CONF_FILE); if (f.canRead()) { return f; } if (log.isDebugEnabled()) { log.debug("Trying configuration file at: " + System.getProperty("user.dir") + File.separator + Constants.DEFAULT_CONF_FILE); } f = new File(System.getProperty("user.dir"), Constants.DEFAULT_CONF_FILE); if (f.canRead()) { return f; } if (log.isDebugEnabled()) { log.debug("Trying configuration file at: /usr/local/etc/" + Constants.DEFAULT_CONF_FILE); } f = new File("/usr/local/etc/", Constants.DEFAULT_CONF_FILE); if (f.canRead()) { return f; } log.error("Could not find the configuration file."); throw new FileNotFoundException("The configuration file could not be found."); } /** * Get a <code>File</code> representing a directory. * * @param dir a <code>String</code> with a directory name * @param type a <code>String</code> describing the type of directory * @return a <code>File</code> value * @exception IOException if an error occurs */ private File getDir(String dir, String type) throws IOException { if (log.isDebugEnabled()) { log.debug("Getting handle to " + type + " directory '" + dir + "'"); } File d = new File(dir); if (!d.exists()) { if (!d.mkdirs()) { log.error("Error creating " + type + " directory '" + d + "'"); throw new IOException("Error creating " + type + " directory '" + d + "'"); } } if (!d.isDirectory()) { log.error("'" + d + "' is not a directory."); throw new IOException("'" + d + "' is not a directory."); } if (!(d.canRead() && d.canWrite())) { log.error("Directory '" + d + "' is not readable/writable"); throw new IOException("Directory '" + d + "' is not readable/writable"); } return d; } protected void finalize() throws Throwable { dispose(); super.finalize(); } // // GETTERS AND SETTERS FOR CONFIGURATION PROPERTIES // /** * Returns Cocoon version string * @return Cocoon version */ public static String getVersion() { return Constants.VERSION; } /** * Set LogKit configuration file name * @param logKit LogKit configuration file */ public void setLogKit(String logKit) { this.logKit = logKit; } /** * Set log level. Default is DEBUG. * @param logLevel log level */ public void setLogLevel(String logLevel) { final Priority priority = Priority.getPriorityForName(logLevel); Hierarchy.getDefaultHierarchy().setDefaultPriority(priority); this.log = Hierarchy.getDefaultHierarchy().getLoggerFor(""); } /** * Set logger category as default logger for the Cocoon engine * @param logger logger category */ public void setLogger(String logger) { this.logger = logger; } /** * Set context directory * @param contextDir context directory */ public void setContextDir(String contextDir) { this.contextDir = contextDir; } /** * Set working directory * @param workDir working directory */ public void setWorkDir(String workDir) { this.workDir = workDir; } public void setAgentOptions(String userAgent) { this.userAgent = userAgent; } public void setAcceptOptions(String accept) { this.accept = accept; } public void setFollowLinks(boolean follow) { followLinks = follow; } public void setConfirmExtensions(boolean confirmExtension) { this.confirmExtension = confirmExtension; } public void setBrokenLinkFile(String filename){ this.brokenLinkFileName = filename; } public void setPrecompileOnly(boolean precompileOnly){ this.precompileOnly = precompileOnly; } public void setVerbose(boolean verbose){ this.verbose = verbose; } public Logger getLogger(){ return this.log; } public boolean isPrecompileOnly(){ return this.precompileOnly; } public String getContextDir(){ return this.contextDir; } public String getWorkDir(){ return this.workDir; } /** * Process single URI into given output stream. * * @param uri to process * @param outputStream to write generated contents into */ public void processURI(String uri, OutputStream outputStream) throws Exception { if (!initialized) { initialize(); } attributes = new HashMap(); empty = new HashMap(); allProcessedLinks = new HashMap(); allTranslatedLinks = new HashMap(); log.info("Processing URI: " + uri); processURI(uri); } public void process(List uris, Destination destination) throws Exception { dest = destination; if (!initialized){ initialize(); } attributes = new HashMap(); empty = new HashMap(); allProcessedLinks = new HashMap(); allTranslatedLinks = new HashMap(); if (process(uris, precompileOnly) == 0) { recursivelyPrecompile(context, context); } } /** * Warms up the engine by accessing the root. * @exception Exception if an error occurs */ public void warmup() throws Exception { //log.info(" [Cocoon might need to compile the sitemaps, this might take a while]"); cocoon.generateSitemap(new LinkSamplingEnvironment("/", context, attributes, null, new LogKitLogger(log))); } public void dispose () { if (initialized) { cocoon.dispose(); initialized = false; cocoon = null; log.debug("Disposed"); } } /** * Process the URI list and process them all independently. * @param uris a <code>Collection</code> of URIs * @param precompileOnly a <code>boolean</code> denoting to process XSP only * @return an <code>int</code> value with the number of links processed * @exception Exception if an error occurs */ private int process(Collection uris, boolean precompileOnly) throws Exception { int nCount = 0; ArrayList links = new java.util.ArrayList(); Iterator i = uris.iterator(); String next; while (i.hasNext()) { next = (String)i.next(); if (!links.contains(next)) { links.add(next); } } while (links.size() > 0) { String url = (String)links.get(0); try { if (allProcessedLinks.get(url) == null) { if (precompileOnly) { this.processXSP(url); } else if (this.followLinks) { i = processURI(url).iterator(); while (i.hasNext()) { next = (String)i.next(); if (!links.contains(next)) { links.add(next); } } } else { processURI(url); } } } catch (ResourceNotFoundException rnfe) { printBroken (url, rnfe.getMessage()); } links.remove(url); nCount++; if (log.isInfoEnabled()) { log.info(" Memory used: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); log.info(" Processed, Translated & Left: " + allProcessedLinks.size() + ", " + allTranslatedLinks.size() + ", " + links.size()); } } return nCount; } /** * Recurse the directory hierarchy and process the XSP's. * @param contextDir a <code>File</code> value for the context directory * @param file a <code>File</code> value for a single XSP file or a directory to scan recursively */ private void recursivelyPrecompile(File contextDir, File file) { if (file.isDirectory()) { String entries[] = file.list(); for (int i = 0; i < entries.length; i++) { recursivelyPrecompile(contextDir, new File(file, entries[i])); } } else if (file.getName().toLowerCase().endsWith(".xmap")) { try { this.processXMAP(IOUtils.getContextFilePath(contextDir.getCanonicalPath(),file.getCanonicalPath())); } catch (Exception e){ //Ignore for now. } } else if (file.getName().toLowerCase().endsWith(".xsp")) { try { this.processXSP(IOUtils.getContextFilePath(contextDir.getCanonicalPath(),file.getCanonicalPath())); } catch (Exception e){ //Ignore for now. } } } /** * Process a single XSP file * * @param uri a <code>String</code> pointing to an xsp URI * @exception Exception if an error occurs */ private void processXSP(String uri) throws Exception { String markupLanguage = "xsp"; String programmingLanguage = "java"; Environment env = new LinkSamplingEnvironment("/", context, attributes, null, new LogKitLogger(log)); cocoon.precompile(uri, env, markupLanguage, programmingLanguage); } /** * Process a single XMAP file * * @param uri a <code>String</code> pointing to an xmap URI * @exception Exception if an error occurs */ private void processXMAP(String uri) throws Exception { String markupLanguage = "sitemap"; String programmingLanguage = "java"; Environment env = new LinkSamplingEnvironment("/", context, attributes, null, new LogKitLogger(log)); cocoon.precompile(uri, env, markupLanguage, programmingLanguage); } /** * Processes the given URI and return all links. The algorithm is the following: * * <ul> * <li>file name for the URI is generated. URI MIME type is checked for * consistency with the URI and, if the extension is inconsistent * or absent, the file name is changed</li> * <li>the link view of the given URI is called and the file names for linked * resources are generated and stored.</li> * <li>for each link, absolute file name is translated to relative path.</li> * <li>after the complete list of links is translated, the link-translating * view of the resource is called to obtain a link-translated version * of the resource with the given link map</li> * <li>list of absolute URI is returned, for every URI which is not yet * present in list of all translated URIs</li> * </ul> * @param uri a <code>String</code> URI to process * @return a <code>Collection</code> containing all links found * @exception Exception if an error occurs */ private Collection processURI(String uri) throws Exception { System.out.print(" * "); // Get parameters, deparameterized URI and path from URI final TreeMap parameters = new TreeMap(); final String deparameterizedURI = NetUtils.deparameterize(uri, parameters); final String path = NetUtils.getPath(uri); final String suri = NetUtils.parameterize(deparameterizedURI, parameters); parameters.put("user-agent", userAgent); parameters.put("accept", accept); // Get file name from URI (without path) String pageURI = deparameterizedURI; if (pageURI.indexOf("/") != -1) { pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1); if (pageURI.length() == 0) { pageURI = "./"; } } String filename = (String)allTranslatedLinks.get(suri); if (filename == null) { filename = mangle(suri); if (confirmExtension) { final String type = getType(deparameterizedURI, parameters); final String ext = NetUtils.getExtension(filename); final String defaultExt = MIMEUtils.getDefaultExtension(type); if ((ext == null) || (!ext.equals(defaultExt))) { filename += defaultExt; } } allTranslatedLinks.put(suri, filename); } // Store processed URI list to avoid eternal loop allProcessedLinks.put(suri, filename); if ("".equals(filename)) { return new ArrayList(); } // Process links final ArrayList absoluteLinks = new ArrayList(); final HashMap translatedLinks = new HashMap(); final Iterator i = this.getLinks(deparameterizedURI, parameters).iterator(); while (i.hasNext()) { String link = (String) i.next(); // Fix relative links starting with "?" String relativeLink = link; if (relativeLink.startsWith("?")) { relativeLink = pageURI + relativeLink; } String absoluteLink = NetUtils.normalize(NetUtils.absolutize(path, relativeLink)); { final TreeMap p = new TreeMap(); absoluteLink = NetUtils.parameterize(NetUtils.deparameterize(absoluteLink, p), p); } String translatedAbsoluteLink = (String)allTranslatedLinks.get(absoluteLink); if (translatedAbsoluteLink == null) { try { translatedAbsoluteLink = this.translateURI(absoluteLink); log.info(" Link translated: " + absoluteLink); allTranslatedLinks.put(absoluteLink, translatedAbsoluteLink); absoluteLinks.add(absoluteLink); } catch (ProcessingException pe) { printBroken(absoluteLink, pe.getMessage()); } } // I have to add also broken links to the absolute links // to be able to generate the "broken link" page absoluteLinks.add(absoluteLink); final String translatedRelativeLink = NetUtils.relativize(path, translatedAbsoluteLink); translatedLinks.put(link, translatedRelativeLink); } printInfo("["+translatedLinks.size()+"] "); try { // Process URI String file = NetUtils.decodePath(filename); OutputStream output = dest.getOutputStream(file); try{ getPage(deparameterizedURI, parameters, translatedLinks, output); printlnInfo(uri); // (can also output type returned by getPage) } catch(ProcessingException pe) { printBroken(filename, DefaultNotifyingBuilder.getRootCause(pe).getMessage()); output.close(); resourceUnavailable(file, uri); } finally { try { if (output != null) output.close(); } catch(IOException ioex) { log.warn(ioex.toString()); } } } catch (Exception rnfe) { System.out.println("XXX" + rnfe); log.warn("Could not process URI: " + deparameterizedURI); if (verbose) System.out.println("Could not process URI: " + deparameterizedURI); } return absoluteLinks; } /** * Translate an URI into a file name. * * @param uri a <code>String</code> value to map * @return a <code>String</code> vlaue for the file * @exception Exception if an error occurs */ private String translateURI(String uri) throws Exception { if (null == uri || "".equals(uri)) { log.warn("translate empty uri"); if (verbose) System.out.println("translate empty uri"); return ""; } HashMap parameters = new HashMap(); parameters.put("user-agent", userAgent); parameters.put("accept", accept); String deparameterizedURI = NetUtils.deparameterize(uri, parameters); String filename = mangle(uri); if (confirmExtension) { String type = getType(deparameterizedURI, parameters); String ext = NetUtils.getExtension(filename); String defaultExt = MIMEUtils.getDefaultExtension(type); if ((ext == null) || (!ext.equals(defaultExt))) { filename += defaultExt; } } return filename; } /** * Generate a <code>resourceUnavailable</code> message. * * @param file being unavailable * @exception IOException if an error occurs */ private void resourceUnavailable(String file, String uri) throws IOException { SimpleNotifyingBean n = new SimpleNotifyingBean(this); n.setType("resource-not-found"); n.setTitle("Resource not Found"); n.setSource("Cocoon commandline (Main.java)"); n.setMessage("Page Not Available."); n.setDescription("The requested resource couldn't be found."); n.addExtraDescription("request-uri", uri); n.addExtraDescription("missing-file", file.toString()); PrintStream out = new PrintStream(dest.getOutputStream(file)); Notifier.notify(n, out, "text/html"); out.flush(); out.close(); } /** * Mangle a URI. * * @param uri a URI to mangle * @return a mangled URI */ private String mangle(String uri) { if (log.isDebugEnabled()) { log.debug("mangle(\"" + uri + "\")"); } if (uri.charAt(uri.length() - 1) == '/') { uri += Constants.INDEX_URI; } uri = uri.replace('"', '\''); uri = uri.replace('?', '_'); uri = uri.replace(':', '_'); if (log.isDebugEnabled()) { log.debug(uri); } return uri; } /** * Samples an URI for its links. * * @param deparameterizedURI a <code>String</code> value of an URI to start sampling from * @param parameters a <code>Map</code> value containing request parameters * @return a <code>Collection</code> of links * @exception Exception if an error occurs */ protected Collection getLinks(String deparameterizedURI, Map parameters) throws Exception { LinkSamplingEnvironment env = new LinkSamplingEnvironment(deparameterizedURI, context, attributes, parameters, new LogKitLogger(log)); processLenient(env); return env.getLinks(); } /** * Processes an URI for its content. * * @param deparameterizedURI a <code>String</code> value of an URI to start sampling from * @param parameters a <code>Map</code> value containing request parameters * @param links a <code>Map</code> value * @param stream an <code>OutputStream</code> to write the content to * @return a <code>String</code> value for the content * @exception Exception if an error occurs */ protected String getPage(String deparameterizedURI, Map parameters, Map links, OutputStream stream) throws Exception { FileSavingEnvironment env = new FileSavingEnvironment(deparameterizedURI, context, attributes, parameters, links, stream, new LogKitLogger(log)); // Here Cocoon can throw an exception if there are errors in processing the page cocoon.process(env); // if we get here, the page was created :-) return env.getContentType(); } /** Class <code>NullOutputStream</code> here. */ static class NullOutputStream extends OutputStream { public void write(int b) throws IOException { } public void write(byte b[]) throws IOException { } public void write(byte b[], int off, int len) throws IOException { } } /** * Analyze the type of content for an URI. * * @param deparameterizedURI a <code>String</code> value to analyze * @param parameters a <code>Map</code> value for the request * @return a <code>String</code> value denoting the type of content * @exception Exception if an error occurs */ protected String getType(String deparameterizedURI, Map parameters) throws Exception { FileSavingEnvironment env = new FileSavingEnvironment(deparameterizedURI, context, attributes, parameters, empty, new NullOutputStream(), new LogKitLogger(log)); processLenient(env); return env.getContentType(); } /** * Try to process something but don't throw a ProcessingException. * * @param env the <code>Environment</code> to process * @return boolean true if no error were cast, false otherwise * @exception Exception if an error occurs, except RNFE */ private boolean processLenient(Environment env) throws Exception { try{ cocoon.process(env); } catch(ProcessingException pe) { return false; } return true; } /** * Print the message of a broken link. * * @param url the <code>URL</code> of the broken link * @param cause of the broken link */ private void printBroken(String url, String cause) { int screenWidth = 67; int causeWidth = screenWidth - 6; printlnInfo(""); printlnInfo("-> [broken page] " + url +" <- "); printlnInfo(""); printInfo(" "); int causeLength = cause.length(), currentStart = -causeWidth, currentEnd = 0; do { currentStart += causeWidth; currentEnd += causeWidth; if (currentEnd>causeLength) { currentEnd=causeLength; } printlnInfo(cause.substring(currentStart, currentEnd)); printInfo(" "); } while(currentEnd < causeLength); printlnInfo(""); if (null != this.brokenLinkWriter) { this.brokenLinkWriter.println(url); } } /** * Print an info message. * * @param message the message to print */ private void printlnInfo (String message) { log.info(message); if (verbose) System.out.println(message); } /** * Print an info message. * * @param message the message to print */ private void printInfo (String message) { log.info(message); if (verbose) System.out.print(message); } /** * This builds the important ClassPath used by this class. It * does so in a neutral way. * It iterates in alphabetical order through every file in the * lib directory and adds it to the classpath. * * Also, we add the files to the ClassLoader for the Cocoon system. * In order to protect ourselves from skitzofrantic classloaders, * we need to work with a known one. * * @param context The context path * @return a <code>String</code> value */ protected static String getClassPath(final String context) { StringBuffer buildClassPath = new StringBuffer(); String classDir = context + "/WEB-INF/classes"; File root = new File(context + "/WEB-INF/lib"); buildClassPath.append(classDir); if (root.isDirectory()) { File[] libraries = root.listFiles(); Arrays.sort(libraries); for (int i = 0; i < libraries.length; i++) { buildClassPath.append(File.pathSeparatorChar) .append(IOUtils.getFullFilename(libraries[i])); } } buildClassPath.append(File.pathSeparatorChar) .append(System.getProperty("java.class.path")); // buildClassPath.append(File.pathSeparatorChar) // .append(getExtraClassPath(context)); if (log.isDebugEnabled()) { log.debug("Context classpath: " + buildClassPath.toString()); } return buildClassPath.toString(); } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/destination/Destination.java Index: Destination.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.destination; import java.io.OutputStream; import java.io.IOException; /** * Defines an interface for a Bean Destination - somewhere that the Bean * can send its generated content. * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: Destination.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ * */ public interface Destination extends Cloneable { public OutputStream getOutputStream(String filename) throws IOException; public Object clone(); } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/destination/DestinationNotFoundException.java Index: DestinationNotFoundException.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.destination; import java.io.IOException; /** * An <code>Exception</code> thrown by the ThreadedDestination when it * cannot find the Destination for a particular Cocoon URI. * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: DestinationNotFoundException.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ * */ public class DestinationNotFoundException extends IOException { public DestinationNotFoundException(String filename){ super("Destination not found for: " + filename); } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/destination/FileDestination.java Index: FileDestination.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.destination; import org.apache.cocoon.util.IOUtils; import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.BufferedOutputStream; /** * A <code>Destination</code> that saves the Cocoon content to the local disk. * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: FileDestination.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ */ public class FileDestination implements Destination { private File dir; public FileDestination(String directory) { this(new File(directory)); } public FileDestination(File directory) { dir = directory; } public OutputStream getOutputStream(String filename) throws IOException { File file = IOUtils.createFile(dir, filename); return new BufferedOutputStream(new FileOutputStream(file)); } public Object clone() { return new FileDestination(dir); } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/destination/NullDestination.java Index: NullDestination.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.destination; import java.io.OutputStream; import java.io.IOException; /** * A <code>Destination</code> that discards the content generated by Cocoon. * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: NullDestination.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ */ public class NullDestination implements Destination { public OutputStream getOutputStream(String dummy) { return new NullOutputStream(); } public Object clone(){ return new NullDestination(); } private class NullOutputStream extends OutputStream { public void write(int b) throws IOException { } public void write(byte b[]) throws IOException { } public void write(byte b[], int off, int len) throws IOException { } } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/bean/destination/ThreadedDestination.java Index: ThreadedDestination.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.destination; import java.io.OutputStream; import java.io.IOException; import java.io.ByteArrayOutputStream; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * <code>Threaded Destination</code>: A <code>Destination</code> for dispatching Cocoon content to * other Destination objects, in a multithreaded environment. * * The threaded destination fulfills the following goals: * - to allow content to be dispatched to a remote server in as close as possible to real time, * by having multiple threads simultaneously dispatching threads * - to allow content to be dispatched to a range of destinations, depending upon the Cocoon * URI of the Content. * * The first of these aims can be achieved with code such as: * * <code>Integer maxThreads = 10; * cocoon.process(targets, new ThreadedDestination( * new FTPDestination("ftp://user:[EMAIL PROTECTED]")), maxThreads);</code> * * Thus the ThreadedDestination object is handed a single threaded destination object with * which to dispatch the content. * * (Note, this object may be cloned for use in a multithreaded environment. Therefore, in order * to be usable with the ThreadedDestination, a Destination object must implement the Cloneable * Interface.) * * For the second scenario, the user builds up a DestinationMap object, which matches Cocoon URIs * to Destination objects. * * Sample code would be: * * <code>Map map = new HashMap(); * HTTPDestination http1 = new HTTPDestination("http://192.168.1.100/foo.asp", "text/html"); * map.add("site/web.html", http1); * * FTPDestination ftp1 = new FTPDestination("ftp://user:[EMAIL PROTECTED]"); * map.add("site/page2.html", ftp1); * * EmailDestination email1 = new EmailDestination("from", "to", "subject", "smtp.myisp.com"); * map.add("site/email.txt", email1); * * FileDestination file1 = new FileDestination("path"); * map.add("site/file.html", file1); * * Integer maxThreads = 10; * cocoon.process(map.getTargets(), new ThreadedDestination(map, maxThreads)); * </code> * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: ThreadedDestination.java,v 1.1 2003/02/02 00:46:21 vgritsenko Exp $ */ public class ThreadedDestination implements Destination { private List results = new LinkedList(); private int maxThreads; private int currentThreads; private Destination destination = null; private Map destinations = null; public ThreadedDestination (Destination destination, int maxThreads) { this.destination = destination; this.maxThreads = maxThreads; } public ThreadedDestination (Map destinationMap, int maxThreads) { this.destinations = destinationMap; this.maxThreads = maxThreads; } public Object clone () { throw new IllegalArgumentException("Not Implemented"); } public OutputStream getOutputStream (final String fileName) throws IOException { return new ByteArrayOutputStream() { public synchronized void close () throws IOException { super.close(); onClose(fileName, this); } }; } void onClose (String fileName, ByteArrayOutputStream stream) throws IOException { Destination d; if (destination != null){ d = destination; } else { d = (Destination)destinations.get(fileName); } if (d == null){ throw new DestinationNotFoundException(fileName); } else { d = (Destination) d.clone(); } results.add(new ThreadedDispatchRequest(stream.toByteArray(), fileName, d)); startThread(); } private synchronized void startThread(){ if (currentThreads < maxThreads){ currentThreads++; new DestinationThread().start(); } } synchronized void endThread(){ currentThreads--; } synchronized ThreadedDispatchRequest getRequest() { if (results.size() > 0) { return (ThreadedDispatchRequest) results.remove(0); } return null; } private class DestinationThread extends Thread { public void run() { ThreadedDispatchRequest request; try { while ((request = getRequest()) != null) { try { request.process(); } catch (IOException e) { // TODO: Handle Exception } } } finally { endThread(); } } } private class ThreadedDispatchRequest { private byte[] content; private String filename; private Destination destination; public ThreadedDispatchRequest (byte[] content, String filename, Destination destination) { this.content = content; this.filename = filename; this.destination = destination; } public void process() throws IOException { OutputStream out = destination.getOutputStream(filename); out.write(content); out.close(); } } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]