KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > atlassian > seraph > config > SecurityConfigImpl


1 package com.atlassian.seraph.config;
2
3 import com.atlassian.seraph.auth.RoleMapper;
4 import com.atlassian.seraph.auth.Authenticator;
5 import com.atlassian.seraph.auth.AuthenticationContext;
6 import com.atlassian.seraph.auth.AuthenticationContextImpl;
7 import com.atlassian.seraph.SecurityService;
8 import com.atlassian.seraph.Initable;
9 import com.atlassian.seraph.util.XMLUtils;
10 import com.atlassian.seraph.controller.SecurityController;
11 import com.atlassian.seraph.interceptor.Interceptor;
12 import com.opensymphony.util.ClassLoaderUtil;
13 import org.apache.log4j.Category;
14 import org.w3c.dom.Element JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.*;
22
23 /**
24  * The main class of Seraph's configuration.
25  * <p>
26  * This class is a Singleton, access it using SecurityConfigFactory.getInstance().
27  */

28 public class SecurityConfigImpl implements Serializable JavaDoc, SecurityConfig
29 {
30     private static final Category log = Category.getInstance(SecurityConfigImpl.class);
31     private static SecurityConfigImpl instance = null;
32
33     public static final String JavaDoc DEFAULT_CONFIG_LOCATION = "seraph-config.xml";
34
35     private String JavaDoc configFileLocation = "seraph-config.xml";
36     protected Authenticator authenticator = null;
37     protected RoleMapper roleMapper = null;
38     protected SecurityController controller;
39     protected List JavaDoc services = null;
40     protected List JavaDoc interceptors = null;
41
42     private String JavaDoc loginURL;
43     private String JavaDoc logoutURL;
44     private String JavaDoc originalURLKey = "seraph_originalurl";
45     private String JavaDoc cookieEncoding;
46     private String JavaDoc loginCookieKey = "seraph.os.cookie";
47     private String JavaDoc linkLoginURL;
48
49     public SecurityConfigImpl(String JavaDoc configFileLocation) throws ConfigurationException
50     {
51         if (configFileLocation != null)
52         {
53             this.configFileLocation = configFileLocation;
54             log.debug("Config file location passed. Location: " + this.configFileLocation);
55         }
56         else
57         {
58             log.debug("Initialising securityConfig using default configFile: " + this.configFileLocation);
59         }
60
61         init();
62     }
63
64     private void init() throws ConfigurationException
65     {
66         services = new ArrayList();
67         interceptors = new ArrayList();
68
69         try
70         {
71             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
72             URL JavaDoc fileUrl = ClassLoaderUtil.getResource(configFileLocation, this.getClass());
73
74             if (fileUrl == null)
75                 throw new IllegalArgumentException JavaDoc("No such XML file: " + configFileLocation);
76
77             // Parse document
78
org.w3c.dom.Document JavaDoc doc = factory.newDocumentBuilder().parse(fileUrl.toString());
79             Element JavaDoc rootEl = doc.getDocumentElement();
80
81             configureParameters(rootEl);
82             configureAuthenticator(rootEl);
83             configureController(rootEl);
84             configureRoleMapper(rootEl);
85             configureServices(rootEl);
86             configureInterceptors(rootEl);
87         }
88         catch (Exception JavaDoc e)
89         {
90             e.printStackTrace();
91             throw new ConfigurationException("Exception configuring from '"+configFileLocation+"': " + e);
92         }
93     }
94
95     protected void configureAuthenticator(Element JavaDoc rootEl) throws ConfigurationException
96     {
97         authenticator = (Authenticator) configureClass(rootEl, "authenticator");
98
99         try
100         {
101             if (authenticator == null)
102             {
103                 authenticator = (Authenticator) ClassLoaderUtil.loadClass(Authenticator.DEFAULT_AUTHENTICATOR, this.getClass()).newInstance();
104                 authenticator.init(new HashMap(), this);
105             }
106         }
107         catch (Exception JavaDoc e)
108         {
109             throw new ConfigurationException("Could not lookup class: " + Authenticator.DEFAULT_AUTHENTICATOR + ":" + e);
110         }
111     }
112
113     protected void configureController(Element JavaDoc rootEl) throws ConfigurationException
114     {
115         controller = (SecurityController) configureClass(rootEl, "controller");
116
117         try
118         {
119             if (controller == null)
120                 controller = (SecurityController) ClassLoaderUtil.loadClass(SecurityController.NULL_CONTROLLER, this.getClass()).newInstance();
121         }
122         catch (Exception JavaDoc e)
123         {
124             throw new ConfigurationException("Could not lookup class: " + SecurityController.NULL_CONTROLLER + ":" + e);
125         }
126     }
127
128     protected void configureRoleMapper(Element JavaDoc rootEl) throws ConfigurationException
129     {
130         roleMapper = (RoleMapper) configureClass(rootEl, "rolemapper");
131     }
132
133     private Initable configureClass(Element JavaDoc rootEl, String JavaDoc tagname) throws ConfigurationException
134     {
135         try
136         {
137             NodeList JavaDoc elementList = rootEl.getElementsByTagName(tagname);
138
139             for (int i = 0; i < elementList.getLength(); i++)
140             {
141                 Element JavaDoc authEl = (Element JavaDoc) elementList.item(i);
142                 String JavaDoc clazz = authEl.getAttribute("class");
143
144                 Initable initable = (Initable) ClassLoaderUtil.loadClass(clazz, this.getClass()).newInstance();
145
146                 Map params = getInitParameters(authEl);
147
148                 initable.init(params, this);
149                 return initable;
150             }
151         }
152         catch (Exception JavaDoc e)
153         {
154             throw new ConfigurationException("Could not create: " + tagname + ": " + e);
155         }
156
157         return null;
158     }
159
160     private void configureParameters(Element JavaDoc rootEl)
161     {
162         NodeList JavaDoc nl = rootEl.getElementsByTagName("parameters");
163         Element JavaDoc parametersEl = (Element JavaDoc) nl.item(0);
164         Map globalParams = getInitParameters(parametersEl);
165
166         loginURL = (String JavaDoc) globalParams.get("login.url");
167         linkLoginURL = (String JavaDoc) globalParams.get("link.login.url");
168         logoutURL = (String JavaDoc) globalParams.get("logout.url");
169
170         if (globalParams.get("original.url.key") != null)
171             originalURLKey = (String JavaDoc) globalParams.get("original.url.key");
172
173         if (globalParams.get("cookie.encoding") != null)
174             cookieEncoding = (String JavaDoc) globalParams.get("cookie.encoding");
175
176         if (globalParams.get("login.cookie.key") != null)
177             loginCookieKey = (String JavaDoc) globalParams.get("login.cookie.key");
178     }
179
180     private void configureServices(Element JavaDoc rootEl) throws ConfigurationException
181     {
182         NodeList JavaDoc nl = rootEl.getElementsByTagName("services");
183
184         if (nl != null && nl.getLength() > 0)
185         {
186             Element JavaDoc servicesEl = (Element JavaDoc) nl.item(0);
187             NodeList JavaDoc serviceList = servicesEl.getElementsByTagName("service");
188
189             for (int i = 0; i < serviceList.getLength(); i++)
190             {
191                 Element JavaDoc serviceEl = (Element JavaDoc) serviceList.item(i);
192                 String JavaDoc serviceClazz = serviceEl.getAttribute("class");
193
194                 if (serviceClazz == null || "".equals(serviceClazz))
195                     throw new ConfigurationException("Service element with bad class attribute");
196
197                 try
198                 {
199                     log.debug("Adding seraph service of class: " + serviceClazz);
200                     SecurityService service = (SecurityService) ClassLoaderUtil.loadClass(serviceClazz, this.getClass()).newInstance();
201
202                     Map serviceParams = getInitParameters(serviceEl);
203
204                     service.init(serviceParams, this);
205
206                     services.add(service);
207                 }
208                 catch (Exception JavaDoc e)
209                 {
210                     e.printStackTrace();
211                     throw new ConfigurationException("Could not getRequest service: " + serviceClazz + ". Exception: " + e);
212                 }
213             }
214         }
215     }
216
217     protected void configureInterceptors(Element JavaDoc rootEl) throws ConfigurationException
218     {
219         NodeList JavaDoc nl = rootEl.getElementsByTagName("interceptors");
220
221         if (nl != null && nl.getLength() > 0)
222         {
223             Element JavaDoc interceptorsEl = (Element JavaDoc) nl.item(0);
224             NodeList JavaDoc interceptorList = interceptorsEl.getElementsByTagName("interceptor");
225
226             for (int i = 0; i < interceptorList.getLength(); i++)
227             {
228                 Element JavaDoc interceptorEl = (Element JavaDoc) interceptorList.item(i);
229                 String JavaDoc interceptorClazz = interceptorEl.getAttribute("class");
230
231                 if (interceptorClazz == null || "".equals(interceptorClazz))
232
233                     throw new ConfigurationException("Interceptor element with bad class attribute");
234
235                 try
236                 {
237                     log.debug("Adding interceptor of class: " + interceptorClazz);
238                     Interceptor interceptor = (Interceptor) ClassLoaderUtil.loadClass(interceptorClazz, this.getClass()).newInstance();
239
240                     Map interceptorParams = getInitParameters(interceptorEl);
241
242                     interceptor.init(interceptorParams, this);
243
244                     interceptors.add(interceptor);
245                 }
246                 catch (Exception JavaDoc e)
247                 {
248                     e.printStackTrace();
249                     throw new ConfigurationException("Could not getRequest service: " + interceptorClazz + ". Exception: " + e);
250                 }
251             }
252         }
253     }
254
255     protected Map getInitParameters(Element JavaDoc el)
256     {
257         Map params = new HashMap();
258
259         NodeList JavaDoc nl = el.getElementsByTagName("init-param");
260
261         for (int i = 0; i < nl.getLength(); i++)
262         {
263             Node JavaDoc initParam = nl.item(i);
264             String JavaDoc paramName = XMLUtils.getContainedText(initParam, "param-name");
265             String JavaDoc paramValue = XMLUtils.getContainedText(initParam, "param-value");
266             params.put(paramName, paramValue);
267         }
268
269         return params;
270     }
271
272     public void destroy()
273     {
274         for (Iterator iterator = services.iterator(); iterator.hasNext();)
275         {
276             SecurityService securityService = (SecurityService) iterator.next();
277             securityService.destroy();
278         }
279
280         for (Iterator iterator = interceptors.iterator(); iterator.hasNext();)
281         {
282             ((Interceptor) iterator.next()).destroy();
283         }
284
285         return;
286     }
287
288     public void addInterceptor(Interceptor interceptor)
289     {
290         interceptors.add(interceptor);
291     }
292     
293     public List JavaDoc getServices()
294     {
295         return services;
296     }
297
298     public String JavaDoc getLoginURL()
299     {
300         return loginURL;
301     }
302
303     public String JavaDoc getLinkLoginURL()
304     {
305         return linkLoginURL;
306     }
307
308     public String JavaDoc getLogoutURL()
309     {
310         return logoutURL;
311     }
312
313     public String JavaDoc getOriginalURLKey()
314     {
315         return originalURLKey;
316     }
317
318     public Authenticator getAuthenticator()
319     {
320         return authenticator;
321     }
322
323     public AuthenticationContext getAuthenticationContext()
324     {
325         return new AuthenticationContextImpl(); //todo - load this from a config file
326
}
327
328     public SecurityController getController()
329     {
330         return controller;
331     }
332
333     /** Instantiates a SecurityConfigImpl. If you just want a {@link SecurityConfig}, use
334      * {@link SecurityConfigFactory#getInstance(java.lang.String)} instead.
335      */

336     public static SecurityConfigImpl getInstance(String JavaDoc configFileLocation) throws ConfigurationException
337     {
338         instance = new SecurityConfigImpl(configFileLocation);
339         return instance;
340     }
341
342     /** Instantiates a SecurityConfigImpl using the default config file.
343      * If you just want a {@link SecurityConfig}, use
344      * {@link SecurityConfigFactory#getInstance()} instead.
345      */

346     public static SecurityConfig getInstance()
347     {
348         if (instance == null)
349         {
350             try
351             {
352                 if (instance == null)
353                 {
354                     instance = new SecurityConfigImpl(DEFAULT_CONFIG_LOCATION);
355                 }
356             }
357             catch (ConfigurationException e)
358             {
359                 log.error("Could not configure SecurityConfigImpl instance: " + e, e);
360             }
361         }
362
363         return instance;
364     }
365
366     public RoleMapper getRoleMapper()
367     {
368         return roleMapper;
369     }
370
371     public List JavaDoc getInterceptors(Class JavaDoc desiredInterceptorClass)
372     {
373         List JavaDoc result = new ArrayList();
374
375         for (Iterator iterator = interceptors.iterator(); iterator.hasNext();)
376         {
377             Interceptor interceptor = (Interceptor) iterator.next();
378
379             if (desiredInterceptorClass.isAssignableFrom(interceptor.getClass()))
380             {
381                 result.add(interceptor);
382             }
383         }
384
385         return result;
386     }
387
388     public String JavaDoc getCookieEncoding()
389     {
390         return cookieEncoding;
391     }
392
393     public String JavaDoc getLoginCookieKey()
394     {
395         return loginCookieKey;
396     }
397
398 }
399
Popular Tags