📄 fileexpressionstore.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: FileExpressionStore.java,v 1.25 2005/07/17 16:31:49 jmettraux Exp $ *///// FileExpressionStore.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.0.04 20.11.2001 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.engine.impl.expool;import openwfe.org.Utils;import openwfe.org.ApplicationContext;import openwfe.org.ServiceException;import openwfe.org.xml.XmlUtils;import openwfe.org.time.Time;import openwfe.org.misc.DirectoryFilter;import openwfe.org.engine.expool.PoolException;import openwfe.org.engine.expressions.UrExpressionId;import openwfe.org.engine.expressions.ReplyException;import openwfe.org.engine.expressions.FlowExpression;import openwfe.org.engine.expressions.FlowExpressionId;import openwfe.org.engine.expressions.ExpressionWithTimeOut;/** * Half an implementation of an expression store * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/07/17 16:31:49 $ * <br>$Id: FileExpressionStore.java,v 1.25 2005/07/17 16:31:49 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public abstract class FileExpressionStore extends AbstractExpressionStore{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(FileExpressionStore.class.getName()); // // CONSTANTS (definitions) /** * Use this parameter to indicate to a FileExpressionStore in which * directory it should save its run data. */ public static String P_WORK_DIRECTORY = "workDirectory"; // // FIELDS private String workDirectory = null; private String urExpressionPath = null; // // CONSTRUCTORS public void init (final String serviceName, final ApplicationContext context, final java.util.Map serviceParams) throws ServiceException { super.init(serviceName, context, serviceParams); // // determine workDirectory this.workDirectory = (String)serviceParams.get(P_WORK_DIRECTORY); this.workDirectory = Utils.getCanonicalPath (getContext().getApplicationDirectory(), this.workDirectory); this.workDirectory += java.io.File.separator; this.urExpressionPath = this.workDirectory + "ur-expression.xml"; log.info("init() workDirectory set to "+this.workDirectory); } // // ABSTRACT METHODS protected abstract void saveExpression (String fileName, FlowExpression fe) throws Exception; protected abstract FlowExpression loadExpression (String fileName) throws Exception; // // METHODS /** * Stores an expression to a file in the pool directory. */ public void storeExpression (final FlowExpression fe) throws PoolException { log.debug("storeExpression() for "+fe.getId()); final long start = System.currentTimeMillis(); final Object monitor = getMonitorMap().getMonitor(fe.getId()); synchronized (monitor) { //log.debug // ("storeExpression() entering >>> "+start+ // " h"+fe.getId().hashCode()); try { // // does WFD directory exist ? final java.io.File dir = new java.io.File(determineDirName(fe.getId())); if ( ! dir.exists()) dir.mkdirs(); // // store expression final String fileName = determineFileName(fe.getId()); // some debug output (remove it when delete() problem is solved) // //if ((new java.io.File(fileName)).exists()) //{ // log.debug // ("storeExpression() "+fileName+" already exists."); //} saveExpression(fileName, fe); } catch (final Exception e) { throw new PoolException ("Failed to store expression", e); } //log.debug // ("storeExpression() leaving <<< "+start+ // " h"+fe.getId().hashCode()); } final long duration = System.currentTimeMillis() - start; log.debug("storeExpression() took "+duration+" ms"); } /** * Removes an expression (deletes its file) from the pool directory. */ public void unstoreExpression (final FlowExpression fe) throws PoolException { String fileName = determineFileName(fe.getId()); log.debug("unstoreExpression() deleting file "+fileName); java.io.File f = new java.io.File(fileName); boolean result = f.delete(); if (result == false) log.debug("unstoreExpression() delete failed"); log.debug("unstoreExpression() unstored "+fe.getId()); if (fe.getParent() == null) { final String dirName = determineDirName(fe.getId()); final java.io.File dir = new java.io.File(dirName); dir.delete(); log.debug("unstoreExpression() removed dir "+dir.getPath()); } } /** * Loads (deserializes) an expression from the pool directory. */ public FlowExpression loadExpression (final FlowExpressionId fei) throws PoolException { log.debug("loadExpression() requested for "+fei); long start = System.currentTimeMillis(); String fileName = determineFileName(fei); log.debug("loadExpression() fileName is "+fileName); FlowExpression result = null; try { result = loadExpression(fileName); //result.setApplicationContext(this.applicationContext); } catch (final Exception e) { //log.debug // ("Failed to load expression", e); throw new PoolException ("Failed to load expression", e); } long duration = System.currentTimeMillis() - start; log.debug("loadExpression() took "+duration+" ms"); return result; } // // file name determination private String getLastDigit (final FlowExpressionId fei) { final String wfid = fei.getParentWorkflowInstanceId(); return wfid.substring(wfid.length()-1); } /** * Determines in which dir an expression should be stored. */ protected String determineDirName (final FlowExpressionId fei) { //log.debug("determineDirName() fei is "+fei); if (UrExpressionId.UR_EXPRESSION_ID.equals(fei)) return this.workDirectory; StringBuffer sb = new StringBuffer(); sb.append(this.workDirectory); sb.append(Utils.ensureForFileName(fei.getWorkflowDefinitionName())); sb.append("--"); sb.append(Utils.ensureForFileName(fei.getWorkflowDefinitionRevision())); sb.append(java.io.File.separator); sb.append(getLastDigit(fei)); sb.append(java.io.File.separator); sb.append(fei.getParentWorkflowInstanceId()); sb.append(java.io.File.separator); return sb.toString(); } /** * Determines under which filename an expression should be stored. * It returns a full (absolute) path. */ protected String determineFileName (final FlowExpressionId fei) { if (UrExpressionId.UR_EXPRESSION_ID.equals(fei)) return this.urExpressionPath; StringBuffer sb = new StringBuffer(); sb.append(determineDirName(fei)); sb.append(Utils.ensureForFileName(fei.getInitialEngineId())); sb.append("--"); sb.append(fei.getWorkflowInstanceId()); sb.append("--"); sb.append(fei.getExpressionName()); sb.append("--"); sb.append(fei.getExpressionId()); sb.append(".xml"); return sb.toString(); } /** * This method returns an iterator on all the expression stored here * whose class is assignable from the given 'assignClass'. * This method is used by daemons checking the content of the store for * purge and timeout purposes. */ public java.util.Iterator contentIterator (final Class assignClass) { final long startTime = System.currentTimeMillis(); final String[] shortNames = determineExpressionShortNames(assignClass); final java.util.List result = new java.util.ArrayList(); // // roam through every directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -