Search in sources :

Example 1 with CheckstyleConfigurationFile

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) Authenticator(java.net.Authenticator)

Example 2 with CheckstyleConfigurationFile

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;
}
Also used : CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) URL(java.net.URL)

Example 3 with CheckstyleConfigurationFile

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;
}
Also used : CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) URLConnection(java.net.URLConnection)

Example 4 with CheckstyleConfigurationFile

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;
}
Also used : PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) File(java.io.File) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 5 with CheckstyleConfigurationFile

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;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) InputSource(org.xml.sax.InputSource) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware)

Aggregations

CheckstyleConfigurationFile (net.sf.eclipsecs.core.config.CheckstyleConfigurationFile)7 PropertyResolver (com.puppycrawl.tools.checkstyle.PropertyResolver)4 InputSource (org.xml.sax.InputSource)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 URLConnection (java.net.URLConnection)2 IContextAware (net.sf.eclipsecs.core.config.configtypes.IContextAware)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Authenticator (java.net.Authenticator)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 PropertyResourceBundle (java.util.PropertyResourceBundle)1 ResourceBundle (java.util.ResourceBundle)1 AdditionalConfigData (net.sf.eclipsecs.core.config.ConfigurationReader.AdditionalConfigData)1