jeudi 27 octobre 2016

Stateless Factory with EJB

I have a requirement to get pdf documents from my system. I'm using Apache Fop for this - and this library is using 2 files to generate pdf - xsl file with structure and styling and xml with data. So I'm getting xsl file from web resources, but now I need to generate xml with data from database. I tried this solution: I have this interface:

public interface PrintableDocument {
    Object getJaxBOjbect(Long personId);
}

That's one of the stateless bean to get object, I need 10 more beans like this to get different data for different documents.

@Stateless
@PrintableDocumentOneQualifier
public class PrintableDocumentOne implements PrintableDocument {

    @Inject
    private SomeRepository repository;

    public Object getJaxBOjbect(Long personId) {
    // Getting information from database
    // formulating Object with data and returning it
    }
}

So now I want to create Factory like this one:

@Stateless
@LocalBean
public class PrintableDocumentsFactory {

    @Inject
    @PrintableDocumentOneQualifier
    private PrintableDocument printableDocumentOne;

    @Inject
    @PrintableDocumentTwoQualifier
    private PrintableDocument printableDocumentTwo;

    private Map<String, PrintableDocument> map = new HashMap<>();

    public void init() {
        map.put("one", printableDocumentOne);
        map.put("two", printableDocumentTwo);
    }

    public PrintableDocument getPrintableDocument(String type) {
        return map.get(type);
    }

}

And on the service bean I want to use this factory:

@Stateless
@Local(DocumentService.class)
public class DocumentServiceBean {

    @Inject
    private PrintableDocumentsFactory factory;

    public byte[] getPdf(InputStream xsl, Long id, String type) {
        PrintableDocument printableDocument = 
             factory.getPrintableDocument(type);
        Object jaxBOject = printableDocument.getJaxBObject(id);
        //Use this object to get pdf and return it to web controller.
    }

}

But now I'm getting null from getPrintableDocument from factory. I think the problem is that I need stateless beans, and they are getting picked back to EJB container, when getPrintableDocument method ends. So my question is: how can I manage this kind of situation?

Aucun commentaire:

Enregistrer un commentaire