aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml8
-rw-r--r--core/src/main/java/fi/iki/elonen/NanoHTTPD.java45
-rw-r--r--pom.xml28
3 files changed, 70 insertions, 11 deletions
diff --git a/.travis.yml b/.travis.yml
index b36965e..dd8e710 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,4 +8,10 @@ install:
script: mvn test
after_success:
- - mvn clean test jacoco:report coveralls:report \ No newline at end of file
+ - mvn clean test jacoco:report coveralls:report
+
+
+notifications:
+ email:
+ - richard.vannieuwenhoven@adesso.at
+ - elonen@iki.fi \ No newline at end of file
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
index 1f77058..de4bc83 100644
--- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
+++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
@@ -1401,12 +1401,23 @@ public abstract class NanoHTTPD {
private final int timeout;
+ private IOException bindException;
+
+ private boolean hasBinded = false;
+
private ServerRunnable(int timeout) {
this.timeout = timeout;
}
@Override
public void run() {
+ try {
+ myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort));
+ hasBinded = true;
+ } catch (IOException e) {
+ this.bindException = e;
+ return;
+ }
do {
try {
final Socket finalAccept = NanoHTTPD.this.myServerSocket.accept();
@@ -1551,13 +1562,19 @@ public abstract class NanoHTTPD {
return res;
}
- private static final void safeClose(Closeable closeable) {
- if (closeable != null) {
- try {
- closeable.close();
- } catch (IOException e) {
- NanoHTTPD.LOG.log(Level.SEVERE, "Could not close", e);
+ private static final void safeClose(Object closeable) {
+ try {
+ if (closeable instanceof Closeable) {
+ ((Closeable) closeable).close();
+ } else if (closeable instanceof Socket) {
+ ((Socket) closeable).close();
+ } else if (closeable instanceof ServerSocket) {
+ ((ServerSocket) closeable).close();
+ } else {
+ throw new IllegalArgumentException("Unknown object to close");
}
+ } catch (IOException e) {
+ NanoHTTPD.LOG.log(Level.SEVERE, "Could not close", e);
}
}
@@ -1857,12 +1874,24 @@ public abstract class NanoHTTPD {
this.myServerSocket = new ServerSocket();
}
this.myServerSocket.setReuseAddress(true);
- this.myServerSocket.bind(this.hostname != null ? new InetSocketAddress(this.hostname, this.myPort) : new InetSocketAddress(this.myPort));
- this.myThread = new Thread(createServerRunnable(timeout));
+ ServerRunnable serverRunnable = createServerRunnable(timeout);
+ this.myThread = new Thread(serverRunnable);
this.myThread.setDaemon(true);
this.myThread.setName("NanoHttpd Main Listener");
this.myThread.start();
+ while (!serverRunnable.hasBinded && serverRunnable.bindException == null) {
+ try {
+ Thread.sleep(10L);
+ } catch (Throwable e) {
+ // on android this may not be allowed, that's why we
+ // catch throwable the wait should be very short because we are
+ // just waiting for the bind of the socket
+ }
+ }
+ if (serverRunnable.bindException != null) {
+ throw serverRunnable.bindException;
+ }
}
/**
diff --git a/pom.xml b/pom.xml
index e2fb386..e6adcf7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,7 +109,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
+ <version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
@@ -292,7 +292,7 @@
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>UTF-8</sourceEncoding>
- <targetJdk>1.7</targetJdk>
+ <targetJdk>1.6</targetJdk>
<skipEmptyReport>false</skipEmptyReport>
<minimumTokens>50</minimumTokens>
</configuration>
@@ -447,5 +447,29 @@
</plugins>
</build>
</profile>
+ <profile>
+ <id>use 1.6 compiler</id>
+ <activation>
+ <file>
+ <exists>/usr/lib/jvm/java-6-openjdk-amd64/bin/javac</exists>
+ </file>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.3</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <verbose>true</verbose>
+ <fork>true</fork>
+ <executable>/usr/lib/jvm/java-6-openjdk-amd64/bin/javac</executable>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
</profiles>
</project>