Java Resource File getLastModificationForResource(String resourceId)

Here you can find the source of getLastModificationForResource(String resourceId)

Description

Get the last modification date if the resource resolves to a file.

License

Open Source License

Parameter

Parameter Description
resourceId a parameter

Declaration

public static long getLastModificationForResource(String resourceId) 

Method Source Code

//package com.java2s;
/**/*from  w w  w.  j  a va  2 s  .  co  m*/
 * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
 * www.integratedmodelling.org. 
    
   This file is part of Thinklab.
    
   Thinklab is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.
    
   Thinklab is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with Thinklab.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Date;

public class Main {
    /**
     * Get the last modification date if the resource resolves to a file. If
     * the resource is a URL, we cannot be sure so we assume it's just been
     * modified and return the current time.
     * 
     * @param resourceId
     * @return
     */
    public static long getLastModificationForResource(String resourceId) {

        long ret = new Date().getTime();

        Object o = getSourceForResource(resourceId);
        if (o instanceof File) {
            ret = ((File) o).lastModified();
        }

        return ret;
    }

    /**
     * Analyze the passed string and determine if it specifies an existing file resource
     * or URL. Return the appropriate object, that must be disambiguated using instanceof.
     * Meant to (inelegantly) solve problems coming from file name encodings in primitive
     * OS (e.g. Windows) that cannot be handled properly in file:// URLs. 
     * @return a valid URL, existing file, or null. No exceptions are thrown.
     */
    public static Object getSourceForResource(String s) {

        File f = new File(s);

        if (f.exists())
            return f;

        URL url = null;
        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
        }

        if (url != null)
            return url;

        return null;
    }
}

Related

  1. getFilename(Class clazz, String resource)
  2. getFileName(final Object resource)
  3. getFileResource(String resourceName)
  4. getInputStream(String resourceOrFile, Class cls)
  5. getInputStreamForResource(String s)
  6. getLastModified(final String resourceName)
  7. getParentResource(final Class c, final String resource)
  8. getSampleDir(File resourcesDir)
  9. getShortName(String resource, Map nsMap)