View Javadoc

1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program 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
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace;
20  
21  import javax.mail.Folder;
22  import javax.mail.MessagingException;
23  import javax.mail.Store;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /***
29   * This class represents the state of a single Wallace server session.
30   * 
31   * An instance of this class is attached to each session and provides access to
32   * the associated user, currently selected Folder and server configuration.
33   * 
34   * @author rnewson
35   */
36  public final class WallaceSession {
37  
38      private WallaceSessionState state = WallaceSessionState.NOT_AUTHENTICATED;
39  
40      private WallaceUser user;
41  
42      private Store store;
43  
44      private Folder selectedFolder;
45  
46      private WallaceServerConfig serverConfig;
47  
48      private SessionHandler sessionHandler;
49  
50      private final Log log = LogFactory.getLog(getClass());
51  
52      public WallaceSession() {
53          // Empty.
54      }
55  
56      public WallaceSessionState getState() {
57          return state;
58      }
59  
60      public void setState(final WallaceSessionState newState) {
61          assert newState != null : "State cannot be set to null.";
62          state = newState;
63      }
64  
65      public void setUser(final WallaceUser newUser) {
66          assert newUser != null : "User cannot be set to null.";
67          user = newUser;
68      }
69  
70      public WallaceUser getUser() {
71          assert user != null : "User should not be null.";
72          return user;
73      }
74  
75      public void setStore(final Store newStore) {
76          assert newStore != null : "Store cannot be set to null.";
77          store = newStore;
78      }
79  
80      public Store getStore() {
81          assert store != null : "Store should not be null.";
82          return store;
83      }
84  
85      /***
86       * Set the currently selected Folder for this session.
87       * 
88       * @return
89       */
90      public void setSelectedFolder(final Folder newFolder) {
91          selectedFolder = newFolder;
92      }
93  
94      /***
95       * Get the currently selected Folder for this session.
96       * 
97       * @return
98       */
99      public Folder getSelectedFolder() {
100         return selectedFolder;
101     }
102 
103     public void setServerConfig(final WallaceServerConfig newServerConfig) {
104         assert newServerConfig != null : "ServerConfig cannot be set to null.";
105         serverConfig = newServerConfig;
106     }
107 
108     public WallaceServerConfig getServerConfig() {
109         assert serverConfig != null : "serverConfig cannot to be null.";
110         return serverConfig;
111     }
112 
113     public void setSessionHandler(final SessionHandler newSessionHandler) {
114         assert newSessionHandler != null : "SessionHandler cannot be set to null.";
115         sessionHandler = newSessionHandler;
116     }
117 
118     public void respond(final ServerResponse serverResponse) {
119         assert sessionHandler != null : "SessionHandler cannot be null.";
120         sessionHandler.respond(serverResponse);
121     }
122 
123     public void close() {
124         if (store != null) {
125             try {
126                 store.close();
127             } catch (final MessagingException e) {
128                 log.warn("Error while closing store.", e);
129             }
130         }
131 
132         assert sessionHandler != null : "SessionHandler cannot be null.";
133         sessionHandler.close();
134     }
135 
136 }