The current version of Bulletin Board looses all posts when the server (or webapp) shuts down. In this section we'll add code to save the posts and restore them when the server restarts.
There are a number of ways we could do this.
The most foolproof method would probably be to use JDBC and keep posts in
a database.
Less elaborate schemes would keep the posts in a disk file:
We could open a file, make the change and close the file each time a post
was added or removed. That would be fairly reliable, but would involve a
lot of overhead.
We're going to assume bulletin board posts aren't mission critical,
and just save the posts when the server shuts down. Besides being simple,
this approach allows us to demonstrate the SrvcProvFtry quiesce method.
The current (V0.2.0) version of BBoardProvFtry isn't very different from V0.1.0, so we won't make a permanent backup.
Define a default path for the saved posts file
Add a private constants section after the public constants:
// Private constants
public static final String DEF_POST_FILE_PATH = "BBPosts.txt";
Create a global field to hold a file object
private File postFile = null;
line to the main class private variables sectionCreate private methods in the BBoardProvFtry main class:
private void savePosts() { ... }
private void restorePosts() { ... }
The methods don't need to be synchronized because they're only called at startup and shutdown.
Add local variables
String postFilePath = DEF_POST_FILE_PATH;
Iterator pitr;
String[] pair;
Get the path to the saved posts file
Check the params list for a file parameter by iterating through it looking for a file tag.
if (pair[0].toLowerCase().startsWith("file")) postFilePath = pair[1];
(We usually make parameter tags case-insensitive.)
Initialize the postFile field. We use a global variable because we'll use it again at shutdown.
postFile = new File(postFilePath);
Restore the bulletin board from the saved posts file
First see if the file exists.
If it does we call restorePosts to restore the bulletin board.
If it doesn't we log a warning and continue:
log.println("BBoardProvFtry WARNING: " + postFilePath + " Does not exist");
We'll leverage the BBoardProvImp.getPostsMsg method to save and restore the bulletin board. The TvList class has readFrom and writeTo methods to read and write the list to disk files. For savePosts, we'll copy the bulk of the getPostsMsg method to the main class and create a method used by both getPostsMsg and savePosts.
Create a getPostList method in the main class
TvList postList = new TvList();
Fill in the savePosts method
Update the BBoardProvImp.getPostsMsg method
The new getPostsMsg method is short, so we just replace the old content.
postMsg.addFirst(TAG_BB_POSTS, "");
postList.readFrom(postSrc);
(Details omitted. It's just plain java.)
Replace the No action needed comment with a call to savePosts:
savePosts();
We don't have to make any changes to the client.
For the native server version of EventWeb, we don't have to do anything.
If the BBPosts.txt file doesn't exist when the server shuts down,
BBoardProvFtry will create it and save the posts in it. If it does exist,
the old version will be overwritten.
For the servlet version, we want to keep configuration files in the root folder of the webapp. To do that, we use the File parameter to set the path to BBPosts.txt
Add the File parameter to the BBoardProvFtry line in RqstRouterServlet.ini:
SrvcProv = eventweb.prov.BBoardProvFtry -File webapps/EvwDemosEc/BBPosts.txt