Search code examples
javaauthenticationgradleoauthratpack

RatpackServer.start in connection with ratpack-pac4j causes non-static context error


I have got very incomprehensible (for me) trouble with RatpackServer's start method.

Fristly, I mean this method

static RatpackServer start(Action<? super RatpackServerSpec> definition)
                throws Exception

and here is my code:

public static void main(String... args) throws Exception {

    ProgramInitializer programInitializer = new ProgramInitializer();
    programInitializer.initialize();


    RatpackServer.start(b -> b
            .serverConfig(s -> s
                    .baseDir(BaseDir.find())
                    .env()
            )
            .registry(
                    Guice.registry(s -> s
                            .module(TextTemplateModule.class, conf ->
                                    conf.setStaticallyCompile(true)
                            )
                            .bindInstance(ServerErrorHandler.class, (ctx, error) -> {
                                logger.error("Unexpected error", error);
                                ctx.render(groovyTemplate("error500.html"));
                            })
                            .bindInstance(ClientErrorHandler.class, (ctx, statusCode) -> {
                                ctx.getResponse().status(statusCode);
                                if (statusCode == 404) {
                                    ctx.render(groovyTemplate("error404.html"));
                                } else if (statusCode == 401) {
                                    ctx.render(groovyTemplate("error401.html"));
                                } else if (statusCode == 403) {
                                    ctx.render(groovyTemplate("error403.html"));
                                } else {
                                    logger.error("Unexpected: {}", statusCode);
                                }
                            })
                    )
            )
            .handlers(c -> { c
                                .get("index.html", ctx -> {
                                    //ctx.redirect(301, "/");
                                    logger.debug("Retrieving user profile...");
                                    final Map<String, Object> model = Maps.newHashMap();
                                    RatpackPac4j.userProfile(ctx)
                                            .route(Optional::isPresent, p -> {
                                                model.put("profile", p);
                                                ctx.render(groovyTemplate(model, "index.html"));
                                            })
                                            .then(p -> {
                                                ctx.render(groovyTemplate(model, "index.html"));
                                            });
                                    //
                                })
                                //.get(ctx -> ctx.render(groovyTemplate("index.html")))

                                .get("hello", ctx -> {
                                    RelativisticModel.select();
                                    //String energy = System.getenv("ENERGY");
                                    //Amount<Mass> m = Amount.valueOf(energy).to(KILOGRAM);
                                    ctx.render(programInitializer.getTweetsList());
                                })

                                .files(f -> f.dir("public"));
                    }
            )
    );
}

everything worked nice till I add this dependency to the build.gradle:

compile 'io.ratpack:ratpack-pac4j:1.3.3'

so that the whole file looks like this:

group 'pl.edu.agh.tai.tdo'
version '1.0'

apply plugin: 'java'
apply plugin: 'idea'

buildscript {
    repositories {
        maven { url "http://oss.jfrog.org/repo" }
        mavenCentral()
        maven { url "http://dl.bintray.com/robfletcher/gradle-plugins" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        jcenter()
    }
    dependencies {
        classpath "io.ratpack:ratpack-gradle:1.0.0"
        classpath ('org.ratpack-framework:ratpack-gradle:0.9.0-SNAPSHOT')
    }
}

apply plugin: "io.ratpack.ratpack-groovy"

repositories {
    maven { url "http://oss.jfrog.org/repo" }
    mavenCentral()
    maven { url "http://repo.springsource.org/repo" } // for springloaded
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    jcenter()
}

mainClassName = "pl.edu.agh.tai.tdo.Main"

dependencies {

    compile "org.ratpack-framework:ratpack-handlebars:0.9.0-SNAPSHOT"

    runtime "org.slf4j:slf4j-simple:1.7.12"
    compile "com.heroku.sdk:heroku-jdbc:0.1.1"
    compile "org.postgresql:postgresql:9.4-1201-jdbc4"
    compile "org.jscience:jscience:4.3.1"

    compile 'io.ratpack:ratpack-pac4j:1.2.0'
    compile 'org.twitter4j:twitter4j-core:4.0.4'
    compile 'ch.qos.logback:logback-classic:1.1.7'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDirs "src/main/resources"
    test.java.srcDir "src/test/java"
}

task stage {
    dependsOn installDist
}

After adding this dependecy in the Main class

Error:java: incompatible types: com.google.inject.Injector is not a functional interface
multiple non-overriding abstract methods found in interface com.google.inject.Injector


Error:(29, 22) java: method start in interface ratpack.server.RatpackServer cannot be applied to given types; required: no
arguments found: (b)->b.ser[...]); }) 
reason: actual and formal argument lists differ in length


Error:(63, 56) java: invalid method reference

non-static method isPresent() cannot be referenced from a static context

And above RatpackServer.start(...) in IntelliJ it's written

Non-static method start() cannot be referenced from a static context

Has anyone such problem? It's really a mystery for me. I try to build my first app based on Ratpack ...


Solution

  • You seem to be mixing and matching multiple versions of Ratpack.

    In your build file I find:

    • Ratpack 0.9.0
    • Ratpack 1.2.0
    • Ratpack 1.0.0

    The version of RatpackServer you are using does not have the start method as a static method.

    I've fixed up the build gradle file for you:

    plugins {
      id 'io.ratpack.ratpack-groovy' version '1.3.3' // downloads ratpack-groovy 1.3.3 which is latest as of 2016-05-12
      id 'idea' // new way of applying idea plugin
    }
    
    group 'pl.edu.agh.tai.tdo'
    version '1.0'
    
    repositories {
        maven { url "http://oss.jfrog.org/repo" }
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        jcenter()
    }
    
    mainClassName = "pl.edu.agh.tai.tdo.Main"
    
    dependencies {
        compile ratpack.dependency('handlebars') // use ratpack gradle plugin to ensure that ratpack-handlebars 1.3.3 is used
        compile ratpack.dependency('pac4j') // use ratpack gradle plugin to ensure that ratpack-pac4j 1.3.3 is used
    
        runtime "org.slf4j:slf4j-simple:1.7.12"
        compile "com.heroku.sdk:heroku-jdbc:0.1.1"
        compile "org.postgresql:postgresql:9.4-1201-jdbc4"
        compile "org.jscience:jscience:4.3.1"
    
        compile 'org.twitter4j:twitter4j-core:4.0.4'
        compile 'ch.qos.logback:logback-classic:1.1.7'
    
        testCompile group: 'junit', name: 'junit', version: '4.11'
    }
    
    task stage(dependsOn: installDist)
    

    The Ratpack Gradle plugin, which gets applied when you invoke

    plugins {
      id 'io.ratpack.ratpack-groovy' version '1.3.3'
    }
    

    Provides a method ratpack.dependency which will pull in the appropriate version of a given module, such as pac4j.

    The following are equivalent:

    dependencies {
        compile ratpack.dependency('pac4j')
    }
    

    and

    dependencies {
        compile 'io.ratpack:ratpack-pac4j:1.3.3'
    }