org.quartz.SchedulerContext 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 context/environment data that can be made available to Jobs as they
* are executed. This feature is much like the ServletContext feature when
* working with J2EE servlets.
*
*
* @see Scheduler#getContext
*
* @author James House
*/
public class SchedulerContext extends DirtyFlagMap implements Serializable {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Data members.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
private boolean allowsTransientData = false;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
*
* Create an empty JobDataMap
.
*
*/
public SchedulerContext() {
super(15);
}
/**
*
* Create a JobDataMap
with the given data.
*
*/
public SchedulerContext(Map map) {
this();
putAll(map);
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
*
* Tell the SchedulerContext
that it should allow non-
* Serializable
data.
*
*
*
* Future versions of Quartz may make distinctions on how it propogates
* data in the SchedulerContext between instances of proxies to a single
* scheduler instance - i.e. if Quartz is being used via RMI.
*
*/
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 SchedulerContext
.
*
*
*
* All keys must be String
s.
*
*/
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 SchedulerContext
.
*
*/
public void put(String key, int value) {
super.put(key, new Integer(value));
}
/**
*
* Adds the given long
value to the SchedulerContext
.
*
*/
public void put(String key, long value) {
super.put(key, new Long(value));
}
/**
*
* Adds the given float
value to the SchedulerContext
.
*
*/
public void put(String key, float value) {
super.put(key, new Float(value));
}
/**
*
* Adds the given double
value to the SchedulerContext
.
*
*/
public void put(String key, double value) {
super.put(key, new Double(value));
}
/**
*
* Adds the given boolean
value to the SchedulerContext
.
*
*/
public void put(String key, boolean value) {
super.put(key, new Boolean(value));
}
/**
*
* Adds the given char
value to the SchedulerContext
.
*
*/
public void put(String key, char value) {
super.put(key, new Character(value));
}
/**
*
* Adds the given String
value to the SchedulerContext
.
*
*/
public void put(String key, String value) {
super.put(key, value);
}
/**
*
* Adds the given Object
value to the SchedulerContext
.
*
*/
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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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 SchedulerContext
.
*
*
* @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.");
}
}
public String[] getKeys() {
return (String[]) keySet().toArray(new String[size()]);
}
}