/*
 * Copyright (c) 2000 jPOS.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:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *    "This product includes software developed by the jPOS project 
 *    (http://www.jpos.org/)". Alternately, this acknowledgment may 
 *    appear in the software itself, if and wherever such third-party 
 *    acknowledgments normally appear.
 *
 * 4. The names "jPOS" and "jPOS.org" must not be used to endorse 
 *    or promote products derived from this software without prior 
 *    written permission. For written permission, please contact 
 *    license@jpos.org.
 *
 * 5. Products derived from this software may not be called "jPOS",
 *    nor may "jPOS" appear in their name, without prior written
 *    permission of the jPOS project.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 JPOS PROJECT OR ITS 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the jPOS Project.  For more
 * information please see <http://www.jpos.org/>.
 */

package org.jpos.bsh;

import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.core.ReConfigurable;
import org.jpos.iso.ISOChannel;
import org.jpos.iso.ISORequestListener;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOSource;
import org.jpos.util.Log;
import bsh.Interpreter;

/**
 * BSHRequestListener - BeanShell based request listener
 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
 * @version $Revision$ $Date$
 */
public class BSHRequestListener extends Log 
    implements ISORequestListener, ReConfigurable 
{
    protected static final String MTI_MACRO = "$mti";
    Configuration cfg;
    public BSHRequestListener () {
        super();
    }
   /**
    * @param cfg
    * <ul>
    *  <li>source - BSH script(s) to run (can be more than one)
    * </ul>
    */
    public void setConfiguration (Configuration cfg) 
        throws ConfigurationException
    {
        this.cfg = cfg;
    }

    public boolean process (ISOSource source, ISOMsg m) {
        try{
            String mti = m.getMTI ();
            String[] bshSource = cfg.getAll ("source");
            for (int i=0; i<bshSource.length; i++) {
                try {
                    Interpreter bsh = new Interpreter ();
                    bsh.set ("source", source);
                    bsh.set ("message", m);
                    bsh.set ("log", this);
                    bsh.set ("cfg", cfg);
                    //replace $mti with the actual value in script file name
                    int idx = bshSource[i].indexOf(MTI_MACRO);
                    String script;
                    if(idx >= 0) 
                        script = bshSource[i].substring(0, idx) + mti + bshSource[i].substring(idx + MTI_MACRO.length());
                    else 
                        script = bshSource[i];
                    bsh.source (script);
                } catch (Exception e) {
                    warn (e);
                }
            }
        }catch (Exception e){
            warn(e);
        }
        return true;
    }
}


@@ -2390,16 +2390,67 @@
 s.bsh;%0A%0A
+import java.util.Arrays;%0Aimport java.util.HashSet;%0A
 import o
@@ -2473,24 +2473,24 @@
 figuration;%0A
-
 import org.j
@@ -3055,16 +3055,83 @@
 %22$mti%22;%0A
+    protected HashSet whitelist;%0A    protected String%5B%5D bshSource;%0A
     Conf
@@ -3234,24 +3234,94 @@
 %0A    * %3Cul%3E%0A
+    *  %3Cli%3Ewhitelist - supported message types (example: %221100,1220%22)%0A
     *  %3Cli%3Es
@@ -3509,24 +3509,184 @@
 .cfg = cfg;%0A
+        bshSource = cfg.getAll (%22source%22);%0A        String%5B%5D mti = cfg.get (%22whitelist%22, %22*%22).split(%22,%22);%0A        whitelist = new HashSet( Arrays.asList(mti) );%0A
     %7D%0A%0A    p
@@ -3805,52 +3805,104 @@
     
-String%5B%5D bshSource = cfg.getAll (%22source%22)
+if (!whitelist.contains(mti) && !whitelist.contains(%22*%22))%0A                mti = %22unsupported%22
 ;%0A
+%0A
     
@@ -4211,85 +4211,9 @@
 g);%0A
-                    //replace $mti with the actual value in script file name
 %0A
+
     
@@ -4306,16 +4306,17 @@
 script;%0A
+%0A
         
@@ -4329,16 +4329,17 @@
       if
+ 
 (idx %3E= 
@@ -4341,16 +4341,99 @@
 x %3E= 0) 
+%7B%0A                        // replace $mti with the actual value in script file name
 %0A       
@@ -4496,16 +4496,44 @@
  + mti +
+%0A                           
  bshSour
@@ -4598,14 +4598,17 @@
     
+ %7D
  else 
+%7B
 %0A   
@@ -4651,16 +4651,39 @@
 rce%5Bi%5D;%0A
+                    %7D%0A%0A
         
@@ -4847,16 +4847,16 @@
 ion e)%7B%0A
-
         
@@ -4864,24 +4864,50 @@
    warn(e);%0A
+            return false;%0A
         %7D%0A  

whitelist support, defaults to be backwards compatible