Re: Servlet
Jacob Kjome <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.barracuda.general |
|---|---|
| Message-ID | <[email protected]> |
I'm not exactly sure what you mean here? You can create a custom event
gateway and extend DefaultEventGateway. You can also use the
DefaultEventGateway in the event-gateway.xml file to register your events
with event handlers. Here is a simple way to do things....
in web.xml....
<servlet>
<servlet-name>ApplicationGateway</servlet-name>
<servlet-class>org.enhydra.barracuda.core.event.ApplicationGateway</servlet-class>
<init-param>
<param-name>ApplicationAssembler</param-name>
<param-value>org.enhydra.barracuda.core.event.DefaultApplicationAssembler</param-value>
</init-param>
<init-param>
<param-name>AssemblyDescriptor</param-name>
<param-value>/WEB-INF/event-gateway.xml</param-value>
</init-param>
<init-param>
<param-name>SAXParser</param-name>
<param-value>org.apache.xerces.parsers.SAXParser</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
in event-gateway.xml...
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<assemble>
<event-gateway
class="org.enhydra.barracuda.core.event.DefaultEventGateway">
<!-- Request event handler for listening for all other request events.
Does work and then allows the request be handled by the
originally requested event. -->
<event-interest
factory="com.mycompany.myapp.servlet.controller.handler.LocalRequestHandler"
event="com.mycompany.myapp.servlet.controller.event.LocalRequestEvent" />
<!-- Individual Request events. These event handlers actually handle
the request, store info in the event context, and pass
execution to the render handler below -->
<event-interest
factory="com.mycompany.myapp.servlet.controller.handler.GetCatalogHandler"
event="com.mycompany.myapp.servlet.controller.event.GetCatalog" />
<event-interest
factory="com.mycompany.myapp.servlet.controller.handler.GetUserInfoHandler"
event="com.mycompany.myapp.servlet.controller.event.GetUserInfo" />
<event-interest
factory="com.mycompany.myapp.servlet.controller.handler.GetOrdersHandler"
event="com.mycompany.myapp.servlet.controller.event.GetOrders" />
<!-- Response Event handler -->
<event-interest
factory="com.mycompany.myapp.servlet.controller.handler.RenderEventHandler"
event="com.mycompany.myapp.servlet.controller.event.RenderEvent" />
</event-gateway>
</assemble>
Prototypical event handler (use this as a template for your custom event
handlers such as GetCatalogHandler, GetUserInfoHandler, and
GetOrdersHandler)....
package com.mycompany.myapp.servlet.controller.handler;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.enhydra.barracuda.core.event.*;
import com.mycompany.myapp.servlet.controller.event.*;
/**
* A control event handler
*/
public class PrototypicalEventHandler extends DefaultListenerFactory {
//define the necessary factory methods
public BaseEventListener getInstance() {
return new ControlHandler();
}
public String getListenerID() {
return getID(ControlHandler.class);
}
//------------------------------------------------------------
// Model 2 - Controller Event Handlers
//------------------------------------------------------------
/**
* handle the request
*/
class ControlHandler extends DefaultBaseEventListener {
public void handleControlEvent(ControlEventContext context) throws
EventException, ServletException, IOException {
//in this case, we're not really doing anything special, so just
//redirect to the RenderEvent view
context.putState(RenderEventHandler.HANDLED_BY, ""+this);
context.getQueue().addEvent(new RenderEvent(context.getEvent()));
}
}
}
Example render handler....
package com.mycompany.myapp.servlet.controller.handler;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.enhydra.barracuda.core.event.*;
import com.mycompany.myapp.servlet.controller.event.*;
/**
* A view event handler
*/
public class RenderEventHandler extends DefaultListenerFactory {
private static final Logger logger =
Logger.getLogger(RenderEventHandler.class.getName());
//package access only
static final String HANDLED_BY = "HandledBy";
//define the necessary factory methods
public BaseEventListener getInstance() {
return new ViewHandler();
}
public String getListenerID() {
return getID(ViewHandler.class);
}
//------------------------------------------------------------
// Model 2 - View Event Handlers
//------------------------------------------------------------
/**
* RenderHandler - handle the request to render the test
* screen
*/
class ViewHandler extends DefaultBaseEventListener {
public void handleViewEvent(ViewEventContext context) throws
EventException, ServletException, IOException {
//unpack the necessary entities from the context
HttpServletResponse resp = context.getResponse();
//set the caching hdrs (allow this page to be cached by browser)
resp.setHeader("Cache-Control","max-age=0");
resp.setDateHeader("Last-Modified", System.currentTimeMillis());
//build the response
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
//header
String title = "Simple Deployment Descriptor Test Screen";
out.println ("<html>");
out.println (" <head>");
out.println (" <title>"+title+"</title>");
out.println (" </head>");
out.println (" <body>");
out.println (" <font face=\"Arial\">");
//body
out.println (" <p>"+title);
out.println (" <p>Handled by:"+context.getState(HANDLED_BY));
//footer
out.println (" </font>");
out.println (" </body>");
out.println ("</html>");
}
}
}
Of course you need to define your events. Do that using an events.xml file
and let the <events> taskdef do the work of generating the events...
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE build-events
PUBLIC "-//Enhydra Barracuda//DTD Barracuda Events 1.0//EN"
"http://barracuda.enhydra.org/Barracuda/dtds/BarracudaEventBuilder.dtd">
<build-events pkg="com.mycompany.myapp.servlet.controller.event">
<req-events>
<event name="LocalRequestEvent">
<event name="GetCatalog"/>
<event name="GetUserInfo"/>
<event name="GetOrders"/>
</event>
</req-events>
<resp-events>
<event name="LocalResponseEvent">
<event name="RenderEvent"/>
</event>
</resp-events>
</build-events>
That's about it. Notice that we didn't even code an application or event
gateway. We just used the default ones that come with Barracuda.
Hope that helps,
Jake
At 12:02 PM 4/4/2003 -0500, you wrote:
>Are there easy ways to extend these GateWay's? If yes,
>what rules should I follow?
>
>Xue-Feng
>
> --- Jacob Kjome <[email protected]> wrote: >
> > You can do one of a couple things...even both at the
> > same time...
> >
> > in web.xml...
> >
> > <servlet>
> >
> > <servlet-name>ApplicationGateway</servlet-name>
> >
> >
><servlet-class>org.enhydra.barracuda.contrib.dbroggisch.examples.controller.DBRApplicationGateway</servlet-class>
> > <init-param>
> >
> > <param-name>ApplicationAssembler</param-name>
> >
> >
><param-value>org.enhydra.barracuda.core.event.DefaultApplicationAssembler</param-value>
> > </init-param>
> > <init-param>
> >
> > <param-name>AssemblyDescriptor</param-name>
> >
> >
><param-value>/WEB-INF/event-gateway.xml</param-value>
> > </init-param>
> > <init-param>
> > <param-name>SAXParser</param-name>
> >
> >
><param-value>org.apache.xerces.parsers.SAXParser</param-value>
> > </init-param>
> > <load-on-startup>3</load-on-startup>
> > </servlet>
> >
> >
> > in DBRApplicationGateway....
> >
> > public void initializeLocal() {
> > specifyEventGateways(new
> > RenderPageEventGateway());
> >
> > }
> >
> >
> > in event-gateway.xml...
> >
> > <?xml version="1.0" encoding="UTF-8"
> > standalone="yes" ?>
> >
> > <assemble>
> >
> > <!-- You can turn on URL rewriting by uncommenting
> > this out
> > <constant
> >
>class="org.enhydra.barracuda.plankton.http.URLRewriter"
> >
> > name="REWRITE_URLS">true</constant>
> > -->
> >
> > <event-gateway
> >
>class="org.enhydra.barracuda.core.event.DefaultEventGateway">
> > <event-gateway
> >
>class="org.enhydra.barracuda.contrib.dbroggisch.examples.controller.ExampleGateway"
> >
> > />
> > </event-gateway>
> >
> > </assemble>
> >
> >
> > So, we've added both RenderPageEventGateway and
> > ExampleGateway as
> > sub-gateways of the custom DBRApplicationGateway.
> >
> >
> >
> > You can also add subgateways to your event gateways
> > like this...
> >
> > In BarracudaConfig (which is an event gateway)....
> >
> > public BarracudaConfig() {
> > ...
> > ...
> > ...
> > //add in sub-gateways
> > this.add(new CompConfig());
> > this.add(new DataConfig());
> > this.add(new DomConfig());
> > this.add(new EventConfig());
> > this.add(new FormsConfig());
> > this.add(new UtilConfig());
> > this.add(new ViewConfig());
> > this.add(new MasterConfig());
> > }
> >
> > or this (in an event-gateway.xml)....
> >
> > <event-gateway
> >
>class="org.enhydra.barracuda.core.event.DefaultEventGateway">
> > <event-gateway
> > class="com.mypackage.MySubEventGateway1" />
> > <event-gateway
> > class="com.mypackage.MySubEventGateway2" />
> > <event-gateway
> > class="com.mypackage.MySubEventGateway3" />
> > </event-gateway>
> >
> >
> > So, you can do everything in application gateway
> > and/or event gateway
> > classes or do everything in the event-gateway.xml
> > file read in by the
> > application assembler. The advantage of doing the
> > former is compile-time
> > assurance that your application is set up right.
> > The advantage of the
> > former is ease of configuration and the ability to
> > mix and match event
> > gateways and/or event handlers without recompiling
> > anything.
> >
> > Does that help?
> >
> > You might also want to read the 30,000 foot overview
> > and the other docs on
> > the event model:
> > http://barracudamvc.org/Barracuda/index_details.html
> >
> >
> > Jake
> >
> >
> > At 10:11 AM 4/4/2003 -0500, you wrote:
> > >Yes, now I see ApplicationGateway has a method
> > >
> > >public final void add(EventGateway eg)
> > >
> > >Where are the codes that actually 'add' all
> > >'DefaultEventGateway's?
> > >
> > >Maybe I should ask differently.
> > >
> > >Each specific DefaultEventGateway contains main
> > >information of MVC for each page. I just want to
> > >understand the code flow how the servlet treats
> > these
> > >DefaultEventGateway's step by step.
> > >
> > >
> > >Thanks,
> > >
> > >Xue-Feng
> > >
> > >
> > > --- Jacob Kjome <[email protected]> wrote: >
> > > > Are you wondering about the ApplicationGateway?
> > > > That extends
> > > > HttpServlet. You add event gateways to the
> > > > application gateway.
> > > >
> > > > Jake
> > > >
> > > > At 07:34 PM 4/3/2003 -0500, you wrote:
> > > > >Hi Jack,
> > > > >
> > > > >I guess that DefaultEventGateway should be
> > comsumed
> > > > by
> > > > >some servlet. Could you please tell me which
> > > > servlet
> > > > >consumes the class which entends
> > > > DefaultEventGateway?
> > > > >Or correct me.
> > > > >
> > > > >Xue-Feng
> > > > >
> > > > >
> > > > >
> > > >
> > >
> >
> >______________________________________________________________________
> > > > >Post your free ad now!
> > http://personals.yahoo.ca
> > > > >_______________________________________________
> > > > >Barracuda mailing list
> > > > >[email protected]
> > > >
> > >http://barracudamvc.org/lists/listinfo/barracuda
> > > >
> > >
> >
> >______________________________________________________________________
> > >Post your free ad now! http://personals.yahoo.ca
> > >_______________________________________________
> > >Barracuda mailing list
> > >[email protected]
> > >http://barracudamvc.org/lists/listinfo/barracuda
> >
>
>______________________________________________________________________
>Post your free ad now! http://personals.yahoo.ca
>_______________________________________________
>Barracuda mailing list
>[email protected]
>http://barracudamvc.org/lists/listinfo/barracuda