Search in sources :

Example 71 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project camel by apache.

the class DefaultMavenArtifactProvider method addArtifactToCatalog.

@Override
public Set<String> addArtifactToCatalog(CamelCatalog camelCatalog, CamelConnectorCatalog camelConnectorCatalog, String groupId, String artifactId, String version) {
    final Set<String> names = new LinkedHashSet<>();
    try {
        if (cacheDirectory != null) {
            LOG.debug("Using cache directory: {}", cacheDirectory);
            System.setProperty("grape.root", cacheDirectory);
        }
        Grape.setEnableAutoDownload(true);
        final ClassLoader classLoader = new GroovyClassLoader();
        Map<String, Object> param = new HashMap<>();
        param.put("classLoader", classLoader);
        param.put("group", groupId);
        param.put("module", artifactId);
        param.put("version", version);
        // no need to download transitive dependencies as we only need to check the component or connector itself
        param.put("validate", false);
        param.put("transitive", false);
        LOG.debug("Downloading {}:{}:{}", groupId, artifactId, version);
        Grape.grab(param);
        // the classloader can load content from the downloaded JAR
        if (camelCatalog != null) {
            scanCamelComponents(camelCatalog, classLoader, names);
        }
        if (camelConnectorCatalog != null) {
            scanCamelConnectors(camelConnectorCatalog, classLoader, groupId, artifactId, version, names);
        }
    } catch (Exception e) {
        LOG.warn("Error during add components from artifact " + groupId + ":" + artifactId + ":" + version + " due " + e.getMessage(), e);
    }
    return names;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 72 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method ensureConstraintsPresent.

@SuppressWarnings("rawtypes")
private void ensureConstraintsPresent(String[] classSource, int classIndexToTest, int constraintCount) throws Exception {
    // We need to do a real test here to make sure
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class[] classes = new Class[classSource.length];
    for (int i = 0; i < classSource.length; i++) {
        classes[i] = gcl.parseClass(classSource[i]);
    }
    GrailsApplication ga = new DefaultGrailsApplication(classes[classIndexToTest]);
    ga.initialise();
    new MappingContextBuilder(ga).build(classes[classIndexToTest]);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, classes[classIndexToTest].getName());
    Map constraints = domainClass.getConstrainedProperties();
    grails.gorm.validation.ConstrainedProperty p = (grails.gorm.validation.ConstrainedProperty) constraints.get("name");
    Collection cons = p.getAppliedConstraints();
    assertEquals("Incorrect number of constraints extracted: " + constraints, constraintCount, cons.size());
}
Also used : GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Collection(java.util.Collection) GrailsDomainClass(grails.core.GrailsDomainClass) Map(java.util.Map) grails.gorm.validation(grails.gorm.validation)

Example 73 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByActionConvention.

public void testDecoratedByActionConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/test2/testAction.gsp", "<html><body><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class Test2Controller {\n" + "def controllerName = 'test2'\n" + "def actionUri = '/test2/testAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Test title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/test2/testAction.gsp", d.getPage());
    assertEquals("test2/testAction", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 74 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByApplicationConvention.

public void testDecoratedByApplicationConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 75 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-maven by grails.

the class DefaultGrailsServices method readGrailsPluginProject.

public GrailsPluginProject readGrailsPluginProject() throws MojoExecutionException {
    final GrailsPluginProject pluginProject = new GrailsPluginProject();
    final File[] files = getBasedir().listFiles(new FilenameFilter() {

        public boolean accept(final File file, final String s) {
            return s.endsWith(FILE_SUFFIX) && s.length() > FILE_SUFFIX.length();
        }
    });
    if (files == null || files.length != 1) {
        throw new MojoExecutionException("Could not find a plugin descriptor. Expected to find exactly one file " + "called FooGrailsPlugin.groovy in '" + getBasedir().getAbsolutePath() + "'.");
    }
    final File descriptor = files[0];
    pluginProject.setFileName(descriptor);
    final String className = descriptor.getName().substring(0, descriptor.getName().length() - ".groovy".length());
    final String pluginName = getLowerCaseHyphenSeparatedName(getLogicalName(className, "GrailsPlugin"));
    pluginProject.setPluginName(pluginName);
    final GroovyClassLoader classLoader = new GroovyClassLoader();
    final AstPluginDescriptorReader reader = new AstPluginDescriptorReader(classLoader);
    final GrailsPluginInfo info = reader.readPluginInfo(new org.codehaus.groovy.grails.io.support.FileSystemResource(descriptor));
    final String version = info.getVersion();
    if (version == null || version.trim().length() == 0) {
        throw new MojoExecutionException("Plugin does not have a version!");
    }
    pluginProject.setVersion(version);
    return pluginProject;
}
Also used : GrailsPluginInfo(org.codehaus.groovy.grails.plugins.GrailsPluginInfo) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) GroovyClassLoader(groovy.lang.GroovyClassLoader) AstPluginDescriptorReader(org.codehaus.groovy.grails.plugins.AstPluginDescriptorReader)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)126 File (java.io.File)26 GroovyObject (groovy.lang.GroovyObject)17 Test (org.junit.jupiter.api.Test)17 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)13 IOException (java.io.IOException)11 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 HashMap (java.util.HashMap)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 Map (java.util.Map)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 BuildException (org.apache.tools.ant.BuildException)8 ArtefactHandler (grails.core.ArtefactHandler)7 GroovyShell (groovy.lang.GroovyShell)7 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7 Script (groovy.lang.Script)6 Decorator (com.opensymphony.module.sitemesh.Decorator)5