001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.impl;
022:
023: import java.util.Collection;
024: import java.util.HashMap;
025:
026: import org.quartz.Scheduler;
027: import org.quartz.SchedulerException;
028:
029: /**
030: * <p>
031: * Holds references to Scheduler instances - ensuring uniqueness, and
032: * preventing garbage collection, and allowing 'global' lookups - all within a
033: * ClassLoader space.
034: * </p>
035: *
036: * @author James House
037: */
038: public class SchedulerRepository {
039:
040: /*
041: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
042: *
043: * Data members.
044: *
045: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
046: */
047:
048: private HashMap schedulers;
049:
050: private static SchedulerRepository inst;
051:
052: /*
053: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
054: *
055: * Constructors.
056: *
057: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
058: */
059:
060: private SchedulerRepository() {
061: schedulers = new HashMap();
062: }
063:
064: /*
065: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
066: *
067: * Interface.
068: *
069: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
070: */
071:
072: public static synchronized SchedulerRepository getInstance() {
073: if (inst == null) {
074: inst = new SchedulerRepository();
075: }
076:
077: return inst;
078: }
079:
080: public synchronized void bind(Scheduler sched)
081: throws SchedulerException {
082:
083: if ((Scheduler) schedulers.get(sched.getSchedulerName()) != null) {
084: throw new SchedulerException("Scheduler with name '"
085: + sched.getSchedulerName() + "' already exists.",
086: SchedulerException.ERR_BAD_CONFIGURATION);
087: }
088:
089: schedulers.put(sched.getSchedulerName(), sched);
090: }
091:
092: public synchronized boolean remove(String schedName) {
093: return (schedulers.remove(schedName) != null);
094: }
095:
096: public synchronized Scheduler lookup(String schedName) {
097: return (Scheduler) schedulers.get(schedName);
098: }
099:
100: public synchronized Collection lookupAll() {
101: return java.util.Collections.unmodifiableCollection(schedulers
102: .values());
103: }
104:
105: }
|