-------------------------------------------------------------------------
PumpServlet 1.0.1

This software is licensed under GNU General Public License
http://www.gnu.org/copyleft/gpl.html

Copyright (C) 2002 Bojan Smojver, Rexursive
-------------------------------------------------------------------------

PumpServlet is a central entry point for all Velocity pages in a web
application, if correctly mapped in web.xml.


Changes since 1.0.0
-------------------
- Apache 2.x with mod_jk 1.2.0 returns different things as compared to Apache
  1.3.x with mod_jk 1.2.0; version 1.0.1 compensates for that by redirecting
  requests originally directed to '/path/to/page/' into requests to
  '/path/to/page/defPage', where defPage is either 'index.vm' or defined in
  web.xml, parameter defaultPage; keep in mind that using POST method to
  such paths will effectively erase all POST parameters since it does an
  external redirect, not a proxy; GET parameters (i.e. the query string) should
  work fine


Most important features of the servlet
--------------------------------------
- uses Velocity 1.2+ non-singleton (or instance) model to create a
  VelocityEngine per application (stored in ServletContext); it will init the
  engine from velocity.properties file
  (context-param: velocity.properties, default: /WEB-INF/velocity.properties)

- it will load and initialise beans (if the bean implements InitProperties
  interface, code supplied with the servlet) of application, session and
  request scope that you give those in pump.xml; properties for the application
  are read from the same file, and will be passed to beans at init time
  (context-param: pump.xml, default: /WEB-INF/pump.xml)

- parser will look for <pump> tag(s) anywhere in the supplied pump.xml, take
  what it understands, scream about obvious errors and ignore the rest

- properties of the application are specified like this:

  --------------------------------------------
  <property name="myprop">my value</property>
  --------------------------------------------

- application beans are available to all pages in an app (just like JSP) and
  are stored in the ServletContext; for instance (pump.xml):

  --------------------------------------------
  <application>
    <bean name="readdata" class="com.lottery.beans.ReadData"/>
  </application>
  --------------------------------------------

  make sure this section appears below the properties section, otherwise
  application beans could be initialised with incomplete properties (since
  parsing of pump.xml is sequential, based on SAX)

- session beans are available to the pages within that session (just like JSP)
  and are stored in HttpSession; for instance (pump.xml):

  --------------------------------------------
  <session>
    <bean name="sessiondata" class="com.lottery.beans.SessionData"/>
  </session>
  --------------------------------------------

- request beans are loaded and initialised every time the correct page is hit;
  those beans are stored in the current request (so that the controller can
  have a go at them); this in pump.xml file:

  --------------------------------------------
  <request>
    <bean name="lottery" class="com.lottery.beans.Win">
      <page path="/lottery/bigwin.vm"/>
      <page path="/lottery/smallwin.vm"/>
    </bean>
  </request>
  --------------------------------------------

  or this:

  --------------------------------------------
  <request>
    <page path="/lottery/bigwin.vm">
      <bean name="lottery" class="com.lottery.beans.Win"/>
    </page>
    <page path="/lottery/smallwin.vm">
      <bean name="lottery" class="com.lottery.beans.Win"/>
    </page>
  </request>
  --------------------------------------------

  would make sure that every time pages /lottery/bigwin.vm or
  /lottery/smallwin.vm are hit, there is a new instance of the lottery bean

  this:

  --------------------------------------------
  <request>
    <bean name="lottery" class="com.lottery.beans.Win"/>
  </request>
  --------------------------------------------

  would make lottery bean available to all pages in the application

- there are no page beans (ie. this is not like JSP ;-)

- all beans are stored into Velocity reference $beans (which is a HashMap) by
  their names specified in pump.xml; there is also the standard $req and $res
  (for Model 1 architectures as well as URL encoding etc.)

- if there is a context-param controller which implements Controller (code
  supplied) interface (and optionally InitProperties interface), it will be
  loaded and initialised and then used as (surprise) MVC controller class for
  the application; its single method (service) is called with request, response
  and Velocity context as parameters; this is where some real magic is
  happening since this method is also permitted to switch the page that is
  going to be loaded because it returns the new template name; you write your
  own class that does whatever custom request processing you have for the app

- if you don't use MVC (or Model 2), Model 1 is assumed, which means that .vm
  pages themselves will be steering the application and calling business logic
  bean calls - just in case you find MVC too complicated for some things; note
  that if you have the controller, you can still have some pages on Model 1 by
  just passing back the same template that went into the controller's service
  method (this is getServletPath() from the request)

- .vm page (template) is then merged, but the result will not go to response
  until we're sure that everything was cool - therefore the
  ByteArrayOutputStream buffer

- if there were problems, servlet will pick up the error page you supply or
  failing that its own little piece of XHTML


A word of caution
-----------------
Since this whole thing is upside-down, using RequestDispatcher's within the
controller will definitely break things. The idea is that the controller uses
regular Java stuff to do whatever is necessary to prepare the beans for
presentation pass. Since there are no other servlets within the application,
dispatching requests would not have the desired effect and would cause havoc
during template rendering.

Forward functionality of dispatchers can be achieved by changing the template
(by returning a different one from controller's service method). If controller
returns an empty string (""), it is a signal for the servlet that the
controller changed response in such manner that no further processing (i.e.
template rendering) should be done. The servlet will return immediately. If
controller returns null, it is a signal to the servlet that something went
wrong and status code 404 (Not Found) will be returned.

Include functionality can be done in Velocity pages themselves based on values
of certain references in the context, prepared by the controller.  But I think
this means breaking MVC to a certain extent, not sure (comments from MVC gurus
welcome). For instance:

Java (controller):
--------------------------------------------
if(needMe) ctx.put("includeMe","true");
--------------------------------------------

Velocity:
--------------------------------------------
#if($includeMe)
  #parse("me.vm")
#end
--------------------------------------------


If you're converting from 0.5.x
-------------------------------
The bash script pr2xml will convert exisiting beans.properties and
application.properties files (for PumpServlet up to version 0.5.x) into an
XML file (pump.xml). If you had request beans per page, you'll have to
convert those manually.


Sample files
------------
There are also other files in this tarball that can simplify deployment of
Velocity pages in Apache + Tomcat + Velocity environment.


Have fun,
Bojan Smojver
bojan@rexursive.com
