use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class CheckpointHelper method save.
public void save(JobManager.Checkpoint checkpoint) throws IOException {
CheckpointFilename cf = CheckpointFilename.createBasic(checkpoint.getJob());
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(cf.getFile());
oos = factory.createObjectOutputStream(fos);
oos.writeObject(checkpoint);
oos.close();
Outbound outboundPayload = checkpoint.getContext().getOutboundPayload();
if (outboundPayload != null && outboundPayload.isDirty()) {
saveOutbound(outboundPayload, cf.getForPayload(false).getFile());
}
Inbound inboundPayload = checkpoint.getContext().getInboundPayload();
if (inboundPayload != null) {
saveInbound(inboundPayload, cf.getForPayload(true).getFile());
}
} catch (IOException e) {
try {
oos.close();
} catch (Exception ex) {
}
try {
fos.close();
} catch (Exception ex) {
}
File file = cf.getFile();
if (file.exists()) {
file.delete();
;
}
file = cf.getForPayload(true).getFile();
if (file.exists()) {
file.delete();
}
file = cf.getForPayload(false).getFile();
if (file.exists()) {
file.delete();
}
throw e;
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class CheckpointHelper method load.
public JobManager.Checkpoint load(CheckpointFilename cf, Outbound outbound) throws IOException, ClassNotFoundException {
FileInputStream fis = null;
ObjectInputStream ois = null;
JobManager.Checkpoint checkpoint;
try {
fis = new FileInputStream(cf.getFile());
ois = factory.createObjectInputStream(fis);
checkpoint = (JobManager.Checkpoint) ois.readObject();
} finally {
try {
ois.close();
} catch (Exception ex) {
}
try {
fis.close();
} catch (Exception ex) {
}
}
if (outbound != null) {
loadOutbound(outbound, cf.getForPayload(false).getFile());
checkpoint.getContext().setOutboundPayload(outbound);
}
Inbound inbound = loadInbound(cf.getForPayload(true).getFile());
checkpoint.getContext().setInboundPayload(inbound);
try {
String username = checkpoint.getJob().getSubjectUsernames().get(0);
Subject subject = authenticationService.impersonate(username, /* groups */
null, /* subject */
null, /* virtual */
false);
checkpoint.getContext().setSubject(subject);
} catch (LoginException e) {
throw new RuntimeException(e);
}
return checkpoint;
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method simplePermanentTransferDirTest.
@Test
public void simplePermanentTransferDirTest() throws Exception {
final String DIR = "x/y/z/";
final String FILE_PREFIX = "x/y/z/";
final String FILE_NAME = "fileB.txt";
final Set<File> desiredResults = new HashSet<File>();
/*
* Create a directory into which we'll transfer some small files.
*/
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
File targetDir = null;
try {
/*
* Choose the directory into which we want the PayloadFilesManager to
* deliver the files.
*/
targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
origDir.mkdir();
/*
* Add the directory first, then add a file in the directory. That
* will let us check to make sure the PayloadFileManager set the
* lastModified time on the directory correctly.
*/
final URI dirURI = URI.create(DIR);
final File dir = new File(origDir, DIR);
dir.mkdirs();
desiredResults.add(dir);
final File file = new File(dir, FILE_NAME);
desiredResults.add(new File(targetDir.toURI().resolve(FILE_PREFIX + FILE_NAME)));
writeFile(file, "Here is the File", "which has an", "additional line");
final long dirCreationTime = dir.lastModified();
new CommonPermTest() {
@Override
protected CommonPermTest init(File targetDir) {
super.init(targetDir);
return this;
}
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.attachFile("application/octet-stream", URI.create(DIR), "test-xfer", dir);
ob.attachFile("text/plain", URI.create(FILE_PREFIX + file.getName()), "test-xfer", file);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
/*
* Extract files to where we want them.
*/
instance.processParts(ib);
final URI extractedDirURI = myTargetDir.toURI().resolve(dirURI);
final File extractedDir = new File(extractedDirURI);
final long extractedLastModified = extractedDir.lastModified();
assertEquals("Directory lastModified mismatch after extraction", dirCreationTime, extractedLastModified);
}
@Override
protected void cleanup() {
for (File f : desiredResults) {
f.delete();
}
PayloadFilesManagerTest.cleanup(origDir);
}
}.init(targetDir).run("simplePermanentTransferDirTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method simplePermanentDirWithNoSlashRemovalTest.
@Test
public void simplePermanentDirWithNoSlashRemovalTest() throws Exception {
final String DIR = "x/";
final String DIR_WITH_NO_SLASH = "x";
final String FILE_A_PREFIX = DIR;
final String FILE_A_NAME = "fileA.txt";
final Set<File> desiredAbsent = new HashSet<File>();
/*
* Create a directory into which we'll copy some small files.
*/
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
origDir.mkdir();
final File dir = new File(origDir, DIR);
dir.mkdir();
final File fileA = new File(dir, FILE_A_NAME);
writeFile(fileA, "This is FileA", "with two lines of content");
File targetDir = null;
try {
/*
* Choose the directory into which we want the PayloadFilesManager to
* deliver the files.
*/
targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
desiredAbsent.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
new CommonPermTest() {
@Override
protected CommonPermTest init(File targetDir) {
super.init(targetDir);
return this;
}
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.attachFile("application/octet-stream", URI.create(DIR), "test-xfer", dir, true);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
/*
* Extract files to where we want them.
*/
instance.processParts(ib);
// listDir("After creation, before deletion", myTargetDir);
/*
* Now ask another PayloadFilesManager to remove a directory
* recursively.
*/
Payload.Outbound ob = PayloadImpl.Outbound.newInstance();
ob.requestFileRemoval(URI.create(DIR_WITH_NO_SLASH), "removeTest", /* dataRequestName */
null, /* props */
true);
ob.requestFileRemoval(URI.create("notThere"), "removeTest", null, true);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ob.writeTo(baos);
baos.close();
final PayloadFilesManager remover = new PayloadFilesManager.Perm(instance.getTargetDir(), null, debugLogger());
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Payload.Inbound removerIB = PayloadImpl.Inbound.newInstance("application/zip", bais);
remover.processParts(removerIB);
// listDir("After deletion" , myTargetDir);
final Set<File> unexpectedlyPresent = new HashSet<File>();
for (File f : desiredAbsent) {
if (f.exists()) {
unexpectedlyPresent.add(f);
}
}
assertEquals("Unexpected files remain after removal request", Collections.EMPTY_SET, unexpectedlyPresent);
}
@Override
protected void cleanup() {
for (File f : desiredAbsent) {
if (f.exists()) {
f.delete();
}
}
PayloadFilesManagerTest.cleanup(origDir);
}
}.init(targetDir).run("simplePermanentDirWithNoSlashRemovalTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method simpleTempRecursiveTransferDirOnlyTest.
@Test
public void simpleTempRecursiveTransferDirOnlyTest() throws Exception {
final String DIR = "x/";
final String Y_SUBDIR = "y/";
final String Z_SUBDIR = "z/";
final List<String> desiredResultsNamePrefixes = new ArrayList<String>();
/*
* Create a directory into which we'll copy some small files.
*/
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
origDir.mkdir();
final File dir = new File(origDir, DIR);
dir.mkdir();
final File ySubdir = new File(dir, Y_SUBDIR);
ySubdir.mkdir();
final File zSubdir = new File(ySubdir, Z_SUBDIR);
zSubdir.mkdir();
File targetDir = null;
try {
/*
* Choose the directory into which we want the PayloadFilesManager to
* deliver the files.
*/
targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
desiredResultsNamePrefixes.add("/x");
desiredResultsNamePrefixes.add("/x/y");
desiredResultsNamePrefixes.add("/x/y/z");
/*
* Add the original directory recursively.
*/
new CommonTempTest() {
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.attachFile("application/octet-stream", URI.create(DIR), "test-xfer", dir, true);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
/*
* Extract files to where we want them.
*/
final List<File> files = instance.processParts(ib);
checkNextFile: for (File f : files) {
for (ListIterator<String> it = desiredResultsNamePrefixes.listIterator(); it.hasNext(); ) {
final String desiredPrefix = it.next().replace("/", File.separator);
if (f.getPath().contains(desiredPrefix)) {
it.remove();
continue checkNextFile;
}
}
}
assertEquals("Unexpected missing files after extraction", Collections.EMPTY_LIST, desiredResultsNamePrefixes);
}
@Override
protected void doCleanup() {
PayloadFilesManagerTest.cleanup(origDir);
}
}.run("simpleTempRecursiveTransferDirOnlyTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
Aggregations