Java Code Examples for org.springframework.core.io.Resource#lastModified()

The following examples show how to use org.springframework.core.io.Resource#lastModified() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
protected PropertiesHolder refreshProperties(String filename, PropertiesHolder propHolder) {
    Properties properties = new Properties();
    long lastModified = -1;
    try {
        Resource[] resources = resolver.getResources(filename + "*" + PROPERTIES_SUFFIX);
        for (Resource resource : resources) {
            String sourcePath = resource.getURI().toString().replace(PROPERTIES_SUFFIX, "");
            PropertiesHolder holder = super.refreshProperties(sourcePath, propHolder);
            properties.putAll(holder.getProperties());
            if (lastModified < resource.lastModified())
                lastModified = resource.lastModified();
        }
    } catch (IOException ignored) {
        throw new BusinessException("load message source error:" + ignored);
    }
    return new PropertiesHolder(properties, lastModified);
}
 
Example 2
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 3
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 4
@Override
public long getLastModified(Object templateSource) {
	Resource resource = (Resource) templateSource;
	try {
		return resource.lastModified();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Could not obtain last-modified timestamp for FreeMarker template in " +
					resource + ": " + ex);
		}
		return -1;
	}
}
 
Example 5
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 6
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 7
@Override
public long getLastModified(Object templateSource) {
	Resource resource = (Resource) templateSource;
	try {
		return resource.lastModified();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Could not obtain last-modified timestamp for FreeMarker template in " +
					resource + ": " + ex);
		}
		return -1;
	}
}
 
Example 8
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 9
@Override
public long getLastModified(Object templateSource) {
	Resource resource = (Resource) templateSource;
	try {
		return resource.lastModified();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Could not obtain last-modified timestamp for FreeMarker template in " +
					resource + ": " + ex);
		}
		return -1;
	}
}
 
Example 10
public TransformedResource(Resource original, byte[] transformedContent) {
	super(transformedContent);
	this.filename = original.getFilename();
	try {
		this.lastModified = original.lastModified();
	}
	catch (IOException ex) {
		// should never happen
		throw new IllegalArgumentException(ex);
	}
}
 
Example 11
@Override
public long getLastModified(Object templateSource) {
	Resource resource = (Resource) templateSource;
	try {
		return resource.lastModified();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug("Could not obtain last-modified timestamp for FreeMarker template in " +
					resource + ": " + ex);
		}
		return -1;
	}
}
 
Example 12
@Override
protected PropertiesHolder refreshProperties(final String filename, final PropertiesHolder propHolder) {
    final Properties properties = new Properties();
    long lastModified = -1;
    if (!(resourceLoader instanceof ResourcePatternResolver)) {
        LOGGER.warn(
                "Resource Loader {} doensn't support getting multiple resources. Default properties mechanism will used",
                resourceLoader.getClass().getName());
        return super.refreshProperties(filename, propHolder);
    }

    try {
        final Resource[] resources = ((ResourcePatternResolver) resourceLoader)
                .getResources(filename + PROPERTIES_SUFFIX);
        for (final Resource resource : resources) {
            final String sourcePath = resource.getURI().toString().replace(PROPERTIES_SUFFIX, "");
            final PropertiesHolder holder = super.refreshProperties(sourcePath, propHolder);
            properties.putAll(holder.getProperties());
            if (lastModified < resource.lastModified()) {
                lastModified = resource.lastModified();
            }
        }
    } catch (final IOException ignored) {
        LOGGER.warn("Resource with filname " + filename + " couldn't load", ignored);
    }
    return new PropertiesHolder(properties, lastModified);
}
 
Example 13
Source Project: olat   File: Settings.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * [used by spring]
 */
protected Settings() {
    // extract the latest build number as date where this class was compiled
    Resource res = new ClassPathResource("org/olat/system/commons/Settings.class");
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        buildDate = new Date(res.lastModified());
        buildIdentifier = formatter.format(buildDate);
    } catch (IOException e) {
        buildIdentifier = "00000000";
    }
}
 
Example 14
Source Project: olat   File: Settings.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * [used by spring]
 */
protected Settings() {
    // extract the latest build number as date where this class was compiled
    Resource res = new ClassPathResource("org/olat/system/commons/Settings.class");
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        buildDate = new Date(res.lastModified());
        buildIdentifier = formatter.format(buildDate);
    } catch (IOException e) {
        buildIdentifier = "00000000";
    }
}
 
Example 15
@Override
	protected Resource getResource(HttpServletRequest request) throws IOException {
		Map<String, Object> pathVariables = (Map<String, Object>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
		String key = (String) pathVariables.get("key");

		Media media = mediaService.getMedia(key);
		if (media == null) {
			return null;
		}

		RequestContextHolder.getRequestAttributes().setAttribute(MEDIA_ATTRIBUTE, media, RequestAttributes.SCOPE_REQUEST);

		int width = ServletRequestUtils.getIntParameter(request, "w", 0);
		int height = ServletRequestUtils.getIntParameter(request, "h", 0);
		Media.ResizeMode mode = Media.ResizeMode.values()[ServletRequestUtils.getIntParameter(request, "m", 0)];

//		Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
//		final Resource prefix = resourceLoader.getResource(blog.getMediaPath());
		final Resource prefix = resourceLoader.getResource(wallRideProperties.getMediaLocation());
		final Resource resource = prefix.createRelative(media.getId());

		if (!resource.exists()) {
			return null;
		}

		Resource resized = resource;
		boolean doResize = (width > 0 || height > 0);
		if (doResize && "image".equals(MediaType.parseMediaType(media.getMimeType()).getType())) {
			resized = prefix.createRelative(String.format("%s.resized.%dx%d-%d",
					media.getId(),
					width, height, mode.ordinal()));
			if (!resized.exists() || resource.lastModified() > resized.lastModified()) {
				File temp = File.createTempFile(
						getClass().getCanonicalName() + ".resized-",
						"." + MediaType.parseMediaType(media.getMimeType()).getSubtype());
				temp.deleteOnExit();
				resizeImage(resource, temp, width, height, mode);

				ExtendedResourceUtils.write(resized, temp);
				FileUtils.deleteQuietly(temp);
			}
		}
		return resized;
	}
 
Example 16
Source Project: genie   File: FetchingCacheServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
private void lookupOrDownload(
    final URI sourceFileUri,
    final File destinationFile
) throws DownloadException, IOException {

    final String uriString = sourceFileUri.toASCIIString();

    log.debug("Lookup: {}", uriString);

    // Unique id to store the resource on local disk
    final String resourceCacheId = getResourceCacheId(sourceFileUri);

    // Get a handle to the resource
    final Resource resource;
    try {
        resource = resourceLoader.getResource(uriString);
    } catch (Throwable t) {
        log.error(
            "Failed to retrieve resource: {}, {} - {}",
            uriString,
            t.getClass().getSimpleName(),
            t.getMessage()
        );
        throw t;
    }

    if (!resource.exists()) {
        throw new DownloadException("Resource not found: " + uriString);
    }

    final long resourceLastModified = resource.lastModified();

    //Handle to resourceCacheId/version
    final File cacheResourceVersionDir = getCacheResourceVersionDir(
        resourceCacheId,
        resourceLastModified
    );

    //Create the resource version dir in cache if it does not exist
    createDirectoryStructureIfNotExists(cacheResourceVersionDir);

    try (
        CloseableLock lock = fileLockFactory.getLock(
            touchCacheResourceVersionLockFile(
                resourceCacheId,
                resourceLastModified
            )
        )
    ) {
        //Critical section begin
        lock.lock();

        //Handle to the resource cached locally
        final File cachedResourceVersionDataFile = getCacheResourceVersionDataFile(
            resourceCacheId,
            resourceLastModified
        );

        if (!cachedResourceVersionDataFile.exists()) {
            log.debug(
                "Cache miss: {} (id: {})",
                uriString,
                resourceCacheId
            );

            // Download the resource into the download file in cache
            // resourceCacheId/version/data.tmp
            final File cachedResourceVersionDownloadFile = getCacheResourceVersionDownloadFile(
                resourceCacheId,
                resourceLastModified
            );
            try (
                InputStream in = resource.getInputStream();
                OutputStream out = new FileOutputStream(cachedResourceVersionDownloadFile)
            ) {
                FileCopyUtils.copy(in, out);
                Files.move(cachedResourceVersionDownloadFile, cachedResourceVersionDataFile);
            }
        } else {
            log.debug(
                "Cache hit: {} (id: {})",
                uriString,
                resourceCacheId
            );
        }

        //Copy from cache data file resourceCacheId/version/DATA_FILE_NAME to targetFile
        Files.copy(cachedResourceVersionDataFile, destinationFile);
        //Critical section end
    } catch (LockException e) {
        throw new DownloadException("Error downloading dependency", e);
    }

    //Clean up any older versions
    cleanUpTaskExecutor.execute(
        new CleanupOlderVersionsTask(resourceCacheId, resourceLastModified)
    );
}