Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
16,064 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,160 @@ | |||
<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>com.daisyworks</groupId> | |||
<artifactId>arduino-toggle-demo</artifactId> | |||
<version>1.0</version> | |||
<packaging>jar</packaging> | |||
<name>arduino-toggle-demo</name> | |||
<description>Demo: how to toggle any Arduino USB port from a simple WebApp</description> | |||
|
|||
<repositories> | |||
<repository> | |||
<id>java.net.2</id> | |||
<name>Java Net 2 Repository</name> | |||
<url>http://download.java.net/maven/2</url> | |||
</repository> | |||
<repository> | |||
<id>apache.incubating</id> | |||
<name>Apache Incubating Repository</name> | |||
<url>http://people.apache.org/repo/m2-incubating-repository</url> | |||
</repository> | |||
<repository> | |||
<id>apache.snapshot</id> | |||
<name>Apache Snapshot Repository</name> | |||
<url>http://people.apache.org/repo/m2-snapshot-repository</url> | |||
</repository> | |||
</repositories> | |||
|
|||
<dependencies> | |||
|
|||
<dependency> | |||
<groupId>com.sun.jersey</groupId> | |||
<artifactId>jersey-json</artifactId> | |||
<version>LATEST</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.sun.jersey.contribs</groupId> | |||
<artifactId>jersey-spring</artifactId> | |||
<version>LATEST</version> | |||
</dependency> | |||
|
|||
<!-- embedded jetty server --> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-webapp</artifactId> | |||
<version>8.0.0.M1</version> | |||
</dependency> | |||
|
|||
<dependency> | |||
<groupId>log4j</groupId> | |||
<artifactId>log4j</artifactId> | |||
<version>1.2.15</version> | |||
<exclusions> | |||
<exclusion> | |||
<groupId>com.sun.jmx</groupId> | |||
<artifactId>jmxri</artifactId> | |||
</exclusion> | |||
<exclusion> | |||
<groupId>javax.jms</groupId> | |||
<artifactId>jms</artifactId> | |||
</exclusion> | |||
<exclusion> | |||
<groupId>com.sun.jdmk</groupId> | |||
<artifactId>jmxtools</artifactId> | |||
</exclusion> | |||
</exclusions> | |||
</dependency> | |||
|
|||
</dependencies> | |||
|
|||
<build> | |||
<finalName>demo</finalName> | |||
<plugins> | |||
|
|||
<!-- enable java 6 --> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-compiler-plugin</artifactId> | |||
<configuration> | |||
<source>1.6</source> | |||
<target>1.6</target> | |||
</configuration> | |||
</plugin> | |||
|
|||
<!-- automatically clean before build --> | |||
<plugin> | |||
<artifactId>maven-clean-plugin</artifactId> | |||
<executions> | |||
<execution> | |||
<id>auto-clean</id> | |||
<phase>validate</phase> | |||
<goals> | |||
<goal>clean</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
|
|||
<!-- @see http://false.ekta.is/2010/12/jettyrun-maven-plugin-file-locking-on-windows-a-better-way/ --> | |||
<plugin> | |||
<groupId>org.mortbay.jetty</groupId> | |||
<artifactId>maven-jetty-plugin</artifactId> | |||
<version>6.1.18</version> | |||
<configuration> | |||
<contextPath>/</contextPath> | |||
<scanIntervalSeconds>5</scanIntervalSeconds> | |||
<connectors> | |||
<connector implementation="org.mortbay.jetty.bio.SocketConnector"> | |||
<port>8080</port> | |||
</connector> | |||
</connectors> | |||
</configuration> | |||
</plugin> | |||
|
|||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-shade-plugin</artifactId> | |||
<executions> | |||
<execution> | |||
<phase>package</phase> | |||
<goals> | |||
<goal>shade</goal> | |||
</goals> | |||
<configuration> | |||
<filters> | |||
<filter> | |||
<artifact>*:*</artifact> | |||
</filter> | |||
</filters> | |||
<transformers> | |||
<!-- have to append all the spring META-INF files or app context | |||
fails --> | |||
<transformer | |||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> | |||
<resource>META-INF/spring.handlers</resource> | |||
</transformer> | |||
<transformer | |||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> | |||
<resource>META-INF/spring.schemas</resource> | |||
</transformer> | |||
<!-- defines the main entry point --> | |||
<transformer | |||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |||
<mainClass>com.daisyworks.demo.MainRunner</mainClass> | |||
</transformer> | |||
</transformers> | |||
</configuration> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
|
|||
</plugins> | |||
</build> | |||
|
|||
|
|||
|
|||
|
|||
|
|||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,71 @@ | |||
package com.daisyworks.demo; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
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.apache.log4j.Logger; | |||
|
|||
/** | |||
* This is the service class that handles the web browser requests | |||
*/ | |||
@Path("/") | |||
public class DoorService { | |||
|
|||
// map of USB ports | |||
private static Map<Integer, Port> map = new HashMap<Integer, Port>(); | |||
|
|||
private static final Logger LOGGER = Logger.getLogger(DoorService.class); | |||
|
|||
// current state of door | |||
private int toggled; | |||
|
|||
/** | |||
* Constructor | |||
*/ | |||
public DoorService() { | |||
// FIXME: remove this auto-generated map to show data in browser | |||
map.put(1, new Port(1, "/dev/ttyUSB0")); | |||
map.put(2, new Port(2, "/dev/ttyUSB1")); | |||
LOGGER.info("---------------- Widget Service CTOR ------------------"); | |||
} | |||
|
|||
/** | |||
* Fetch the list of ports; returns a JSON document with each {@link Port} object rendered as JSON | |||
* @return a JSON document with each {@link Port} object rendered as JSON | |||
*/ | |||
@GET | |||
@Path("/door/ports") | |||
@Produces(MediaType.APPLICATION_JSON) | |||
public PortCollection getPorts() { | |||
LOGGER.debug("Getting all ports"); | |||
return new PortCollection(map.values()); | |||
} | |||
|
|||
/** | |||
* Toggle the state of the door on/off | |||
* @param portId the id of the USB port you want to control | |||
* @param val the value of the door where <tt>1 => open</tt> and <tt>0 => closed</tt> | |||
* @return A string that indicates if it was successful or not as "true" or "false" | |||
*/ | |||
@GET | |||
@Path("/door/toggle/{portId}/{val}") | |||
@Produces(MediaType.TEXT_PLAIN) | |||
public String toggle(@PathParam("portId") Integer portId, @PathParam("val") Integer val) { | |||
toggled = val; | |||
if(map.containsKey(portId)) { | |||
Port port = map.get(portId); | |||
LOGGER.info("Toggle USB port "+port.getPath()+" =>" + ((toggled == 1) ? "on" : "off")); | |||
return "true"; | |||
} else { | |||
LOGGER.error("No port found for that id: "+portId); | |||
return "false"; | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,60 @@ | |||
package com.daisyworks.demo; | |||
|
|||
import java.io.PrintWriter; | |||
import java.io.StringWriter; | |||
import java.io.Writer; | |||
import java.net.URL; | |||
|
|||
import org.eclipse.jetty.server.Server; | |||
import org.eclipse.jetty.webapp.WebAppContext; | |||
|
|||
public class MainRunner { | |||
|
|||
private static final int DEFAULT_PORT = 8080; | |||
|
|||
/** | |||
* Launches an embedded Jetty server running the webapp herein | |||
* | |||
* Specify arg[0] as port number or else it will try 8080 | |||
* @param args | |||
*/ | |||
public static void main(String[] args) { | |||
int port = DEFAULT_PORT; | |||
if(args.length > 0) { | |||
try { | |||
port = Integer.parseInt(args[0]); | |||
} catch(Exception e) { | |||
System.err.println("Did you mean to specify a port? Re-run me with something like: `java -jar <filename.jar> 8081` for example to run on port 8081; I am going to try port 8080 which is my default."); | |||
} | |||
} | |||
|
|||
try { | |||
Server server = new Server(port); | |||
|
|||
final URL waUrl = MainRunner.class.getClassLoader().getResource("webapp"); | |||
final String waUrlString = waUrl.toExternalForm(); | |||
System.err.println("Starting Jetty server on port: "+port+": "+waUrlString); | |||
|
|||
WebAppContext ctx = new WebAppContext(); | |||
ctx.setWar(waUrlString); | |||
ctx.setContextPath("/demo"); | |||
ctx.setResourceBase("/WEB-INF/web.xml"); | |||
ctx.setParentLoaderPriority(true); | |||
|
|||
server.setHandler(ctx); | |||
server.start(); | |||
server.join(); | |||
} catch(Exception ex) { | |||
System.err.println("Failed to launch embedded Jetty server \n"+ getStackTraceAsString(ex)); | |||
System.exit(1); | |||
} | |||
} | |||
|
|||
public static String getStackTraceAsString(Throwable t) { | |||
final Writer result = new StringWriter(); | |||
final PrintWriter printWriter = new PrintWriter(result); | |||
t.printStackTrace(printWriter); | |||
return result.toString(); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
package com.daisyworks.demo; | |||
|
|||
import javax.xml.bind.annotation.XmlRootElement; | |||
|
|||
/** | |||
* A generic widget object that can be serialized; modify to suit your needs. | |||
*/ | |||
@XmlRootElement(name = "port") | |||
public class Port { | |||
|
|||
private String path; | |||
private Integer id; | |||
|
|||
public Port() { } | |||
|
|||
public Port(int id, String path) { this.id = id; this.path = path; } | |||
|
|||
public void setPath(String path) { | |||
this.path = path; | |||
} | |||
|
|||
public String getPath() { | |||
return path; | |||
} | |||
|
|||
public Integer getId() { | |||
return id; | |||
} | |||
|
|||
public void setId(Integer id) { | |||
this.id = id; | |||
} | |||
|
|||
@Override | |||
public String toString() { | |||
return String.format("{id=%s, path:%s}", | |||
id, path); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
package com.daisyworks.demo; | |||
|
|||
import java.util.Collection; | |||
|
|||
import javax.xml.bind.annotation.XmlElementWrapper; | |||
import javax.xml.bind.annotation.XmlRootElement; | |||
|
|||
/** | |||
* Wrapper class for a collection of {@link Port} objects for JAXB purposes | |||
*/ | |||
@XmlRootElement(name="ports") | |||
public class PortCollection { | |||
|
|||
private Collection<Port> ports; | |||
|
|||
public PortCollection() { } | |||
|
|||
public PortCollection(Collection<Port> ports) { | |||
this.ports = ports; | |||
} | |||
|
|||
@XmlElementWrapper(name="ports") | |||
public Collection<Port> getPorts() { | |||
return ports; | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
log4j.rootLogger=INFO, stdout, logfile | |||
|
|||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender | |||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | |||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n | |||
|
|||
log4j.appender.logfile=org.apache.log4j.RollingFileAppender | |||
log4j.appender.logfile.File=/opt/tomcat/logs/cxf-rest-example.log | |||
log4j.appender.logfile.MaxFileSize=512KB | |||
# Keep three backup files. | |||
log4j.appender.logfile.MaxBackupIndex=3 | |||
# Pattern to output: date priority [category] - message | |||
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout | |||
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n | |||
|
|||
log4j.logger.org.springframework=DEBUG | |||
log4j.logger.org.eclipse=DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<beans xmlns="http://www.springframework.org/schema/beans" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xmlns:context="http://www.springframework.org/schema/context" | |||
xsi:schemaLocation=" | |||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd | |||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> | |||
|
|||
<context:component-scan base-package="com.daisyworks.demo"/> | |||
<bean id="doorService" class="com.daisyworks.demo.DoorService" /> | |||
|
|||
</beans> |
Oops, something went wrong.