• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Abstract Command Factory (mapping commands)

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm implementing my own FrontController FrameWork and I have some questions regarding the best way to map my Factory to my Commands. Here is how it looks at the moment.


Let say I have a use case called InsertNews and that request shall be mapped to InsertNewsCommand. The problem here is that I need to do the following to be able to locate my command:

This solution doesn't look very good so what do you think about letting the CommandFactory to read an XML file and find the element/attributes that match "InsertNew" etc?

Is there any better solution, please let me know.
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An XML file is one way to specify configuration. It's most appropriate when the configuration info is fairly complex or structured.

In my own Front Controller implementation, I just need to equate a command verb with a class name that implements the command, so I just use a properties file.
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read something about the context parameters, what about having it there, in the web.xml together with the controller it belongs to?



Originally posted by Bear Bibeault:
In my own Front Controller implementation, I just need to equate a command verb with a class name that implements the command, so I just use a properties file.

Sounds interesting, would you mind to show a small example?

[ September 08, 2006: Message edited by: Joel Hutters ]

[ September 08, 2006: Message edited by: Joel Hutters ]
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would definitley suggest using a configuration file or some sort of mapping mechanism. The file would map command names to command classes.




You could write your own parser or use the properties class.

The Properties class, since 1.5, includes a convenient mechanism for parsing xml configuration files. It may save you some time. Refer to the java doc and dtd for more information. And there is always the load(is) method. Again check the doc. The properties class uses synchronized methods.

After you have the mappings, I would continue with the same logic mentioned above.




Also with mappings, it is easy to override or add new implementations without compiling. Example:






The code above is completely extemporaneous. Take some time to find, research, and weigh the advantages/disadvantages before choosing a solution.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel Hutters:
I read something about the context parameters, what about having it there, in the web.xml together with the controller it belongs to?



I try to keep my web.xml a minimal as possible. Also, if you store it web.xml, you have to reload the app every time you make a change.

With an "external" configuration, you have the option of reloading it on-the-fly.

Sounds interesting, would you mind to show a small example?



If I had a verb such as doSomethingWonderful, there would be an entry in the command verbs propertie file along the lines of:

doSomethingWonderful=org.bibeault.some.project.whatever.DoSomethingWonderfulCommand

if the Front Controller is mapped to /command, the url could be:

http://my.server.com/contextPath/command/doSomethingWonderful

It's pretty basic and simple, which is exactly how I like it.
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic answers both of you! Rashid Mayes, that solution sounds exactly like something I would like to implement. Just one question though, where would you put the procedure of loading the property/xml file?

This seems to be helpful:
http://www-128.ibm.com/developerworks/java/library/j-tiger02254.html?ca=dgr-SEOn-JDK_5.0-XML
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I specify the properties file as an init param to the Front Controller servlet and the init() of that servlet triggers the initial loading.

The actual loading is done by another class so that it can be re-loaded on the fly at any time via an admin "command".
[ September 08, 2006: Message edited by: Bear Bibeault ]
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds good and I get the point, but how do you specify it as a param? I mean the init method that I am overriding in my servlet doesn't take any arguments. Would you mind showing that to? I mean this doesn't work...

Your answer is very much appreciated, just the last detail I need to know before I feel that I'm ready to start implementing it.

At last, where do you put your Property file? /etc in development structur and /WEB-INF in deployment?
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to load my XML file with this in my servlet:

my servlet is located in appfolder/WEB-INF/classes/com.blog.web and the XML-file is located in the root directory appfolder/ of my web-app. All I get is "(No such file or directory)"

[ September 08, 2006: Message edited by: Joel Hutters ]
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel Hutters:
but how do you specify it as a param?



You really need to take some time and read through the Servlet Specification. This is all basic servlet knowledge that you need to have under your belt. It's not a hard read.

You set init params in the <servlet> element in the deployment descriptor, and retrieve them via the servletConfig that is available to your instantiated servlet.


At last, where do you put your Property file? /etc in development structur and /WEB-INF in deployment?



WEB-INF is pretty much customary. Why would you put it in /etc for developemnt? It's best to develop in the same structure you use for deployment.

new FileInputStream("/commands.xml");



First, I wouldn't use XML. It's cool that JDK 1.5 allows that now, but I just don't see the advantage of all that extra markup when a properties file will do.

Secondly, that code assume that the file is in the root of your file system. Not the case, is it?

Again, reading the servlet specification you'll find methods that allow you to read property streams from the web app structure without having to know where the file is physically lcoated in the file system.
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I started studying Servlets and JSP last week so I still have much to learn, and I am aware of that. I mentioned the init in a post earlier in this thread, and I understood what you mean a time after replying to your message,

About putting it in the /etc was a typo by me, I have spent to much time in front of the screen today.

Thanks for explaining the case regarding the method loading the XML-file, I assumed it started at the web-root of my application but I was wrong. I will follow your advice and use the method you described earlier instead. I have much more to learn
[ September 08, 2006: Message edited by: Joel Hutters ]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you're really digging in. Which is good, but be sure not to get ahead of yourself.

The JSP Spec is a bit daunting (but you should at least read the section on the EL), but the Servlet Spec is a quick read and you should digest it to give yourself an overview of what's possible and what is available to you.

Grabbing a good, modern book on the subject wouldn't hurt either.

And of course, you have JavaRanch at your disposal.
 
Joel Hutters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advices

I'm reading the book Head First Servlets and JSP and developing an application at the same time. I'm sure I will learn by my mistakes but I wont skip the important reading of the basics.

I got the Properties File working with help of the ResourceBundle class, is that how you do it?
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I most often use PropertyResourceBundle in conjunction with ServletContext.getResourceAsStream().
 
Grow your own food... or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic