use of net.sf.eclipsecs.core.config.CheckstyleConfigurationFile in project eclipse-cs by checkstyle.
the class RemoteConfigurationType method getCheckstyleConfiguration.
/**
* {@inheritDoc}
*/
@Override
public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration) throws CheckstylePluginException {
boolean useCacheFile = Boolean.valueOf(checkConfiguration.getAdditionalData().get(KEY_CACHE_CONFIG)).booleanValue();
CheckstyleConfigurationFile data = new CheckstyleConfigurationFile();
synchronized (Authenticator.class) {
// $NON-NLS-1$
String currentRedirects = System.getProperty("http.maxRedirects");
Authenticator oldAuthenticator = RemoteConfigAuthenticator.getDefault();
try {
// resolve the true configuration file URL
data.setResolvedConfigFileURL(resolveLocation(checkConfiguration));
Authenticator.setDefault(new RemoteConfigAuthenticator(data.getResolvedConfigFileURL()));
boolean originalFileSuccess = false;
byte[] configurationFileData = null;
try {
// $NON-NLS-1$ //$NON-NLS-2$
System.setProperty("http.maxRedirects", "3");
URLConnection connection = data.getResolvedConfigFileURL().openConnection();
// get the configuration file data
configurationFileData = getBytesFromURLConnection(connection);
// get last modification timestamp
data.setModificationStamp(connection.getLastModified());
originalFileSuccess = true;
} catch (IOException e) {
if (useCacheFile) {
configurationFileData = getBytesFromCacheFile(checkConfiguration);
} else {
throw e;
}
}
data.setCheckConfigFileBytes(configurationFileData);
// get the properties bundle
byte[] additionalPropertiesBytes = null;
if (originalFileSuccess) {
additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL());
} else if (useCacheFile) {
additionalPropertiesBytes = getBytesFromCacheBundleFile(checkConfiguration);
}
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
// get the property resolver
PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
data.setPropertyResolver(resolver);
// write to cache file
if (originalFileSuccess && useCacheFile) {
writeToCacheFile(checkConfiguration, configurationFileData, additionalPropertiesBytes);
}
} catch (UnknownHostException e) {
CheckstylePluginException.rethrow(e, NLS.bind(Messages.RemoteConfigurationType_errorUnknownHost, e.getMessage()));
} catch (FileNotFoundException e) {
CheckstylePluginException.rethrow(e, NLS.bind(Messages.RemoteConfigurationType_errorFileNotFound, e.getMessage()));
} catch (IOException | URISyntaxException e) {
CheckstylePluginException.rethrow(e);
} finally {
Authenticator.setDefault(oldAuthenticator);
if (currentRedirects != null) {
// $NON-NLS-1$
System.setProperty("http.maxRedirects", currentRedirects);
} else {
// $NON-NLS-1$
System.getProperties().remove("http.maxRedirects");
}
}
}
return data;
}
use of net.sf.eclipsecs.core.config.CheckstyleConfigurationFile in project eclipse-cs by checkstyle.
the class CheckerFactory method getCacheKey.
/**
* Build a unique cache key for the check configuration.
*
* @param config
* the check configuration
* @param project
* the project being checked
* @return the unique cache key
* @throws CheckstylePluginException
* error getting configuration file data
*/
private static String getCacheKey(ICheckConfiguration config, IProject project) throws CheckstylePluginException {
CheckstyleConfigurationFile configFileData = config.getCheckstyleConfiguration();
URL configLocation = configFileData.getResolvedConfigFileURL();
String checkConfigName = config.getName() + "#" + (config.isGlobal() ? "Global" : "Local");
String cacheKey = project.getName() + "#" + configLocation + "#" + checkConfigName;
return cacheKey;
}
use of net.sf.eclipsecs.core.config.CheckstyleConfigurationFile in project eclipse-cs by checkstyle.
the class ConfigurationType method getCheckstyleConfiguration.
@Override
public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration) throws CheckstylePluginException {
CheckstyleConfigurationFile data = new CheckstyleConfigurationFile();
try {
// resolve the true configuration file URL
data.setResolvedConfigFileURL(resolveLocation(checkConfiguration));
URLConnection connection = data.getResolvedConfigFileURL().openConnection();
connection.connect();
// get last modification timestamp
data.setModificationStamp(connection.getLastModified());
// get the configuration file data
byte[] configurationFileData = getBytesFromURLConnection(connection);
data.setCheckConfigFileBytes(configurationFileData);
// get the properties bundle
byte[] additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL());
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
// get the property resolver
PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
data.setPropertyResolver(resolver);
} catch (IOException | URISyntaxException e) {
CheckstylePluginException.rethrow(e);
}
return data;
}
use of net.sf.eclipsecs.core.config.CheckstyleConfigurationFile in project eclipse-cs by checkstyle.
the class ConfigurationType method getPropertyResolver.
/**
* Gets the property resolver for this configuration type used to expand property values within
* the checkstyle configuration.
*
* @param checkConfiguration
* the actual check configuration
* @return the property resolver
* @throws IOException
* error creating the property resolver
* @throws URISyntaxException
*/
protected PropertyResolver getPropertyResolver(ICheckConfiguration config, CheckstyleConfigurationFile configFile) throws IOException, URISyntaxException {
MultiPropertyResolver multiResolver = new MultiPropertyResolver();
multiResolver.addPropertyResolver(new ResolvablePropertyResolver(config));
File f = URIUtil.toFile(configFile.getResolvedConfigFileURL().toURI());
if (f != null) {
multiResolver.addPropertyResolver(new StandardPropertyResolver(f.toString()));
} else {
multiResolver.addPropertyResolver(new StandardPropertyResolver(configFile.getResolvedConfigFileURL().toString()));
}
multiResolver.addPropertyResolver(new ClasspathVariableResolver());
multiResolver.addPropertyResolver(new SystemPropertyResolver());
if (configFile.getAdditionalPropertiesBundleStream() != null) {
ResourceBundle bundle = new PropertyResourceBundle(configFile.getAdditionalPropertiesBundleStream());
multiResolver.addPropertyResolver(new ResourceBundlePropertyResolver(bundle));
}
return multiResolver;
}
use of net.sf.eclipsecs.core.config.CheckstyleConfigurationFile in project eclipse-cs by checkstyle.
the class CheckerFactory method createChecker.
/**
* Creates a checker for a given configuration file.
*
* @param config
* the check configuration data
* @param project
* the project to create the checker for
* @return the checker for the given configuration file
* @throws CheckstyleException
* the configuration file had errors
* @throws CheckstylePluginException
* the configuration could not be read
*/
public static Checker createChecker(ICheckConfiguration config, IProject project) throws CheckstyleException, CheckstylePluginException {
String cacheKey = getCacheKey(config, project);
CheckstyleConfigurationFile configFileData = config.getCheckstyleConfiguration();
Checker checker = tryCheckerCache(cacheKey, configFileData.getModificationStamp());
// clear Checkstyle internal caches upon checker reuse
if (checker != null) {
checker.clearCache();
}
// no cache hit
if (checker == null) {
PropertyResolver resolver = configFileData.getPropertyResolver();
// context
if (resolver instanceof IContextAware) {
((IContextAware) resolver).setProjectContext(project);
}
InputSource in = null;
try {
in = configFileData.getCheckConfigFileInputSource();
checker = createCheckerInternal(in, resolver, project);
} finally {
Closeables.closeQuietly(in.getByteStream());
}
// store checker in cache
Long modified = new Long(configFileData.getModificationStamp());
sCheckerMap.put(cacheKey, checker);
sModifiedMap.put(cacheKey, modified);
}
return checker;
}
Aggregations