001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.usecase.scheduling.impl;
019:
020: import java.util.Date;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.cocoon.components.ContextHelper;
033: import org.apache.cocoon.components.cron.CronJob;
034: import org.apache.cocoon.components.cron.JobScheduler;
035: import org.apache.cocoon.components.cron.JobSchedulerEntry;
036: import org.apache.cocoon.environment.Request;
037: import org.apache.cocoon.environment.Session;
038: import org.apache.lenya.ac.Identity;
039: import org.apache.lenya.ac.Machine;
040: import org.apache.lenya.ac.User;
041: import org.apache.lenya.cms.usecase.Usecase;
042: import org.apache.lenya.cms.usecase.scheduling.UsecaseScheduler;
043:
044: /**
045: * <p>
046: * Usecase scheduler implementation.
047: * </p>
048: * <p>
049: * The names of the scheduled jobs have the syntax
050: * <code>{usecaseName}:{userId}</code>.
051: *
052: * @version $Id: UsecaseSchedulerImpl.java 535201 2007-05-04 12:24:27Z andreas $
053: */
054: public class UsecaseSchedulerImpl extends AbstractLogEnabled implements
055: UsecaseScheduler, Serviceable, Contextualizable {
056:
057: /**
058: * @see org.apache.lenya.cms.usecase.scheduling.UsecaseScheduler#schedule(org.apache.lenya.cms.usecase.Usecase,
059: * java.util.Date)
060: */
061: public void schedule(Usecase usecase, Date date) {
062: JobScheduler scheduler = null;
063: try {
064: scheduler = (JobScheduler) this .manager
065: .lookup(JobScheduler.ROLE);
066:
067: Parameters parameters = new Parameters();
068: String[] names = usecase.getParameterNames();
069: for (int i = 0; i < names.length; i++) {
070: Object value = usecase.getParameter(names[i]);
071: if (value instanceof String) {
072: parameters.setParameter(names[i], (String) value);
073: } else {
074: getLogger()
075: .warn(
076: "Parameter ["
077: + names[i]
078: + "] = ["
079: + value
080: + "] ("
081: + value.getClass()
082: .getName()
083: + ") ignored, only string values are supported.");
084: }
085: }
086:
087: Map objects = new HashMap();
088: objects.put(UsecaseCronJob.USECASE_NAME, usecase.getName());
089: objects.put(UsecaseCronJob.SOURCE_URL, usecase
090: .getSourceURL());
091:
092: String userId = "";
093: Request request = ContextHelper.getRequest(this .context);
094: Session session = request.getSession(false);
095: if (session != null) {
096: Identity identity = (Identity) session
097: .getAttribute(Identity.class.getName());
098: if (identity != null) {
099: User user = identity.getUser();
100: if (user != null) {
101: userId = user.getId();
102: objects.put(UsecaseCronJob.USER_ID, userId);
103: }
104: Machine machine = identity.getMachine();
105: if (machine != null) {
106: objects.put(UsecaseCronJob.MACHINE_IP, machine
107: .getIp());
108: }
109: }
110: }
111:
112: String role = CronJob.class.getName() + "/usecase";
113: String name = getJobName(usecase, userId);
114: scheduler.fireJobAt(date, name, role, parameters, objects);
115:
116: } catch (Exception e) {
117: getLogger().error("Could not create job: ", e);
118: throw new RuntimeException(e);
119: } finally {
120: if (scheduler != null) {
121: this .manager.release(scheduler);
122: }
123: }
124: }
125:
126: protected String getJobName(Usecase usecase, String userId) {
127: return usecase.getName() + ":" + userId;
128: }
129:
130: protected ServiceManager manager;
131:
132: /**
133: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
134: */
135: public void service(ServiceManager manager) throws ServiceException {
136: this .manager = manager;
137: }
138:
139: private Context context;
140:
141: /**
142: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
143: */
144: public void contextualize(Context context) throws ContextException {
145: this .context = context;
146: }
147:
148: /**
149: * @see org.apache.lenya.cms.usecase.scheduling.UsecaseScheduler#getJobs()
150: */
151: public JobSchedulerEntry[] getJobs() {
152: JobScheduler scheduler = null;
153: JobSchedulerEntry[] entries = null;
154: try {
155: scheduler = (JobScheduler) this .manager
156: .lookup(JobScheduler.ROLE);
157:
158: String[] jobNames = scheduler.getJobNames();
159: entries = new JobSchedulerEntry[jobNames.length];
160:
161: for (int i = 0; i < jobNames.length; i++) {
162: JobSchedulerEntry entry = scheduler
163: .getJobSchedulerEntry(jobNames[i]);
164: entries[i] = entry;
165: }
166:
167: } catch (Exception e) {
168: getLogger().error("Could not obtain job list: ", e);
169: throw new RuntimeException(e);
170: } finally {
171: if (scheduler != null) {
172: this.manager.release(scheduler);
173: }
174: }
175: return entries;
176: }
177:
178: }
|