Example usage for java.net URLConnection getLastModified

List of usage examples for java.net URLConnection getLastModified

Introduction

In this page you can find the example usage for java.net URLConnection getLastModified.

Prototype

public long getLastModified() 

Source Link

Document

Returns the value of the last-modified header field.

Usage

From source file:org.apache.myfaces.trinidadbuild.plugin.faces.AbstractFacesMojo.java

protected boolean isModifiedSince(URL[] urls, long lastModified) throws IOException {
    for (int i = 0; i < urls.length; i++) {
        URLConnection conn = urls[i].openConnection();
        if (conn.getLastModified() > lastModified)
            return true;
    }/*from  w ww.ja va  2s.c  o m*/

    return false;
}

From source file:org.atricore.idbus.bundles.apache.tiles.OsgiDefinitionsFactory.java

/**
 * Indicates whether the DefinitionsFactory is out of date and needs to be
 * reloaded.//from w  w  w.  ja v  a  2 s .  c o m
 *
 * @return If the factory needs refresh.
 */
public boolean refreshRequired() {
    boolean status = false;

    Set<String> urls = lastModifiedDates.keySet();

    try {
        for (String urlPath : urls) {
            Long lastModifiedDate = lastModifiedDates.get(urlPath);
            URL url = new URL(urlPath);
            URLConnection connection = url.openConnection();
            connection.connect();
            long newModDate = connection.getLastModified();
            if (newModDate != lastModifiedDate) {
                status = true;
                break;
            }
        }
    } catch (Exception e) {
        logger.warn("Exception while monitoring update times.", e);
        return true;
    }
    return status;
}

From source file:org.apache.myfaces.trinidadbuild.plugin.faces.parse.FacesConfigParser.java

public void merge(FacesConfigBean owner, URL url) throws MojoExecutionException {
    try {// ww w  . jav  a  2  s.c o  m
        URLConnection conn = url.openConnection();
        long lastModified = conn.getLastModified();
        InputStream is = conn.getInputStream();

        if (is != null) {
            // Establish the current last-modified value
            // As new components are added, they will remember
            // this current value as their own last-modified
            owner.touch(lastModified);

            Digester digester = createDigester();
            digester.push(url);
            digester.push(owner);
            digester.parse(is);
            is.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to parse " + url, e);
    } catch (SAXException e) {
        throw new MojoExecutionException("Failed to parse " + url, e);
    } catch (ParserConfigurationException e) {
        throw new MojoExecutionException("Failed to parse " + url, e);
    }
}

From source file:org.squale.welcom.outils.WelcomConfigurator.java

/**
 * Initialise les proprits du fichier de configuration - lastDate - isGoodResources
 * /*from  w w w.  ja v a2s .  c o  m*/
 * @param pFileName fichier de configuration
 */
private void initializeFile(final String pFileName) {

    String realFileName = "";

    this.fileName = pFileName;

    InputStream is = null;

    if (!GenericValidator.isBlankOrNull(fileName)) {

        // Set up to load the property resource for this locale key, if we can
        realFileName = fileName.replace('.', '/') + ".properties";

        goodFileResources = true;
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        // Recherche la date de derniere modification
        final URL url = classLoader.getResource(realFileName);
        URLConnection urlConn;
        try {
            urlConn = url.openConnection();
            urlConn.setUseCaches(false);
            urlConn.connect();
            urlConn.getLastModified();
            lastDateFile = new Date(urlConn.getLastModified());
        } catch (final Exception e) {
            goodFileResources = false;
        }

        // Regarde si le fichier n'est pas vide
        is = classLoader.getResourceAsStream(realFileName);
        goodFileResources = (is != null);
    }

}

From source file:spring.osgi.io.OsgiBundleResource.java

public long lastModified() throws IOException {
    URLConnection con = getURL().openConnection();
    con.setUseCaches(false);//from ww w.  j a v a 2  s  . c  o m
    long time = con.getLastModified();
    // the implementation doesn't return the proper time stamp
    if (time == 0) {
        if (OsgiResourceUtils.PREFIX_TYPE_BUNDLE_JAR == searchType)
            return bundle.getLastModified();
    }
    // there is nothing else we can do
    return time;
}

From source file:org.opencms.main.CmsStaticResourceHandler.java

/**
 * Sets the response headers.<p>//from   ww w.  j  a  v  a 2s.  c o  m
 *
 * @param request the request
 * @param response the response
 * @param filename the file name
 * @param resourceURL the resource URL
 */
protected void setResponseHeaders(HttpServletRequest request, HttpServletResponse response, String filename,
        URL resourceURL) {

    String cacheControl = "public, max-age=0, must-revalidate";
    int resourceCacheTime = getCacheTime(filename);
    if (resourceCacheTime > 0) {
        cacheControl = "max-age=" + String.valueOf(resourceCacheTime);
    }
    response.setHeader("Cache-Control", cacheControl);
    response.setDateHeader("Expires", System.currentTimeMillis() + (resourceCacheTime * 1000));

    // Find the modification timestamp
    long lastModifiedTime = 0;
    URLConnection connection = null;
    try {
        connection = resourceURL.openConnection();
        lastModifiedTime = connection.getLastModified();
        // Remove milliseconds to avoid comparison problems (milliseconds
        // are not returned by the browser in the "If-Modified-Since"
        // header).
        lastModifiedTime = lastModifiedTime - (lastModifiedTime % 1000);
        response.setDateHeader("Last-Modified", lastModifiedTime);

        if (browserHasNewestVersion(request, lastModifiedTime)) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
    } catch (Exception e) {
        // Failed to find out last modified timestamp. Continue without it.
        LOG.debug("Failed to find out last modified timestamp. Continuing without it.", e);
    } finally {
        try {
            if (connection != null) {
                // Explicitly close the input stream to prevent it
                // from remaining hanging
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700
                InputStream is = connection.getInputStream();
                if (is != null) {
                    is.close();
                }
            }
        } catch (Exception e) {
            LOG.info("Error closing URLConnection input stream", e);
        }
    }

    // Set type mime type if we can determine it based on the filename
    String mimetype = OpenCms.getResourceManager().getMimeType(filename, "UTF-8");
    if (mimetype != null) {
        response.setContentType(mimetype);
    }
}

From source file:org.atricore.idbus.bundles.apache.tiles.OsgiDefinitionsFactory.java

/**
 * Creates and returns a {@link Definitions} set by reading
 * configuration data from the applied sources.
 *
 * @return The definitions holder object, filled with base definitions.
 * @throws DefinitionsFactoryException if an error occurs reading the
 * sources.//from  ww w .j a  va 2  s.  c o m
 */
public Definitions readDefinitions() throws DefinitionsFactoryException {
    Definitions definitions = createDefinitions();
    try {
        for (Object source1 : sources) {
            URL source = (URL) source1;
            URLConnection connection = source.openConnection();
            connection.connect();
            lastModifiedDates.put(source.toExternalForm(), connection.getLastModified());
            Map<String, Definition> defsMap = reader.read(connection.getInputStream());
            definitions.addDefinitions(defsMap);
        }
    } catch (IOException e) {
        throw new DefinitionsFactoryException("I/O error accessing source.", e);
    }
    return definitions;
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Use this if last modified in File didnt work , possible in compressed
 * file But it seem that it checks for the modification time for the hall
 * compressed file it worth checking./*  w  w  w . j  a v  a2 s  . co  m*/
 *
 * @param resourceName
 *            the resource name
 * @return the last modified
 */
public static long getLastModified(final String resourceName) {
    try {
        final URL url = Integer.class.getResource(resourceName);
        if (url == null) {
            return -1;
        }
        final URLConnection con = url.openConnection();
        final long lastModified = con.getLastModified();
        con.getInputStream().close();
        return lastModified;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.springframework.js.resource.ResourceServlet.java

protected long getLastModified(HttpServletRequest request) {
    if (log.isDebugEnabled()) {
        log.debug("Checking last modified of resource: " + request.getPathInfo());
    }//from ww w  . ja v  a 2 s .  com
    URL[] resources;
    try {
        resources = getRequestResourceURLs(request);
    } catch (MalformedURLException e) {
        return -1;
    }

    if (resources == null || resources.length == 0) {
        return -1;
    }

    long lastModified = -1;

    for (URL resource : resources) {
        URLConnection resourceConn;
        try {
            resourceConn = resource.openConnection();
        } catch (IOException e) {
            return -1;
        }
        if (resourceConn.getLastModified() > lastModified) {
            lastModified = resourceConn.getLastModified();
        }
    }
    return lastModified;
}

From source file:org.apache.hadoop.mapred.PoolManager.java

/**
 * Reload allocations file if it hasn't been loaded in a while
 */// w  w w  .j a v  a 2  s.c o m
public void reloadAllocsIfNecessary() {
    long time = System.currentTimeMillis();
    if (time > lastReloadAttempt + ALLOC_RELOAD_INTERVAL) {
        lastReloadAttempt = time;
        if (null == allocFile) {
            return;
        }
        try {
            // Get last modified time of alloc file depending whether it's a String
            // (for a path name) or an URL (for a classloader resource)
            long lastModified;
            if (allocFile instanceof String) {
                File file = new File((String) allocFile);
                lastModified = file.lastModified();
            } else { // allocFile is an URL
                URLConnection conn = ((URL) allocFile).openConnection();
                lastModified = conn.getLastModified();
            }
            if (lastModified > lastSuccessfulReload && time > lastModified + ALLOC_RELOAD_WAIT) {
                reloadAllocs();
                lastSuccessfulReload = time;
                lastReloadAttemptFailed = false;
            }
        } catch (Exception e) {
            // Throwing the error further out here won't help - the RPC thread
            // will catch it and report it in a loop. Instead, just log it and
            // hope somebody will notice from the log.
            // We log the error only on the first failure so we don't fill up the
            // JobTracker's log with these messages.
            if (!lastReloadAttemptFailed) {
                LOG.error("Failed to reload fair scheduler config file - " + "will use existing allocations.",
                        e);
            }
            lastReloadAttemptFailed = true;
        }
    }
}