summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-21 12:40:53 +0200
committerSimon Rettberg2015-07-21 12:40:53 +0200
commitd13036a0227139287c682ebf63c73a4d459ccbdf (patch)
treee2baf3b12ed3fd950097e6dd865322d848e34d75
parentThrift API changes, add configurable timeout to file transfer classes (diff)
downloadmaster-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.tar.gz
master-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.tar.xz
master-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.zip
Extended Util class
-rw-r--r--src/main/java/org/openslx/filetransfer/Transfer.java13
-rw-r--r--src/main/java/org/openslx/network/ProxyProperties.java4
-rw-r--r--src/main/java/org/openslx/util/Util.java54
-rw-r--r--src/main/java/org/openslx/util/vm/VmwareMetaData.java6
4 files changed, 49 insertions, 28 deletions
diff --git a/src/main/java/org/openslx/filetransfer/Transfer.java b/src/main/java/org/openslx/filetransfer/Transfer.java
index fc3d1d8..9bf2250 100644
--- a/src/main/java/org/openslx/filetransfer/Transfer.java
+++ b/src/main/java/org/openslx/filetransfer/Transfer.java
@@ -14,6 +14,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.apache.log4j.Logger;
+import org.openslx.util.Util;
public abstract class Transfer
{
@@ -296,17 +297,7 @@ public abstract class Transfer
*/
static protected void safeClose( Closeable... list )
{
- if ( list == null )
- return;
- for ( Closeable c : list ) {
- if ( c == null )
- continue;
- try {
- c.close();
- } catch ( Throwable t ) {
- // Silcence...
- }
- }
+ Util.safeClose( list );
}
/**
diff --git a/src/main/java/org/openslx/network/ProxyProperties.java b/src/main/java/org/openslx/network/ProxyProperties.java
index 71a9b24..5dacc57 100644
--- a/src/main/java/org/openslx/network/ProxyProperties.java
+++ b/src/main/java/org/openslx/network/ProxyProperties.java
@@ -39,7 +39,7 @@ public class ProxyProperties
// Integers //
public static int getProxyPort()
{
- return Util.tryToParseInt( properties.getProperty( "PROXY_PORT", "0" ) );
+ return Util.parseInt( properties.getProperty( "PROXY_PORT", "0" ), 0 );
}
static
@@ -63,7 +63,7 @@ public class ProxyProperties
} catch ( IOException e ) {
log.warn( "Could not load proxy properties from '/opt/openslx/proxy/conf'." );
} finally {
- Util.streamClose( stream );
+ Util.safeClose( stream );
}
}
diff --git a/src/main/java/org/openslx/util/Util.java b/src/main/java/org/openslx/util/Util.java
index 9f98007..24daf4c 100644
--- a/src/main/java/org/openslx/util/Util.java
+++ b/src/main/java/org/openslx/util/Util.java
@@ -3,7 +3,6 @@ package org.openslx.util;
import java.io.Closeable;
import org.apache.log4j.Logger;
-import org.openslx.util.Util;
public class Util
{
@@ -43,24 +42,24 @@ public class Util
}
}
-
/**
- * Tries to parse an int. Returns 0 on error.
+ * Parse the given String as a base10 integer.
+ * If the string does not represent a valid integer, return the given
+ * default value.
*
- * @param s
- * The string to parse
- * @return The parsed int or 0 on error
+ * @param value string representation to parse to an int
+ * @param defaultValue fallback value if given string can't be parsed
+ * @return
*/
- public static int tryToParseInt( String s )
- {
+ public static int parseInt(String value, int defaultValue) {
try {
- return Integer.parseInt( s );
- } catch ( NumberFormatException e ) {
- return 0;
+ return Integer.parseInt(value);
+ } catch (Exception e) {
+ return defaultValue;
}
}
- public static void streamClose( Closeable... closeable )
+ public static void safeClose( Closeable... closeable )
{
for ( Closeable c : closeable ) {
if ( c == null )
@@ -71,4 +70,35 @@ public class Util
}
}
}
+
+ public static void safeClose( AutoCloseable... closeable )
+ {
+ for ( AutoCloseable c : closeable ) {
+ if ( c == null )
+ continue;
+ try {
+ c.close();
+ } catch ( Throwable t ) {
+ }
+ }
+ }
+
+ public static boolean sleep(int millis) {
+ try {
+ Thread.sleep( millis );
+ return true;
+ } catch ( InterruptedException e ) {
+ Thread.currentThread().interrupt();
+ return false;
+ }
+ }
+
+ public static boolean joinThread(Thread t) {
+ try {
+ t.join();
+ return true;
+ } catch ( InterruptedException e ) {
+ return false;
+ }
+ }
}
diff --git a/src/main/java/org/openslx/util/vm/VmwareMetaData.java b/src/main/java/org/openslx/util/vm/VmwareMetaData.java
index 703075c..37ee1b3 100644
--- a/src/main/java/org/openslx/util/vm/VmwareMetaData.java
+++ b/src/main/java/org/openslx/util/vm/VmwareMetaData.java
@@ -70,7 +70,7 @@ public class VmwareMetaData extends VmMetaData
offset += ret;
}
} finally {
- Util.streamClose( fr );
+ Util.safeClose( fr );
}
init( data, offset );
}
@@ -112,7 +112,7 @@ public class VmwareMetaData extends VmMetaData
handleLoadEntry( entry );
}
} finally {
- Util.streamClose( reader );
+ Util.safeClose( reader );
}
// Now find the HDDs and add to list
for ( Entry<String, Controller> cEntry : disks.entrySet() ) {
@@ -205,7 +205,7 @@ public class VmwareMetaData extends VmMetaData
} catch ( Exception e ) {
LOGGER.warn( "Could not detect charset, fallback to latin1", e );
} finally {
- Util.streamClose( csDetectReader );
+ Util.safeClose( csDetectReader );
}
// Dumb fallback
return "ISO-8859-1";