[mozzes] r16 committed - initial import of REST mozzes module implementation in jersey and exam...

1 view
Skip to first unread message

moz...@googlecode.com

unread,
Feb 27, 2010, 7:46:30 AM2/27/10
to mozzes-d...@googlegroups.com
Revision: 16
Author: branko.kokanovic
Date: Sat Feb 27 04:46:07 2010
Log: initial import of REST mozzes module implementation in jersey and
example of it
http://code.google.com/p/mozzes/source/detail?r=16

Added:
/trunk/mozzesApplication/mozzesApplicationExample/log

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/pom.xml

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest/jersey

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest/jersey/RestOneTeamService.java

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest/jersey/RestTeamsService.java

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/resources

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/test

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/test/java

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/test/resources
/trunk/mozzesApplication/mozzesApplicationRestJersey
/trunk/mozzesApplication/mozzesApplicationRestJersey/pom.xml
/trunk/mozzesApplication/mozzesApplicationRestJersey/src
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyConfiguration.java

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyModule.java

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyServerListener.java

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceFactory.java

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceProvider.java

/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceProviderFactory.java
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/resources
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/test
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/test/java
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/test/resources
Modified:

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/pom.xml

/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/java/org/mozzes/application/example/server/ExampleServer.java

=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/pom.xml
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,37 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.mozzes.application</groupId>
+ <artifactId>mozzesApplicationExampleRestJersey</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <parent>
+ <artifactId>mozzesApplicationExample</artifactId>
+ <groupId>org.mozzes.application</groupId>
+ <version>trunk-SNAPSHOT</version>
+ </parent>
+ <dependencies>
+ <dependency>
+ <groupId>org.mozzes.application</groupId>
+ <artifactId>mozzesApplicationServer</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mozzes.rest.jersey</groupId>
+ <artifactId>mozzesRestJersey</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mozzes.application</groupId>
+ <artifactId>mozzesApplicationExampleCommon</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>xstream</groupId>
+ <artifactId>xstream</artifactId>
+ <version>1.2.2</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+</project>
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest/jersey/RestOneTeamService.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,51 @@
+package org.mozzes.application.example.rest.jersey;
+
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.mozzes.application.common.client.MozzesClient;
+import org.mozzes.application.example.common.domain.Team;
+import org.mozzes.application.example.common.service.TeamAdministration;
+import org.mozzes.rest.jersey.guice.MozzesGuiceFactory;
+
+import com.google.inject.Inject;
+import com.sun.jersey.api.NotFoundException;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+@Path("team/{teamId}/")
+@MozzesGuiceFactory
+/**
+ * Rest service for getting information about one team
+ * @author stalker
+ */
+public class RestOneTeamService {
+
+ @Inject
+ private MozzesClient mozzesClient;
+
+ @GET
+ @Produces( { MediaType.TEXT_XML })
+ public String getCountry(@PathParam("teamId") String teamId) {
+ Integer id;
+ try {
+ id = Integer.parseInt(teamId);
+ } catch (NumberFormatException e) {
+ throw new NotFoundException("Team " + teamId + " not found");
+ }
+ List<Team> teams =
mozzesClient.getService(TeamAdministration.class).findAll();
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("team", Team.class);
+ for (Team team : teams) {
+ if (team.getId().equals(id)) {
+ return xstream.toXML(team);
+ }
+ }
+ throw new NotFoundException("Team " + teamId + " not found");
+ }
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleRestJersey/src/main/java/org/mozzes/application/example/rest/jersey/RestTeamsService.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,41 @@
+package org.mozzes.application.example.rest.jersey;
+
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.mozzes.application.common.client.MozzesClient;
+import org.mozzes.application.example.common.domain.Team;
+import org.mozzes.application.example.common.service.TeamAdministration;
+import org.mozzes.rest.jersey.guice.MozzesGuiceFactory;
+
+import com.google.inject.Inject;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+@Path("/teams")
+@MozzesGuiceFactory
+/**
+ * Rest service for getting information about all teams
+ * @author stalker
+ */
+public class RestTeamsService {
+
+ @Inject
+ private MozzesClient mozzesClient;
+
+ public RestTeamsService() {
+ }
+
+ @GET
+ @Produces( { MediaType.TEXT_XML })
+ public String getXml() {
+ List<Team> teams =
mozzesClient.getService(TeamAdministration.class).findAll();
+ XStream xstream = new XStream(new DomDriver());
+ xstream.alias("team", Team.class);
+ return xstream.toXML(teams);
+ }
+}
=======================================
--- /dev/null
+++ /trunk/mozzesApplication/mozzesApplicationRestJersey/pom.xml Sat Feb 27
04:46:07 2010
@@ -0,0 +1,46 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.mozzes.rest.jersey</groupId>
+ <artifactId>mozzesRestJersey</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <parent>
+ <artifactId>mozzesApplication</artifactId>
+ <groupId>org.mozzes.application</groupId>
+ <version>trunk-SNAPSHOT</version>
+ </parent>
+ <repositories>
+ <repository>
+ <id>maven2-repository.dev.java.net</id>
+ <name>Java.net Repository for Maven</name>
+ <url>http://download.java.net/maven/2/</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>org.mozzes.application</groupId>
+ <artifactId>mozzesApplicationModule</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+<dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-server</artifactId>
+ <version>1.1.4</version>
+</dependency>
+<dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-json</artifactId>
+ <version>1.1.4</version>
+</dependency>
+<dependency>
+ <groupId>com.sun.jersey.contribs</groupId>
+ <artifactId>jersey-guice</artifactId>
+ <version>1.1.4.1</version>
+</dependency>
+<dependency>
+ <groupId>com.sun.grizzly</groupId>
+ <artifactId>grizzly-servlet-webserver</artifactId>
+ <version>1.9.8</version>
+</dependency>
+ </dependencies>
+</project>
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyConfiguration.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,25 @@
+package org.mozzes.rest.jersey;
+
+/**
+ * Configuration for Jersey server, consisting of base URI and root
package where services will reside
+ * @author stalker
+ */
+class RestJerseyConfiguration {
+
+ private final String baseUri;
+ private final String rootResourcePackage;
+
+ RestJerseyConfiguration(String baseUri, String rootResourcePackage) {
+ super();
+ this.baseUri = baseUri;
+ this.rootResourcePackage = rootResourcePackage;
+ }
+
+ String getBaseUri() {
+ return baseUri;
+ }
+
+ String getRootResourcePackage() {
+ return rootResourcePackage;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyModule.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,42 @@
+package org.mozzes.rest.jersey;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.mozzes.application.module.ApplicationModule;
+import org.mozzes.application.module.ServerLifecycleListener;
+
+import com.google.inject.Binder;
+import com.google.inject.Provider;
+import com.google.inject.Scopes;
+
+/**
+ * Mozzes application module for REST Jersey implementation
+ * @author stalker
+ */
+public class RestJerseyModule extends ApplicationModule {
+
+ private final RestJerseyConfiguration configuration;
+
+ public RestJerseyModule(String baseUri, String rootResourcePackage) {
+ configuration = new RestJerseyConfiguration(baseUri,
rootResourcePackage);
+ }
+
+ @Override
+ public void doCustomBinding(Binder binder) {
+ binder.bind(RestJerseyConfiguration.class).toProvider(new
Provider<RestJerseyConfiguration>() {
+ @Override
+ public RestJerseyConfiguration get() {
+ return configuration;
+
+ }
+ }).in(Scopes.SINGLETON);
+ }
+
+ @Override
+ public List<Class<? extends ServerLifecycleListener>>
getServerListeners() {
+ List<Class<? extends ServerLifecycleListener>> list = new
ArrayList<Class<? extends ServerLifecycleListener>>();
+ list.add(RestJerseyServerListener.class);
+ return list;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/RestJerseyServerListener.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,56 @@
+package org.mozzes.rest.jersey;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mozzes.application.module.ServerInitializationException;
+import org.mozzes.application.module.ServerLifecycleListener;
+import org.mozzes.rest.jersey.guice.MozzesGuiceProviderFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.sun.grizzly.http.SelectorThread;
+import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
+
+/**
+ * Simple Mozzes server listener whose purpose is to start Grizzly
embedded server and configure Jersey
+ * @author stalker
+ */
+public class RestJerseyServerListener implements ServerLifecycleListener {
+
+ private static final Logger logger =
LoggerFactory.getLogger(RestJerseyServerListener.class);
+
+ @Inject
+ private RestJerseyConfiguration configuration;
+ @Inject
+ private Injector guiceInjector;
+
+ private SelectorThread threadSelector;
+
+ @Override
+ public void shutdown() {
+ threadSelector.stopEndpoint();
+ }
+
+ @Override
+ public void startup() throws ServerInitializationException {
+ MozzesGuiceProviderFactory.setGuiceProvider(guiceInjector);
+
+ final Map<String, String> initParams = new HashMap<String, String>();
+ initParams.put("com.sun.jersey.config.property.packages",
configuration.getRootResourcePackage());
+
+ logger.info("Starting grizzly...");
+ try {
+ threadSelector =
GrizzlyWebContainerFactory.create(configuration.getBaseUri(), initParams);
+ } catch (IllegalArgumentException e) {
+ logger.error("Error starting grizly", e);
+ throw new ServerInitializationException(e);
+ } catch (IOException e) {
+ logger.error("Error starting grizly", e);
+ throw new ServerInitializationException(e);
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceFactory.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,19 @@
+package org.mozzes.rest.jersey.guice;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import
com.sun.jersey.server.spi.component.ResourceComponentProviderFactoryClass;
+
+@Target( { ElementType.TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@ResourceComponentProviderFactoryClass(MozzesGuiceProviderFactory.class)
+/**
+ * @author stalker
+ */
+public @interface MozzesGuiceFactory {
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceProvider.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,38 @@
+package org.mozzes.rest.jersey.guice;
+
+import com.google.inject.Injector;
+import com.sun.jersey.api.core.HttpContext;
+import com.sun.jersey.api.model.AbstractResource;
+import com.sun.jersey.server.spi.component.ResourceComponentProvider;
+
+/**
+ * @author stalker
+ */
+public class MozzesGuiceProvider implements ResourceComponentProvider {
+
+ private AbstractResource abstractResource;
+ private Injector guiceInjector = null;
+
+ public MozzesGuiceProvider(Injector guiceInjector) {
+ this.guiceInjector = guiceInjector;
+ }
+
+ @Override
+ public Object getInstance(HttpContext hc) {
+ return guiceInjector.getInstance(abstractResource.getResourceClass());
+ }
+
+ @Override
+ public void init(AbstractResource abstractResource) {
+ this.abstractResource = abstractResource;
+ }
+
+ @Override
+ public Object getInstance() {
+ return guiceInjector.getInstance(abstractResource.getResourceClass());
+ }
+
+ @Override
+ public void destroy() {
+ }
+}
=======================================
--- /dev/null
+++
/trunk/mozzesApplication/mozzesApplicationRestJersey/src/main/java/org/mozzes/rest/jersey/guice/MozzesGuiceProviderFactory.java
Sat Feb 27 04:46:07 2010
@@ -0,0 +1,66 @@
+/*
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common Development
+ * and Distribution License("CDDL") (collectively, the "License"). You
+ * may not use this file except in compliance with the License. You can
obtain
+ * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
+ * or jersey/legal/LICENSE.txt. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ * When distributing the software, include this License Header Notice in
each
+ * file and include the License file at jersey/legal/LICENSE.txt.
+ * Sun designates this particular file as subject to the "Classpath"
exception
+ * as provided by Sun in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
License
+ * Header, with the fields enclosed by brackets [] replaced by your own
+ * identifying information: "Portions Copyrighted [year]
+ * [name of copyright owner]"
+ *
+ * Contributor(s):
+ *
+ * If you wish your version of this file to be governed by only the CDDL or
+ * only the GPL Version 2, indicate your decision by adding "[Contributor]
+ * elects to include this software in this distribution under the [CDDL or
GPL
+ * Version 2] license." If you don't indicate a single choice of license,
a
+ * recipient has the option to distribute your version of this file under
+ * either the CDDL, the GPL Version 2 or to extend the choice of license to
+ * its licensees as provided above. However, if you add GPL Version 2 code
+ * and therefore, elected the GPL Version 2 license, then the option
applies
+ * only if the new code is made subject to such option by the copyright
+ * holder.
+ */
+
+package org.mozzes.rest.jersey.guice;
+
+import com.google.inject.Injector;
+import com.sun.jersey.core.spi.component.ioc.IoCComponentProvider;
+import com.sun.jersey.server.spi.component.ResourceComponentProvider;
+import
com.sun.jersey.server.spi.component.ResourceComponentProviderFactory;
+
+/**
+ * A simple provider that maintains a singleton resource class instance
+ * @author stalker
+ */
+public final class MozzesGuiceProviderFactory implements
ResourceComponentProviderFactory {
+
+ private static Injector guiceInjector;
+
+ public static void setGuiceProvider(Injector guiceInjector) {
+ MozzesGuiceProviderFactory.guiceInjector = guiceInjector;
+ }
+
+ @Override
+ public ResourceComponentProvider
getComponentProvider(IoCComponentProvider icp, Class<?> c) {
+ return new MozzesGuiceProvider(guiceInjector);
+ }
+
+ @Override
+ public ResourceComponentProvider getComponentProvider(Class<?> c) {
+ return new MozzesGuiceProvider(guiceInjector);
+ }
+}
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/pom.xml
Tue Feb 23 16:09:19 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/pom.xml
Sat Feb 27 04:46:07 2010
@@ -36,5 +36,11 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>org.mozzes.application</groupId>
+ <artifactId>mozzesApplicationExampleRestJersey</artifactId>
+ <version>trunk-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
=======================================
---
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/java/org/mozzes/application/example/server/ExampleServer.java
Wed Feb 24 05:48:21 2010
+++
/trunk/mozzesApplication/mozzesApplicationExample/mozzesApplicationExampleServer/src/main/java/org/mozzes/application/example/server/ExampleServer.java
Sat Feb 27 04:46:07 2010
@@ -15,6 +15,7 @@
import org.mozzes.application.remoting.server.RemotingPlugin;
import org.mozzes.application.server.MozzesServer;
import org.mozzes.application.server.MozzesServerConfiguration;
+import org.mozzes.rest.jersey.RestJerseyModule;

@@ -25,6 +26,7 @@
MozzesServerConfiguration cfg = new MozzesServerConfiguration();
cfg.addApplicationModule(new
HibernatePlugin(HibernateConfigurationType.ANNOTATION));
cfg.addApplicationModule(new RemotingPlugin(PORT));
+ cfg.addApplicationModule(new
RestJerseyModule("http://localhost:8080/", "org.mozzes.application.example.rest.jersey"));

cfg.addService(MatchAdministration.class, MatchAdministrationImpl.class);
cfg.addService(TeamAdministration.class, TeamAdministrationImpl.class);

Reply all
Reply to author
Forward
0 new messages