org.quartz.JobDataMap Maven / Gradle / Ivy
/*
* Copyright 2004-2005 OpenSymphony
*
* Licensed under 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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
/*
* Previously Copyright (c) 2001-2004 James House
*/
package org.quartz;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import org.quartz.utils.DirtyFlagMap;
/**
*
* Holds state information for Job
instances.
*
*
*
* JobDataMap
instances are stored once when the Job
* is added to a scheduler. They are also re-persisted after every execution of
* StatefulJob
instances.
*
*
*
* JobDataMap
instances can also be stored with a
* Trigger
. This can be useful in the case where you have a Job
* that is stored in the scheduler for regular/repeated use by multiple
* Triggers, yet with each independent triggering, you want to supply the
* Job with different data inputs.
*
*
*
* The JobExecutionContext
passed to a Job at execution time
* also contains a convenience JobDataMap
that is the result
* of merging the contents of the trigger's JobDataMap (if any) over the
* Job's JobDataMap (if any).
*
*
* @see Job
* @see StatefulJob
* @see Trigger
* @see JobExecutionContext
*
* @author James House
*/
public class JobDataMap extends DirtyFlagMap implements Serializable {
private static final long serialVersionUID = -6939901990106713909L;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Data members.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
private boolean allowsTransientData = false;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
*
* Create an empty JobDataMap
.
*
*/
public JobDataMap() {
super(15);
}
/**
*
* Create a JobDataMap
with the given data.
*
*/
public JobDataMap(Map map) {
this();
putAll(map);
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
*
* Tell the JobDataMap
that it should allow non- Serializable
* data.
*
*
*
* If the JobDataMap
does contain non- Serializable
* objects, and it belongs to a non-volatile Job
that is
* stored in a JobStore
that supports persistence, then
* those elements will be nulled-out during persistence.
*
*/
public void setAllowsTransientData(boolean allowsTransientData) {
if (containsTransientData() && !allowsTransientData)
throw new IllegalStateException(
"Cannot set property 'allowsTransientData' to 'false' "
+ "when data map contains non-serializable objects.");
this.allowsTransientData = allowsTransientData;
}
public boolean getAllowsTransientData() {
return allowsTransientData;
}
public boolean containsTransientData() {
if (!getAllowsTransientData()) // short circuit...
return false;
String[] keys = getKeys();
for (int i = 0; i < keys.length; i++) {
Object o = super.get(keys[i]);
if (!(o instanceof Serializable)) return true;
}
return false;
}
/**
*
* Nulls-out any data values that are non-Serializable.
*
*/
public void removeTransientData() {
if (!getAllowsTransientData()) // short circuit...
return;
String[] keys = getKeys();
for (int i = 0; i < keys.length; i++) {
Object o = super.get(keys[i]);
if (!(o instanceof Serializable)) remove(keys[i]);
}
}
/**
*
* Adds the name-value pairs in the given Map
to the JobDataMap
.
*
*
*
* All keys must be String
s, and all values must be Serializable
.
*
*/
public void putAll(Map map) {
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
Object key = itr.next();
Object val = map.get(key);
put(key, val);
// will throw IllegalArgumentException if value not serilizable
}
}
/**
*
* Adds the given int
value to the Job
's
* data map.
*
*/
public void put(String key, int value) {
super.put(key, new Integer(value));
}
/**
*
* Adds the given long
value to the Job
's
* data map.
*
*/
public void put(String key, long value) {
super.put(key, new Long(value));
}
/**
*
* Adds the given float
value to the Job
's
* data map.
*
*/
public void put(String key, float value) {
super.put(key, new Float(value));
}
/**
*
* Adds the given double
value to the Job
's
* data map.
*
*/
public void put(String key, double value) {
super.put(key, new Double(value));
}
/**
*
* Adds the given boolean
value to the Job
's
* data map.
*
*/
public void put(String key, boolean value) {
super.put(key, new Boolean(value));
}
/**
*
* Adds the given char
value to the Job
's
* data map.
*
*/
public void put(String key, char value) {
super.put(key, new Character(value));
}
/**
*
* Adds the given String
value to the Job
's
* data map.
*
*/
public void put(String key, String value) {
super.put(key, value);
}
/**
*
* Adds the given boolean
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, boolean value) {
String strValue = new Boolean(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Boolean
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Boolean value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given char
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, char value) {
String strValue = new Character(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Character
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Character value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given double
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, double value) {
String strValue = new Double(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Double
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Double value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given float
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, float value) {
String strValue = new Float(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Float
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Float value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given int
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, int value) {
String strValue = new Integer(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Integer
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Integer value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given long
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, long value) {
String strValue = new Long(value).toString();
super.put(key, strValue);
}
/**
*
* Adds the given Long
value as a string version to the
* Job
's data map.
*
*/
public void putAsString(String key, Long value) {
String strValue = value.toString();
super.put(key, strValue);
}
/**
*
* Adds the given Serializable
object value to the JobDataMap
.
*
*/
public Object put(Object key, Object value) {
if (!(key instanceof String))
throw new IllegalArgumentException(
"Keys in map must be Strings.");
return super.put(key, value);
}
/**
*
* Retrieve the identified int
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not an Integer.
*/
public int getInt(String key) {
Object obj = get(key);
try {
return ((Integer) obj).intValue();
} catch (Exception e) {
throw new ClassCastException("Identified object is not an Integer.");
}
}
/**
*
* Retrieve the identified long
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a Long.
*/
public long getLong(String key) {
Object obj = get(key);
try {
return ((Long) obj).longValue();
} catch (Exception e) {
throw new ClassCastException("Identified object is not a Long.");
}
}
/**
*
* Retrieve the identified float
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a Float.
*/
public float getFloat(String key) {
Object obj = get(key);
try {
return ((Float) obj).floatValue();
} catch (Exception e) {
throw new ClassCastException("Identified object is not a Float.");
}
}
/**
*
* Retrieve the identified double
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a Double.
*/
public double getDouble(String key) {
Object obj = get(key);
try {
return ((Double) obj).doubleValue();
} catch (Exception e) {
throw new ClassCastException("Identified object is not a Double.");
}
}
/**
*
* Retrieve the identified boolean
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a Boolean.
*/
public boolean getBoolean(String key) {
Object obj = get(key);
try {
return ((Boolean) obj).booleanValue();
} catch (Exception e) {
throw new ClassCastException("Identified object is not a Boolean.");
}
}
/**
*
* Retrieve the identified char
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a Character.
*/
public char getChar(String key) {
Object obj = get(key);
try {
return ((Character) obj).charValue();
} catch (Exception e) {
throw new ClassCastException(
"Identified object is not a Character.");
}
}
/**
*
* Retrieve the identified String
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public String getString(String key) {
Object obj = get(key);
try {
return (String) obj;
} catch (Exception e) {
throw new ClassCastException("Identified object is not a String.");
}
}
/**
*
* Retrieve the identified int
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public int getIntFromString(String key) {
Object obj = get(key);
return new Integer((String) obj).intValue();
}
/**
*
* Retrieve the identified int
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String or Integeger.
*/
public long getIntValue(String key) {
Object obj = get(key);
if(obj instanceof String)
return getIntFromString(key);
else
return getInt(key);
}
/**
*
* Retrieve the identified int
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Integer getIntegerFromString(String key) {
Object obj = get(key);
return new Integer((String) obj);
}
/**
*
* Retrieve the identified boolean
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public boolean getBooleanValueFromString(String key) {
Object obj = get(key);
return new Boolean((String) obj).booleanValue();
}
/**
*
* Retrieve the identified boolean
value from the
* JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String or Boolean.
*/
public boolean getBooleanValue(String key) {
Object obj = get(key);
if(obj instanceof String)
return getBooleanValueFromString(key);
else
return getBoolean(key);
}
/**
*
* Retrieve the identified Boolean
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Boolean getBooleanFromString(String key) {
Object obj = get(key);
return new Boolean((String) obj);
}
/**
*
* Retrieve the identified char
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public char getCharFromString(String key) {
Object obj = get(key);
return ((String) obj).charAt(0);
}
/**
*
* Retrieve the identified Character
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Character getCharacterFromString(String key) {
Object obj = get(key);
return new Character(((String) obj).charAt(0));
}
/**
*
* Retrieve the identified double
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public double getDoubleValueFromString(String key) {
Object obj = get(key);
return new Double((String) obj).doubleValue();
}
/**
*
* Retrieve the identified double
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String or Double.
*/
public double getDoubleValue(String key) {
Object obj = get(key);
if(obj instanceof String)
return getDoubleValueFromString(key);
else
return getDouble(key);
}
/**
*
* Retrieve the identified Double
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Double getDoubleFromString(String key) {
Object obj = get(key);
return new Double((String) obj);
}
/**
*
* Retrieve the identified float
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public float getFloatValueFromString(String key) {
Object obj = get(key);
return new Float((String) obj).floatValue();
}
/**
*
* Retrieve the identified float
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String or Float.
*/
public float getFloatValue(String key) {
Object obj = get(key);
if(obj instanceof String)
return getFloatValueFromString(key);
else
return getFloat(key);
}
/**
*
* Retrieve the identified Float
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Float getFloatFromString(String key) {
Object obj = get(key);
return new Float((String) obj);
}
/**
*
* Retrieve the identified long
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public long getLongValueFromString(String key) {
Object obj = get(key);
return new Long((String) obj).longValue();
}
/**
*
* Retrieve the identified long
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String or Long.
*/
public long getLongValue(String key) {
Object obj = get(key);
if(obj instanceof String)
return getLongValueFromString(key);
else
return getLong(key);
}
/**
*
* Retrieve the identified Long
value from the JobDataMap
.
*
*
* @throws ClassCastException
* if the identified object is not a String.
*/
public Long getLongFromString(String key) {
Object obj = get(key);
return new Long((String) obj);
}
public String[] getKeys() {
return (String[]) keySet().toArray(new String[size()]);
}
}