Usually in this case, you use the unit of work pattern and the lazy load.  I
usually handle it like so:

public class UserData
{
    private final static Object _lock = new Object();
    private ServletContext context;
    private int state;
    private String path;

    private UserData(ServletContext context, String path)
    {
        this.context = context;
        this.path = path;
    }

    public void registerNew() throws Exception
    {
       // do your parsing based on context
       this.state = CLEAN;
    }

    public static UserData getInstance(ServletContext context, String file)
        throws Exception
    {
        String key = genContextKey(file);
        UserData userData = (UserData) context.getAttribute(key);
        if (userData == null)
        {
            // make sure we don't create multiple copies
            synchronized(_lock)
            {
               // double check that a previous thread didn't
               // already make one right before me
               userData = (UserData) context.getAttribute(key);
               if (userData == null)
               {
                   // create a new one
                   userData = new UserData(context, file);
                   userData.registerNew();

                   // now store it for other threads
                   context.setAttribute(key, userData);
               }
            }
        }
        return userData;
    }
}


in your action then:

UserData userData = UserData.getInstance(context, "somefile.xml");
Collection employees = userData.getAllEmployees();

userData.addEmployee(new Employee("Craig"));
userData.registerClean();


As a side note, look at Commons Digester in reading your XML file and
bringing it in as Objects so you can work with ActionForms and other
business logic (if that's what you want to do).

-Jacob

| -----Original Message-----
| From: Peng Tuck Kwok [mailto:[EMAIL PROTECTED]]
| Sent: Tuesday, February 11, 2003 12:25 AM
| To: Struts Users Mailing List
| Subject: Re: Some questions about struts
| 
| Thanks for the prompt reply Jacob.
| I do need the xml data to persist, one xml which contains a bunch of
| users who can log in & the other xml contains a list of numbers that
| belong to the bunch of users.
| Since I need the application to read these xml files on startup, does
| that mean I need to create a class which extends ActionServlet and
| populate the servlet context like this ? :
|        getServletContext().setAttribute(LOCALES_KEY, locales);
| 
| 
| 
| 
| 
| 
| Jacob Hookom wrote:
| > You can have the ActionServlet determine where the source XML is coming
| > from.  Then, either forward to a JSP and use JSTL to handle the
| rendering,
| > or use a transformer and write directly to the OutputStream within the
| > ActionServlet, and return null instead of an ActionForward.
| >
| > -Jacob
| >
| > | -----Original Message-----
| > | From: Peng Tuck Kwok [mailto:[EMAIL PROTECTED]]
| > | Sent: Tuesday, February 11, 2003 12:06 AM
| > | To: Struts Users Mailing List S
| > | Subject: Some questions about struts
| > |
| > | I have a few questions I need to ask about struts. First of I've done
| > | some simple examples that allow my struts example to access the
| database
| > | and validate fields passed by an action form. All is good and well.
| > |
| > | Now I want my application to read say two xml files and when a page is
| > | requested, the page would display the data from the xml.
| > | My question is when I pre-process the xml file on startup, how do I
| make
| > | the xml data available to struts on startup? Should I use a
| > | ActionServlet to do this ? Or am I barking up the wrong tree here?
| > |
| > |
| > |
| > |
| > | ---------------------------------------------------------------------
| > | To unsubscribe, e-mail: [EMAIL PROTECTED]
| > | For additional commands, e-mail: [EMAIL PROTECTED]
| >
| >
| > ---------------------------------------------------------------------
| > To unsubscribe, e-mail: [EMAIL PROTECTED]
| > For additional commands, e-mail: [EMAIL PROTECTED]
| >
| >
| 
| 
| 
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: [EMAIL PROTECTED]
| For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to