Java Code Examples for org.springframework.util.ResourceUtils#extractJarFileURL()

The following examples show how to use org.springframework.util.ResourceUtils#extractJarFileURL() . 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
Source File: DefaultPersistenceUnitManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Try to determine the persistence unit root URL based on the given
 * "defaultPersistenceUnitRootLocation".
 * @return the persistence unit root URL to pass to the JPA PersistenceProvider
 * @see #setDefaultPersistenceUnitRootLocation
 */
@Nullable
private URL determineDefaultPersistenceUnitRootUrl() {
	if (this.defaultPersistenceUnitRootLocation == null) {
		return null;
	}
	try {
		URL url = this.resourcePatternResolver.getResource(this.defaultPersistenceUnitRootLocation).getURL();
		return (ResourceUtils.isJarURL(url) ? ResourceUtils.extractJarFileURL(url) : url);
	}
	catch (IOException ex) {
		throw new PersistenceException("Unable to resolve persistence unit root URL", ex);
	}
}
 
Example 2
Source File: PersistenceUnitReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine the persistence unit root URL based on the given resource
 * (which points to the {@code persistence.xml} file we're reading).
 * @param resource the resource to check
 * @return the corresponding persistence unit root URL
 * @throws IOException if the checking failed
 */
@Nullable
static URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
	URL originalURL = resource.getURL();

	// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
	if (ResourceUtils.isJarURL(originalURL)) {
		return ResourceUtils.extractJarFileURL(originalURL);
	}

	// Check META-INF folder
	String urlToString = originalURL.toExternalForm();
	if (!urlToString.contains(META_INF)) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}
	if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}

	String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
	if (persistenceUnitRoot.endsWith("/")) {
		persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
	}
	return new URL(persistenceUnitRoot);
}
 
Example 3
Source File: DefaultPersistenceUnitManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Try to determine the persistence unit root URL based on the given
 * "defaultPersistenceUnitRootLocation".
 * @return the persistence unit root URL to pass to the JPA PersistenceProvider
 * @see #setDefaultPersistenceUnitRootLocation
 */
@Nullable
private URL determineDefaultPersistenceUnitRootUrl() {
	if (this.defaultPersistenceUnitRootLocation == null) {
		return null;
	}
	try {
		URL url = this.resourcePatternResolver.getResource(this.defaultPersistenceUnitRootLocation).getURL();
		return (ResourceUtils.isJarURL(url) ? ResourceUtils.extractJarFileURL(url) : url);
	}
	catch (IOException ex) {
		throw new PersistenceException("Unable to resolve persistence unit root URL", ex);
	}
}
 
Example 4
Source File: PersistenceUnitReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine the persistence unit root URL based on the given resource
 * (which points to the {@code persistence.xml} file we're reading).
 * @param resource the resource to check
 * @return the corresponding persistence unit root URL
 * @throws IOException if the checking failed
 */
@Nullable
static URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
	URL originalURL = resource.getURL();

	// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
	if (ResourceUtils.isJarURL(originalURL)) {
		return ResourceUtils.extractJarFileURL(originalURL);
	}

	// Check META-INF folder
	String urlToString = originalURL.toExternalForm();
	if (!urlToString.contains(META_INF)) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}
	if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}

	String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
	if (persistenceUnitRoot.endsWith("/")) {
		persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
	}
	return new URL(persistenceUnitRoot);
}
 
Example 5
Source File: DefaultPersistenceUnitManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to determine the persistence unit root URL based on the given
 * "defaultPersistenceUnitRootLocation".
 * @return the persistence unit root URL to pass to the JPA PersistenceProvider
 * @see #setDefaultPersistenceUnitRootLocation
 */
private URL determineDefaultPersistenceUnitRootUrl() {
	if (this.defaultPersistenceUnitRootLocation == null) {
		return null;
	}
	try {
		URL url = this.resourcePatternResolver.getResource(this.defaultPersistenceUnitRootLocation).getURL();
		return (ResourceUtils.isJarURL(url) ? ResourceUtils.extractJarFileURL(url) : url);
	}
	catch (IOException ex) {
		throw new PersistenceException("Unable to resolve persistence unit root URL", ex);
	}
}
 
Example 6
Source File: PersistenceUnitReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the persistence unit root URL based on the given resource
 * (which points to the {@code persistence.xml} file we're reading).
 * @param resource the resource to check
 * @return the corresponding persistence unit root URL
 * @throws IOException if the checking failed
 */
static URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
	URL originalURL = resource.getURL();

	// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
	if (ResourceUtils.isJarURL(originalURL)) {
		return ResourceUtils.extractJarFileURL(originalURL);
	}

	// Check META-INF folder
	String urlToString = originalURL.toExternalForm();
	if (!urlToString.contains(META_INF)) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}
	if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}

	String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
	if (persistenceUnitRoot.endsWith("/")) {
		persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
	}
	return new URL(persistenceUnitRoot);
}
 
Example 7
Source File: PersistenceUnitReader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the persistence unit root URL based on the given resource
 * (which points to the {@code persistence.xml} file we're reading).
 * @param resource the resource to check
 * @return the corresponding persistence unit root URL
 * @throws IOException if the checking failed
 */
protected URL determinePersistenceUnitRootUrl(Resource resource) throws IOException {
	URL originalURL = resource.getURL();

	// If we get an archive, simply return the jar URL (section 6.2 from the JPA spec)
	if (ResourceUtils.isJarURL(originalURL)) {
		return ResourceUtils.extractJarFileURL(originalURL);
	}

	// check META-INF folder
	String urlToString = originalURL.toExternalForm();
	if (!urlToString.contains(META_INF)) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" should be located inside META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}
	if (urlToString.lastIndexOf(META_INF) == urlToString.lastIndexOf('/') - (1 + META_INF.length())) {
		if (logger.isInfoEnabled()) {
			logger.info(resource.getFilename() +
					" is not located in the root of META-INF directory; cannot determine persistence unit root URL for " +
					resource);
		}
		return null;
	}

	String persistenceUnitRoot = urlToString.substring(0, urlToString.lastIndexOf(META_INF));
	if (persistenceUnitRoot.endsWith("/")) {
		persistenceUnitRoot = persistenceUnitRoot.substring(0, persistenceUnitRoot.length() - 1);
	}
	return new URL(persistenceUnitRoot);
}
 
Example 8
Source File: AbstractFileResolvingResource.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation determines the underlying File (or jar file, in case
 * of a resource in a jar/zip).
 */
@Override
protected File getFileForLastModifiedCheck() throws IOException {
    URL url = getURL();
    if (ResourceUtils.isJarURL(url)) {
        URL actualUrl = ResourceUtils.extractJarFileURL(url);
        if (actualUrl.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
            return VfsResourceDelegate.getResource(actualUrl).getFile();
        }
        return ResourceUtils.getFile(actualUrl, "Jar URL");
    } else {
        return getFile();
    }
}