📄 droflosession.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: DrofloSession.java,v 1.64 2005/05/17 16:40:20 jmettraux Exp $ *///// DrofloSession.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.droflo;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpServletRequest;import openwfe.org.StackMap;import openwfe.org.ApplicationContext;import openwfe.org.engine.Definitions;import openwfe.org.engine.launch.Launcher;import openwfe.org.engine.workitem.HistoryItem;import openwfe.org.engine.workitem.InFlowWorkItem;import openwfe.org.engine.expressions.BuildException;import openwfe.org.engine.expressions.FlowExpression;import openwfe.org.engine.expressions.DefineExpression;import openwfe.org.engine.expressions.FlowExpressionId;import openwfe.org.engine.expressions.ProcessDefinition;import openwfe.org.engine.expressions.CompositeFlowExpression;import openwfe.org.engine.expressions.map.ExpressionMap;import openwfe.org.engine.expressions.sync.SyncExpression;/** * A kind of flow definition... * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: DrofloSession.java,v 1.64 2005/05/17 16:40:20 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class DrofloSession{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(DrofloSession.class.getName()); // // CONSTANTS & co public final static String DROFLO_SESSION = "drofloSession"; public final static String IN_EDIT_MODE = "droflo.inEditMode"; public final static String APPLICATION_CONFIGURATION = "applicationConfiguration"; private final static int UNDO_LEVELS = 10; // // FIELDS private ApplicationContext applicationContext = null; private ProcessDefinition flowDefinition = null; private String webappUrl = null; private Throwable exception = null; private boolean inEditMode = false; private boolean helpVisible = true; private FlowExpression expressionToEdit = null; private java.util.List undoStack = new java.util.ArrayList(UNDO_LEVELS); private InFlowWorkItem workitem = null; // // CONSTRUCTORS public DrofloSession (final String webappUrl, final ApplicationContext applicationContext, final boolean inEditMode) { this.webappUrl = webappUrl; this.applicationContext = applicationContext; this.inEditMode = inEditMode; log.debug("DrofloSession() this.inEditMode = "+this.inEditMode); } // // METHODS public String getWebappUrl () { return this.webappUrl; } public InFlowWorkItem getWorkItem () { return this.workitem; } public boolean isInEditMode () { return //this.inEditMode && (this.expressionToEdit == null); this.inEditMode; } public void setInEditMode (final boolean b) { this.inEditMode = b; } public boolean isHelpVisible () { return this.helpVisible; } public void toggleHelp () { this.helpVisible = !this.helpVisible; } public Launcher getLauncher () { return openwfe.org.engine.Definitions .getLauncher(getApplicationContext()); } /** * loads a worfklow in the session */ public void loadWorkflow (final String flowDefinitionUrl) throws BuildException { this.flowDefinition = getLauncher() .loadProcessDefinition(flowDefinitionUrl); } /** * removes an expression from the current flow definition */ public void removeExpression (final String expId) { final FlowExpressionId id = this.flowDefinition .getFlowExpressionId(expId); this.flowDefinition.removeChild(id); } private FlowExpressionId buildExpressionId (String expId, String expName) { if (expId == null || expName == null) return null; FlowExpressionId model = this.flowDefinition.getId(); FlowExpressionId result = (FlowExpressionId)model.clone(); result.setExpressionId(expId); result.setExpressionName(expName); return result; } public void addExpression (final String expressionName, final String expId) throws BuildException { log.debug("addExpression() expressionName is >"+expressionName+"<"); log.debug("addExpression() expId is >"+expId+"<"); final FlowExpression exp = this.flowDefinition.getExpression(expId); FlowExpressionId id = null; if (exp != null) id = exp.getId(); this.flowDefinition.add(expressionName, id); } public void moveExpression (final String action, final String expId) { log.debug ("moveExpression() action is >"+action+"< expId is >"+expId+"<"); final FlowExpression fe = this.flowDefinition.getExpression(expId); final CompositeFlowExpression cfe = (CompositeFlowExpression)this.flowDefinition.get(fe.getParent()); // should directly work as the 'moveX' button is only presented // in a sequence or something like this if (action.equals("moveup")) { cfe.moveUpChild(fe.getId()); } else { cfe.moveDownChild(fe.getId()); } } public void newFlowDefinition () { try { this.flowDefinition = new ProcessDefinition (getApplicationContext(), "http://localhost:7079/__new_flow__1.0.xml", "__new_flow", "1.0"); } catch (final BuildException be) { log.warn("Failed to build new flow definition !", be); } } public ProcessDefinition getFlowDefinition () { return this.flowDefinition; } public void setException (Throwable t) { this.exception = t; } public void clearException () { this.exception = null; } public ApplicationContext getApplicationContext () { return this.applicationContext; } /* * * RENDER FLOW * */ public void renderFlow (final PrintWriter out, final HttpServletRequest req) throws RenderingException { try { final long drofloLinkCounter = Long.parseLong(req.getParameter("dlc")); this.workitem = (InFlowWorkItem)ApplicationContext .lookupSharedContext().lookup("workitem"+drofloLinkCounter); } catch (NumberFormatException nfe) { this.workitem = null; } log.debug ("renderFlow() this.workitem == null : "+(this.workitem == null)); DrofloHelper.getInstance(req).renderFlow(this, out, req); } public void updateExpression (final HttpServletRequest req) throws RenderingException { DrofloHelper.getInstance(req).parseRequest(this, req); } public void loadExpressionToEdit (final String expId) { this.expressionToEdit = this.flowDefinition.getExpression(expId); } public void unloadExpressionToEdit () { this.expressionToEdit = null; } public FlowExpression getExpressionToEdit () { return this.expressionToEdit; } public String getExpressionToEditName () { return getExpressionName(this.expressionToEdit); } /* * * returns just the subProcess * / protected String getSubProcessDefinitionName (String fullPath) { String[] ss = fullPath.split("/"); if (ss.length <= 1) return fullPath; return ss[ss.length-1]; } */ public void outputDefinitionAsXml (final PrintWriter out) throws java.io.IOException { out.println(this.flowDefinition.outputAsXmlString()); } /** * Returns the expressionMap OpenWFE service that this session uses */ public ExpressionMap getExpressionMap () { return Definitions.getExpressionMap(this.applicationContext); } /** * Given a expression, returns the corresponding short name * (found in the XML process definition as a tag) */ public String getExpressionName (Object expression) { if (expression == null) return "--null-expression--"; return getExpressionMap().getName(expression.getClass()); } // // undo methods public boolean canUndo () { return this.undoStack.size() > 0; } public void stackForUndo () { if (this.flowDefinition == null) return; try { this.undoStack.add(0, this.flowDefinition.outputAsXmlString()); while (this.undoStack.size() > UNDO_LEVELS) this.undoStack.remove(UNDO_LEVELS); } catch (Exception e) { log.warn ("stackForUndo() stacking failed", e); } } public void popForUndo () { try { final String sFlowDefinition = (String)this.undoStack.get(0); /* this.flowDefinition = getFlowBuilder().parseWorkflow ((String)this.flowDefinition.getAttributes().get("name"), sFlowDefinition); */ this.flowDefinition = getLauncher() .loadProcessDefinitionFromString(sFlowDefinition); } catch (Exception e) { log.warn ("popForUndo() popping failed", e); } this.undoStack.remove(0); } // // STATIC METHODS public static DrofloSession getSession (final HttpServletRequest req) { HttpSession httpSession = req.getSession(true); return (DrofloSession)httpSession.getAttribute(DROFLO_SESSION); } public static boolean inEditMode (final HttpServletRequest req) { Object o = req.getAttribute(IN_EDIT_MODE); if (o == null || ! (o instanceof Boolean)) return false; return ((Boolean)o).booleanValue(); } private static String isolateUrl (final HttpServletRequest req) { final StringBuffer rawUrl = req.getRequestURL(); log.debug("isolateUrl() for >"+rawUrl+"<"); int i = rawUrl.indexOf("/servlet"); if (i < 0) i = rawUrl.lastIndexOf("/"); final String result = rawUrl.substring(0, i) + "/"; log.debug("isolateUrl() result is >"+result+"<"); return result; } public static DrofloSession getOrCreateSession (final HttpServletRequest req) { HttpSession httpSession = req.getSession(true); DrofloSession result = (DrofloSession)httpSession .getAttribute(req.getContextPath()+DrofloSession.DROFLO_SESSION); final String sEdit = req.getParameter("edit"); //log.debug("getOrCreateSession() sEdit is >"+sEdit+"<"); if (result != null) { //result.setInEditMode("true".equals(sEdit)); if ("true".equals(sEdit)) result.setInEditMode(true); return result; } // // build ApplicationContext ApplicationContext applicationContext = null; try { ServletContext servletContext = httpSession.getServletContext(); String configFileName = servletContext .getInitParameter(APPLICATION_CONFIGURATION); configFileName = servletContext.getRealPath(configFileName); log.debug("getOrCreateSession() configFileName : "+configFileName); openwfe.org.app.XmlApplicationContextBuilder builder = (openwfe.org.app.XmlApplicationContextBuilder) openwfe.org.app.XmlApplicationContextBuilder.class .newInstance(); applicationContext = builder.build(configFileName); } catch (final Throwable t) { log.warn ("Problem with parameter '"+APPLICATION_CONFIGURATION+"'", t); throw new IllegalArgumentException ("Parameter '"+APPLICATION_CONFIGURATION+ "' is missing in WEB-INF/web.xml "+ "or this configuration file is corrupt."); } // // build DrofloSession result = new DrofloSession (isolateUrl(req), applicationContext, "true".equalsIgnoreCase(sEdit)); httpSession.setAttribute (req.getContextPath()+DrofloSession.DROFLO_SESSION, result); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -