Source Code Cross Referenced for LoginModuleUtils.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » auth » module » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.auth.module 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.auth.module;
019:
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:        import java.util.Arrays;
023:
024:        import javax.security.auth.login.LoginException;
025:
026:        public class LoginModuleUtils {
027:
028:            public static enum ACTION {
029:                no_action, login, commit, logout
030:            };
031:
032:            public final static class LoginModuleStatus {
033:
034:                private static enum PHASE {
035:                    uninitialized, initialized, logined, committed
036:                };
037:
038:                private PHASE phase;
039:
040:                public LoginModuleStatus() {
041:                    phase = PHASE.uninitialized;
042:                }
043:
044:                public void initialized() {
045:                    phase = PHASE.initialized;
046:                }
047:
048:                public void logined() {
049:                    phase = PHASE.logined;
050:                }
051:
052:                public void committed() {
053:                    phase = PHASE.committed;
054:                }
055:
056:                public void logouted() {
057:                    phase = PHASE.logined;
058:                }
059:
060:                public boolean isLoggined() {
061:                    return phase.equals(PHASE.logined)
062:                            || phase.equals(PHASE.committed);
063:                }
064:
065:                public boolean isCommitted() {
066:                    return phase.equals(PHASE.committed);
067:                }
068:
069:                public ACTION checkAbout() {
070:                    switch (phase) {
071:                    case uninitialized:
072:                    case initialized:
073:                        return ACTION.no_action;
074:                    default:
075:                        return ACTION.logout;
076:                    }
077:                }
078:
079:                public ACTION checkLogin() throws LoginException {
080:                    switch (phase) {
081:                    case uninitialized:
082:                        throw new LoginException(
083:                                "Login Module is not initialized.");
084:                    case initialized:
085:                        return ACTION.login;
086:                    default:
087:                        return ACTION.no_action;
088:                    }
089:                }
090:
091:                public ACTION checkCommit() throws LoginException {
092:                    switch (phase) {
093:                    case uninitialized:
094:                        throw new LoginException(
095:                                "Login Module is not initialized.");
096:                    case initialized:
097:                        return ACTION.logout;
098:                    case logined:
099:                        return ACTION.commit;
100:                    default:
101:                        return ACTION.no_action;
102:                    }
103:                }
104:
105:                public ACTION checkLogout() throws LoginException {
106:                    switch (phase) {
107:                    case uninitialized:
108:                        throw new LoginException(
109:                                "Login Module is not initialized.");
110:                    case initialized:
111:                    case logined:
112:                        return ACTION.no_action;
113:                    default:
114:                        return ACTION.logout;
115:                    }
116:                }
117:            }
118:
119:            /**
120:             * Reads the password stored in an inputstream to a char array.
121:             * 
122:             * @param in
123:             *            an inputstream which stores the password.
124:             * @return a char array which contains the password.
125:             * @throws IOException
126:             */
127:            public static char[] getPassword(InputStream in) throws IOException {
128:                char[] buffer = new char[512];
129:
130:                // 1.Just ASCII encoding is supported. bytes read from inputstream is
131:                // cast and put into char array.
132:                // 2.just read one line.
133:                int length = 0;
134:                int nextChar = -1;
135:                boolean hasCarriage = false;
136:
137:                do {
138:                    nextChar = in.read();
139:                    if (nextChar == -1 || nextChar == '\n') {
140:                        break;
141:                    }
142:
143:                    if (hasCarriage) {
144:                        buffer = appendChars(buffer, '\r', length++);
145:                        hasCarriage = false;
146:                    }
147:
148:                    if (nextChar == '\r') {
149:                        hasCarriage = true;
150:                    } else {
151:                        buffer = appendChars(buffer, (char) nextChar, length++);
152:                    }
153:
154:                } while (true);
155:
156:                if (length == 0) {
157:                    return null;
158:                }
159:
160:                char[] password = new char[length];
161:                System.arraycopy(buffer, 0, password, 0, length);
162:                return password;
163:            }
164:
165:            private static char[] appendChars(char[] src, char c, int position) {
166:                char[] dest = src;
167:                if (position == src.length) {
168:                    dest = new char[src.length * 2];
169:                    System.arraycopy(src, 0, dest, 0, src.length);
170:                }
171:                dest[position] = c;
172:                return dest;
173:            }
174:
175:            /**
176:             * Cleans the password stored in one char array.
177:             * 
178:             * @param password
179:             *            the char array which contains password.
180:             */
181:            public static void clearPassword(char[] password) {
182:                if (password == null || password.length == 0) {
183:                    return;
184:                }
185:                Arrays.fill(password, '\0');
186:            }
187:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.