Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.dev.resource.impl;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.dev.resource.Resource;
import com.google.gwt.dev.resource.ResourceOracle;
import com.google.gwt.dev.util.log.speedtracer.CompilerEventType;
import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger;
import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
import com.google.gwt.dev.util.msg.Message0;
import com.google.gwt.dev.util.msg.Message1String;
import org.apache.commons.collections.map.AbstractReferenceMap;
import org.apache.commons.collections.map.ReferenceMap;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* The normal implementation of {@link ResourceOracle}.
*/
public class ResourceOracleImpl implements ResourceOracle {
private static class Messages {
static final Message1String EXAMINING_PATH_ROOT = new Message1String(
TreeLogger.DEBUG, "Searching for resources within $0");
static final Message1String IGNORING_SHADOWED_RESOURCE = new Message1String(
TreeLogger.DEBUG,
"Resource '$0' is being shadowed by another resource higher in the classpath having the same name; this one will not be used");
static final Message0 REFRESHING_RESOURCES = new Message0(TreeLogger.TRACE,
"Refreshing resources");
}
/**
* Wrapper object around a resource to change its path when it is rerooted.
*/
private static class RerootedResource extends AbstractResource {
private final String path;
private final AbstractResource resource;
public RerootedResource(AbstractResource resource, PathPrefix pathPrefix) {
this.path = pathPrefix.getRerootedPath(resource.getPath());
this.resource = resource;
}
@Override
public ClassPathEntry getClassPathEntry() {
return resource.getClassPathEntry();
}
@Override
public long getLastModified() {
return resource.getLastModified();
}
@Override
public String getLocation() {
return resource.getLocation();
}
@Override
public String getPath() {
return path;
}
@Override
public InputStream openContents() {
return resource.openContents();
}
@Override
public boolean wasRerooted() {
return true;
}
}
private static class ResourceData implements Comparable {
public final PathPrefix pathPrefix;
public final AbstractResource resource;
public ResourceData(AbstractResource resource, PathPrefix pathPrefix) {
this.resource = pathPrefix.shouldReroot() ? new RerootedResource(
resource, pathPrefix) : resource;
this.pathPrefix = pathPrefix;
}
public int compareTo(ResourceData other) {
// Rerooted takes precedence over not rerooted.
if (this.resource.wasRerooted() != other.resource.wasRerooted()) {
return this.resource.wasRerooted() ? 1 : -1;
}
// Compare priorities of the path prefixes, high number == high priority.
return this.pathPrefix.getPriority() - other.pathPrefix.getPriority();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ResourceData)) {
return false;
}
ResourceData other = (ResourceData) o;
return this.pathPrefix.getPriority() == other.pathPrefix.getPriority()
&& this.resource.wasRerooted() == other.resource.wasRerooted();
}
@Override
public int hashCode() {
return (pathPrefix.getPriority() << 1) + (resource.wasRerooted() ? 1 : 0);
}
@Override
public String toString() {
return "{" + resource + "," + pathPrefix + "}";
}
}
@SuppressWarnings("unchecked")
private static final Map> classPathCache = new ReferenceMap(
AbstractReferenceMap.WEAK, AbstractReferenceMap.HARD);
public static ClassPathEntry createEntryForUrl(TreeLogger logger, URL url)
throws URISyntaxException, IOException {
if (url.getProtocol().equals("file")) {
File f = new File(url.toURI());
String lowerCaseFileName = f.getName().toLowerCase(Locale.ENGLISH);
if (f.isDirectory()) {
return new DirectoryClassPathEntry(f);
} else if (f.isFile() && lowerCaseFileName.endsWith(".jar")) {
return ZipFileClassPathEntry.get(f);
} else if (f.isFile() && lowerCaseFileName.endsWith(".zip")) {
return ZipFileClassPathEntry.get(f);
} else {
// It's a file ending in neither jar nor zip, speculatively try to
// open as jar/zip anyway.
try {
return ZipFileClassPathEntry.get(f);
} catch (Exception ignored) {
}
logger.log(TreeLogger.TRACE, "Unexpected entry in classpath; " + f
+ " is neither a directory nor an archive (.jar or .zip)");
return null;
}
} else {
logger.log(TreeLogger.WARN, "Unknown URL type for " + url, null);
return null;
}
}
/**
* Preinitializes the classpath from the thread default {@link ClassLoader}.
*/
public static void preload(TreeLogger logger) {
preload(logger, Thread.currentThread().getContextClassLoader());
}
/**
* Preinitializes the classpath for a given {@link ClassLoader}.
*/
public static void preload(TreeLogger logger, ClassLoader classLoader) {
Event resourceOracle = SpeedTracerLogger.start(
CompilerEventType.RESOURCE_ORACLE, "phase", "preload");
List entries = getAllClassPathEntries(logger, classLoader);
for (ClassPathEntry entry : entries) {
// We only handle pre-indexing jars, the file system could change.
if (entry instanceof ZipFileClassPathEntry) {
ZipFileClassPathEntry zpe = (ZipFileClassPathEntry) entry;
zpe.index(logger);
}
}
resourceOracle.end();
}
/**
* Rescans the associated paths to recompute the available resources.
*
* TODO(conroy,scottb): This synchronization could be improved upon to allow
* disjoint sets of oracles to be refreshed simultaneously.
*
* @param logger status and error details are written here
* @param first At least one ResourceOracleImpl must be passed to refresh
* @param rest Callers may optionally pass several oracles
*/
public static synchronized void refresh(
TreeLogger logger, ResourceOracleImpl first, ResourceOracleImpl... rest) {
int len = 1 + rest.length;
ResourceOracleImpl[] oracles = new ResourceOracleImpl[1 + rest.length];
oracles[0] = first;
System.arraycopy(rest, 0, oracles, 1, rest.length);
Event resourceOracle =
SpeedTracerLogger.start(CompilerEventType.RESOURCE_ORACLE, "phase", "refresh");
TreeLogger refreshBranch = Messages.REFRESHING_RESOURCES.branch(logger,
null);
/*
* Allocate fresh data structures in anticipation of needing to honor the
* "new identity for the collections if anything changes" guarantee. Use a
* LinkedHashMap because we do not want the order to change.
*/
List