Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mentawai/core/SingleInstanceBaseAction.java


1   /*
2    * Mentawai Web Framework http://mentawai.lohis.com.br/
3    * Copyright (C) 2005  Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package org.mentawai.core;
20  
21  import java.util.*;
22  
23  import org.mentawai.message.*;
24  
25  /**
26   * This is the base class for single instance actions, in other words, 
27   * those actions that will be instantiated only once and shared among all requests.
28   * Sometimes, when you looking for every performance drop, you may not want to create a new action instance for every request.
29   * This class keeps the action data in thread locals,
30   * so that each thread has its own input, output, session, application and locale.
31   * The actions extending this class must be configurated with a SingleInstanceActionConfig.
32   * It is your responsibility to make your action thread-safe, otherwise you should stick with the BaseAction class.
33   * 
34   * @author Sergio Oliveira
35   */
36  public abstract class SingleInstanceBaseAction extends BaseAction {
37      
38      private ThreadLocal input = new ThreadLocal();
39      private ThreadLocal output = new ThreadLocal();
40      private ThreadLocal session = new ThreadLocal();
41      private ThreadLocal application = new ThreadLocal();
42      private ThreadLocal loc = new ThreadLocal();
43    
44      /**
45       * Creates a SingleInstanceBaseAction.
46       */
47    public SingleInstanceBaseAction() { }
48      
49    public void setInput(Input input) {
50      this.input.set(input);
51    }
52    
53    public void setOutput(Output output) {
54      this.output.set(output);
55    }
56    
57    public void setSession(Context session) {
58      this.session.set(session);
59    }
60    
61    public void setApplication(Context application) {
62      this.application.set(application);
63    }
64    
65    public void setLocale(Locale loc) {
66      this.loc.set(loc);
67    }
68      
69    public Input getInput() {
70      return (Input) input.get();
71    }
72    
73    public Output getOutput() {
74      return (Output) output.get();
75    }
76    
77    public Context getSession() {
78      return (Context) session.get();
79    }
80    
81    public Context getApplication() {
82      return (Context) application.get();
83    }
84    
85    public Locale getLocale() {
86      return (Locale) loc.get();
87    }
88  }
89  
90