📄 schedulercontext.java
字号:
/* * Copyright James House (c) 2001-2004 * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */package org.quartz;import java.io.Serializable;import java.util.Iterator;import java.util.Map;import org.quartz.utils.DirtyFlagMap;/** * <p> * 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. * </p> * * @see Scheduler#getContext * * @author James House */public class SchedulerContext extends DirtyFlagMap implements Serializable { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private boolean allowsTransientData = false; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * <p> * Create an empty <code>JobDataMap</code>. * </p> */ public SchedulerContext() { super(15); } /** * <p> * Create a <code>JobDataMap</code> with the given data. * </p> */ public SchedulerContext(Map map) { this(); putAll(map); } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * <p> * Tell the <code>SchedulerContext</code> that it should allow non- * <code>Serializable</code> data. * </p> * * <p> * 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. * </p> */ 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; } /** * <p> * Nulls-out any data values that are non-Serializable. * </p> */ 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]); } } /** * <p> * Adds the name-value pairs in the given <code>Map</code> to the <code>SchedulerContext</code>. * </p> * * <p> * All keys must be <code>String</code>s. * </p> */ 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 } } /** * <p> * Adds the given <code>int</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, int value) { super.put(key, new Integer(value)); } /** * <p> * Adds the given <code>long</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, long value) { super.put(key, new Long(value)); } /** * <p> * Adds the given <code>float</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, float value) { super.put(key, new Float(value)); } /** * <p> * Adds the given <code>double</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, double value) { super.put(key, new Double(value)); } /** * <p> * Adds the given <code>boolean</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, boolean value) { super.put(key, new Boolean(value)); } /** * <p> * Adds the given <code>char</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, char value) { super.put(key, new Character(value)); } /** * <p> * Adds the given <code>String</code> value to the <code>SchedulerContext</code>. * </p> */ public void put(String key, String value) { super.put(key, value); } /** * <p> * Adds the given <code>Object</code> value to the <code>SchedulerContext</code>. * </p> */ 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); } /** * <p> * Retrieve the identified <code>int</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>long</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>float</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>double</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>boolean</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>char</code> value from the <code>SchedulerContext</code>. * </p> * * @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."); } } /** * <p> * Retrieve the identified <code>String</code> value from the <code>SchedulerContext</code>. * </p> * * @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()]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -