001: /*
002: * Copyright 2007 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: package org.pentaho.core.subscribe;
014:
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import java.text.DateFormat;
018: import java.util.Date;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.Map;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.pentaho.core.repository.IContentItem;
027: import org.pentaho.core.repository.content.ContentRepositoryOutputHandler;
028: import org.pentaho.core.runtime.IRuntimeContext;
029: import org.pentaho.core.services.BaseRequestHandler;
030: import org.pentaho.core.session.IPentahoSession;
031: import org.pentaho.core.session.StandaloneSession;
032: import org.pentaho.core.session.UserSession;
033: import org.pentaho.core.solution.HttpSessionParameterProvider;
034: import org.pentaho.core.solution.IOutputHandler;
035: import org.pentaho.core.solution.SimpleOutputHandler;
036: import org.pentaho.core.solution.SimpleParameterProvider;
037: import org.pentaho.core.system.PentahoBase;
038: import org.pentaho.core.system.PentahoSystem;
039: import org.pentaho.core.util.UIUtil;
040: import org.pentaho.messages.Messages;
041: import org.pentaho.messages.util.LocaleHelper;
042: import org.pentaho.util.UUIDUtil;
043:
044: import com.pentaho.repository.subscribe.ISubscriptionRepository;
045: import com.pentaho.repository.subscribe.Schedule;
046: import com.pentaho.repository.subscribe.SubscribeContent;
047: import com.pentaho.repository.subscribe.Subscription;
048: import com.pentaho.security.UserDetailsRoleListService;
049:
050: public class SubscriptionExecute extends PentahoBase {
051:
052: private static final boolean debug = PentahoSystem.debug;
053:
054: private static final long serialVersionUID = -6053183867424885168L;
055:
056: private static final Log logger = LogFactory
057: .getLog(SubscriptionExecute.class);
058:
059: private String logId;
060:
061: public Log getLogger() {
062: return logger;
063: }
064:
065: public String getLogId() {
066: return logId;
067: }
068:
069: public IPentahoSession getEffectiveUserSession(String userName) {
070: UserDetailsRoleListService userDetailsRoleListService = PentahoSystem
071: .getUserDetailsRoleListService();
072: if (userDetailsRoleListService != null) {
073: return userDetailsRoleListService
074: .getEffectiveUserSession(userName);
075: } else {
076: UserSession us = new UserSession(userName, null);
077: us.setAuthenticated(userName);
078: return us;
079: }
080: }
081:
082: public void execute(String scheduleReference) {
083:
084: PentahoSystem.systemEntryPoint();
085: try {
086: String sessionId = "scheduler-" + UUIDUtil.getUUIDAsString(); //$NON-NLS-1$
087: StandaloneSession scheduleSession = new StandaloneSession(
088: scheduleReference, sessionId);
089:
090: ISubscriptionRepository subscriptionRepository = PentahoSystem
091: .getSubscriptionRepository(scheduleSession);
092: Schedule sched = subscriptionRepository
093: .getScheduleByScheduleReference(scheduleReference);
094: if (sched == null) {
095: error(Messages
096: .getErrorString(
097: "SubscriptionExecute.ERROR_0001_UNABLE_TO_GET_SCHEDULE", scheduleReference)); //$NON-NLS-1$
098: return;
099: }
100:
101: Date lastExeTm = sched.getLastTrigger();
102: sched.setLastTrigger(new Date());
103:
104: List subscriptionList = subscriptionRepository
105: .getSubscriptionsForSchedule(scheduleReference);
106:
107: info("FIRE: " + scheduleReference); //$NON-NLS-1$
108: for (int i = 0; i < subscriptionList.size(); ++i) {
109: Subscription sub = (Subscription) subscriptionList
110: .get(i);
111: System.out.println(sub);
112: Map paramMap = new HashMap();
113: paramMap.putAll(sub.getParameters());
114:
115: SubscribeContent subContent = sub.getContent();
116: PentahoSystem.ActionInfo contentInfo = PentahoSystem
117: .parseActionString(subContent
118: .getActionReference());
119:
120: String jobName = sub.getUser() + " : " + sub.getTitle(); //$NON-NLS-1$
121: IPentahoSession userSession = getEffectiveUserSession(sub
122: .getUser());
123:
124: paramMap.put("solution", contentInfo.getSolutionName()); //$NON-NLS-1$
125: paramMap.put("path", contentInfo.getPath()); //$NON-NLS-1$
126: paramMap.put("action", contentInfo.getActionName()); //$NON-NLS-1$
127:
128: paramMap.put("SUB_SCHEDULED_EXECUTE", "true"); //$NON-NLS-1$ //$NON-NLS-2$
129: paramMap
130: .put("SUB_EXECUTE_TIME", sched.getLastTrigger()); //$NON-NLS-1$
131: paramMap.put("SUB_PREV_EXECUTE_TIME", lastExeTm); //$NON-NLS-1$
132: paramMap.put("SUB_SCHEDULE", sched.getCronString()); //$NON-NLS-1$
133: paramMap.put("SUB_SCHEDULE_NAME", sched.getTitle()); //$NON-NLS-1$
134: paramMap
135: .put(
136: "SUB_SCHEDULE_REF", sched.getScheduleReference()); //$NON-NLS-1$
137: paramMap.put(
138: "SUB_SCHEDULE_DESC", sched.getDescription()); //$NON-NLS-1$
139: paramMap.put("SUB_NAME", sub.getTitle()); //$NON-NLS-1$
140: paramMap.put("SUB_ID", sub.getId()); //$NON-NLS-1$
141:
142: execute(jobName, paramMap, userSession);
143: }
144: } finally {
145: PentahoSystem.systemExitPoint();
146:
147: }
148:
149: }
150:
151: public void execute(String jobName, Map parametersMap,
152: IPentahoSession userSession) {
153: try {
154: LocaleHelper.setLocale(Locale.getDefault());
155: logId = "Pro Subscription:" + jobName; //$NON-NLS-1$
156:
157: Date now = new Date();
158: logger.info(Messages.getString(
159: "SubscriptionExecute.INFO_TRIGGER_TIME", jobName, //$NON-NLS-1$
160: DateFormat.getDateInstance().format(now),
161: DateFormat.getTimeInstance().format(now)));
162:
163: String solutionName = (String) parametersMap
164: .get("solution"); //$NON-NLS-1$
165: String actionPath = (String) parametersMap.get("path"); //$NON-NLS-1$
166: String actionName = (String) parametersMap.get("action"); //$NON-NLS-1$
167: String instanceId = null;
168: String processId = this .getClass().getName();
169:
170: if (solutionName == null) {
171: error(Messages
172: .getErrorString("SubscriptionExecute.ERROR_0001_SOLUTION_NAME_MISSING")); //$NON-NLS-1$
173: return;
174: }
175: if (actionPath == null) {
176: error(Messages
177: .getErrorString("SubscriptionExecute.ERROR_0002_ACTION_PATH_MISSING")); //$NON-NLS-1$
178: return;
179: }
180: if (actionName == null) {
181: error(Messages
182: .getErrorString("SubscriptionExecute.ERROR_0003_ACTION_NAME_MISSING")); //$NON-NLS-1$
183: return;
184: }
185: if (debug) {
186: if (debug)
187: debug(Messages
188: .getString(
189: "SubscriptionExecute.DEBUG_EXECUTION_INFO", solutionName + "/" + actionPath + "/" + actionName)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
190: }
191:
192: boolean ignoreSubscriptionOutput = "true".equalsIgnoreCase((String) parametersMap.get("SUB_IGNORE_OUTPUT")); //$NON-NLS-1$ //$NON-NLS-2$
193:
194: String subscriptionId = (String) parametersMap
195: .get("SUB_ID"); //$NON-NLS-1$
196: String subscriptionName = (String) parametersMap
197: .get("SUB_NAME"); //$NON-NLS-1$
198: IOutputHandler outputHandler = null;
199: if (ignoreSubscriptionOutput) {
200: outputHandler = new SimpleOutputHandler(
201: (OutputStream) null, false);
202: } else {
203: String contentPath = SubscriptionHelper
204: .getSubscriptionOutputLocation(solutionName,
205: actionPath, actionName);
206: outputHandler = new ContentRepositoryOutputHandler(
207: contentPath, subscriptionId, solutionName,
208: userSession);
209: ((ContentRepositoryOutputHandler) outputHandler)
210: .setWriteMode(IContentItem.WRITEMODE_KEEPVERSIONS);
211: }
212: SimpleParameterProvider parameterProvider = new SimpleParameterProvider(
213: parametersMap);
214:
215: BaseRequestHandler requestHandler = new BaseRequestHandler(
216: userSession, null, outputHandler,
217: parameterProvider, null);
218: requestHandler.setParameterProvider(
219: HttpSessionParameterProvider.SCOPE_SESSION,
220: new HttpSessionParameterProvider(userSession));
221:
222: requestHandler.setInstanceId(instanceId);
223: requestHandler.setProcessId(processId);
224: requestHandler.setAction(actionPath, actionName);
225: requestHandler.setSolutionName(solutionName);
226: IRuntimeContext rt = null;
227: try {
228: rt = requestHandler.handleActionRequest(0, 0);
229: if (!ignoreSubscriptionOutput
230: && !outputHandler.contentDone()) {
231: IContentItem outputContentItem = outputHandler
232: .getOutputContentItem(
233: IOutputHandler.RESPONSE,
234: IOutputHandler.CONTENT,
235: subscriptionName, null, rt
236: .getSolutionName(), rt
237: .getInstanceId(),
238: "text/html"); //$NON-NLS-1$
239: outputContentItem.setMimeType("text/html"); //$NON-NLS-1$
240: try {
241: if ((rt != null)
242: && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
243: StringBuffer buffer = new StringBuffer();
244: UIUtil.formatSuccessMessage(
245: "text/html", rt, buffer, false); //$NON-NLS-1$
246: OutputStream os = outputContentItem
247: .getOutputStream(actionName);
248: os.write(buffer.toString().getBytes(
249: LocaleHelper.getSystemEncoding()));
250: outputContentItem.closeOutputStream();
251: } else {
252: // we need an error message...
253: StringBuffer buffer = new StringBuffer();
254: UIUtil
255: .formatFailureMessage(
256: "text/html", rt, buffer, requestHandler.getMessages()); //$NON-NLS-1$
257: OutputStream os = outputContentItem
258: .getOutputStream(actionName);
259: os.write(buffer.toString().getBytes(
260: LocaleHelper.getSystemEncoding()));
261: os.close();
262: }
263: } catch (IOException ex) {
264: error(ex.getLocalizedMessage());
265: }
266: }
267: } finally {
268: if (rt != null) {
269: rt.dispose();
270: }
271: }
272: if (debug)
273: logger
274: .debug(Messages
275: .getString(
276: "SubscriptionExecute.DEBUG_FINISHED_EXECUTION", jobName)); //$NON-NLS-1$
277: } catch (Throwable t) {
278: logger.error("Error Executing Job", t); //$NON-NLS-1$
279: }
280: }
281:
282: }
|