001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.configuration; 029 030import org.opencms.main.CmsContextInfo; 031import org.opencms.main.CmsLog; 032import org.opencms.main.OpenCms; 033import org.opencms.scheduler.CmsScheduleManager; 034import org.opencms.scheduler.CmsScheduledJobInfo; 035 036import java.util.ArrayList; 037import java.util.Iterator; 038import java.util.List; 039 040import org.apache.commons.digester3.Digester; 041 042import org.dom4j.Element; 043 044/** 045 * Scheduled jobs configuration class.<p> 046 */ 047public class CmsSchedulerConfiguration extends A_CmsXmlConfiguration { 048 049 /** The name of the DTD for this configuration. */ 050 public static final String CONFIGURATION_DTD_NAME = "opencms-scheduler.dtd"; 051 052 /** The name of the default XML file for this configuration. */ 053 public static final String DEFAULT_XML_FILE_NAME = "opencms-scheduler.xml"; 054 055 /** The node name for the job "active" value. */ 056 public static final String N_ACTIVE = "active"; 057 058 /** The node name for a job class. */ 059 public static final String N_CLASS = "class"; 060 061 /** The node name for the job context. */ 062 public static final String N_CONTEXT = "context"; 063 064 /** The node name for the job cron expression. */ 065 public static final String N_CRONEXPRESSION = "cronexpression"; 066 067 /** The node name for the context encoding. */ 068 public static final String N_ENCODING = "encoding"; 069 070 /** The node name for a job. */ 071 public static final String N_JOB = "job"; 072 073 /** The node name for individual locales. */ 074 public static final String N_LOCALE = "locale"; 075 076 /** The node name for the job parameters. */ 077 public static final String N_PARAMETERS = "parameters"; 078 079 /** The node name for the context project name. */ 080 public static final String N_PROJECT = "project"; 081 082 /** The node name for the context remote addr. */ 083 public static final String N_REMOTEADDR = "remoteaddr"; 084 085 /** The node name for the context requested uri. */ 086 public static final String N_REQUESTEDURI = "requesteduri"; 087 088 /** The node name for the job "reuseinstance" value. */ 089 public static final String N_REUSEINSTANCE = "reuseinstance"; 090 091 /** The node name for the scheduler. */ 092 public static final String N_SCHEDULER = "scheduler"; 093 094 /** The node name for the context site root. */ 095 public static final String N_SITEROOT = "siteroot"; 096 097 /** The node name for the context user name. */ 098 public static final String N_USERNAME = "user"; 099 100 /** The list of jobs for the scheduler. */ 101 private List<CmsScheduledJobInfo> m_configuredJobs; 102 103 /** The configured schedule manager. */ 104 private CmsScheduleManager m_scheduleManager; 105 106 /** 107 * Adds a new job description for the scheduler.<p> 108 * 109 * @param jobInfo the job description to add 110 */ 111 public void addJobFromConfiguration(CmsScheduledJobInfo jobInfo) { 112 113 m_configuredJobs.add(jobInfo); 114 115 if (CmsLog.INIT.isInfoEnabled()) { 116 CmsLog.INIT.info( 117 Messages.get().getBundle().key( 118 Messages.INIT_SCHEDULER_CONFIG_JOB_3, 119 jobInfo.getJobName(), 120 jobInfo.getClassName(), 121 jobInfo.getContextInfo().getUserName())); 122 } 123 } 124 125 /** 126 * Generates the schedule manager.<p> 127 */ 128 public void addScheduleManager() { 129 130 m_scheduleManager = new CmsScheduleManager(m_configuredJobs); 131 } 132 133 /** 134 * @see org.opencms.configuration.I_CmsXmlConfiguration#addXmlDigesterRules(org.apache.commons.digester3.Digester) 135 */ 136 public void addXmlDigesterRules(Digester digester) { 137 138 // add scheduler creation rule 139 digester.addCallMethod("*/" + N_SCHEDULER, "addScheduleManager"); 140 141 // add scheduler job creation rule 142 digester.addObjectCreate("*/" + N_SCHEDULER + "/" + N_JOB, CmsScheduledJobInfo.class); 143 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_NAME, "jobName"); 144 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CLASS, "className"); 145 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CRONEXPRESSION, "cronExpression"); 146 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_REUSEINSTANCE, "reuseInstance"); 147 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_ACTIVE, "active"); 148 digester.addSetNext("*/" + N_SCHEDULER + "/" + N_JOB, "addJobFromConfiguration"); 149 150 // add job context creation rule 151 digester.addObjectCreate("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT, CmsContextInfo.class); 152 digester.addBeanPropertySetter( 153 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_USERNAME, 154 "userName"); 155 digester.addBeanPropertySetter( 156 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_PROJECT, 157 "projectName"); 158 digester.addBeanPropertySetter( 159 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_SITEROOT, 160 "siteRoot"); 161 digester.addBeanPropertySetter( 162 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_REQUESTEDURI, 163 "requestedUri"); 164 digester.addBeanPropertySetter( 165 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_LOCALE, 166 "localeName"); 167 digester.addBeanPropertySetter("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_ENCODING); 168 digester.addBeanPropertySetter( 169 "*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT + "/" + N_REMOTEADDR, 170 "remoteAddr"); 171 digester.addSetNext("*/" + N_SCHEDULER + "/" + N_JOB + "/" + N_CONTEXT, "setContextInfo"); 172 173 // add generic parameter rules for jobs, password handler 174 digester.addCallMethod( 175 "*/" + I_CmsXmlConfiguration.N_PARAM, 176 I_CmsConfigurationParameterHandler.ADD_PARAMETER_METHOD, 177 2); 178 digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 0, I_CmsXmlConfiguration.A_NAME); 179 digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 1); 180 } 181 182 /** 183 * @see org.opencms.configuration.I_CmsXmlConfiguration#generateXml(org.dom4j.Element) 184 */ 185 public Element generateXml(Element parent) { 186 187 // generate system node and sub notes 188 Element schedulerElement = parent.addElement(N_SCHEDULER); 189 if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 190 m_configuredJobs = OpenCms.getScheduleManager().getJobs(); 191 } 192 193 Iterator<CmsScheduledJobInfo> jobs = m_configuredJobs.iterator(); 194 while (jobs.hasNext()) { 195 CmsScheduledJobInfo jobInfo = jobs.next(); 196 Element jobElement = schedulerElement.addElement(N_JOB); 197 jobElement.addElement(N_NAME).addText(jobInfo.getJobName()); 198 jobElement.addElement(N_CLASS).addText(jobInfo.getClassName()); 199 jobElement.addElement(N_REUSEINSTANCE).addText(String.valueOf(jobInfo.isReuseInstance())); 200 jobElement.addElement(N_ACTIVE).addText(String.valueOf(jobInfo.isActive())); 201 jobElement.addElement(N_CRONEXPRESSION).addCDATA(jobInfo.getCronExpression()); 202 Element contextElement = jobElement.addElement(N_CONTEXT); 203 contextElement.addElement(N_USERNAME).setText(jobInfo.getContextInfo().getUserName()); 204 contextElement.addElement(N_PROJECT).setText(jobInfo.getContextInfo().getProjectName()); 205 contextElement.addElement(N_SITEROOT).setText(jobInfo.getContextInfo().getSiteRoot()); 206 contextElement.addElement(N_REQUESTEDURI).setText(jobInfo.getContextInfo().getRequestedUri()); 207 contextElement.addElement(N_LOCALE).setText(jobInfo.getContextInfo().getLocaleName()); 208 contextElement.addElement(N_ENCODING).setText(jobInfo.getContextInfo().getEncoding()); 209 contextElement.addElement(N_REMOTEADDR).setText(jobInfo.getContextInfo().getRemoteAddr()); 210 CmsParameterConfiguration jobParameters = jobInfo.getConfiguration(); 211 if ((jobParameters != null) && (jobParameters.size() > 0)) { 212 Element parameterElement = jobElement.addElement(N_PARAMETERS); 213 jobParameters.appendToXml(parameterElement); 214 } 215 } 216 217 return schedulerElement; 218 } 219 220 /** 221 * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdFilename() 222 */ 223 public String getDtdFilename() { 224 225 return CONFIGURATION_DTD_NAME; 226 } 227 228 /** 229 * Returns the configured schedule manager.<p> 230 * 231 * @return the configured schedule manager 232 */ 233 public CmsScheduleManager getScheduleManager() { 234 235 return m_scheduleManager; 236 } 237 238 /** 239 * @see org.opencms.configuration.A_CmsXmlConfiguration#initMembers() 240 */ 241 @Override 242 protected void initMembers() { 243 244 setXmlFileName(DEFAULT_XML_FILE_NAME); 245 m_configuredJobs = new ArrayList<CmsScheduledJobInfo>(); 246 } 247 248}