Launcher also starts Jersey resource and binds it to /chat/
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 05 May 2013 15:38:59 +0200
changeset 6ecb1ef414295
parent 5 f0a52c72ef74
child 7 bf3a8e6dd84a
Launcher also starts Jersey resource and binds it to /chat/
serverside/nbactions.xml
serverside/pom.xml
serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java
     1.1 --- a/serverside/nbactions.xml	Sat May 04 07:21:05 2013 +0200
     1.2 +++ b/serverside/nbactions.xml	Sun May 05 15:38:59 2013 +0200
     1.3 @@ -29,17 +29,7 @@
     1.4          <actionName>run</actionName>
     1.5          <goals>
     1.6              <goal>process-classes</goal>
     1.7 -            <goal>bck2brwsr:brwsr</goal>
     1.8 +            <goal>exec:java</goal>
     1.9          </goals>
    1.10      </action>
    1.11 -    <action>
    1.12 -        <actionName>debug</actionName>
    1.13 -        <goals>
    1.14 -            <goal>process-classes</goal>
    1.15 -            <goal>bck2brwsr:brwsr</goal>
    1.16 -        </goals>
    1.17 -        <properties>
    1.18 -            <jpda.listen>maven</jpda.listen>
    1.19 -        </properties>
    1.20 -    </action>
    1.21  </actions>
     2.1 --- a/serverside/pom.xml	Sat May 04 07:21:05 2013 +0200
     2.2 +++ b/serverside/pom.xml	Sun May 05 15:38:59 2013 +0200
     2.3 @@ -15,26 +15,18 @@
     2.4    <properties>
     2.5      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     2.6      <net.java.html.version>0.2</net.java.html.version>
     2.7 -    <bck2brwsr.version>0.7</bck2brwsr.version>
     2.8 +    <bck2brwsr.version>0.8-SNAPSHOT</bck2brwsr.version>
     2.9      <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
    2.10      <brwsr.startpage>org/apidesign/bck2brwsr/demo/serverside/chat.html</brwsr.startpage>
    2.11    </properties>
    2.12    <build>
    2.13        <plugins>
    2.14            <plugin>
    2.15 -              <groupId>org.apidesign.bck2brwsr</groupId>
    2.16 -              <artifactId>bck2brwsr-maven-plugin</artifactId>
    2.17 -              <version>${bck2brwsr.version}</version>
    2.18 -              <executions>
    2.19 -                  <execution>
    2.20 -                      <goals>
    2.21 -                          <goal>brwsr</goal>
    2.22 -                      </goals>
    2.23 -                  </execution>
    2.24 -              </executions>
    2.25 +              <groupId>org.codehaus.mojo</groupId>
    2.26 +              <artifactId>exec-maven-plugin</artifactId>
    2.27 +              <version>1.2.1</version>
    2.28                <configuration>
    2.29 -                  <startpage>${brwsr.startpage}</startpage>
    2.30 -                  <launcher>bck2brwsr</launcher>
    2.31 +                  <mainClass>org.apidesign.bck2brwsr.demo.serverside.ChatServerResource</mainClass>
    2.32                </configuration>
    2.33            </plugin>
    2.34           <plugin>
    2.35 @@ -65,7 +57,13 @@
    2.36        <groupId>${project.groupId}</groupId>
    2.37        <artifactId>launcher.http</artifactId>
    2.38        <version>${bck2brwsr.version}</version>
    2.39 -      <scope>runtime</scope>
    2.40 +      <scope>compile</scope>
    2.41 +      <exclusions>
    2.42 +        <exclusion>
    2.43 +          <artifactId>grizzly-http-server</artifactId>
    2.44 +          <groupId>org.glassfish.grizzly</groupId>
    2.45 +        </exclusion>
    2.46 +      </exclusions>
    2.47      </dependency>
    2.48      <dependency>
    2.49        <groupId>${project.groupId}</groupId>
    2.50 @@ -80,5 +78,26 @@
    2.51        <version>6.5.2</version>
    2.52        <scope>test</scope>
    2.53      </dependency>
    2.54 +    <dependency>
    2.55 +      <groupId>org.glassfish.jersey.connectors</groupId>
    2.56 +      <artifactId>jersey-grizzly-connector</artifactId>
    2.57 +      <version>2.0-SNAPSHOT</version>
    2.58 +    </dependency>
    2.59 +    <dependency>
    2.60 +      <groupId>org.glassfish.jersey.containers</groupId>
    2.61 +      <artifactId>jersey-container-grizzly2-http</artifactId>
    2.62 +      <version>2.0-SNAPSHOT</version>
    2.63 +      <type>jar</type>
    2.64 +    </dependency>
    2.65 +    <dependency>
    2.66 +      <groupId>org.glassfish.grizzly</groupId>
    2.67 +      <artifactId>grizzly-http-server</artifactId>
    2.68 +      <version>2.3.1</version>
    2.69 +    </dependency>
    2.70 +    <dependency>
    2.71 +      <groupId>org.glassfish.jersey.incubator</groupId>
    2.72 +      <artifactId>html-json</artifactId>
    2.73 +      <version>0.1-SNAPSHOT</version>
    2.74 +    </dependency>
    2.75    </dependencies>
    2.76  </project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/serverside/src/main/java/org/apidesign/bck2brwsr/demo/serverside/ChatServerResource.java	Sun May 05 15:38:59 2013 +0200
     3.3 @@ -0,0 +1,86 @@
     3.4 +/**
     3.5 + * The MIT License (MIT)
     3.6 + *
     3.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.8 + *
     3.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    3.10 + * of this software and associated documentation files (the "Software"), to deal
    3.11 + * in the Software without restriction, including without limitation the rights
    3.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    3.13 + * copies of the Software, and to permit persons to whom the Software is
    3.14 + * furnished to do so, subject to the following conditions:
    3.15 + *
    3.16 + * The above copyright notice and this permission notice shall be included in
    3.17 + * all copies or substantial portions of the Software.
    3.18 + *
    3.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    3.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    3.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    3.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    3.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    3.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    3.25 + * THE SOFTWARE.
    3.26 + */
    3.27 +package org.apidesign.bck2brwsr.demo.serverside;
    3.28 +
    3.29 +import java.io.Closeable;
    3.30 +import java.lang.reflect.Field;
    3.31 +import java.util.ArrayList;
    3.32 +import java.util.List;
    3.33 +import java.util.concurrent.Callable;
    3.34 +import javax.ws.rs.Consumes;
    3.35 +import javax.ws.rs.GET;
    3.36 +import javax.ws.rs.PUT;
    3.37 +import javax.ws.rs.Path;
    3.38 +import javax.ws.rs.Produces;
    3.39 +import javax.ws.rs.core.MediaType;
    3.40 +import net.java.html.json.Context;
    3.41 +import org.apidesign.bck2brwsr.launcher.Launcher;
    3.42 +import org.glassfish.grizzly.http.server.HttpServer;
    3.43 +import org.glassfish.grizzly.http.server.ServerConfiguration;
    3.44 +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer;
    3.45 +import org.glassfish.jersey.server.ContainerFactory;
    3.46 +import org.glassfish.jersey.server.ResourceConfig;
    3.47 +
    3.48 +/** Server side of the chat application.
    3.49 + */
    3.50 +@Path("/")
    3.51 +public final class ChatServerResource {
    3.52 +    public static void main(String... args) throws Exception {
    3.53 +        ResourceConfig rc = new ResourceConfig(ChatServerResource.class);
    3.54 +        GrizzlyHttpContainer c = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
    3.55 +        Closeable s = Launcher.showURL(
    3.56 +            "bck2brwsr", ChatClient.class.getClassLoader(),
    3.57 +            "/org/apidesign/bck2brwsr/demo/serverside/chat.html"
    3.58 +        );
    3.59 +        Launcher l = (Launcher) s;
    3.60 +        Callable<?> f = (Callable<?>) l;
    3.61 +        HttpServer server = (HttpServer) f.call();
    3.62 +        ServerConfiguration conf = server.getServerConfiguration();
    3.63 +        conf.addHttpHandler(c, "/chat");
    3.64 +        System.in.read();
    3.65 +        s.close();
    3.66 +    }
    3.67 +    
    3.68 +    private List<Message> msgs = new ArrayList<>();
    3.69 +    {
    3.70 +        Message welcome = new Message(Context.findDefault(Message.class));
    3.71 +        welcome.setUser("system");
    3.72 +        welcome.setComment("Welcome and enjoy!");
    3.73 +        msgs.add(welcome);
    3.74 +    }
    3.75 +    
    3.76 +    @Produces(MediaType.APPLICATION_JSON)
    3.77 +    @GET public Query getResources() {
    3.78 +        Query q = new Query(Context.findDefault(Query.class));
    3.79 +        q.getMessages().addAll(msgs);
    3.80 +        return q;
    3.81 +    }
    3.82 +    
    3.83 +    @Consumes(MediaType.APPLICATION_JSON)
    3.84 +    @PUT public void publish(Message msg) {
    3.85 +        if (msg != null) {
    3.86 +            msgs.add(msg);
    3.87 +        }
    3.88 +    }
    3.89 +}