001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Sep 17, 2007
014: * @author Will Gorman
015: */
016: package org.pentaho.plugin.versionchecker;
017:
018: import org.pentaho.core.session.IPentahoSession;
019: import org.pentaho.core.system.IPentahoSystemListener;
020: import org.pentaho.core.system.ISystemSettings;
021: import org.pentaho.core.system.PentahoSystem;
022: import org.pentaho.plugin.quartz.QuartzSystemListener;
023: import org.quartz.JobDetail;
024: import org.quartz.Scheduler;
025: import org.quartz.SimpleTrigger;
026: import org.quartz.Trigger;
027:
028: public class VersionCheckSystemListener implements
029: IPentahoSystemListener {
030:
031: private static final String VERSION_CHECK_JOBNAME = "PentahoSystemVersionCheck"; //$NON-NLS-1$
032:
033: // config params in the pentaho.xml file
034: private static final String VERSION_CHECK_CONFIG = "version-check-config"; //$NON-NLS-1$
035: private static final String REPEAT_INTERVAL_SECONDS = "repeat-interval-seconds"; //$NON-NLS-1$
036: private static final String RELEASE_FLAGS = "release-flags"; //$NON-NLS-1$
037: private static final String DISABLE = "disable"; //$NON-NLS-1$
038:
039: public boolean startup(IPentahoSession session) {
040: if (PentahoVersionCheckReflectHelper
041: .isVersionCheckerAvailable()) {
042: // register version check job
043: try {
044: final ISystemSettings config = PentahoSystem
045: .getSystemSettings();
046: int repeatSeconds = 86400; // daily
047: int versionRequestFlags = -1;
048: boolean disable = false;
049: if (config != null) {
050:
051: // pull out repeat seconds from pentaho.xml
052: try {
053: repeatSeconds = Integer
054: .parseInt(config
055: .getSystemSetting(
056: VERSION_CHECK_CONFIG
057: + "/" + REPEAT_INTERVAL_SECONDS, "86400")); //$NON-NLS-1$ //$NON-NLS-2$
058: } catch (Exception e) {
059: // ignore parsing error
060: }
061:
062: if (repeatSeconds < (43200)) { // Force maximum number of times to check in a day
063: repeatSeconds = 43200;
064: }
065:
066: // pull out release flags from pentaho.xml
067: try {
068: String releaseFlags = config
069: .getSystemSetting(
070: VERSION_CHECK_CONFIG
071: + "/" + RELEASE_FLAGS, "minor, ga").toLowerCase(); //$NON-NLS-1$//$NON-NLS-2$
072:
073: boolean requestMajorReleases = releaseFlags
074: .indexOf("major") >= 0; //$NON-NLS-1$
075: boolean requestMinorReleases = releaseFlags
076: .indexOf("minor") >= 0; //$NON-NLS-1$
077: boolean requestRCReleases = releaseFlags
078: .indexOf("rc") >= 0; //$NON-NLS-1$
079: boolean requestGAReleases = releaseFlags
080: .indexOf("ga") >= 0; //$NON-NLS-1$
081: boolean requestMilestoneReleases = releaseFlags
082: .indexOf("milestone") >= 0; //$NON-NLS-1$
083:
084: versionRequestFlags = (requestMajorReleases ? 4
085: : 0)
086: + (requestMinorReleases ? 8 : 0)
087: + (requestRCReleases ? 16 : 0)
088: + (requestGAReleases ? 32 : 0)
089: + (requestMilestoneReleases ? 64 : 0);
090:
091: } catch (Exception e) {
092: // ignore errors
093: }
094:
095: // pull out disable flag from pentaho.xml
096: try {
097: disable = "true".equals(config.getSystemSetting(VERSION_CHECK_CONFIG //$NON-NLS-1$
098: + "/" + DISABLE, "false")); //$NON-NLS-1$ //$NON-NLS-2$
099: } catch (Exception e) {
100: // ignore errors
101: }
102: }
103:
104: if (!disable) {
105: scheduleJob(versionRequestFlags, repeatSeconds);
106: } else {
107: deleteJobIfNecessary();
108: }
109: } catch (Exception e) {
110: // ignore errors
111: }
112:
113: } else {
114: deleteJobIfNecessary();
115: }
116: return true;
117: }
118:
119: protected void scheduleJob(int versionRequestFlags,
120: int repeatSeconds) throws Exception {
121: Scheduler sched = QuartzSystemListener.getSchedulerInstance();
122:
123: JobDetail jobDetail = new JobDetail(VERSION_CHECK_JOBNAME,
124: Scheduler.DEFAULT_GROUP, VersionCheckerJob.class);
125:
126: // setup version request flags
127: jobDetail.getJobDataMap().put(
128: VersionCheckerJob.VERSION_REQUEST_FLAGS,
129: versionRequestFlags);
130:
131: // do not persist the job
132: jobDetail.setVolatility(true);
133:
134: Trigger trigger = new SimpleTrigger(
135: "DailyTrigger", Scheduler.DEFAULT_GROUP, SimpleTrigger.REPEAT_INDEFINITELY, repeatSeconds * 1000); //$NON-NLS-1$
136:
137: // do not persist the trigger
138: trigger.setVolatility(true);
139:
140: if (sched.getJobDetail(jobDetail.getName(),
141: Scheduler.DEFAULT_GROUP) != null) {
142: sched.deleteJob(jobDetail.getName(),
143: Scheduler.DEFAULT_GROUP);
144: }
145: sched.scheduleJob(jobDetail, trigger);
146: }
147:
148: protected void deleteJobIfNecessary() {
149: try {
150: // delete the job if it exists in the system
151: Scheduler sched = QuartzSystemListener
152: .getSchedulerInstance();
153: if (sched.getJobDetail(VERSION_CHECK_JOBNAME,
154: Scheduler.DEFAULT_GROUP) != null) {
155: sched.deleteJob(VERSION_CHECK_JOBNAME,
156: Scheduler.DEFAULT_GROUP);
157: }
158: } catch (Exception e) {
159: // ignore errors
160: }
161: }
162:
163: public void shutdown() {
164: }
165: }
|