
java.fedora.server.journal.helpers.JournalHelper Maven / Gradle / Ivy
Show all versions of fcrepo-client Show documentation
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.server.journal.helpers;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import fedora.server.journal.JournalConstants;
import fedora.server.journal.JournalException;
import fedora.server.utilities.StreamUtility;
/**
*
* A collection of utility methods for use in the Journal classes.
*
*
* @author [email protected]
* @version $Id: JournalHelper.java 5367 2006-12-08 08:51:00 +0000 (Fri, 08 Dec
* 2006) cwilper $
*/
public class JournalHelper
implements JournalConstants {
private JournalHelper() {
// no need to instantiate this class - all methods are static.
}
/**
* Copy an input stream to a temporary file, so we can hand an input stream
* to the delegate and have another input stream for the journal.
*/
public static File copyToTempFile(InputStream serialization)
throws IOException, FileNotFoundException {
File tempFile = createTempFile();
StreamUtility.pipeStream(serialization,
new FileOutputStream(tempFile),
4096);
return tempFile;
}
/**
* Create a temporary file. The "File" object that we return is really an
* instance of "JournalTempFile", so we can detect it later in isTempFile().
*/
public static File createTempFile() throws IOException {
File rawTempFile = File.createTempFile("fedora-journal-temp", ".xml");
return new JournalTempFile(rawTempFile);
}
/**
* Is this file one that we created as a temp file?
*/
public static boolean isTempFile(File file) {
return file instanceof JournalTempFile;
}
/**
* Capture the full stack trace of an Exception, and return it in a String.
*/
public static String captureStackTrace(Throwable e) {
StringWriter buffer = new StringWriter();
e.printStackTrace(new PrintWriter(buffer));
return buffer.toString();
}
/**
* Look in the system parameters and create an instance of the named class.
*
* @param parameterName
* The name of the system parameter that contains the classname
* @param argClasses
* What types of arguments are required by the constructor?
* @param args
* Arguments to provide to the instance constructor.
* @param parameters
* The system parameters
* @return the new instance created
*/
public static Object createInstanceAccordingToParameter(String parameterName,
Class>[] argClasses,
Object[] args,
Map parameters)
throws JournalException {
String className = parameters.get(parameterName);
if (className == null) {
throw new JournalException("No parameter '" + parameterName + "'");
}
return createInstanceFromClassname(className, argClasses, args);
}
/**
* Create an instance of the named class.
*
* @param className
* The classname of the desired instance
* @param argClasses
* What types of arguments are required by the constructor?
* @param args
* Arguments to provide to the instance constructor.
* @return the new instance created
*/
public static Object createInstanceFromClassname(String className,
Class>[] argClasses,
Object[] args)
throws JournalException {
try {
Class> clazz = Class.forName(className);
Constructor> constructor = clazz.getConstructor(argClasses);
return constructor.newInstance(args);
} catch (Exception e) {
throw new JournalException(e);
}
}
/**
* Format a date for the journal or the log.
*/
public static String formatDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMP_FORMAT);
return formatter.format(date);
}
/**
* Parse a date from the journal.
*/
public static Date parseDate(String date) throws JournalException {
try {
SimpleDateFormat parser = new SimpleDateFormat(TIMESTAMP_FORMAT);
return parser.parse(date);
} catch (ParseException e) {
throw new JournalException(e);
}
}
/**
* Create the name for a Journal file or a log file, based on the prefix and
* the current date.
*/
public static String createTimestampedFilename(String filenamePrefix,
Date date) {
SimpleDateFormat formatter =
new SimpleDateFormat(FORMAT_JOURNAL_FILENAME_TIMESTAMP);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return filenamePrefix + formatter.format(date) + "Z";
}
}