Package org.apache.tapestry.asset

Source Code of org.apache.tapestry.asset.AssetExternalizer

/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.asset;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.ClassResolver;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.engine.IPropertySource;
import org.apache.tapestry.util.StringSplitter;

/**
*  Responsible for copying assets from the classpath to an external directory that
*  is visible to the web server. The externalizer is stored inside
*  the {@link ServletContext} as a named attribute.
*
<p>The externalizer uses the name <code>org.apache.tapestry.AssetExternalizer.<i>application name</i>
</code>.  It configures itself using two additional
*  properties (searching in
{@link org.apache.tapestry.IEngine#getPropertySource()}.
*
<table border=1>
<tr> <th>Parameter</th> <th>Description</th> </tr>
<tr valign=top>
*    <td><code>org.apache.tapestry.asset.dir</code> </td>
*      <td>The directory to which assets will be copied.</td> </tr>
<tr valign=top>
*    <td><code>org.apache.tapestry.asset.URL</code> </td>
*      <td>The corresponding URL for the asset directory.</td> </tr>
</table>
*
* <p>If either of these parameters is null, then no externalization occurs.
* Private assets will still be available, just less efficiently, as the application
* will be invoked via its servlet and, ultimately, the {@link AssetService} will need
* to retrieve the asset.
*
* <p>Assets maintain thier directory structure when copied.  For example,
* an asset with a resource path of <code>/com/skunkworx/Banner.gif</code> would
* be copied to the file system as <code><i>dir</i>/com/skunkworx/Banner.gif</code> and
* would have a URL of <code><i>URL</i>/com/skunkworx/Banner.gif</code>.
*
* <p>The externalizer will create any directories as needed.
*
* <p>The externalizer will not overwrite existing files.  When a new version of the application
* is deployed with changed assets, there are two deployment stategies:
* <ul>
* <li>Delete the existing asset directory and allow the externalizer to recreate and
* repopulate it.
* <li>Change the asset directory and URL, allowing the old and new assets to exist
*  side-by-side.
* </ul>
*
* <p>When using the second approach, it is best to use a directory that has
* a version number in it, for example, <code>D:/inetpub/assets/0</code> mapped to the URL
* <code>/assets/0</code>.  When a new version of the application is deployed, the trailing
* version number is incremented from 0 to 1.
*
@author Howard Lewis Ship
*
**/

public class AssetExternalizer
{public static com.cortexeb.tools.clover.d __CLOVER_36_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
    private static final Log LOG = LogFactory.getLog(AssetExternalizer.class);

    private ClassResolver _resolver;
    private File _assetDir;
    private String _URL;

    /**
     *  A map from resource path (as a String) to final URL (as a String).
     *
     **/

    private Map _resources = new HashMap();

    private static final int BUFFER_SIZE = 2048;

    protected AssetExternalizer(IRequestCycle cycle)
    {try { __CLOVER_36_0.M[236]++;
        __CLOVER_36_0.S[857]++;_resolver = cycle.getEngine().getClassResolver();
   
        __CLOVER_36_0.S[858]++;IPropertySource properties = cycle.getEngine().getPropertySource();


        __CLOVER_36_0.S[859]++;String directory = properties.getPropertyValue("org.apache.tapestry.asset.dir");

        __CLOVER_36_0.S[860]++;if ((((directory == null) && (++__CLOVER_36_0.CT[174] != 0)) || (++__CLOVER_36_0.CF[174] == 0))){
            __CLOVER_36_0.S[861]++;return;}

        __CLOVER_36_0.S[862]++;_URL = properties.getPropertyValue("org.apache.tapestry.asset.URL");

        __CLOVER_36_0.S[863]++;if ((((_URL == null) && (++__CLOVER_36_0.CT[175] != 0)) || (++__CLOVER_36_0.CF[175] == 0))){
            __CLOVER_36_0.S[864]++;return;}

        __CLOVER_36_0.S[865]++;_assetDir = new File(directory);

        __CLOVER_36_0.S[866]++;LOG.debug("Initialized with directory " + _assetDir + " mapped to " + _URL);
    } finally { }}

    protected void externalize(String resourcePath) throws IOException
    {try { __CLOVER_36_0.M[237]++;
        __CLOVER_36_0.S[867]++;String[] path;
        __CLOVER_36_0.S[868]++;int i;
        __CLOVER_36_0.S[869]++;File file;
        __CLOVER_36_0.S[870]++;StringSplitter splitter;
        __CLOVER_36_0.S[871]++;InputStream in;
        __CLOVER_36_0.S[872]++;OutputStream out;
        __CLOVER_36_0.S[873]++;int bytesRead;
        __CLOVER_36_0.S[874]++;URL inputURL;
        __CLOVER_36_0.S[875]++;byte[] buffer;

        __CLOVER_36_0.S[876]++;if ((((LOG.isDebugEnabled()) && (++__CLOVER_36_0.CT[176] != 0)) || (++__CLOVER_36_0.CF[176] == 0))){
            __CLOVER_36_0.S[877]++;LOG.debug("Externalizing " + resourcePath);}

        __CLOVER_36_0.S[878]++;file = _assetDir;

        // Resources are always split by the unix seperator, even on Win32.

        __CLOVER_36_0.S[879]++;splitter = new StringSplitter('/');

        __CLOVER_36_0.S[880]++;path = splitter.splitToArray(resourcePath);

        // The path is expected to start with a leading slash, but the StringSplitter
        // will ignore that leading slash.

        __CLOVER_36_0.S[881]++;for (i = 0; (((i < path.length - 1) && (++__CLOVER_36_0.CT[177] != 0)) || (++__CLOVER_36_0.CF[177] == 0)); i++){
        {
            // Doing it this way makes sure the path seperators are right.

            __CLOVER_36_0.S[882]++;file = new File(file, path[i]);
        }}

        // Make sure the directories exist.

        __CLOVER_36_0.S[883]++;file.mkdirs();

        __CLOVER_36_0.S[884]++;file = new File(file, path[path.length - 1]);

        // If the file exists, then assume all is well.  This is OK for development,
        // but there may be multithreading (or even multiprocess) race conditions
        // around the creation of the file.

        __CLOVER_36_0.S[885]++;if ((((file.exists()) && (++__CLOVER_36_0.CT[178] != 0)) || (++__CLOVER_36_0.CF[178] == 0))){
            __CLOVER_36_0.S[886]++;return;}

        // Get the resource and copy it to the file.

        __CLOVER_36_0.S[887]++;inputURL = _resolver.getResource(resourcePath);
        __CLOVER_36_0.S[888]++;if ((((inputURL == null) && (++__CLOVER_36_0.CT[179] != 0)) || (++__CLOVER_36_0.CF[179] == 0))){
            __CLOVER_36_0.S[889]++;throw new IOException(Tapestry.format("missing-resource", resourcePath));}

        __CLOVER_36_0.S[890]++;in = inputURL.openStream();

        __CLOVER_36_0.S[891]++;out = new FileOutputStream(file);

        __CLOVER_36_0.S[892]++;buffer = new byte[BUFFER_SIZE];

        __CLOVER_36_0.S[893]++;while (true){
        {
            __CLOVER_36_0.S[894]++;bytesRead = in.read(buffer, 0, BUFFER_SIZE);
            __CLOVER_36_0.S[895]++;if ((((bytesRead < 0) && (++__CLOVER_36_0.CT[180] != 0)) || (++__CLOVER_36_0.CF[180] == 0))){
                __CLOVER_36_0.S[896]++;break;}

            __CLOVER_36_0.S[897]++;out.write(buffer, 0, bytesRead);
        }}

        __CLOVER_36_0.S[898]++;in.close();
        __CLOVER_36_0.S[899]++;out.close();

        // The file is copied!
    } finally { }}

    /**
     *  Gets the externalizer singleton for the application.  If it does not already
     *  exist, it is created and stored into the {@link ServletContext}.
     *
     *  <p>Each Tapestry application within a single {@link ServletContext}
     *  will have its own externalizer; they are differentiated by the
     *  application name.
     *
     *  @see org.apache.tapestry.spec.ApplicationSpecification#getName()
     *
     **/

    public static AssetExternalizer get(IRequestCycle cycle)
    {try { __CLOVER_36_0.M[238]++;
        __CLOVER_36_0.S[900]++;HttpServlet servlet = cycle.getRequestContext().getServlet();
        __CLOVER_36_0.S[901]++;ServletContext context = servlet.getServletContext();

        __CLOVER_36_0.S[902]++;String servletName = servlet.getServletName();
       
        __CLOVER_36_0.S[903]++;String attributeName = "org.apache.tapestry.AssetExternalizer:" + servletName;

        __CLOVER_36_0.S[904]++;AssetExternalizer result = (AssetExternalizer) context.getAttribute(attributeName);

        __CLOVER_36_0.S[905]++;if ((((result == null) && (++__CLOVER_36_0.CT[181] != 0)) || (++__CLOVER_36_0.CF[181] == 0))){
        {
            __CLOVER_36_0.S[906]++;result = new AssetExternalizer(cycle);
            __CLOVER_36_0.S[907]++;context.setAttribute(attributeName, result);
        }}

        __CLOVER_36_0.S[908]++;return result;
    } finally { }}

    /**
     *  Gets the URL to a private resource.  If the resource was
     *  previously copied out of the classpath, the previously
     *  generated URL is returned.
     *
     *  <p>If the asset directory and URL are not configured, then
     *  returns null.
     *
     *  <p>Otherwise, the asset is copied out to the asset directory,
     *  the URL is constructed (and recorded for later) and the URL is
     *  returned.
     *
     *  <p>This method is not explicitly synchronized but should work
     *  multi-threaded.  It synchronizes on the internal
     *  <code>Map</code> used to map resource paths to URLs.
     *
     *  @param resourcePath The full path of the resource within the
     *  classpath.  This is expected to include a leading slash.  For
     *  example: <code>/com/skunkworx/Banner.gif</code>.
     *
     **/

    public String getURL(String resourcePath)
    {try { __CLOVER_36_0.M[239]++;
        __CLOVER_36_0.S[909]++;String result;

        __CLOVER_36_0.S[910]++;if ((((_assetDir == null) && (++__CLOVER_36_0.CT[182] != 0)) || (++__CLOVER_36_0.CF[182] == 0))){
            __CLOVER_36_0.S[911]++;return null;}

        __CLOVER_36_0.S[912]++;synchronized (_resources)
        {
            __CLOVER_36_0.S[913]++;result = (String) _resources.get(resourcePath);

            __CLOVER_36_0.S[914]++;if ((((result != null) && (++__CLOVER_36_0.CT[183] != 0)) || (++__CLOVER_36_0.CF[183] == 0))){
                __CLOVER_36_0.S[915]++;return result;}

            __CLOVER_36_0.S[916]++;try
            {
                __CLOVER_36_0.S[917]++;externalize(resourcePath);
            }
            catch (IOException ex)
            {
                __CLOVER_36_0.S[918]++;throw new ApplicationRuntimeException(
                    Tapestry.format("AssetExternalizer.externalize-failure", resourcePath, _assetDir),
                    ex);
            }

            __CLOVER_36_0.S[919]++;result = _URL + resourcePath;

            __CLOVER_36_0.S[920]++;_resources.put(resourcePath, result);

            __CLOVER_36_0.S[921]++;return result;
        }
    } finally { }}
}
TOP

Related Classes of org.apache.tapestry.asset.AssetExternalizer

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