KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > jacc > JPolicyContextHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JPolicyContextHandler.java,v 1.2 2004/04/19 09:10:25 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.jacc;
28
29 import javax.security.jacc.PolicyContextException JavaDoc;
30 import javax.security.jacc.PolicyContextHandler JavaDoc;
31
32 import org.objectweb.jonas.common.Log;
33
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * JOnAS class which contains all Context Handler to register inside the
39  * PolicyContext
40  * @author Florent Benoit
41  * @see jacc Section 4.6.1
42  */

43 public class JPolicyContextHandler implements PolicyContextHandler JavaDoc {
44
45     /**
46      * Container Subject Policy Context Handler
47      * @see jacc section 4.6.1.1
48      */

49     private static final String JavaDoc CONTAINER_SUBJECT = "javax.security.auth.Subject.container";
50
51     /**
52      * SOAPMessage Policy Context Handler
53      * @see jacc 4.6.1.2
54      */

55     private static final String JavaDoc SOAP_MESSAGE = "javax.xml.soap.SOAPMessage";
56
57     /**
58      * HttpServletRequest Policy Context Handler
59      * @see jacc 4.6.1.3
60      */

61     private static final String JavaDoc HTTP_SERVLET_REQUEST = "javax.servlet.http.HttpServletRequest";
62
63     /**
64      * EnterpriseBean Policy Context Handler
65      * @see jacc 4.6.1.4
66      */

67     private static final String JavaDoc ENTERPRISE_BEAN = "javax.ejb.EnterpriseBean";
68
69     /**
70      * EJB Arguments Policy Context Handler
71      * @see jacc 4.6.1.5
72      */

73     private static final String JavaDoc EJB_ARGUMENTS = "javax.ejb.arguments";
74
75     /**
76      * List of supported Context Handler
77      */

78     private static final String JavaDoc[] SUPPORTED_KEYS = new String JavaDoc[] {CONTAINER_SUBJECT, SOAP_MESSAGE, HTTP_SERVLET_REQUEST,
79             ENTERPRISE_BEAN, EJB_ARGUMENTS};
80
81     /**
82      * Logger
83      */

84     private static Logger logger = Log.getLogger(Log.JONAS_JACC_SECURITY_PREFIX);
85
86     /**
87      * Default constructor
88      */

89     public JPolicyContextHandler() {
90         super();
91
92     }
93
94     /**
95      * @param key the supported key
96      * @return true if the key is supported, else false.
97      * @throws PolicyContextException (never used here)
98      * @see javax.security.jacc.PolicyContextHandler#supports(java.lang.String)
99      */

100     public boolean supports(String JavaDoc key) throws PolicyContextException JavaDoc {
101         for (int k = 0; k < SUPPORTED_KEYS.length; k++) {
102             if (key.equals(SUPPORTED_KEYS[k])) {
103                 return true;
104             }
105         }
106         return false;
107     }
108
109     /**
110      * Supported keys by this JOnAS Context Handler
111      * @see javax.security.jacc.PolicyContextHandler#getKeys()
112      */

113     public String JavaDoc[] getKeys() throws PolicyContextException JavaDoc {
114         return SUPPORTED_KEYS;
115     }
116
117     /**
118      * @param key the key for the object that we want to retrieve
119      * @param data the handler data given by PolicyContext class
120      * @return the object for the specified key and with the given data object
121      * @throws PolicyContextException (never used)
122      * @see javax.security.jacc.PolicyContextHandler#getContext(java.lang.String,
123      * java.lang.Object)
124      */

125     public Object JavaDoc getContext(String JavaDoc key, Object JavaDoc data) throws PolicyContextException JavaDoc {
126
127         if (logger.isLoggable(BasicLevel.DEBUG)) {
128             logger.log(BasicLevel.DEBUG, "Asking key" + key);
129         }
130
131         // Handle only JOnAS HandleData
132
if (data == null || !(data instanceof JPolicyContextHandlerData)) {
133             logger.log(BasicLevel.WARN, "No data object or Data object not instance of JPolicyContextHandlerData");
134             return null;
135         }
136
137         Object JavaDoc contextObject = null;
138         JPolicyContextHandlerData jPolicyContextHandlerData = (JPolicyContextHandlerData) data;
139
140         if (key.equals(HTTP_SERVLET_REQUEST)) {
141             if (logger.isLoggable(BasicLevel.DEBUG)) {
142                 logger.log(BasicLevel.DEBUG, "Key == '" + HTTP_SERVLET_REQUEST + "'");
143             }
144             contextObject = jPolicyContextHandlerData.getHttpServletRequest();
145         } else if (key.equals(EJB_ARGUMENTS)) {
146             if (logger.isLoggable(BasicLevel.DEBUG)) {
147                 logger.log(BasicLevel.DEBUG, "Key == '" + EJB_ARGUMENTS + "'");
148             }
149             contextObject = jPolicyContextHandlerData.getEjbArguments();
150         } else if (key.equals(CONTAINER_SUBJECT)) {
151             if (logger.isLoggable(BasicLevel.DEBUG)) {
152                 logger.log(BasicLevel.DEBUG, "Key == '" + CONTAINER_SUBJECT + "'");
153             }
154             contextObject = jPolicyContextHandlerData.getContainerSubject();
155
156         }
157
158         if (logger.isLoggable(BasicLevel.DEBUG)) {
159             logger.log(BasicLevel.DEBUG, "Returning object '" + contextObject + "' for key '" + key + "'");
160         }
161         return contextObject;
162
163     }
164
165 }
Popular Tags