diff options
| author | Simon Rettberg | 2015-06-02 19:53:31 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-06-02 19:53:31 +0200 |
| commit | 1bc83891c68ee269727e81a13cc70da698bcc7a7 (patch) | |
| tree | b052a72ad7d65864068752f71c5ed2b49a171276 /dozentenmodulserver/src/main/java/util | |
| parent | [server] Started work on the internal file server (diff) | |
| download | tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.tar.gz tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.tar.xz tutor-module-1bc83891c68ee269727e81a13cc70da698bcc7a7.zip | |
[server] Compiling again, still lots of stubs
Diffstat (limited to 'dozentenmodulserver/src/main/java/util')
4 files changed, 150 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/util/Constants.java b/dozentenmodulserver/src/main/java/util/Constants.java new file mode 100644 index 00000000..8ac5dabd --- /dev/null +++ b/dozentenmodulserver/src/main/java/util/Constants.java @@ -0,0 +1,21 @@ +package util; + +import fileserv.FileChunk; + +public class Constants { + public static final String INCOMPLETE_UPLOAD_SUFFIX = ".part"; + public static final int MAX_UPLOADS; + + static { + long maxMem = Runtime.getRuntime().maxMemory(); + if (maxMem == Long.MAX_VALUE) { + // Apparently the JVM was started without a memory limit (no -Xmx cmdline), + // so we assume a default of 256MB + maxMem = 256l * 1024l * 1024l; + } + maxMem /= 1024l * 1024l; + // Now maxMem is the amount of memory in MiB + + MAX_UPLOADS = (int) Math.max(1, (maxMem - 64) / (FileChunk.CHUNK_SIZE_MIB + 1)); + } +} diff --git a/dozentenmodulserver/src/main/java/util/FileSystem.java b/dozentenmodulserver/src/main/java/util/FileSystem.java new file mode 100644 index 00000000..5f5a1e08 --- /dev/null +++ b/dozentenmodulserver/src/main/java/util/FileSystem.java @@ -0,0 +1,25 @@ +package util; + +import java.io.File; + +import org.apache.log4j.Logger; + +public class FileSystem { + + private static final Logger LOGGER = Logger.getLogger(FileSystem.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()); + } +} diff --git a/dozentenmodulserver/src/main/java/util/Formatter.java b/dozentenmodulserver/src/main/java/util/Formatter.java new file mode 100644 index 00000000..5c5982fb --- /dev/null +++ b/dozentenmodulserver/src/main/java/util/Formatter.java @@ -0,0 +1,59 @@ +package util; + +import java.io.File; +import java.util.UUID; + +import models.Configuration; + +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.openslx.imagemaster.thrift.iface.UserInfo; + +public class Formatter { + + private static final DateTimeFormatter vmNameDateFormat = DateTimeFormat.forPattern("dd_HH-mm-ss"); + + /** + * Generate a unique file name used for a virtual machine + * image that is currently uploading. + * + * @return Absolute path name of file + */ + public static File getTempImageName() { + return new File(Configuration.getCurrentVmStorePath(), UUID.randomUUID().toString() + + Constants.INCOMPLETE_UPLOAD_SUFFIX); + } + + /** + * Generate a file name for the given VM based on owner and display name. + * + * @param user The user associated with the VM, e.g. the owner + * @param imageName Name of the VM + * @return File name for the VM derived from the function's input + */ + public static String vmName(UserInfo user, String imageName) { + return cleanFileName(vmNameDateFormat.print(System.currentTimeMillis()) + "_" + user.lastName + "_" + + imageName); + } + + /** + * Make sure file name contains only a subset of ascii characters and is not + * too long. + * + * @param name What we want to turn into a file name + * @return A sanitized form of name that should be safe to use as a file + * name + */ + public static String cleanFileName(String name) { + if (name == null) + return "null"; + name = name.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_"); + if (name.length() > 120) + name = name.substring(0, 120); + return name; + } + + public static String userFullName(UserInfo ui) { + return ui.firstName + " " + ui.lastName; + } +} diff --git a/dozentenmodulserver/src/main/java/util/Util.java b/dozentenmodulserver/src/main/java/util/Util.java new file mode 100644 index 00000000..28f522b8 --- /dev/null +++ b/dozentenmodulserver/src/main/java/util/Util.java @@ -0,0 +1,45 @@ +package util; + +import java.io.Closeable; + +public class Util { + + /** + * Try to close everything passed to this method. Never throw an exception + * if it fails, just silently continue. + * + * @param item One or more objects that are Closeable + */ + public static void safeClose(Closeable... item) { + if (item == null) + return; + for (Closeable c : item) { + if (c == null) + continue; + try { + c.close(); + } catch (Exception e) { + } + } + } + + /** + * Try to close everything passed to this method. Never throw an exception + * if it fails, just silently continue. + * + * @param item One or more objects that are AutoCloseable + */ + public static void safeClose(AutoCloseable... item) { + if (item == null) + return; + for (AutoCloseable c : item) { + if (c == null) + continue; + try { + c.close(); + } catch (Exception e) { + } + } + } + +} |
