KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > lib > wrapper > JResourceManagerWrapper


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * --------------------------------------------------------------------------
22  * $Id: JResourceManagerWrapper.java,v 1.1 2004/05/28 14:02:57 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.security.lib.wrapper;
26
27 import java.io.Reader JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import org.objectweb.jonas.security.JResources;
32 import org.objectweb.jonas.security.SecurityServiceException;
33 import org.objectweb.jonas.server.LoaderManager;
34
35 /**
36  * @author Guillaume Sauthier
37  */

38 public class JResourceManagerWrapper {
39
40     /**
41      * JResourcesManager instance
42      */

43     private static Object JavaDoc managerInstance = null;
44
45     /**
46      *
47      */

48     private static Method JavaDoc addResourcesMethod = null;
49
50     /**
51      * Empty private constructor for Utility Classes
52      */

53     private JResourceManagerWrapper() {
54     }
55
56     /**
57      * Add JResource(s) created from parsing of the reader contents inside the
58      * JResources given instance.
59      * @param jres JResources element where JResource will be addded.
60      * @param reader XML Content Reader
61      * @param xml filename / xml : used in Error messages
62      * @throws SecurityServiceException When parsing fails
63      */

64     public static void addResources(JResources jres, Reader JavaDoc reader, String JavaDoc xml) throws SecurityServiceException {
65         if (managerInstance == null) {
66             managerInstance = getJResourceManagerInstance();
67         }
68
69         if (addResourcesMethod == null) {
70             addResourcesMethod = getJResourceManagerMethod();
71         }
72
73         try {
74             addResourcesMethod.invoke(managerInstance, new Object JavaDoc[] {jres, reader, xml});
75         } catch (InvocationTargetException JavaDoc e) {
76             Throwable JavaDoc t = e.getTargetException();
77             if (t instanceof SecurityServiceException) {
78                 throw (SecurityServiceException) t;
79             } else if (t instanceof Error JavaDoc) {
80                 throw (Error JavaDoc) t;
81             }
82             throw new SecurityServiceException("Exception during JResourcesManager.addResources invocation", e);
83         } catch (Exception JavaDoc e) {
84             throw new SecurityServiceException("Exception during JResourcesManager.addResources invocation", e);
85         }
86     }
87
88     /**
89      * @return Returns the JResourceManager.addResources Method
90      * @throws SecurityServiceException When addResources invocation fails
91      */

92     private static Method JavaDoc getJResourceManagerMethod() throws SecurityServiceException {
93         try {
94             return managerInstance.getClass().getMethod("addResources",
95                     new Class JavaDoc[] {JResources.class, Reader JavaDoc.class, String JavaDoc.class});
96         } catch (Exception JavaDoc e) {
97             throw new SecurityServiceException("Cannot get JResourcesManager.addResources method", e);
98         }
99     }
100
101     /**
102      * @return Returns the JResourceManager unique instance.
103      * @throws SecurityServiceException When getInstance invocation fails
104      */

105     private static Object JavaDoc getJResourceManagerInstance() throws SecurityServiceException {
106
107         LoaderManager lm = LoaderManager.getInstance();
108         try {
109             ClassLoader JavaDoc tools = lm.getToolsLoader();
110
111             Class JavaDoc jrmClass = tools.loadClass("org.objectweb.jonas.security.lib.JResourceManager");
112             Method JavaDoc m = jrmClass.getMethod("getInstance", new Class JavaDoc[] {});
113             return m.invoke(null, new Object JavaDoc[] {});
114         } catch (InvocationTargetException JavaDoc e) {
115             Throwable JavaDoc t = e.getTargetException();
116             if (t instanceof SecurityServiceException) {
117                 throw (SecurityServiceException) t;
118             } else if (t instanceof Error JavaDoc) {
119                 throw (Error JavaDoc) t;
120             }
121             throw new SecurityServiceException("InvocationTargetException during JResourcesManager.getInstance invocation : " + e.getMessage(), e);
122         } catch (Exception JavaDoc e) {
123             throw new SecurityServiceException("Exception during JResourcesManager.getInstance invocation : " + e.getMessage(), e);
124         }
125     }
126
127 }
Popular Tags