src.openwfe.org.decision.impl.FileDecisionTableFactory Maven / Gradle / Ivy
/*
* Copyright (c) 2006, 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: AbstractService.java 2587 2006-05-09 09:15:59Z jmettraux $
*/
//
// FileDecisionTableFactory.java
//
// [email protected]
//
// generated with
// jtmpl 1.1.01 2004/05/19 ([email protected])
//
package openwfe.org.decision.impl;
import openwfe.org.Utils;
import openwfe.org.MapUtils;
import openwfe.org.decision.DecisionTable;
import openwfe.org.decision.DecisionException;
import openwfe.org.decision.DecisionTableFactory;
/**
* Turning CSV files into decision tables.
*
* CVS Info :
*
$Author$
*
$Id$
*
* @author [email protected]
*/
public abstract class FileDecisionTableFactory
implements DecisionTableFactory
{
private final static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(FileDecisionTableFactory.class.getName());
//
// CONSTANTS & co
/**
*
* <param>
* <param-name>table:pharma-one</param-name>
* <param-value>etc/engine/decisions/pharma1.csv</param-value>
* </param>
* <param>
* <param-name>table:pharma-two</param-name>
* <param-value>http://intranet/decisions/pharma2.csv</param-value>
* </param>
*
*
* The prefix "table:" indicates that the rest of the param name is
* a table name and that the table value is a the path or the URL to
* the decision table (saved as a CSV file (comma separated value)).
* If the 'tableUrlPrefix' is set, none of the tables indicated with
* this 'table:' prefix will be taken into account.
*/
public final static String TABLE_PREFIX
= "table:";
/**
* By default this parameter is set to 'true'. When set to 'false',
* the decision factory will never check for fresher versions
* of the tables.
*/
public final static String P_REFRESH_TABLES
= "refreshTables";
/**
* When this parameter is used, tables prefixed with 'table:' are not
* taken into account; this parameter allows to specify the beginning of
* a URL, something like "http://our.intranet.com/dtables/", a table
* request for 'our-product-matching-table' will look for
* "http://our.intranet.com/dtables/our-product-matching-table.csv".
* There is of course no default.
*/
public final static String P_TABLE_URL_PREFIX
= "tableUrlPrefix";
//
// FIELDS
private java.util.Map tableUrls = null;
private java.util.Map tables = null;
private boolean refresh = true;
private String tableUrlPrefix = null;
//
// CONSTRUCTORS
/**
* Given a set of parameters, readies the factory for a later call
* to buildTables().
*/
public void init (final java.util.Map parameters)
throws DecisionException
{
this.tableUrls = new java.util.HashMap();
this.tables = new java.util.HashMap();
//
// should refresh ?
this.refresh = MapUtils
.getAsBoolean(parameters, P_REFRESH_TABLES, true);
log.info("init() refresh ? "+this.refresh);
//
// use a table directory ?
this.tableUrlPrefix = MapUtils
.getAsString(parameters, P_TABLE_URL_PREFIX);
if (this.tableUrlPrefix != null)
{
if (log.isDebugEnabled())
{
log.debug
("init() tableUrlPrefix was >"+this.tableUrlPrefix+"<");
}
this.tableUrlPrefix = Utils.expandUrl(this.tableUrlPrefix);
if (this.tableUrlPrefix.startsWith("file:"))
{
if ( ! this.tableUrlPrefix.endsWith(java.io.File.separator))
this.tableUrlPrefix += java.io.File.separator;
}
else if ( ! this.tableUrlPrefix.endsWith("/"))
{
this.tableUrlPrefix += "/";
}
log.info ("init() using tableUrlPrefix >"+this.tableUrlPrefix+"<");
return;
}
//
// enumerate tables
final java.util.Iterator it = parameters.entrySet().iterator();
while (it.hasNext())
{
final java.util.Map.Entry e = (java.util.Map.Entry)it.next();
final String pName = (String)e.getKey();
final String pValue = (String)e.getValue();
if ( ! pName.startsWith(TABLE_PREFIX)) continue;
final String tableName = pName.substring(TABLE_PREFIX.length());
final String tableUrl = Utils.expandUrl(pValue);
this.tableUrls.put(tableName, tableUrl);
log.info("init() table '"+tableName+"' -> '"+tableUrl+"'");
}
}
//
// METHODS from DecisionTableFactory
/**
* Returns a table with a certain name.
* Returns null if there are no known table with that name.
*/
public synchronized DecisionTable getTable (final String tableName)
throws DecisionException
{
final String tableUrl = this.getTableUrl(tableName);
if (tableUrl == null) return null;
TableEntry entry = (TableEntry)this.tables.get(tableName);
if (entry != null && ( ! this.refresh))
//
// no need to refresh
//
return entry.table;
if (entry != null)
//
// should we refresh ?
{
final long lastModified = Utils.getLastModified(tableUrl);
if (lastModified <= entry.lastModified)
//
// no need to refresh
//
return entry.table;
}
//
// [re]load table
entry = load(tableName, tableUrl);
this.tables.put(tableName, entry);
log.info("getTable() '"+tableName+"' [re]loaded from "+tableUrl);
return entry.table;
}
//
// METHODS
protected boolean isAutoRefresh ()
{
return this.refresh;
}
protected java.util.Map getTableUrls ()
{
return this.tableUrls;
}
protected java.util.Map getTables ()
{
return this.tables;
}
protected String getTableUrlPrefix ()
{
return this.tableUrlPrefix;
}
protected TableEntry load
(final String tableName, final String tableUrl)
throws
DecisionException
{
final DecisionTable table = loadTable(tableName);
return new TableEntry(Utils.getLastModified(tableUrl), table);
}
protected String getTableUrl (String tableName)
{
if (this.tableUrlPrefix != null)
{
if ( ! tableName.endsWith(".csv"))
tableName = tableName + ".csv";
return this.tableUrlPrefix + tableName;
}
return (String)this.tableUrls.get(tableName);
}
//
// STATIC METHODS
//
// INNER CLASSES
protected static class TableEntry
{
public long lastModified = -1;
public DecisionTable table = null;
public TableEntry (final long lastModified, final DecisionTable table)
{
this.lastModified = lastModified;
this.table = table;
}
}
}