KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > sudo > Sudo


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Sudo.java
20  *
21  * The object responsible for running code as a specified user.
22  */

23
24 // package path
25
package com.rift.coad.lib.security.sudo;
26
27 // log 4 j imports
28
import org.apache.log4j.Logger;
29
30 // coadunation imports
31
import com.rift.coad.lib.configuration.Configuration;
32 import com.rift.coad.lib.configuration.ConfigurationFactory;
33 import com.rift.coad.lib.security.AuthorizationException;
34 import com.rift.coad.lib.security.UserSession;
35 import com.rift.coad.lib.security.ThreadPermissionSession;
36 import com.rift.coad.lib.security.ThreadsPermissionContainer;
37 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor;
38 import com.rift.coad.lib.security.Validator;
39 import com.rift.coad.lib.security.user.UserSessionManager;
40 import com.rift.coad.lib.security.user.UserSessionManagerAccessor;
41 import com.rift.coad.lib.security.user.UserStoreManager;
42 import com.rift.coad.lib.security.user.UserStoreManagerAccessor;
43 import com.rift.coad.lib.thread.BasicThread;
44
45
46 /**
47  * The object responsible for running code as a specified user.
48  *
49  * @author Brett Chaldecott
50  */

51 public class Sudo {
52     
53     // class constants
54
private final static String JavaDoc ROLE = "role";
55     
56     // static member variables
57
private static Logger log =
58         Logger.getLogger(Sudo.class.getName());
59     private static String JavaDoc role = null;
60     
61     // setup the role
62
static {
63         try {
64             Configuration configuration =
65                     ConfigurationFactory.getInstance().getConfig(Sudo.class);
66             role = configuration.getString(ROLE);
67         } catch (Exception JavaDoc ex) {
68             log.error("Failed to retrieve the sudo role : " + ex.getMessage(),ex);
69         }
70     }
71     
72     
73     /**
74      * Creates a new instance of Sudo
75      */

76     private Sudo() {
77     }
78     
79     
80     /**
81      * This method will get called to run a thread as another user.
82      *
83      * @param username The name of the user to run the handler as.
84      * @param handler The reference to the object that will be called after the
85      * user has been set correctly.
86      * @exception SudoException
87      * @exception Exception
88      */

89     public static void sudoThreadByUser (String JavaDoc username,
90             SudoCallbackHandler handler) throws SudoException, Exception JavaDoc {
91         Validator.validate(Sudo.class,role);
92         ThreadsPermissionContainer threadsPermissionContainer =
93                     ThreadsPermissionContainerAccessor.getInstance().
94                     getThreadsPermissionContainer();
95         UserStoreManager userStoreManager =
96                     UserStoreManagerAccessor.getInstance().
97                     getUserStoreManager();
98         
99         // retrieve the use session information
100
Thread JavaDoc currentThread = null;
101         ThreadPermissionSession currentPermissions = null;
102         UserSession newUserSession = null;
103         try {
104             // retrieve the current user session
105
currentThread = Thread.currentThread();
106             currentPermissions = threadsPermissionContainer.getSession(
107                     new Long JavaDoc(currentThread.getId()));
108             newUserSession = userStoreManager.getUserInfo(username);
109         } catch (Exception JavaDoc ex) {
110             throw new SudoException(
111                     "Failed to retrieve the necessary user information : " +
112                     ex.getMessage(),ex);
113         }
114         
115         // set user
116
threadsPermissionContainer.putSession(new Long JavaDoc(currentThread.getId()),
117                 new ThreadPermissionSession(
118                 new Long JavaDoc(currentThread.getId()),newUserSession));
119         log.info("Set [" + currentThread.getId() + "] user from [" +
120                 currentPermissions.getUser().getName() + "] to [" +
121                 newUserSession.getName() + "] to run the command on : " +
122                 handler.getClass().getName());
123         
124         try {
125             handler.process();
126         } finally {
127             // reset the user session
128
threadsPermissionContainer.putSession(new Long JavaDoc(currentThread.getId()),
129                     currentPermissions);
130             // set the user back
131
log.info("Set user back from [" + newUserSession.getName() +
132                     "] to [" + currentPermissions.getUser().getName() +
133                     "] after running command on : " + handler.getClass().getName());
134         }
135     }
136     
137     
138     /**
139      * This method will sudo a user to a user session id.
140      *
141      * @param sessionId The id of the session to sudo.
142      * @param handler The reference to the handler.
143      * @exception SudoException
144      * @exception Exception
145      */

146     public static void sudoThreadBySessionId(String JavaDoc sessionId,
147             SudoCallbackHandler handler) throws SudoException, Exception JavaDoc {
148         Validator.validate(Sudo.class,role);
149         ThreadsPermissionContainer threadsPermissionContainer =
150                     ThreadsPermissionContainerAccessor.getInstance().
151                     getThreadsPermissionContainer();
152         UserSessionManager userSessionManager =
153                 UserSessionManagerAccessor.getInstance().
154                 getUserSessionManager();
155         // retrieve the use session information
156
Thread JavaDoc currentThread = null;
157         ThreadPermissionSession currentPermissions = null;
158         UserSession newUserSession = null;
159         try {
160             // retrieve the current user session
161
currentThread = Thread.currentThread();
162             currentPermissions = threadsPermissionContainer.getSession(
163                     new Long JavaDoc(currentThread.getId()));
164             newUserSession = userSessionManager.getSessionById(sessionId);
165         } catch (Exception JavaDoc ex) {
166             throw new SudoException(
167                     "Failed to retrieve the necessary user information : " +
168                     ex.getMessage(),ex);
169         }
170         
171         // set user
172
threadsPermissionContainer.putSession(new Long JavaDoc(currentThread.getId()),
173                 new ThreadPermissionSession(
174                 new Long JavaDoc(currentThread.getId()),newUserSession));
175         
176         log.info("Set [" + currentThread.getId() + "] user from ["
177                 + currentPermissions.getUser().getName() + "] to [" +
178                 newUserSession.getName() + "] to run the command on : " +
179                 handler.getClass().getName());
180         
181         try {
182             handler.process();
183         } finally {
184             // reset the user session
185
threadsPermissionContainer.putSession(new Long JavaDoc(currentThread.getId()),
186                     currentPermissions);
187             
188             // set the user back
189
log.info("Set user back from [" + newUserSession.getName() +
190                     "] to [" + currentPermissions.getUser().getName() +
191                     "] after running command on : " + handler.getClass().getName());
192         }
193     }
194 }
195
Popular Tags