SchedulerService.java revision a1699f6c06918aac3baa40b3d46e82690c491518
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012 ForgeRock AS. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
*/
/**
* Scheduler service using Quartz
*
* @author aegloff
* @author ckienle
*/
@Component(name = "org.forgerock.openidm.scheduler", immediate=true, policy=ConfigurationPolicy.REQUIRE)
@Properties({
})
public class SchedulerService implements RequestHandler {
// Keys in the OSGi configuration
// Valid configuration values
// Default service PID prefix to use if the invokeService name is a fragment
// Misfire Policies
// Internal service tracker
private static Scheduler inMemoryScheduler;
private Map<String, ScheduleConfigService> configMap = new HashMap<String, ScheduleConfigService>();
private boolean executePersistentSchedules = false;
private boolean started = false;
// Optional user defined name for this instance, derived from the file install name
// Tracks OSGi services that match the configured service PID
+ " to conform to the 'schedule-<name>.json' format");
return;
}
synchronized (CONFIG_SERVICE_LOCK) {
// TODO: This should be reworked to start after all "core" services are available
started = true;
// Initialize the schedulers (if they haven't been already)
// Start processing schedules
if (executePersistentSchedules) {
} else {
}
try {
} catch (ObjectAlreadyExistsException e) {
}
}
}
}
public void registerConfigService(ScheduleConfigService service) throws SchedulerException, ParseException {
synchronized (CONFIG_SERVICE_LOCK) {
boolean update = false;
update = true;
}
if (!started) {
logger.debug("The Scheduler Service has not been started yet, storing new Schedule {}", service.getJobName());
} else {
try {
} catch (ObjectAlreadyExistsException e) {
}
}
}
}
synchronized (CONFIG_SERVICE_LOCK) {
if (!started) {
} else {
}
}
}
try {
}
} catch (SchedulerException e) {
} finally {
}
try {
}
} catch (SchedulerException e) {
}
}
/**
* Schedules a job.
*
* @param scheduleConfig The schedule configuration
* @param jobName The job name
* @param update Whether to delete the old job if present
* @return true if the job was scheduled, false otherwise
* @throws SchedulerException
* @throws ParseException
* @throws ObjectAlreadyExistsException
*/
try {
// Lock access to the scheduler so that a schedule is not added during a config update
synchronized (LOCK) {
if (scheduleConfig.getConcurrentExecution()) {
scheduleClass = SchedulerServiceJob.class;
} else {
scheduleClass = StatefulSchedulerServiceJob.class;
}
// Check if the new or updated job is disabled
/*if (!scheduleConfig.getEnabled()) {
logger.info("Schedule {} is disabled", jobName);
if (jobExists(jobName, scheduleConfig.getPersisted()) &&
scheduler.deleteJob(jobName, GROUP_NAME)) {
logger.debug("Schedule was deleted from scheduler");
}
return false;
}*/
// Attempt to add the scheduler
if (update) {
if (exists) {
// Update the job by first deleting it, then scheduling the new version
}
}
// check if it is enabled
if (scheduleConfig.getEnabled()) {
// Set to non-durable so that jobs won't persist after last firing
job.setDurability(false);
// Schedule the Job (with trigger)
} else {
// Set the job to durable so that it can exist without a trigger (since the job is "disabled")
job.setDurability(true);
// Add the job (no trigger)
}
}
}
} catch (ParseException ex) {
throw ex;
} catch (ObjectAlreadyExistsException ex) {
throw ex;
} catch (SchedulerException ex) {
throw ex;
}
return true;
}
/**
* Creates and returns a CronTrigger using the supplied schedule configuration.
*
* @param scheduleConfig The schedule configuration
* @param jobName The name of the job to associate the trigger with
* @return The created Trigger
* @throws ParseException
*/
private CronTrigger createTrigger(ScheduleConfig scheduleConfig, String jobName) throws ParseException {
}
}
}
}
return trigger;
}
/**
* Creates and returns a JobDataMap using the supplied schedule configuration.
*
* @param scheduleConfig The schedule configuration
* @return The created JobDataMap
*/
map.put(ScheduledService.CONFIG_NAME, "scheduler"+ (configFactoryPID != null ? "-" + configFactoryPID : ""));
return map;
}
/**
* Returns the Scheduler corresponding to whether the supplied schedule configuration is persistent.
*
* @param scheduleConfig The schedule configuration
* @return The Scheduler
* @throws SchedulerException
*/
if (scheduleConfig.getPersisted()) {
return persistentScheduler;
}
return inMemoryScheduler;
}
/**
* Creates a random Job name.
*
* @return A job name
*/
private String createJobName() {
}
/**
* Determines if a job already exists.
*
* @param jobName The name of the job
* @param persisted If the job is persisted or not
* @return True if the job exists, false otherwise
* @throws SchedulerException
*/
if (!persisted) {
} else {
}
}
public void handleCreate(ServerContext context, CreateRequest request, ResultHandler<Resource> handler) {
try {
try {
id = createJobName();
} else {
}
// Check defaults
scheduleConfig.setEnabled(true);
}
scheduleConfig.setPersisted(true);
}
try {
} catch (ParseException e) {
throw new BadRequestException(e);
} catch (ObjectAlreadyExistsException e) {
throw new ConflictException(e);
} catch (SchedulerException e) {
throw new InternalServerErrorException(e);
}
} catch (JsonException e) {
throw new BadRequestException("Error creating schedule", e);
}
} catch (Throwable t) {
}
}
public void handleRead(ServerContext context, ReadRequest request, ResultHandler<Resource> handler) {
try {
try {
} else {
throw new NotFoundException("Schedule does not exist");
}
} catch (SchedulerException e) {
throw new InternalServerErrorException(e);
}
} catch (Throwable t) {
}
}
public void handleUpdate(ServerContext context, UpdateRequest request, ResultHandler<Resource> handler) {
try {
try {
throw new BadRequestException( "No ID specified");
}
// Default incoming config to "persisted" if not specified
if (persistedValue == null) {
}
try {
throw new NotFoundException();
} else {
// Update the Job
}
} catch (ParseException e) {
throw new BadRequestException( e);
} catch (ObjectAlreadyExistsException e) {
throw new ConflictException(e);
} catch (SchedulerException e) {
throw new InternalServerErrorException(e);
}
} catch (JsonException e) {
throw new BadRequestException("Error updating schedule", e);
}
} catch (Throwable t) {
}
}
public void handleDelete(ServerContext context, DeleteRequest request, ResultHandler<Resource> handler) {
try {
try {
throw new BadRequestException("No ID specified");
}
try {
} else {
throw new NotFoundException("Schedule does not exist");
}
} catch (SchedulerException e) {
throw new InternalServerErrorException(e);
}
} catch (JsonException e) {
throw new BadRequestException( "Error updating schedule", e);
}
} catch (Throwable t) {
}
}
public void handlePatch(ServerContext context, PatchRequest request, ResultHandler<Resource> handler) {
}
try {
throw new BadRequestException( "query-id parameters");
}
try {
try {
// Query all the Job IDs in both schedulers
if (persistentJobNames != null) {
}
}
if (inMemoryJobNames != null) {
}
}
} else {
}
} catch (SchedulerException e) {
throw new InternalServerErrorException( e);
}
} catch (JsonException e) {
throw new BadRequestException("Error updating schedule", e);
}
}
} catch (Throwable t) {
}
}
public void handleAction(ServerContext context, ActionRequest request, final ResultHandler<JsonValue> handler) {
try {
throw new BadRequestException("Expecting _action parameter");
}
try {
id = createJobName();
try {
throw new BadRequestException("Schedule already exists");
}
}
}
});
} catch (SchedulerException e) {
throw new InternalServerErrorException(e);
}
} else {
}
} catch (JsonException e) {
throw new BadRequestException("Error updating schedule", e);
}
} catch (Throwable t) {
}
}
throw new BadRequestException("Invalid Resource ID");
}
return resourceId;
}
}
return id;
}
public boolean isConfigured() {
if (schedulerConfig == null) {
return false;
}
return true;
}
if (alreadyConfigured) {
// Close current scheduler
}
}
executePersistentSchedules = true;
} else {
executePersistentSchedules = false;
}
}
private void createPersistentScheduler() throws SchedulerException {
// Get the persistent scheduler using our custom JobStore implementation
}
private void initInMemoryScheduler() throws SchedulerException {
try {
if (inMemoryScheduler == null) {
// Quartz tries to be too smart about classloading,
// but relies on the thread context classloader to load classload helpers
// That is not a good idea in OSGi,
// hence, hand it the OSGi classloader for the ClassLoadHelper we want it to find
// Get the in-memory scheduler
// Must use DirectSchedulerFactory instance so that it does not confict with
// the StdSchedulerFactory (used to create the persistent schedulers).
// Set back to the original thread context classloader
}
} catch (SchedulerException ex) {
throw ex;
}
}
try {
} catch (IOException ex) {
}
return jsonValue;
}
}