KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > control > LoginWorker


1 /*
2  * $Id: LoginWorker.java 6011 2005-10-24 21:57:50Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.control;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34 import javax.servlet.jsp.PageContext JavaDoc;
35 import javax.transaction.Transaction JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilFormatOut;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.transaction.GenericTransactionException;
44 import org.ofbiz.entity.transaction.TransactionUtil;
45
46 /**
47  * Common Workers
48  *
49  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
50  * @version $Rev: 6011 $
51  * @since 2.0
52  */

53 public class LoginWorker {
54     
55     public final static String JavaDoc module = LoginWorker.class.getName();
56
57     public static final String JavaDoc EXTERNAL_LOGIN_KEY_ATTR = "externalLoginKey";
58
59     /** This Map is keyed by the randomly generated externalLoginKey and the value is a UserLogin GenericValue object */
60     public static Map JavaDoc externalLoginKeys = new HashMap JavaDoc();
61     
62     public static String JavaDoc makeLoginUrl(PageContext JavaDoc pageContext) {
63         return makeLoginUrl(pageContext, "checkLogin");
64     }
65
66     public static String JavaDoc makeLoginUrl(ServletRequest JavaDoc request) {
67         return makeLoginUrl(request, "checkLogin");
68     }
69     
70     public static String JavaDoc makeLoginUrl(PageContext JavaDoc pageContext, String JavaDoc requestName) {
71         return makeLoginUrl(pageContext.getRequest(), requestName);
72     }
73     public static String JavaDoc makeLoginUrl(ServletRequest JavaDoc request, String JavaDoc requestName) {
74         String JavaDoc queryString = null;
75
76         Enumeration JavaDoc parameterNames = request.getParameterNames();
77
78         while (parameterNames != null && parameterNames.hasMoreElements()) {
79             String JavaDoc paramName = (String JavaDoc) parameterNames.nextElement();
80
81             if (paramName != null) {
82                 if (queryString == null) queryString = paramName + "=" + request.getParameter(paramName);
83                 else queryString = queryString + "&" + paramName + "=" + request.getParameter(paramName);
84             }
85         }
86
87         String JavaDoc loginUrl = "/" + requestName + "/" + UtilFormatOut.checkNull((String JavaDoc) request.getAttribute("_CURRENT_VIEW_"));
88
89         if (queryString != null) loginUrl = loginUrl + "?" + UtilFormatOut.checkNull(queryString);
90
91         return loginUrl;
92     }
93     
94     /**
95      * Gets (and creates if necessary) a key to be used for an external login parameter
96      */

97     public static String JavaDoc getExternalLoginKey(HttpServletRequest JavaDoc request) {
98         //Debug.logInfo("Running getExternalLoginKey, externalLoginKeys.size=" + externalLoginKeys.size(), module);
99
GenericValue userLogin = (GenericValue) request.getAttribute("userLogin");
100
101         String JavaDoc externalKey = (String JavaDoc) request.getAttribute(EXTERNAL_LOGIN_KEY_ATTR);
102         if (externalKey != null) return externalKey;
103
104         HttpSession JavaDoc session = request.getSession();
105         synchronized (session) {
106             // if the session has a previous key in place, remove it from the master list
107
String JavaDoc sesExtKey = (String JavaDoc) session.getAttribute(EXTERNAL_LOGIN_KEY_ATTR);
108             if (sesExtKey != null) {
109                 externalLoginKeys.remove(sesExtKey);
110             }
111
112             //check the userLogin here, after the old session setting is set so that it will always be cleared
113
if (userLogin == null) return "";
114
115             //no key made yet for this request, create one
116
while (externalKey == null || externalLoginKeys.containsKey(externalKey)) {
117                 externalKey = "EL" + Long.toString(Math.round(Math.random() * 1000000)) + Long.toString(Math.round(Math.random() * 1000000));
118             }
119
120             request.setAttribute(EXTERNAL_LOGIN_KEY_ATTR, externalKey);
121             session.setAttribute(EXTERNAL_LOGIN_KEY_ATTR, externalKey);
122             externalLoginKeys.put(externalKey, userLogin);
123             return externalKey;
124         }
125     }
126
127     public static void cleanupExternalLoginKey(HttpSession JavaDoc session) {
128         String JavaDoc sesExtKey = (String JavaDoc) session.getAttribute(EXTERNAL_LOGIN_KEY_ATTR);
129         if (sesExtKey != null) {
130             externalLoginKeys.remove(sesExtKey);
131         }
132     }
133
134     public static void setLoggedOut(String JavaDoc userLoginId, GenericDelegator delegator) {
135         Transaction JavaDoc parentTx = null;
136         boolean beganTransaction = false;
137
138         try {
139             try {
140                 parentTx = TransactionUtil.suspend();
141             } catch (GenericTransactionException e) {
142                 Debug.logError(e, "Cannot suspend current transaction: " + e.getMessage(), module);
143             }
144
145             try {
146                 beganTransaction = TransactionUtil.begin();
147
148                 GenericValue userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId));
149                 userLogin.set("hasLoggedOut", "Y");
150                 userLogin.store();
151             } catch (GenericEntityException e) {
152                 String JavaDoc errMsg = "Unable to set logged out flag on UserLogin";
153                 Debug.logError(e, errMsg, module);
154                 try {
155                     TransactionUtil.rollback(beganTransaction, errMsg, e);
156                 } catch (GenericTransactionException e2) {
157                     Debug.logError(e2, "Could not rollback nested transaction: " + e.getMessage(), module);
158                 }
159             } finally {
160                 try {
161                     TransactionUtil.commit(beganTransaction);
162                 } catch (GenericTransactionException e) {
163                     Debug.logError(e, "Could not commit nested transaction: " + e.getMessage(), module);
164                 }
165             }
166         } finally {
167             // resume/restore parent transaction
168
if (parentTx != null) {
169                 try {
170                     TransactionUtil.resume(parentTx);
171                     Debug.logVerbose("Resumed the parent transaction.", module);
172                 } catch (GenericTransactionException ite) {
173                     Debug.logError(ite, "Cannot resume transaction: " + ite.getMessage(), module);
174                 }
175             }
176         }
177     }
178 }
179
Popular Tags