File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Abstract Command Factory (mapping commands) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Abstract Command Factory (mapping commands)" Watch "Abstract Command Factory (mapping commands)" New topic
Author

Abstract Command Factory (mapping commands)

Joel Hutters
Greenhorn

Joined: Sep 07, 2006
Posts: 21
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joel Hutters
Greenhorn

Joined: Sep 07, 2006
Posts: 21
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 ]
Rashid Mayes
Ranch Hand

Joined: Jan 11, 2006
Posts: 160
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.


Rashid Mayes
http://www.hostj2me.com/ - http://www.worlddeveloper.org/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Sep 07, 2006
Posts: 21
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Sep 07, 2006
Posts: 21
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

Joined: Sep 07, 2006
Posts: 21
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Sep 07, 2006
Posts: 21
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

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

Joined: Sep 07, 2006
Posts: 21
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

I most often use PropertyResourceBundle in conjunction with ServletContext.getResourceAsStream().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Command Factory (mapping commands)