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.Collections;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.context.Context;
026: import org.apache.avalon.framework.context.ContextException;
027: import org.apache.avalon.framework.context.Contextualizable;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceSelector;
031: import org.apache.cocoon.components.CocoonComponentManager;
032: import org.apache.cocoon.components.ContextHelper;
033: import org.apache.cocoon.components.cron.ConfigurableCronJob;
034: import org.apache.cocoon.components.cron.ServiceableCronJob;
035: import org.apache.cocoon.environment.Environment;
036: import org.apache.cocoon.environment.ObjectModelHelper;
037: import org.apache.cocoon.environment.Request;
038: import org.apache.cocoon.environment.Session;
039: import org.apache.cocoon.environment.commandline.CommandLineRequest;
040: import org.apache.lenya.ac.AccessControlException;
041: import org.apache.lenya.ac.AccessController;
042: import org.apache.lenya.ac.AccessControllerResolver;
043: import org.apache.lenya.ac.Identifiable;
044: import org.apache.lenya.ac.Identity;
045: import org.apache.lenya.ac.Machine;
046: import org.apache.lenya.ac.User;
047: import org.apache.lenya.ac.UserManager;
048: import org.apache.lenya.cms.usecase.Usecase;
049: import org.apache.lenya.cms.usecase.UsecaseInvoker;
050:
051: /**
052: * Job to schedule usecase execution.
053: *
054: * @version $Id: UsecaseCronJob.java 535152 2007-05-04 10:14:41Z andreas $
055: */
056: public class UsecaseCronJob extends ServiceableCronJob implements
057: ConfigurableCronJob, Contextualizable {
058:
059: /**
060: * Initializes the job.
061: * @param usecase The usecase.
062: */
063: public void setup(Usecase usecase) {
064: this .usecaseName = usecase.getName();
065: String[] keys = usecase.getParameterNames();
066: for (int i = 0; i < keys.length; i++) {
067: this .parameters.put(keys[i], usecase.getParameter(keys[i]));
068: }
069: }
070:
071: private String usecaseName;
072: private String sourceUrl;
073: private String userId;
074: private String machineIp;
075:
076: private Map parameters = new HashMap();
077:
078: protected static final String USECASE_NAME = "usecaseName";
079: protected static final String SOURCE_URL = "sourceUrl";
080: protected static final String USER_ID = "userId";
081: protected static final String MACHINE_IP = "machineIp";
082:
083: /**
084: * @return The name of the usecase to execute.
085: */
086: public String getUsecaseName() {
087: return this .usecaseName;
088: }
089:
090: /**
091: * @return The ID of the user who scheduled the job.
092: */
093: public String getUserId() {
094: return this .userId;
095: }
096:
097: protected String getSourceURL() {
098: return this .sourceUrl;
099: }
100:
101: protected Map getParameters() {
102: return Collections.unmodifiableMap(this .parameters);
103: }
104:
105: /**
106: * @see org.apache.cocoon.components.cron.CronJob#execute(java.lang.String)
107: */
108: public void execute(String jobname) {
109: UsecaseInvoker invoker = null;
110: try {
111: setupOriginalRequest();
112: authorizeRequest();
113:
114: invoker = (UsecaseInvoker) this .manager
115: .lookup(UsecaseInvoker.ROLE);
116: invoker.invoke(getSourceURL(), getUsecaseName(),
117: getParameters());
118:
119: } catch (Exception e) {
120: throw new RuntimeException(e);
121: } finally {
122: if (invoker != null) {
123: this .manager.release(invoker);
124: }
125: }
126: }
127:
128: /**
129: * Creates a new request object based on the information from the original request which
130: * triggered the usecase.
131: */
132: protected void setupOriginalRequest() {
133: Environment env = CocoonComponentManager
134: .getCurrentEnvironment();
135:
136: Request request = ContextHelper.getRequest(this .context);
137: Map attributes = new HashMap();
138: for (Enumeration e = request.getAttributeNames(); e
139: .hasMoreElements();) {
140: String key = (String) e.nextElement();
141: attributes.put(key, request.getAttribute(key));
142: }
143:
144: Map requestParameters = new HashMap();
145: for (Enumeration e = request.getParameterNames(); e
146: .hasMoreElements();) {
147: String key = (String) e.nextElement();
148: requestParameters.put(key, request.getParameter(key));
149: }
150:
151: Map objectModel = ContextHelper.getObjectModel(this .context);
152: objectModel.put(ObjectModelHelper.REQUEST_OBJECT,
153: new CommandLineRequest(env, request.getContextPath(),
154: request.getServletPath(), getSourceURL(),
155: attributes, requestParameters));
156: }
157:
158: /**
159: * Initializes the session with the access control information.
160: * @throws AccessControlException if an error occurs.
161: * @throws ServiceException if the access controller resolver could not be created.
162: */
163: protected void authorizeRequest() throws AccessControlException,
164: ServiceException {
165:
166: ServiceSelector selector = null;
167: AccessControllerResolver acResolver = null;
168: AccessController controller = null;
169: try {
170: selector = (ServiceSelector) this .manager
171: .lookup(AccessControllerResolver.ROLE + "Selector");
172: acResolver = (AccessControllerResolver) selector
173: .select(AccessControllerResolver.DEFAULT_RESOLVER);
174: controller = acResolver
175: .resolveAccessController(getSourceURL());
176:
177: getLogger().debug("Add identity to session");
178: getLogger().debug("User ID: [" + this .userId + "]");
179: getLogger().debug("Machine: [" + this .machineIp + "]");
180:
181: Request request = ContextHelper.getRequest(this .context);
182: controller.setupIdentity(request);
183: Session session = request.getSession(false);
184: Identity identity = (Identity) session
185: .getAttribute(Identity.class.getName());
186: Identifiable[] identifiables = identity.getIdentifiables();
187: for (int i = 0; i < identifiables.length; i++) {
188: identity.removeIdentifiable(identifiables[i]);
189: }
190:
191: UserManager userManager = controller
192: .getAccreditableManager().getUserManager();
193: if (this .userId != null) {
194: User user = userManager.getUser(this .userId);
195:
196: if (user == null) {
197: throw new RuntimeException("User [" + this .userId
198: + "] does not exist!");
199: }
200:
201: identity.addIdentifiable(user);
202: }
203: if (this .machineIp != null) {
204: Machine machine = new Machine(this .machineIp);
205: identity.addIdentifiable(machine);
206: }
207:
208: controller.authorize(request);
209:
210: } finally {
211: if (selector != null) {
212: if (acResolver != null) {
213: if (controller != null) {
214: acResolver.release(controller);
215: }
216: selector.release(acResolver);
217: }
218: this .manager.release(selector);
219: }
220: }
221:
222: }
223:
224: /**
225: * @see org.apache.cocoon.components.cron.ConfigurableCronJob#setup(org.apache.avalon.framework.parameters.Parameters,
226: * java.util.Map)
227: */
228: public void setup(Parameters parameters, Map objects) {
229: this .parameters.putAll(Parameters.toProperties(parameters));
230: this .usecaseName = (String) objects.get(USECASE_NAME);
231: this .sourceUrl = (String) objects.get(SOURCE_URL);
232: this .userId = (String) objects.get(USER_ID);
233: this .machineIp = (String) objects.get(MACHINE_IP);
234: }
235:
236: private Context context;
237:
238: /**
239: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
240: */
241: public void contextualize(Context context) throws ContextException {
242: this.context = context;
243: }
244:
245: }
|