KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > context > AuthenticationContextProvider


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.authentication.context;
17
18 import org.apache.avalon.framework.component.Component;
19 import org.apache.avalon.framework.logger.AbstractLogEnabled;
20 import org.apache.avalon.framework.service.ServiceException;
21 import org.apache.avalon.framework.service.ServiceManager;
22 import org.apache.avalon.framework.service.Serviceable;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.webapps.authentication.AuthenticationConstants;
26 import org.apache.cocoon.webapps.authentication.AuthenticationManager;
27 import org.apache.cocoon.webapps.authentication.user.RequestState;
28 import org.apache.cocoon.webapps.authentication.user.UserHandler;
29 import org.apache.cocoon.webapps.session.context.SessionContext;
30 import org.apache.cocoon.webapps.session.context.SessionContextProvider;
31
32
33 /**
34  * Context provider for the authentication context
35  *
36  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
37  * @version CVS $Id: AuthenticationContextProvider.java 30932 2004-07-29 17:35:38Z vgritsenko $
38 */

39 public final class AuthenticationContextProvider
40 extends AbstractLogEnabled
41 implements SessionContextProvider, ThreadSafe, Component, Serviceable {
42
43     protected ServiceManager manager;
44     
45     /**
46      * Get the context
47      * @param name The name of the context
48      * @return The context
49      * @throws ProcessingException If the context is not available.
50      */

51     public SessionContext getSessionContext(String JavaDoc name)
52     throws ProcessingException {
53         AuthenticationContext context = null;
54         if (name.equals(AuthenticationConstants.SESSION_CONTEXT_NAME) ) {
55             
56             AuthenticationManager authManager = null;
57             RequestState state = null;
58             try {
59                 authManager = (AuthenticationManager)this.manager.lookup(AuthenticationManager.ROLE);
60                 state = authManager.getState();
61             } catch (ServiceException ignore) {
62             } finally {
63                 this.manager.release( authManager );
64             }
65             
66             if ( null != state ) {
67                 UserHandler handler = state.getHandler();
68                 if ( handler != null ) {
69                     context = handler.getContext();
70                 }
71             }
72         }
73         return context;
74     }
75
76     /**
77      * Does the context exist?
78      */

79     public boolean existsSessionContext(String JavaDoc name)
80     throws ProcessingException {
81         return (this.getSessionContext( name ) != null);
82     }
83
84     /* (non-Javadoc)
85      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
86      */

87     public void service(ServiceManager manager) throws ServiceException {
88         this.manager = manager;
89     }
90
91 }
92
Popular Tags