Java tutorial
package de.alexkamp.sandbox.model; /** * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import com.fasterxml.jackson.core.JsonGenerator; import de.alexkamp.sandbox.Sandbox; import de.alexkamp.sandbox.SandboxFactory; import de.alexkamp.sandbox.SandboxUtils; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.util.*; /** * Created by akampmann on 3/16/15. */ public class SandboxData implements Iterable<Mount> { private final SandboxFactory factory; private final String identifier; private final File basePath; private boolean asRoot; private final List<Mount> mounts = new ArrayList<>(); public SandboxData(SandboxFactory factory, String identifier, File basePath) { this.factory = factory; this.identifier = identifier; this.basePath = basePath; this.asRoot = false; } public SandboxData copyTo(File source, String target) throws IOException { File targetFile = new File(basePath, target); if (source.isDirectory()) { SandboxUtils.copyDirectory(source, targetFile); } else { ensureExistance(targetFile); Files.copy(source.toPath(), targetFile.toPath()); } return this; } private void ensureExistance(File targetFile) { File targetDir = targetFile.getParentFile(); if (!targetDir.exists()) { if (!targetDir.mkdirs()) { throw new IllegalStateException("Could not create directory " + targetDir.getName()); } } } public SandboxData copyTo(InputStream source, String target) throws IOException { File targetFile = new File(basePath, target); ensureExistance(targetFile); Files.copy(source, targetFile.toPath()); return this; } public SandboxData stayRoot() { this.asRoot = true; return this; } public SandboxData withSysMount() { mounts.add(SpecialMount.SYS); failIfExistsCreate(new File(basePath, "sys")); return this; } public SandboxData withProcMount() { mounts.add(SpecialMount.PROC); failIfExistsCreate(new File(basePath, "proc")); return this; } public SandboxData withDevMount() { mounts.add(new ReadOnlyMount("/dev", "/dev")); failIfExistsCreate(new File(basePath, "dev")); return this; } public SandboxData withReadOnlyMount(String origPath, String targetPath) { mounts.add(new ReadOnlyMount(origPath, targetPath)); failIfExistsCreate(new File(basePath, targetPath)); return this; } public Sandbox start() { return factory.start(this); } public void toJson(JsonGenerator sender) throws IOException { sender.writeStartObject(); sender.writeObjectField("Path", basePath.getAbsolutePath()); sender.writeObjectField("Identifier", identifier); sender.writeObjectField("AsRoot", asRoot); sender.writeArrayFieldStart("Mounts"); for (Mount m : mounts) { m.toJSON(sender); } sender.writeEndArray(); sender.writeEndObject(); } private void failIfExistsCreate(File proc) { if (proc.exists()) { throw new IllegalStateException("You can not have a folder /" + proc.getName() + " in a sandbox."); } if (!proc.mkdirs()) { throw new IllegalStateException("Could not create: " + proc.getAbsolutePath()); } } public String getIdentifier() { return identifier; } @Override public Iterator<Mount> iterator() { return mounts.iterator(); } public File getBaseDir() { return basePath; } }