Re: Accessing ServletContext from Startup Actions

Anthony Eden <[email protected]> Tue, 13 Jul 2004 15:44:38 -1000
Newsgroups gmane.comp.java.jpublish.devel
Message-ID <[email protected]>
Yes, the VFS can resolve absolute paths.  Anything beginning with
file:/// will resolve using the file system, I believe, however I
suggest that you avoid using the VFS directly just for the loading of
configuration files.  Also, using getRealPath() is a bad idea and should
be avoided if possible.  There are numerous alternatives.

First you can use the ClassLoader resource methods to load
configurations from the class path.  Then you can put config files in
the WEB-INF/classes directory and load them.

Another option is to define a repository in jpublish.xml and pull all
configurations from that repository using:

Repository respository = site.getRepository("repository_name");
InputStream in = repository.getContent("/config").getContentInputStream();

Finally, you can indeed get the ServletContext in startup actions and
then you could use the ServletContext resource accessor methods to get
the configuration.  All of these options avoid the use of getRealPath()
and will be handled much better across various web containers.  In the
the case of the example below I'd rewriting the servlet you're initing
to properly load using the ServletContext resource methods and then just
pass it the relative path.

Sincerely,
Anthony Eden

Duane Adams wrote:

>How do i access the ServletContext from a startup action?
>
>I would like to get the full path to a properties file the WEB-INF folder
>and make it available to a startup action. Usually in a regular servlet, i
>would do something like:
>
>String realPath =
>getServletContext().getRealPath("WEB-INF/file.properties")
>
>The user guide suggests that the site object is available to me in a startup
>action. So i should be able to do:
>
>ServletContext sc = context.getApplication()
>String realPath = sc.getRealPath("WEB-INF/file.properties")
>
>However, the user guide also says that the ServletContext or "application"
>is not available to startup actions.
>
>Is their another way to do this? Can the vfs resolve full paths?
>
>Basically, i'm trying to turn this:
>
>public InitSomeServlet extends HttpServlet {
>    public init(ServletConfig config) {
>                try{
>                    ServletContext sc =  getServletContext()
>                    String realPathToConfig =
>sc.getRealPath(config.getInitParameter("config"))
>                    SomeServlet.init(realPathToConfig)
>
>                } catch(...){}
>    }
>}
>
>into this (startup action):
>
>public InitSomeServlet implements Action {
>    public execute(RequestContext context, Configuration config){
>        try{
>            ServletContext sc = context.getApplication()
>            String realPathToConfig =
>sc.getRealPath(config.getValue("config"))
>            SomeServlet.init(realPathToConfig)
>
>        } catch(...) {}
>    }
>}
>
>any guidance would be appreciated.
>
>Thanks,
>Duane Adams
>
>
>
>-------------------------------------------------------
>This SF.Net email sponsored by Black Hat Briefings & Training.
>Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
>digital self defense, top technical experts, no vendor pitches, 
>unmatched networking opportunities. Visit www.blackhat.com
>_______________________________________________
>Jpublish-developer mailing list
>[email protected]
>https://lists.sourceforge.net/lists/listinfo/jpublish-developer
>  
>




-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com