Dear Wiki user,

You have subscribed to a wiki page or wiki category on "James Wiki" for change 
notification.

The following page has been changed by GuillermoGrandes:
http://wiki.apache.org/james/BeanShell

The comment on the change is:
BeanShell in Apache James for Scripting

New page:
= 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 and source-code (universal language), we have a few 
here:

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

{{{
#!java
// This is a simple Scripted Mailet that Ghost mails
setStrictJava(true);

void ghostMail(Mail mail, Matcher matcher) {       // Ghost ShellMailet
  mail.setState(Mail.GHOST);
  return;
}

return this;
}}}

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

{{{
#!java
// This is a simple Scripted Matcher that match all mails
import java.util.Collection;

setStrictJava(true);

Collection matchAll(Mail mail, Matcher matcher) { // All ShellMatcher
  return mail.getRecipients();
}

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: [http://james.apache.org/server/spoolmanager_configuration_2_3.html 
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 [http://www.beanshell.org/ BeanShell]!! 
[http://www.beanshell.org/bsh-2.0b4.jar bsh-2.0b4.jar] (or newest)

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

{{{
#!java
package bsh.james.mailet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.mail.Message;
import javax.mail.MessagingException;
import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
import org.apache.mailet.MailetConfig;

import bsh.Interpreter;
import bsh.NameSpace;
import bsh.EvalError;

/**
 * James Mailet via BeanShell
 * @author Guillermo Grandes
 */
public class BeanShell extends GenericMailet {
  private String script = "";         // Script Filename
  private String method = "";         // Script Method name
  private long lastModified = 0;      // Script Las Modification Time
  private Interpreter bsInt = null;   // BeanShell Interpreter
  private NameSpace bsNs = null;      // BeanShell NameSpace
  private File scriptFile = null;     // Script File Object

  public void init(MailetConfig config) throws MessagingException {
    super.init(config);
    script = getInitParameter("script");
    method = getInitParameter("method");
    log("mailet script=" + script + " method=" + method);
    scriptFile = new File(script);
    lastModified = 0;
  }

  public void service(Mail mail) throws MessagingException {
    try {
      reloadScript();                               // Check for reloading
      //
      bsNs.invokeMethod(method, new java.lang.Object[] { mail, this }, bsInt);
    } catch (Throwable e) {
      String err = (new StringBuffer(160).append("Error to process mail (")
        .append(e.getClass().getName()).append(")[").append(script)
        .append("] :").append(e.getMessage())).toString();
      log(err, e);
      throw new MessagingException(err);
    }
  }
  protected void reloadScript() throws FileNotFoundException, IOException, 
EvalError {
    if (scriptFile.lastModified() <= lastModified) return;
    log("Reloading Script File: " + script);
    if (bsNs != null) bsNs.clear();                 // Clean
    bsInt = new Interpreter();                      // Construct an interpreter
    bsNs = bsInt.getNameSpace();                    // Get NameSpace
    bsNs.importClass("org.apache.mailet.Mail");     // Import basic Packages
    bsNs.importClass("org.apache.mailet.Mailet");
    bsNs.importClass("org.apache.mailet.Matcher");
    bsInt.source(script);                           // Source an external 
script file
    lastModified = scriptFile.lastModified();
  }

}
}}}

See also: [http://james.apache.org/server/custom_mailet_2_3.html Custom Mailets]

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

{{{
#!java
package bsh.james.matcher;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;

import javax.mail.Message;
import javax.mail.MessagingException;
import org.apache.mailet.GenericMatcher;
import org.apache.mailet.Mail;
import org.apache.mailet.MatcherConfig;

import bsh.Interpreter;
import bsh.NameSpace;
import bsh.EvalError;

/**
 * James Matcher via BeanShell
 * @author Guillermo Grandes
 */
public class BeanShell extends GenericMatcher {
  private String condition = "";
  private String script = "";         // Script Filename
  private String method = "";         // Script Method name
  private long lastModified = 0;      // Script Las Modification Time
  private Interpreter bsInt = null;   // BeanShell Interpreter
  private NameSpace bsNs = null;      // BeanShell NameSpace
  private File scriptFile = null;     // Script File Object

  public void init(MatcherConfig config) throws MessagingException {
    super.init(config);
    String[] cond = getCondition().split(",");
    script = cond[0];
    method = cond[1];
    log("matcher condition="+ condition + " script=" + script + " method=" + 
method);
    scriptFile = new File(script);
    lastModified = 0;
  }

  public Collection match(Mail mail) throws MessagingException {
    try {
      reloadScript();                               // Check for reloading
      //
      return (Collection)bsNs.invokeMethod(method, new java.lang.Object[] { 
mail, this }, bsInt);
    } catch (Throwable e) {
      String err = (new StringBuffer(160).append("Error to process mail (")
        .append(e.getClass().getName()).append(")[").append(script)
        .append("] :").append(e.getMessage())).toString();
      log(err, e);
      throw new MessagingException(err);
    }
  }
  protected void reloadScript() throws FileNotFoundException, IOException, 
EvalError {
    if (scriptFile.lastModified() <= lastModified) return;
    log("Reloading Script File: " + script);
    if (bsNs != null) bsNs.clear();                 // Clean
    bsInt = new Interpreter();                      // Construct an interpreter
    bsNs = bsInt.getNameSpace();                    // Get NameSpace
    bsNs.importClass("org.apache.mailet.Mail");     // Import basic Packages
    bsNs.importClass("org.apache.mailet.Mailet");
    bsNs.importClass("org.apache.mailet.Matcher");
    bsInt.source(script);                           // Source an external 
script file
    lastModified = scriptFile.lastModified();
  }
}
}}}

See also: [http://james.apache.org/server/custom_matcher_2_3.html Custom 
Matchers]

=== How to compile: ===

{{{
#!/bin/bash
# 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 version of [wiki:Self:BeanShell.jar 
BeanShell.jar]

=== 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"... [http://translate.google.com/ GoogleTranslation!]

Reply via email to