⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcemanagerimpl.java

📁 velocity 的脚本语言的全部代码集合
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                rsvc.error(
                    "ResourceManager.getResource() exception: " + pee);
                
                throw pee;
            }
            catch( Exception eee )
            {
                rsvc.error(
                    "ResourceManager.getResource() exception: " + eee);
                
                throw eee;
            }
        }
        else
        {            
            try
            {
                /*
                 *  it's not in the cache, so load it.
                 */

                resource = loadResource( resourceName, resourceType, encoding );
                      
                if (resource.getResourceLoader().isCachingOn())
                {
                    globalCache.put(resourceName, resource);
                }                    
            }
            catch( ResourceNotFoundException rnfe2 )
            {
                rsvc.error(
                    "ResourceManager : unable to find resource '" + resourceName + 
                        "' in any resource loader.");
                
                throw rnfe2;
            }
            catch( ParseErrorException pee )
            {
                rsvc.error(
                    "ResourceManager.getResource() parse exception: " + pee);
                
                throw pee;
            }
            catch( Exception ee )
            {
                rsvc.error(
                    "ResourceManager.getResource() exception new: " + ee);

                throw ee;
            }
        }

        return resource;
    }
    
    /**
     *  Loads a resource from the current set of resource loaders
     *
     * @param resourceName The name of the resource to retrieve.
     * @param resourceType The type of resource (<code>RESOURCE_TEMPLATE</code>,
     *                     <code>RESOURCE_CONTENT</code>, etc.).
     * @param encoding  The character encoding to use.
     * @return Resource with the template parsed and ready.
     * @throws ResourceNotFoundException if template not found
     *          from any available source.
     * @throws ParseErrorException if template cannot be parsed due
     *          to syntax (or other) error.
     * @throws Exception if a problem in parse
     */    
    protected Resource loadResource(String resourceName, int resourceType, String encoding )
        throws ResourceNotFoundException, ParseErrorException, Exception
    {
        Resource resource = ResourceFactory.getResource(resourceName, resourceType);
                
        resource.setRuntimeServices( rsvc );

        resource.setName( resourceName );
        resource.setEncoding( encoding );
                
        /* 
         * Now we have to try to find the appropriate
         * loader for this resource. We have to cycle through
         * the list of available resource loaders and see
         * which one gives us a stream that we can use to
         * make a resource with.
         */

        long howOldItWas = 0;  // Initialize to avoid warnings

        ResourceLoader resourceLoader = null;

        for (int i = 0; i < resourceLoaders.size(); i++)
        {
            resourceLoader = (ResourceLoader) resourceLoaders.get(i);
            resource.setResourceLoader(resourceLoader);
            
            /*
             *  catch the ResourceNotFound exception
             *  as that is ok in our new multi-loader environment
             */

            try 
            {
                if (resource.process())
                {
                     /*
                      *  FIXME  (gmj)
                      *  moved in here - technically still 
                      *  a problem - but the resource needs to be 
                      *  processed before the loader can figure 
                      *  it out due to to the new 
                      *  multi-path support - will revisit and fix
                      */

                     if ( logWhenFound )
                     {
                         rsvc.info("ResourceManager : found " + resourceName + 
                                      " with loader " + resourceLoader.getClassName() );
                     }
   
                     howOldItWas = resourceLoader.getLastModified( resource );
                     break;
                 }
            }
            catch( ResourceNotFoundException rnfe )
            {
                /*
                 *  that's ok - it's possible to fail in
                 *  multi-loader environment
                 */
            }
        }
                
        /*
         * Return null if we can't find a resource.
         */
        if (resource.getData() == null)
        {
            throw new ResourceNotFoundException(
                "Unable to find resource '" + resourceName + "'");
        }

        /*
         *  some final cleanup
         */
         
        resource.setLastModified( howOldItWas );
         
        resource.setModificationCheckInterval(
            resourceLoader.getModificationCheckInterval());
        
        resource.touch();
    
        return resource;            
    }
    
    /**
     *  Takes an existing resource, and 'refreshes' it.  This
     *  generally means that the source of the resource is checked
     *  for changes according to some cache/check algorithm
     *  and if the resource changed, then the resource data is
     *  reloaded and re-parsed.
     *
     *  @param resource resource to refresh
     *
     * @throws ResourceNotFoundException if template not found
     *          from current source for this Resource
     * @throws ParseErrorException if template cannot be parsed due
     *          to syntax (or other) error.
     * @throws Exception if a problem in parse
     */
    protected void refreshResource( Resource resource, String encoding )
        throws ResourceNotFoundException, ParseErrorException, Exception
    {
        /* 
         * The resource knows whether it needs to be checked
         * or not, and the resource's loader can check to
         * see if the source has been modified. If both
         * these conditions are true then we must reload
         * the input stream and parse it to make a new
         * AST for the resource.
         */
        if ( resource.requiresChecking() )
        {
            /*
             *  touch() the resource to reset the counters
             */

            resource.touch();
            
            if(  resource.isSourceModified() )
            {
                /*
                 *  now check encoding info.  It's possible that the newly declared
                 *  encoding is different than the encoding already in the resource
                 *  this strikes me as bad...
                 */
                
                if (!resource.getEncoding().equals( encoding ) )
                {
                    rsvc.error("Declared encoding for template '" + resource.getName() 
                                  + "' is different on reload.  Old = '" + resource.getEncoding()
                                  + "'  New = '" + encoding );
    
                    resource.setEncoding( encoding );
                }

                /*
                 *  read how old the resource is _before_
                 *  processing (=>reading) it
                 */
                long howOldItWas = resource.getResourceLoader().getLastModified( resource );

                /*
                 *  read in the fresh stream and parse
                 */

                resource.process();

                /*
                 *  now set the modification info and reset
                 *  the modification check counters
                 */
                
                resource.setLastModified( howOldItWas );
            }
        }
    }

     /**
     * Gets the named resource.  Returned class type corresponds to specified type
     * (i.e. <code>Template</code> to <code>RESOURCE_TEMPLATE</code>).
     *
     * @param resourceName The name of the resource to retrieve.
     * @param resourceType The type of resource (<code>RESOURCE_TEMPLATE</code>,
     *                     <code>RESOURCE_CONTENT</code>, etc.).
     * @return Resource with the template parsed and ready.
     * @throws ResourceNotFoundException if template not found
     *          from any available source.
     * @throws ParseErrorException if template cannot be parsed due
     *          to syntax (or other) error.
     * @throws Exception if a problem in parse
     *
     *  @deprecated Use
     *  {@link #getResource(String resourceName, int resourceType, 
     *          String encoding )}
     */
    public Resource getResource(String resourceName, int resourceType  )
        throws ResourceNotFoundException, ParseErrorException, Exception
    {  
        return getResource( resourceName, resourceType, RuntimeConstants.ENCODING_DEFAULT);
    }

    /**
     *  Determines is a template exists, and returns name of the loader that 
     *  provides it.  This is a slightly less hokey way to support
     *  the Velocity.templateExists() utility method, which was broken
     *  when per-template encoding was introduced.  We can revisit this.
     *
     *  @param resourceName Name of template or content resource
     *  @return class name of loader than can provide it
     */
    public String getLoaderNameForResource(String resourceName )
    {
        ResourceLoader resourceLoader = null;
       
        /*
         *  loop through our loaders...
         */
        for (int i = 0; i < resourceLoaders.size(); i++)
        { 
            resourceLoader = (ResourceLoader) resourceLoaders.get(i);

            InputStream is = null;

            /*
             *  if we find one that can provide the resource,
             *  return the name of the loaders's Class
             */
            try
            {
                is=resourceLoader.getResourceStream( resourceName );
               
                if( is != null)
                {
                    return resourceLoader.getClass().toString();
                }
            }
            catch( ResourceNotFoundException e)
            {
                /*
                 * this isn't a problem.  keep going
                 */
            }
            finally
            {
                /*
                 *  if we did find one, clean up because we were 
                 *  returned an open stream
                 */
                if (is != null)
                {
                    try
                    {
                        is.close();
                    }
                    catch( IOException ioe)
                    {
                    }
                }
            }
        }

        return null;
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -