Search in sources :

Example 1 with Environment

use of grails.util.Environment in project grails-core by grails.

the class DefaultGrailsPlugin method evaluatePluginScopes.

private void evaluatePluginScopes() {
    // Damn I wish Java had closures
    pluginScopes = evaluateIncludeExcludeProperty(SCOPES, new Closure(this) {

        private static final long serialVersionUID = 1;

        @Override
        public Object call(Object arguments) {
            final String scopeName = ((String) arguments).toUpperCase();
            try {
                return BuildScope.valueOf(scopeName);
            } catch (IllegalArgumentException e) {
                throw new GrailsConfigurationException("Plugin " + this + " specifies invalid scope [" + scopeName + "]");
            }
        }
    });
    pluginEnvs = evaluateIncludeExcludeProperty(ENVIRONMENTS, new Closure(this) {

        private static final long serialVersionUID = 1;

        @Override
        public Object call(Object arguments) {
            String envName = (String) arguments;
            Environment env = Environment.getEnvironment(envName);
            if (env != null)
                return env.getName();
            return arguments;
        }
    });
}
Also used : GrailsConfigurationException(org.grails.core.exceptions.GrailsConfigurationException) Environment(grails.util.Environment)

Example 2 with Environment

use of grails.util.Environment in project grails-core by grails.

the class DefaultGrailsPlugin method evaluateOnChangeListener.

private void evaluateOnChangeListener() {
    if (pluginBean.isReadableProperty(ON_SHUTDOWN)) {
        onShutdownListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_SHUTDOWN);
    }
    if (pluginBean.isReadableProperty(ON_CONFIG_CHANGE)) {
        onConfigChangeListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_CONFIG_CHANGE);
    }
    if (pluginBean.isReadableProperty(ON_CHANGE)) {
        onChangeListener = (Closure) GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, ON_CHANGE);
    }
    Environment env = Environment.getCurrent();
    final boolean warDeployed = env.isWarDeployed();
    final boolean reloadEnabled = env.isReloadEnabled();
    if (!((reloadEnabled || !warDeployed))) {
        return;
    }
    Object referencedResources = GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(plugin, WATCHED_RESOURCES);
    try {
        List resourceList = null;
        if (referencedResources instanceof String) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Configuring plugin " + this + " to watch resources with pattern: " + referencedResources);
            }
            resourceList = Collections.singletonList(referencedResources.toString());
        } else if (referencedResources instanceof List) {
            resourceList = (List) referencedResources;
        }
        if (resourceList == null) {
            return;
        }
        List<String> resourceListTmp = new ArrayList<String>();
        final String baseLocation = env.getReloadLocation();
        for (Object ref : resourceList) {
            String stringRef = ref.toString();
            if (warDeployed) {
                addBaseLocationPattern(resourceListTmp, baseLocation, stringRef);
            } else {
                addBaseLocationPattern(resourceListTmp, baseLocation, stringRef);
            }
        }
        watchedResourcePatternReferences = new String[resourceListTmp.size()];
        for (int i = 0; i < watchedResourcePatternReferences.length; i++) {
            String resRef = resourceListTmp.get(i);
            watchedResourcePatternReferences[i] = resRef;
        }
        watchedResourcePatterns = new WatchPatternParser().getWatchPatterns(Arrays.asList(watchedResourcePatternReferences));
    } catch (IllegalArgumentException e) {
        if (GrailsUtil.isDevelopmentEnv()) {
            LOG.debug("Cannot load plug-in resource watch list from [" + GrailsArrayUtils.toString(watchedResourcePatternReferences) + "]. This means that the plugin " + this + ", will not be able to auto-reload changes effectively. Try running grails upgrade.: " + e.getMessage());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Environment(grails.util.Environment) List(java.util.List) ArrayList(java.util.ArrayList) WatchPatternParser(org.grails.plugins.support.WatchPatternParser)

Example 3 with Environment

use of grails.util.Environment in project grails-core by grails.

the class DefaultGrailsPlugin method supportsCurrentScopeAndEnvironment.

public boolean supportsCurrentScopeAndEnvironment() {
    BuildScope bs = BuildScope.getCurrent();
    Environment e = Environment.getCurrent();
    return supportsEnvironment(e) && supportsScope(bs);
}
Also used : BuildScope(grails.util.BuildScope) Environment(grails.util.Environment)

Example 4 with Environment

use of grails.util.Environment in project grails-core by grails.

the class EnvironmentAwarePropertySource method initialize.

private void initialize() {
    if (propertyNames == null) {
        propertyNames = new ArrayList<>();
        Environment env = Environment.getCurrent();
        String key = "environments." + env.getName();
        for (PropertySource propertySource : source) {
            if ((propertySource != this) && // plugin default configuration is not allowed to be environment aware (GRAILS-12123)
            !propertySource.getName().contains("plugin") && propertySource instanceof EnumerablePropertySource) {
                EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
                for (String propertyName : enumerablePropertySource.getPropertyNames()) {
                    if (propertyName.startsWith(key) && propertyName.length() > key.length()) {
                        propertyNames.add(propertyName.substring(key.length() + 1));
                    }
                }
            }
        }
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) Environment(grails.util.Environment) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource)

Example 5 with Environment

use of grails.util.Environment in project grails-core by grails.

the class EnvironmentAwarePropertySource method getProperty.

@Override
public Object getProperty(String name) {
    initialize();
    if (!propertyNames.contains(name)) {
        return null;
    }
    Environment env = Environment.getCurrent();
    String key = "environments." + env.getName() + '.' + name;
    for (PropertySource propertySource : source) {
        if (propertySource != this) {
            Object value = propertySource.getProperty(key);
            if (value != null)
                return value;
        }
    }
    return null;
}
Also used : Environment(grails.util.Environment) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource)

Aggregations

Environment (grails.util.Environment)5 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)2 PropertySource (org.springframework.core.env.PropertySource)2 BuildScope (grails.util.BuildScope)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GrailsConfigurationException (org.grails.core.exceptions.GrailsConfigurationException)1 WatchPatternParser (org.grails.plugins.support.WatchPatternParser)1