How to change freemarker file in web project at runtime? - java

I use embedded jetty, osgi in my web project.
I want to change the code in freemarker files in the web project at runtime. What is the best way to realize it?
Best regards.

Assuming the templates will be replaced under the control of the Web application, when you know a template has been replaced, you could call Configuration.removeTemplateFromCache on it, so that it will be guaranteed that the next Configuration.getTemplate will return the new file. This also means that you don't have to set a low template_update_delay on the production server.
The more tricky question is if FreeMarker can accidentally see a partially replaced or exclusively locked (can't open it) file while you are replacing it. During development you don't care because it has near 0 chance, but on a production server you do. This is up to the TemplateLoader implementation, and then the storage behind it. Like if it's DB-backed, there's certainly no such risk. For File-s, I guess there can be such glitches. Unfortunately, you can't force FreeMarker to keep a template in the template-cache while you are replacing it. But you can achieve something very similar on the TemplateLoader-level instead. You could create a custom TemplateLoader to which you can tell something like myTemplateLoader.mirrorToMemory(fileName), then you replace the file, then you call myTemplateLoader.releaseMirrorOf(fileName). And then you invalidate the template-cache entry as I said earlier.

Related

Adding functionality - Java servlet built with Ant

I have with me a working copy of Apache FOP's Java Servlet source code.
I have compiled the code - which handles a doGet request - and deployed it on Tomcat on my localhost, and it works and its good.
BUT I need to add functionality for a POST method to the file.
I can do that, but I'm not sure how to use the source code. All of my experience in Java has basically been behind an IDE.
The servlet is built by an Ant script, build.xml, that seems to contain references to variable names. Do I need to add to this file at all? Can i just change stuff in my servlet.java, and go straight to building?
Yes, you can do that. If you have the source code and can build the project, then you can start a simple text editor, change the servlet Java file and then build the whole thing again.
But it will be time consuming. It can take a while until compilation problem are reported leading to high turnaround times. And you are unable to debug your code if something went wrong.
For some very small changes this approach might be suitable. But for larger changes it would be better to invest the time to configure your IDE for web application debugging.

MessageResouce files in JSF

My application suppose to work with different languages. In order to provide this feature, I have created different messageResource files for each language.
Each resource files contain the same keys but values are different according to which language application is running. I load the specific language resource file on application load up.
This is working very fine.
However, as we are adding more features, the resource files for each language becoming very long, which makes it difficult to manage (edit) for non-techy person like Content editor guy.
Therefore, I would like to know, how can I redesign or remodularise in such a way that it will be easy to manage for Content Writers?
I hope I clear the scenario but please shout, if any thing needs?
I do not know a i18n standard mechanism in the Java world to split message properties in chunks. However, I have made good experiences with a standalone properties-edit-tool like jLokalize. It's pretty user-friendly even for non-programmers: hand over the properties files to the content writer and let him/her load the files in the editor.

Library to access Javadoc from Java

I want to find a library that I can use from my Java application that will allow me to access specific Javadoc in the scope of my project (I specify where Javadocs are located). Just like in Netbeans, I want to potentially access the Javadoc from html files locally and remotely, and from source.
I expect that I could use code from Netbeans to achieve this, but I don't know how, and I can't easily digest their documentation.
Today I started thinking about the same thing.
From CI point of view, I could use #author annotation to send e-mail to someone, who wrote a test that is failing with error, not with a failure.
Google didn't help me (or I didn't google deep enough), so I started wondering how to do it on my own.
First thing that came to my mind is writing a little tool that will check all *.java files specified in a directory, bound file name to annotations and allow user to perform some actions on them.
Is that reasonable?

How to reuse/extend closed java webapp (.ear)?

I've got webapp project written in java (.ear).
It is running under glassfish-3 server.
I really have to extend its functonality. but there is no way to change even a line of it.
Is it possible to wrap that webapp or anything else (it has no API, only html for user)?
I know it's very generic, but i cannot provide a code or other useful things.
A similar situation to mine would be (expect my case needs much SECURITY because of sensitive data)...
Let's have a blog application (this would be the java closed webapp), which allows everything You expect from an blog app, BUT one instance handles one user.
You would like to allow new user to create blog (new instances of blog app?). The instances should be isolated each other.. but it's simple to achieve. Different instance different db and so on..
I do not expect solution (because of my generic description) but direction where to go.
I'm java novice, but i can read and reason so.. :)
.ear is just a zipped file, see doc http://en.wikipedia.org/wiki/EAR_(file_format). You can always unzip it and get hold of all files. JSP and config-files can be altered but not the java-classes (unless you decompile them).
This not a complete solution but hopefully a hint in some direction...

Handling context-path refs when migrating "/" site to Java EE packaging

An existing Java site is designed to run under "/" on tomcat and there are many specific references to fixed absolute paths like "/dir/dir/page".
Want to migrate this to Java EE packaging, where the site will need to run under a context-root e.g. "/dir/dir/page" becomes "/my-context-root/dir/dir/page"
Now, the context-root can be easily with ServletRequest.getContextPath(), but that still means a lot of code changes to migrate a large code base. Most of these references are in literal HTML.
I've experimented with using servlet filters to do rewrites on the oubound HTML, and that seems to work fine. But it does introduce some overhead, and I wouldn't see it as a permanent solution. (see EnforceContextRootFilter-1.0-src.zip for the servlet filter approach).
Are there any better approaches to solving this problem? Anything obvious I'm missing? All comments appreciated!
Check out a related question
Also consider URLRewriteFilter
Another thing (I keep editing this darn post). If you're using JSP (versus static HTML or something else) you could also create a Tag File to replace the common html tags with links (notably a, img, form). So link can become <t:a href="/root/path">link</t:a>. Then the tag can do the translation for you.
This change can be easily done "en masse", using something like sed.
sed -e 's/<a/<t:a/g' -e 's/<\/a>/<\/t:a>/g' old/x.jsp > new/x.jsp
Form actions may be a bit trickier than sed, but you get the idea.
the apache world used Redirects(mod_rewrite) to do the same.
The Servlet world started using filters
The ruby world (or the RoR) does more of the same stuff and they call it routing.
So, there's no getting around it (Unless you want to use smart regex through out -- which has been tried and it works just fine).

Categories