users@glassfish.java.net

ServiceLoader/Lookup equivalent in EJB3 enviroment

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Mon, 17 Dec 2007 23:05:47 +0100

Hi there,
In my EJB3 application I have a problem with reports generation. There
are lots of different kind of reports our customer want to have. I was
thinking I could create some interface that would describe a report
from the server-side perspective, something like this:

public interface Report<Q,R> {
  boolean supports(String reportName);
  R generateReport(Q queryParameter);
}
Q stands for query parameter, R for result.

Every report could implement this interface like, for example this one:

public class RegionsActivityReport<DateInterval, ActivityReportResult> {

  public boolean supports(String reportName) {
    return reportName.equals("RegionsActivityReport");
  }

  public ActivityReportResult generateReport(DateInterval queryParameter) {
   ....implementation....
  }
}

In JavaSE enviroment, I could use ServiceLoader, put the names of all
implementations in META-INF and then, something like this:

public class ReportService {
  ServiceLoader<Report> reportsLoader = ServiceLoader.load(Report.class);
  [...]
  public Object doReport(String reportName, Object query) {
    for (Report<?,?> report : reportLoader) {
      if (report.supports(reportName) {
        return report.generateReport(query);
      }
    }
  }
}

The point is, I would like my report implementations to be session
beans, so I could use injection, ejb context, entityManager and
whatever else any particular report would like to use, but
unfortunately I cannot find anything equivalent to Java6's
ServiceLoader or NetBeans' lookup in EJB3 world :(

Can you give me some advice?
Thanks,
Witold Szczerba