summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util
diff options
context:
space:
mode:
authorSimon Rettberg2016-04-13 18:38:47 +0200
committerSimon Rettberg2016-04-13 18:38:47 +0200
commit34ca2905c38d17bbded01cf7497eca790e760a39 (patch)
tree4dbaff08d7f88d48e685bd514b907c8d77571f16 /src/main/java/org/openslx/util
parentremove ruleId from NetRule struct (diff)
downloadmaster-sync-shared-34ca2905c38d17bbded01cf7497eca790e760a39.tar.gz
master-sync-shared-34ca2905c38d17bbded01cf7497eca790e760a39.tar.xz
master-sync-shared-34ca2905c38d17bbded01cf7497eca790e760a39.zip
Preparations/changes for global image sync
Diffstat (limited to 'src/main/java/org/openslx/util')
-rw-r--r--src/main/java/org/openslx/util/FsUtil.java36
-rw-r--r--src/main/java/org/openslx/util/GrowingThreadPoolExecutor.java85
-rw-r--r--src/main/java/org/openslx/util/ThriftUtil.java34
3 files changed, 155 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/util/FsUtil.java b/src/main/java/org/openslx/util/FsUtil.java
new file mode 100644
index 0000000..76571ef
--- /dev/null
+++ b/src/main/java/org/openslx/util/FsUtil.java
@@ -0,0 +1,36 @@
+package org.openslx.util;
+
+import java.io.File;
+
+import org.apache.log4j.Logger;
+
+public class FsUtil
+{
+
+ private static final Logger LOGGER = Logger.getLogger( FsUtil.class );
+
+ public static String getRelativePath( File absolutePath, File parentDir )
+ {
+ String file;
+ String dir;
+ try {
+ file = absolutePath.getCanonicalPath();
+ dir = parentDir.getCanonicalPath() + File.separator;
+ } catch ( Exception e ) {
+ LOGGER.error( "Could not get relative path for " + absolutePath.toString(), e );
+ return null;
+ }
+ if ( !file.startsWith( dir ) )
+ return null;
+ return file.substring( dir.length() );
+ }
+
+ public static String sanitizeFileName( String fileName )
+ {
+ fileName = fileName.replaceAll( "[^a-zA-Z0-9_\\-]+", "_" );
+ if ( fileName.length() > 40 )
+ fileName = fileName.substring( 0, 40 );
+ return fileName;
+ }
+
+}
diff --git a/src/main/java/org/openslx/util/GrowingThreadPoolExecutor.java b/src/main/java/org/openslx/util/GrowingThreadPoolExecutor.java
new file mode 100644
index 0000000..7b0b2d9
--- /dev/null
+++ b/src/main/java/org/openslx/util/GrowingThreadPoolExecutor.java
@@ -0,0 +1,85 @@
+package org.openslx.util;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Grows to maximum pool size before queueing. See
+ * http://stackoverflow.com/a/20153234/2043481
+ */
+public class GrowingThreadPoolExecutor extends ThreadPoolExecutor {
+ private int userSpecifiedCorePoolSize;
+ private int taskCount;
+
+ /**
+ * The default rejected execution handler
+ */
+ private static final RejectedExecutionHandler defaultHandler =
+ new AbortPolicy();
+
+ public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
+ TimeUnit unit, BlockingQueue<Runnable> workQueue) {
+ this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, defaultHandler);
+ }
+
+ public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
+ BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
+ this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler);
+ }
+
+ public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
+ BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
+ this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(),
+ handler);
+ }
+
+ public GrowingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
+ BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
+ super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
+ userSpecifiedCorePoolSize = corePoolSize;
+ }
+
+ @Override
+ public void execute(Runnable runnable) {
+ synchronized (this) {
+ taskCount++;
+ setCorePoolSizeToTaskCountWithinBounds();
+ }
+ super.execute(runnable);
+ }
+
+ @Override
+ protected void afterExecute(Runnable runnable, Throwable throwable) {
+ super.afterExecute(runnable, throwable);
+ synchronized (this) {
+ taskCount--;
+ setCorePoolSizeToTaskCountWithinBounds();
+ }
+ }
+
+ private void setCorePoolSizeToTaskCountWithinBounds() {
+ int threads = taskCount;
+ if (threads < userSpecifiedCorePoolSize)
+ threads = userSpecifiedCorePoolSize;
+ if (threads > getMaximumPoolSize())
+ threads = getMaximumPoolSize();
+ super.setCorePoolSize(threads);
+ }
+
+ public void setCorePoolSize(int corePoolSize) {
+ synchronized (this) {
+ userSpecifiedCorePoolSize = corePoolSize;
+ }
+ }
+
+ @Override
+ public int getCorePoolSize() {
+ synchronized (this) {
+ return userSpecifiedCorePoolSize;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/openslx/util/ThriftUtil.java b/src/main/java/org/openslx/util/ThriftUtil.java
new file mode 100644
index 0000000..58019a7
--- /dev/null
+++ b/src/main/java/org/openslx/util/ThriftUtil.java
@@ -0,0 +1,34 @@
+package org.openslx.util;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ThriftUtil {
+
+ public static List<byte[]> unwrapByteBufferList(List<ByteBuffer> blockHashes) {
+ if (blockHashes == null || blockHashes.isEmpty())
+ return null;
+ List<byte[]> hashList = new ArrayList<>(blockHashes.size());
+ for (ByteBuffer hash : blockHashes) {
+ byte[] buffer = new byte[hash.remaining()];
+ hash.mark();
+ hash.get(buffer);
+ hash.reset();
+ hashList.add(buffer);
+ }
+ return hashList;
+ }
+
+ public static byte[] unwrapByteBuffer(ByteBuffer buffer) {
+ byte[] byteArray = null;
+ if (buffer != null) {
+ byteArray = new byte[buffer.remaining()];
+ buffer.mark();
+ buffer.get(byteArray);
+ buffer.reset();
+ }
+ return byteArray;
+ }
+
+}