vgritsenko    2002/09/22 20:30:44

  Modified:    .        changes.xml
               src/blocks/fop/conf fop.xmap
               src/blocks/fop/java/org/apache/cocoon/serialization
                        FOPSerializer.java
  Log:
  backport fixes from 203 branch
  
  Revision  Changes    Path
  1.257     +9 -1      xml-cocoon2/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/changes.xml,v
  retrieving revision 1.256
  retrieving revision 1.257
  diff -u -r1.256 -r1.257
  --- changes.xml       21 Sep 2002 17:31:41 -0000      1.256
  +++ changes.xml       23 Sep 2002 03:30:44 -0000      1.257
  @@ -40,6 +40,14 @@
    </devs>
   
    <release version="@version@" date="@date@">
  +  <action dev="VG" type="update">
  +    FOP user configuration can now be specified as URL or relative path
  +    as the value of the &gt;user-config&lt; element.
  +  </action>
  +  <action dev="VG" type="fix" fixes-bug="6533">
  +    Load default FOP configuration only once, do not overwrite any user
  +    configuration.
  +  </action>
     <action dev="VG" type="fix" fixes-bug="11856" due-to="Stefan Seifert" 
due-to-email="[EMAIL PROTECTED]">
       Remove unnessesary code in SVGBuilder. This also fixes intermittent
       ClassCastExceptions in Batik code.
  
  
  
  1.2       +14 -1     xml-cocoon2/src/blocks/fop/conf/fop.xmap
  
  Index: fop.xmap
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/blocks/fop/conf/fop.xmap,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- fop.xmap  28 Aug 2002 17:46:56 -0000      1.1
  +++ fop.xmap  23 Sep 2002 03:30:44 -0000      1.2
  @@ -1,6 +1,19 @@
   <?xml version="1.0"?>
   <xmap xpath="/sitemap/components/serializers" unless="serializer[@name='fo2pdf']">
  -    <map:serializer logger="sitemap.serializer.fo2pdf" name="fo2pdf" 
src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf"/>
  +    <map:serializer logger="sitemap.serializer.fo2pdf" name="fo2pdf" 
src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf">
  +      <!-- This element specifies URL to FOP user configuration file.
  +           It can be absolute file URL or relative to the servlet context.
  +           Examples:
  +
  +      <user-config>file:/C:/cocoon/fop-config.xml</user-config>
  +      <user-config>WEB-INF/fop-config.xml</user-config>
  +      -->
  +
  +      <!-- Should serializer set content length header or not?
  +           Default is true.
  +      <set-content-length>true</set-content-length>
  +      -->
  +    </map:serializer>
   
       <map:serializer logger="sitemap.serializer.fo2ps" name="fo2ps" 
src="org.apache.cocoon.serialization.FOPSerializer" 
mime-type="application/postscript"/>
   
  
  
  
  1.3       +102 -56   
xml-cocoon2/src/blocks/fop/java/org/apache/cocoon/serialization/FOPSerializer.java
  
  Index: FOPSerializer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/blocks/fop/java/org/apache/cocoon/serialization/FOPSerializer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FOPSerializer.java        13 Sep 2002 11:25:35 -0000      1.2
  +++ FOPSerializer.java        23 Sep 2002 03:30:44 -0000      1.3
  @@ -53,50 +53,56 @@
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.component.Composable;
  +import org.apache.avalon.framework.component.ComponentManager;
  +import org.apache.avalon.framework.component.ComponentException;
   import org.apache.cocoon.caching.CacheableProcessingComponent;
   import org.apache.cocoon.components.renderer.ExtendableRendererFactory;
   import org.apache.cocoon.components.renderer.RendererFactory;
  +import org.apache.cocoon.components.url.URLFactory;
   import org.apache.cocoon.util.ClassUtils;
  +import org.apache.cocoon.environment.URLFactorySourceResolver;
  +import org.apache.cocoon.environment.Source;
   import org.apache.excalibur.source.SourceValidity;
   import org.apache.excalibur.source.impl.validity.NOPValidity;
   import org.apache.fop.apps.Driver;
   import org.apache.fop.apps.Options;
   import org.apache.fop.messaging.MessageHandler;
   import org.apache.fop.render.Renderer;
  +import org.apache.fop.configuration.ConfigurationParser;
   
   import java.io.OutputStream;
  +import java.io.File;
  +import java.io.Serializable;
  +import java.net.MalformedURLException;
   
   /**
    * @author ?
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Vadim Gritsenko</a>
    * @version CVS $Id$
    */
   public class FOPSerializer
   extends AbstractSerializer
  -implements Configurable, CacheableProcessingComponent {
  +implements Composable, Configurable, CacheableProcessingComponent {
   
       /**
  -      * The <code>Options</code> used by FOP.
  -      */
  -    protected Options options;
  +     * The Renderer Factory to use
  +     */
  +    protected static RendererFactory factory = 
ExtendableRendererFactory.getRendererFactoryImplementation();
   
       /**
  -      * The <code>Driver</code> which is FOP.
  -      */
  +     * The <code>Driver</code> which is FOP.
  +     */
       protected Driver driver;
   
       /**
  -      * The current <code>Renderer</code>.
  -      */
  +     * The current <code>Renderer</code>.
  +     */
       protected Renderer renderer;
   
       /**
  -      * The Renderer Factory to use
  -      */
  -    protected static RendererFactory factory = 
ExtendableRendererFactory.getRendererFactoryImplementation();
  -
  -    /**
  -      * The current <code>mime-type</code>.
  -      */
  +     * The current <code>mime-type</code>.
  +     */
       protected String mimetype;
   
       /**
  @@ -109,63 +115,96 @@
        */
       protected boolean setContentLength = true;
   
  -    /** This logger is used for FOP */
  +    /**
  +     * This logger is used for FOP
  +     */
       protected org.apache.avalon.framework.logger.Logger logger;
   
       /**
  -      * Set the configurations for this serializer.
  -      */
  -    public void configure(Configuration conf)
  -    throws ConfigurationException {
  +     * It is used to make sure that default Options loaded only once.
  +     */
  +    private static boolean configured = false;
  +
  +    /**
  +     * Manager to get URLFactory from.
  +     */
  +    protected ComponentManager manager;
  +
  +    /**
  +     * Set the component manager for this serializer.
  +     */
  +    public void compose(ComponentManager componentManager)
  +            throws ComponentException {
  +        this.manager = componentManager;
  +    }
  +
  +    /**
  +     * Set the configurations for this serializer.
  +     */
  +    public void configure(Configuration conf) throws ConfigurationException {
  +
           this.logger = getLogger().getChildLogger("fop");
           MessageHandler.setScreenLogger(this.logger);
  -        String userConfig = null;
  -        java.io.File userConfigFile = null;
   
  -        if (conf != null) {
  -            Configuration child = conf.getChild("user-config");
  -            if (child != null) {
  +        // FIXME: VG: Initialize static FOP configuration with defaults, only once.
  +        // FOP has static config, but that's going to change in the near future.
  +        // Then this code should be reviewed.
  +        synchronized (FOPSerializer.class) {
  +            if (!configured) {
                   try {
  -                    userConfig = child.getAttribute("src");
  -                } catch(Exception ex) {
  -                    // No config file specified
  +                    if (getLogger().isDebugEnabled()) {
  +                        getLogger().debug("Loading default configuration");
  +                    }
  +                    new Options();
  +                } catch (Exception e) {
  +                    getLogger().error("Cannot load default configuration. 
Proceeding.", e);
                   }
  +                configured = true;
               }
  -
  -            this.setContentLength = 
conf.getChild("set-content-length").getValueAsBoolean(true);
           }
   
  -        // Check for null, use external or inbuilt config.
  -        if(userConfig != null) {
  +        this.setContentLength = 
conf.getChild("set-content-length").getValueAsBoolean(true);
  +
  +        // Old syntax: Attribute src of element user-config contains file
  +        String configUrl = conf.getChild("user-config").getAttribute("src", null);
  +        if (configUrl != null) {
               try {
  -                userConfigFile = new java.io.File(userConfig);
  -                options = new Options(userConfigFile);
  -                if (this.getLogger().isDebugEnabled()) {
  -                    getLogger().debug("Using config file " + userConfig);
  -                }
  -            } catch (Exception ex) {
  -                throw new ConfigurationException("Cannot load config " + 
userConfig, ex);
  +                // VG: Old version of serializer supported only files
  +                configUrl = new File(configUrl).toURL().toExternalForm();
  +            } catch (MalformedURLException e) {
  +                getLogger().warn("Can not load config file " + configUrl, e);
  +                configUrl = null;
               }
           } else {
  +            // New syntax: Element user-config contains URL
  +            configUrl = conf.getChild("user-config").getValue(null);
  +        }
  +        if(configUrl != null) {
  +            URLFactory urlFactory = null;
  +            Source configSource = null;
               try {
  -                options = new Options();
  -                if (this.getLogger().isDebugEnabled()) {
  -                    getLogger().debug("Using default config file");
  +                // FIXME: How to do without URLFactory but relative to context?
  +                urlFactory = (URLFactory)manager.lookup(URLFactory.ROLE);
  +                URLFactorySourceResolver urlResolver = new 
URLFactorySourceResolver(urlFactory, manager);
  +                configSource = urlResolver.resolve(configUrl);
  +                if (getLogger().isDebugEnabled()) {
  +                    getLogger().debug("Loading configuration from " + 
configSource.getSystemId());
                   }
  +                configSource.toSAX(new ConfigurationParser());
               } catch (Exception e) {
  -                getLogger().warn("Cannot load default config ", e);
  +                getLogger().warn("Cannot load configuration from " + configUrl);
  +                throw new ConfigurationException("Cannot load configuration from " 
+ configUrl, e);
  +            } finally {
  +                manager.release(urlFactory);
  +                if (configSource != null) {
  +                    configSource.recycle();
  +                }
               }
           }
   
           // Get the mime type.
           this.mimetype = conf.getAttribute("mime-type");
  -        if (this.getLogger().isDebugEnabled()) {
  -            getLogger().debug("FOPSerializer mime-type:" + mimetype);
  -        }
   
  -        // Using the Renderer Factory, get the default renderer
  -        // for this MIME type.
  -        this.renderer = factory.createRenderer(mimetype);
           // Iterate through the parameters, looking for a renderer reference
           Configuration[] parameters = conf.getChildren("parameter");
           for (int i = 0; i < parameters.length; i++) {
  @@ -175,10 +214,16 @@
                   try {
                       this.renderer = (Renderer)ClassUtils.newInstance(rendererName);
                   } catch (Exception ex) {
  +                    getLogger().error("Cannot load  class " + rendererName, ex);
                       throw new ConfigurationException("Cannot load class " + 
rendererName, ex);
                   }
               }
           }
  +        if (this.renderer == null) {
  +            // Using the Renderer Factory, get the default renderer
  +            // for this MIME type.
  +            this.renderer = factory.createRenderer(mimetype);
  +        }
   
           // Do we have a renderer yet?
           if (this.renderer == null ) {
  @@ -203,20 +248,22 @@
       public void setOutputStream(OutputStream out) {
           // load the fop driver
           this.driver = new Driver();
  -
           this.driver.setLogger(this.logger);
           if (this.rendererName == null) {
               this.renderer = factory.createRenderer(mimetype);
           } else {
               try {
                   this.renderer = (Renderer)ClassUtils.newInstance(this.rendererName);
  -            } catch (Exception ex) {
  -                throw new RuntimeException("Cannot load class " + 
this.rendererName);
  +            } catch (Exception e) {
  +                if (getLogger().isWarnEnabled()) {
  +                    getLogger().warn("Cannot load  class " + this.rendererName, e);
  +                }
  +                throw new RuntimeException("Cannot load class " + this.rendererName 
+ "(" + e + ")");
               }
           }
           this.driver.setRenderer(this.renderer);
           this.driver.setOutputStream(out);
  -        this.setContentHandler( this.driver.getContentHandler() );
  +        setContentHandler(this.driver.getContentHandler());
       }
   
       /**
  @@ -227,7 +274,7 @@
        * @return The generated key or <code>0</code> if the component
        *              is currently not cacheable.
        */
  -    public java.io.Serializable generateKey() {
  +    public Serializable generateKey() {
           return "1";
       }
   
  @@ -248,7 +295,6 @@
         */
       public void recycle() {
           super.recycle();
  -        this.options = null;
           this.driver = null;
           this.renderer = null;
       }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to