All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.fedora.server.journal.helpers.ParameterHelper Maven / Gradle / Ivy

Go to download

The Fedora Client is a Java Library that allows API access to a Fedora Repository. The client is typically one part of a full Fedora installation.

The newest version!
/*
 * -----------------------------------------------------------------------------
 *
 * 

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.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import fedora.server.journal.JournalConstants; import fedora.server.journal.JournalException; /** *

* Title: ParameterHelper.java *

*

* Description: A collection of utility methods to help the Journal * classes to read parameter values. *

* * @author [email protected] * @version $Id: ParameterHelper.java 5025 2006-09-01 22:08:17 +0000 (Fri, 01 * Sep 2006) cwilper $ */ public class ParameterHelper implements JournalConstants { private ParameterHelper() { // no need to instantiate - all methods are static. } private static void validateParameters(Map parameters) { if (parameters == null) { throw new NullPointerException("'parameters' may not be null."); } } private static void validateParameterName(String parameterName) { if (parameterName == null) { throw new NullPointerException("'parameterName' may not be null."); } } /** * Get an optional String parameter, If not found, use the default value. * * @throws NullPointerException * if either 'parameters' or 'parameterName' is null. */ public static String getOptionalStringParameter(Map parameters, String parameterName, String defaultValue) { validateParameters(parameters); validateParameterName(parameterName); String value = parameters.get(parameterName); return value == null ? defaultValue : value; } /** * Get an optional boolean parameter. If not found, use the default value. * * @throws JournalException * if a value is supplied that is neither "true" nor "false". * @throws NullPointerException * if either 'parameters' or 'parameterName' is null. */ public static boolean getOptionalBooleanParameter(Map parameters, String parameterName, boolean defaultValue) throws JournalException { validateParameters(parameters); validateParameterName(parameterName); String string = parameters.get(parameterName); if (string == null) { return defaultValue; } else if (string.equals(VALUE_FALSE)) { return false; } else if (string.equals(VALUE_TRUE)) { return true; } else { throw new JournalException("'" + parameterName + "' parameter must be '" + VALUE_FALSE + "'(default) or '" + VALUE_TRUE + "'"); } } /** * Look in the parameters for the path to a writable directory. The * parameter is required. */ public static File parseParametersForWritableDirectory(Map parameters, String parameterName) throws JournalException { String directoryString = parameters.get(parameterName); if (directoryString == null) { throw new JournalException("'" + parameterName + "' is required."); } File directory = new File(directoryString); if (!directory.exists()) { throw new JournalException("Directory '" + directory + "' does not exist."); } if (!directory.isDirectory()) { throw new JournalException("Directory '" + directory + "' is not a directory."); } if (!directory.canWrite()) { throw new JournalException("Directory '" + directory + "' is not writable."); } return directory; } /** * Look for a string to use as a prefix for the names of the journal files. * Default is "fedoraJournal" */ public static String parseParametersForFilenamePrefix(Map parameters) { return getOptionalStringParameter(parameters, PARAMETER_JOURNAL_FILENAME_PREFIX, DEFAULT_FILENAME_PREFIX); } /** * Get the size limit parameter (or let it default), and convert it to * bytes. */ public static long parseParametersForSizeLimit(Map parameters) throws JournalException { String sizeString = getOptionalStringParameter(parameters, PARAMETER_JOURNAL_FILE_SIZE_LIMIT, DEFAULT_SIZE_LIMIT); Pattern p = Pattern.compile("([0-9]+)([KMG]?)"); Matcher m = p.matcher(sizeString); if (!m.matches()) { throw new JournalException("Parameter '" + PARAMETER_JOURNAL_FILE_SIZE_LIMIT + "' must be an integer number of bytes, " + "optionally followed by 'K', 'M', or 'G', " + "or a 0 to indicate no size limit"); } long size = Long.parseLong(m.group(1)); String factor = m.group(2); if ("K".equals(factor)) { size *= 1024; } else if ("M".equals(factor)) { size *= 1024 * 1024; } else if ("G".equals(factor)) { size *= 1024 * 1024 * 1024; } return size; } /** * Get the age limit parameter (or let it default), and convert it to * milliseconds. */ public static long parseParametersForAgeLimit(Map parameters) throws JournalException { String ageString = ParameterHelper .getOptionalStringParameter(parameters, PARAMETER_JOURNAL_FILE_AGE_LIMIT, DEFAULT_AGE_LIMIT); Pattern p = Pattern.compile("([0-9]+)([DHM]?)"); Matcher m = p.matcher(ageString); if (!m.matches()) { throw new JournalException("Parameter '" + PARAMETER_JOURNAL_FILE_AGE_LIMIT + "' must be an integer number of seconds, optionally " + "followed by 'D'(days), 'H'(hours), or 'M'(minutes), " + "or a 0 to indicate no age limit"); } long age = Long.parseLong(m.group(1)) * 1000; String factor = m.group(2); if ("D".equals(factor)) { age *= 24 * 60 * 60; } else if ("H".equals(factor)) { age *= 60 * 60; } else if ("M".equals(factor)) { age *= 60; } return age; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy