Differences between revisions 6 and 7
Revision 6 as of 2006-08-28 22:27:44
Size: 9603
Comment: Added Jar with Source
Revision 7 as of 2009-09-20 22:58:53
Size: 9611
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
I am a person of examples ([wiki:Self:BeanShellExamples more...]) and source-code (universal language), we have a few here: I am a person of examples ([[BeanShellExamples|more...]]) and source-code (universal language), we have a few here:
Line 59: Line 59:
See also: [http://james.apache.org/server/spoolmanager_configuration_2_3.html Mailet Configuration] See also: [[http://james.apache.org/server/spoolmanager_configuration_2_3.html|Mailet Configuration]]
Line 68: Line 68:
First, you need [http://www.beanshell.org/ BeanShell]!! [http://www.beanshell.org/bsh-2.0b4.jar bsh-2.0b4.jar] (or newest)

There is the Bundle, Source code of Mailets & Examples, [attachment:JamesBeanShell-src.jar JamesBeanShell-src.jar] MD5(d505af432990f7b2bfd3872bbe23cfe3)
First, you need [[http://www.beanshell.org/|BeanShell]]!! [[http://www.beanshell.org/bsh-2.0b4.jar|bsh-2.0b4.jar]] (or newest)

There is the Bundle, Source code of Mailets & Examples, [[attachment:JamesBeanShell-src.jar|JamesBeanShell-src.jar]] MD5(d505af432990f7b2bfd3872bbe23cfe3)
Line 147: Line 147:
See also: [http://james.apache.org/server/custom_mailet_2_3.html Custom Mailets] See also: [[http://james.apache.org/server/custom_mailet_2_3.html|Custom Mailets]]
Line 226: Line 226:
See also: [http://james.apache.org/server/custom_matcher_2_3.html Custom Matchers] See also: [[http://james.apache.org/server/custom_matcher_2_3.html|Custom Matchers]]
Line 247: Line 247:
If you don't want to compile, this is a compiled version of [attachment:BeanShell.jar BeanShell.jar] MD5(eebb68d340ec9c4622e5b1ca523f2a8d) If you don't want to compile, this is a compiled version of [[attachment:BeanShell.jar|BeanShell.jar]] MD5(eebb68d340ec9c4622e5b1ca523f2a8d)
Line 276: Line 276:
PD: My english is "peasant"... [http://translate.google.com/ GoogleTranslation!] PD: My english is "peasant"... [[http://translate.google.com/|GoogleTranslation!]]

BeanShell in Apache James for Scripting


Do you remember MailetVsProcmail or MailetIdeas? yes... Good! :-)

Let us imagine something like this, sendmail, perl,... all in java, james without need of recompile, restart, on-the-fly...

I am a person of examples (more...) and source-code (universal language), we have a few here:

BeanShell Mailet: /opt/james/bsh/mailets.bsh

   1 // This is a simple Scripted Mailet that Ghost mails
   2 setStrictJava(true);
   3 
   4 void ghostMail(Mail mail, Matcher matcher) {       // Ghost ShellMailet
   5   mail.setState(Mail.GHOST);
   6   return;
   7 }
   8 
   9 return this;

BeanShell Matcher: /opt/james/bsh/matchers.bsh

   1 // This is a simple Scripted Matcher that match all mails
   2 import java.util.Collection;
   3 
   4 setStrictJava(true);
   5 
   6 Collection matchAll(Mail mail, Matcher matcher) { // All ShellMatcher
   7   return mail.getRecipients();
   8 }
   9 
  10 return this;

Mailet/Matcher Configuration: SAR-INF/config.xml

<!-- BeanShell Mailet Example -->
<mailet match="All" class="BeanShell">
  <script>/opt/james/bsh/mailets.bsh</script>
  <method>ghostMail</method>
</mailet>

<!-- BeanShell Matcher Example -->
<mailet match="BeanShell=/opt/james/bsh/matchers.bsh,matchAll" class="SetMimeHeader">
  <name>X-BeanShell</name>
  <value>true</value>
</mailet>

See also: Mailet Configuration

And this is only the begin... the rest.... your imagination!! :-)

Do you want??? yes?... continue reading... :-)


HOW-TO :: Enable BeanShell Scripting in Apache James

First, you need BeanShell!! bsh-2.0b4.jar (or newest)

There is the Bundle, Source code of Mailets & Examples, JamesBeanShell-src.jar MD5(d505af432990f7b2bfd3872bbe23cfe3)

The BeanShell Mailet: src/bsh/james/mailet/BeanShell.java

   1 /*
   2  BeanShell Mailet for James
   3  Author: Guillermo Grandes
   4 
   5  The ASF licenses this file to you under the Apache License v2.0
   6  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
   7 
   8  http://www.apache.org/licenses/LICENSE-2.0
   9  */
  10 package bsh.james.mailet;
  11 
  12 import java.io.File;
  13 import java.io.FileNotFoundException;
  14 import java.io.IOException;
  15 
  16 import javax.mail.Message;
  17 import javax.mail.MessagingException;
  18 import org.apache.mailet.GenericMailet;
  19 import org.apache.mailet.Mail;
  20 import org.apache.mailet.MailetConfig;
  21 
  22 import bsh.Interpreter;
  23 import bsh.NameSpace;
  24 import bsh.EvalError;
  25 
  26 public class BeanShell extends GenericMailet {
  27   private String script = "";         // Script Filename
  28   private String method = "";         // Script Method name
  29   private long lastModified = 0;      // Script Las Modification Time
  30   private Interpreter bsInt = null;   // BeanShell Interpreter
  31   private NameSpace bsNs = null;      // BeanShell NameSpace
  32   private File scriptFile = null;     // Script File Object
  33 
  34   public void init(MailetConfig config) throws MessagingException {
  35     super.init(config);
  36     script = getInitParameter("script");
  37     method = getInitParameter("method");
  38     log("mailet script=" + script + " method=" + method);
  39     scriptFile = new File(script);
  40     lastModified = 0;
  41   }
  42 
  43   public void service(Mail mail) throws MessagingException {
  44     try {
  45       reloadScript();                               // Check for reloading
  46       //
  47       bsNs.invokeMethod(method, new java.lang.Object[] { mail, this }, bsInt);
  48     } catch (Throwable e) {
  49       String err = (new StringBuffer(160).append("Error to process mail (")
  50         .append(e.getClass().getName()).append(")[").append(script)
  51         .append("] :").append(e.getMessage())).toString();
  52       log(err, e);
  53       throw new MessagingException(err);
  54     }
  55   }
  56   protected void reloadScript() throws FileNotFoundException, IOException, EvalError {
  57     if (scriptFile.lastModified() <= lastModified) return;
  58     log("Reloading Script File: " + script);
  59     if (bsNs != null) bsNs.clear();                 // Clean
  60     bsInt = new Interpreter();                      // Construct an interpreter
  61     bsNs = bsInt.getNameSpace();                    // Get NameSpace
  62     bsNs.importClass("org.apache.mailet.Mail");     // Import basic Packages
  63     bsNs.importClass("org.apache.mailet.Mailet");
  64     bsNs.importClass("org.apache.mailet.Matcher");
  65     bsInt.source(script);                           // Source an external script file
  66     lastModified = scriptFile.lastModified();
  67   }
  68 
  69 }

See also: Custom Mailets

The BeanShell Matcher: src/bsh/james/matcher/BeanShell.java

   1 /*
   2  BeanShell Matcher for James
   3  Author: Guillermo Grandes
   4 
   5  The ASF licenses this file to you under the Apache License v2.0
   6  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
   7 
   8  http://www.apache.org/licenses/LICENSE-2.0
   9  */
  10 package bsh.james.matcher;
  11 
  12 import java.io.File;
  13 import java.io.FileNotFoundException;
  14 import java.io.IOException;
  15 import java.util.Collection;
  16 
  17 import javax.mail.Message;
  18 import javax.mail.MessagingException;
  19 import org.apache.mailet.GenericMatcher;
  20 import org.apache.mailet.Mail;
  21 import org.apache.mailet.MatcherConfig;
  22 
  23 import bsh.Interpreter;
  24 import bsh.NameSpace;
  25 import bsh.EvalError;
  26 
  27 public class BeanShell extends GenericMatcher {
  28   private String condition = "";
  29   private String script = "";         // Script Filename
  30   private String method = "";         // Script Method name
  31   private long lastModified = 0;      // Script Las Modification Time
  32   private Interpreter bsInt = null;   // BeanShell Interpreter
  33   private NameSpace bsNs = null;      // BeanShell NameSpace
  34   private File scriptFile = null;     // Script File Object
  35 
  36   public void init(MatcherConfig config) throws MessagingException {
  37     super.init(config);
  38     String[] cond = getCondition().split(",");
  39     script = cond[0];
  40     method = cond[1];
  41     log("matcher condition="+ condition + " script=" + script + " method=" + method);
  42     scriptFile = new File(script);
  43     lastModified = 0;
  44   }
  45 
  46   public Collection match(Mail mail) throws MessagingException {
  47     try {
  48       reloadScript();                               // Check for reloading
  49       //
  50       return (Collection)bsNs.invokeMethod(method, new java.lang.Object[] { mail, this }, bsInt);
  51     } catch (Throwable e) {
  52       String err = (new StringBuffer(160).append("Error to process mail (")
  53         .append(e.getClass().getName()).append(")[").append(script)
  54         .append("] :").append(e.getMessage())).toString();
  55       log(err, e);
  56       throw new MessagingException(err);
  57     }
  58   }
  59   protected void reloadScript() throws FileNotFoundException, IOException, EvalError {
  60     if (scriptFile.lastModified() <= lastModified) return;
  61     log("Reloading Script File: " + script);
  62     if (bsNs != null) bsNs.clear();                 // Clean
  63     bsInt = new Interpreter();                      // Construct an interpreter
  64     bsNs = bsInt.getNameSpace();                    // Get NameSpace
  65     bsNs.importClass("org.apache.mailet.Mail");     // Import basic Packages
  66     bsNs.importClass("org.apache.mailet.Mailet");
  67     bsNs.importClass("org.apache.mailet.Matcher");
  68     bsInt.source(script);                           // Source an external script file
  69     lastModified = scriptFile.lastModified();
  70   }
  71 }

See also: Custom Matchers

How to compile:

# Setup the CP variable with:
# bsh-2.0b4.jar (download)
# mailet-2.3.jar mailet-api-2.3.jar mail-1.4.0.jar james-2.3.0rc1.jar (inside of james.sar)
# example:
CP=".:lib/bsh-2.0b4.jar:lib/mailet-2.3.jar:lib/mailet-api-2.3.jar"
CP="$CP:lib/mail-1.4.0.jar:lib/james-2.3.0rc1.jar"
# Compile
mkdir -pm755 build/
javac -source 1.4 -target 1.4 -d build/ -cp $CP src/bsh/james/mailet/BeanShell.java
javac -source 1.4 -target 1.4 -d build/ -cp $CP src/bsh/james/matcher/BeanShell.java
# Create the Jar
jar cvf BeanShell.jar -C build/ ./

Remember, your need the jar-libs inside of jamer.sar for compile ;-) If you don't want to compile, this is a compiled version of BeanShell.jar MD5(eebb68d340ec9c4622e5b1ca523f2a8d)

Add the Jars to James Instalation

  • You have 2 options:
    1. copy BeanShell.jar & bsh-X.XX.jar to james/lib/ (global phoenix)

    2. add the files inside jamer.sar!SAR-INF/lib/ (local)

Add packages to James: SAR-INF/config.xml

   <!-- Set the Java packages from which to load mailets and matchers -->
   <mailetpackages>
      <mailetpackage>org.apache.james.transport.mailets</mailetpackage>
      <mailetpackage>org.apache.james.transport.mailets.smime</mailetpackage>
      <mailetpackage>bsh.james.mailet</mailetpackage>
   </mailetpackages>
   <matcherpackages>
      <matcherpackage>org.apache.james.transport.matchers</matcherpackage>
      <matcherpackage>org.apache.james.transport.matchers.smime</matcherpackage>
      <matcherpackage>bsh.james.matcher</matcherpackage>
   </matcherpackages>

I will forget something? ummmm, already we will see... :-)

If you need: guillermo.grandes AT gmail DOT com (Preferred Spanish language!).

PD: My english is "peasant"... GoogleTranslation!

BeanShell (last edited 2009-09-20 22:58:53 by localhost)