i have filter dinamically mapping servlet classes:
@override public void init( filterconfig filterconfig ) throws servletexception { servletcontext = filterconfig.getservletcontext(); file directory = getconventiondirectory(); filesysteminspector fileinspector = new filesysteminspector(); set<actioninfodata> actions = fileinspector.getactions( directory ); ( actioninfodata action : actions ) { servletcontext .addservlet( action.getservletname(), action.getclassname() ) .addmapping( action.getservletmapping() ); } } then when access given mapping ejb not injected.
@ejb private i18nmanager i18nmanager; @override protected void doget( httpservletrequest request, httpservletresponse response ) throws servletexception, ioexception { i18nmanager i18n = i18nmanager; //null } if manually create mapping in web.xml the given ejb working in servlet. makes me wonder if fact registering servlets @ runtime container not consider servlets managed.
if case proper way inject ejbs servlets without changing way dinamically registered via filter?
is via jndi way inject ejbs?
edit 1: have tried implement servletcontextlistener class suggested "will" using following code in web.xml:
<listener> <listener-class>com.megafone.web.filter.convention.initservlet</listener-class> </listener> and relevant part of implementation:
... @override public void contextinitialized( servletcontextevent sce ) { servletcontext servletcontext = sce.getservletcontext(); filesysteminspector fileinspector = new filesysteminspector(); set<actioninfodata> actions = fileinspector.getactions( getconventiondirectory() ); ( actioninfodata action : actions ) { servletcontext .addservlet( action.getservletname(), action.getclassname() ) .addmapping( action.getservletmapping() ); } } ... unfortunately not make container inject ejbs , null pointer remains. making custom type safe jndi lookup service. far more expensive using proper injection (correct me if wrong, have done no experiments regarding performance yet).
using:
java ee 6
jboss 7.1
the problem seems related this reported bug yet unresolved. resource resolution works fine managed beans defined jsf specification, not cdi managed beans. annotating dynamic servlet classes @javax.faces.bean.managedbean should fix problem (yes, quite ugly solution):
@managedbean public class dynservlet extends httpservlet { private static final long serialversionuid = 1l; @ejb private loginservice loginservice; public dynservlet() { super(); } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.getoutputstream().println( "request made to: " + getclass().getsimplename()); response.getoutputstream().println("login service: " + loginservice); return; } } @weblistener public class dynamicservletloadlistener implements servletcontextlistener { public dynamicservletloadlistener() { super(); } @override public void contextdestroyed(final servletcontextevent contextevent) { return; } @override public void contextinitialized(final servletcontextevent contextevent) { contextevent.getservletcontext().addservlet("dynservlet", dynservlet.class) .addmapping("/services/dynservlet"); } } tested jee6 (ofc) , both jboss 7.1.1 , 7.2.0 (eap 6.1.0 alpha).
edit:
the problem dynamic mapped servlets pretty deep in base jboss architecture. use jbossweb (a forked version of tomcat) servlet implementation, , in guts of context management code makes determination wether instantiate new components via injection or regular new. afaik of date, servlets need annotated in fashion in order them processed via injection: had mentioned @managedbean in original answer looks annotating @webservlet works well.
Comments
Post a Comment