KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > acting > SessionAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.session.acting;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.acting.ServiceableAction;
25 import org.apache.cocoon.environment.Redirector;
26 import org.apache.cocoon.environment.SourceResolver;
27 import org.apache.cocoon.webapps.session.SessionManager;
28
29 /**
30  * This action creates and terminates a session.
31  * The action is controlled via parameters. The action parameter defines
32  * the action (creating or terminating).
33  * The value "create" creates a new session (if not already available)
34  * The value "terminate" terminates the session. The termination can be controlled
35  * with a second parameter "mode": The default value "immediately" terminates
36  * the session, the value "if-unused" terminates the session only if no
37  * session context is available anymore. This means the user must not have
38  * any own session context and must not be authenticated anymore using
39  * the uthentication framework.
40  *
41  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
42  * @version CVS $Id: SessionAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
43 */

44 public final class SessionAction
45 extends ServiceableAction
46 implements ThreadSafe {
47
48     public Map JavaDoc act(Redirector redirector,
49                    SourceResolver resolver,
50                    Map JavaDoc objectModel,
51                    String JavaDoc source,
52                    Parameters par)
53     throws ProcessingException {
54         SessionManager sessionManager = null;
55         try {
56             sessionManager = (SessionManager)this.manager.lookup(SessionManager.ROLE);
57             final String JavaDoc action = par.getParameter("action", "create");
58           
59             if ( action.equals("create") ) {
60                 
61                 // create a session
62
sessionManager.createSession();
63                 
64             } else if ( action.equals("terminate") ) {
65                 
66                 // terminate a session
67
final String JavaDoc mode = par.getParameter("mode", "immediately");
68                 if ( mode.equals("immediately") ) {
69                     sessionManager.terminateSession(true);
70                 } else if ( mode.equals("if-unused") ) {
71                     sessionManager.terminateSession(false);
72                 } else {
73                     throw new ProcessingException("Unknown mode " + mode + " for action " + action);
74                 }
75                 
76             } else {
77                 throw new ProcessingException("Unknown action: " + action);
78             }
79         } catch (ServiceException ce) {
80             throw new ProcessingException("Error during lookup of sessionManager component.", ce);
81         } finally {
82             this.manager.release( sessionManager );
83         }
84
85         return EMPTY_MAP;
86     }
87
88 }
89
Popular Tags