Source Code Cross Referenced for Command.java in  » JMX » jmanage » org » jmanage » cmdui » 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 » JMX » jmanage » org.jmanage.cmdui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright 2004-2005 jManage.org
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */package org.jmanage.cmdui;
016:
017:        import org.jmanage.cmdui.util.In;
018:        import org.jmanage.cmdui.util.Out;
019:        import org.jmanage.core.util.Loggers;
020:        import org.jmanage.core.services.AuthService;
021:        import org.jmanage.core.services.ServiceFactory;
022:        import org.jmanage.core.services.ServiceContextImpl;
023:        import org.jmanage.core.services.ServiceException;
024:
025:        import org.jmanage.core.auth.UnAuthorizedAccessException;
026:        import org.jmanage.core.util.PasswordField;
027:        import org.jmanage.util.StringUtils;
028:
029:        import java.util.StringTokenizer;
030:        import java.util.logging.Logger;
031:        import java.util.logging.Level;
032:        import java.io.IOException;
033:
034:        /**
035:         *
036:         * date:  Feb 4, 2005
037:         * @author	Rakesh Kalra
038:         */
039:        public class Command {
040:
041:            private static final Logger logger = Loggers
042:                    .getLogger(Command.class);
043:
044:            private String username;
045:            private String password;
046:            private String url;
047:            private String name;
048:            private Level logLevel = Level.WARNING; // default log level
049:            private String[] args;
050:
051:            static Command get(String line, Command authenticatedCommand)
052:                    throws InvalidCommandException {
053:
054:                assert authenticatedCommand.username != null
055:                        && authenticatedCommand.password != null;
056:                String[] args = toArgs(line);
057:                Command command = get(args);
058:                assert command.username == null && command.password == null
059:                        && command.url == null : "Invalid command.";
060:                command.username = authenticatedCommand.username;
061:                command.password = authenticatedCommand.password;
062:                command.url = authenticatedCommand.url;
063:                command.logLevel = authenticatedCommand.logLevel;
064:                return command;
065:            }
066:
067:            static Command get(String[] args) throws InvalidCommandException {
068:
069:                Command command = new Command();
070:                for (int index = 0; index < args.length; index++) {
071:                    if (args[index].equals("-username")) {
072:                        command.username = args[++index];
073:                    } else if (args[index].equals("-password")) {
074:                        command.password = args[++index];
075:                    } else if (args[index].equals("-url")) {
076:                        command.url = args[++index];
077:                    } else if (args[index].startsWith("-verbose")) {
078:                        int i = args[index].indexOf("=");
079:                        if (i != -1) {
080:                            command.logLevel = Level.parse(args[index]
081:                                    .substring(i + 1));
082:                        } else {
083:                            command.logLevel = Level.FINE; // default verbose level
084:                        }
085:                    } else {
086:                        /* we have the command name and arguments */
087:                        command.name = args[index++];
088:                        CommandHandlerFactory.validateCommand(command.name);
089:                        command.args = new String[args.length - index];
090:                        int argsIndex = 0;
091:                        while (index < args.length) {
092:                            command.args[argsIndex++] = args[index++];
093:                        }
094:                    }
095:                }
096:                logger.fine("Command=" + command);
097:                return command;
098:            }
099:
100:            private Command() {
101:            }
102:
103:            public String getUsername() {
104:                return username;
105:            }
106:
107:            public String getPassword() {
108:                return password;
109:            }
110:
111:            public String getUrl() {
112:                return url;
113:            }
114:
115:            public String getName() {
116:                return name;
117:            }
118:
119:            public String[] getArgs() {
120:                return args;
121:            }
122:
123:            public Level getLogLevel() {
124:                return logLevel;
125:            }
126:
127:            public boolean isAuthRequired() {
128:                boolean authRequired = true;
129:                if (name != null
130:                        && (name.equals(CommandConstants.HELP) || name
131:                                .equals(CommandConstants.EXIT))) {
132:                    /* no auth required */
133:                    authRequired = false;
134:                }
135:                return authRequired;
136:            }
137:
138:            public boolean authenticate() throws IOException {
139:
140:                while (true) {
141:                    if (username == null) {
142:                        Out.print("Username: ");
143:                        username = In.readLine();
144:                    }
145:
146:                    if (password == null) {
147:                        /* get the password */
148:                        password = new String(PasswordField
149:                                .getPassword("Password:"));
150:                    }
151:
152:                    /* authenticate with the server */
153:                    AuthService authService = ServiceFactory.getAuthService();
154:                    try {
155:                        authService.login(new ServiceContextImpl(), username,
156:                                password);
157:                        break;
158:                    } catch (ServiceException e) {
159:                        Out.println(e.getMessage());
160:                        username = null;
161:                        password = null;
162:                    } catch (Exception e) {
163:                        throw new RuntimeException(e);
164:                    }
165:                }
166:
167:                return true;
168:            }
169:
170:            public String toString() {
171:                StringBuffer buff = new StringBuffer();
172:                buff.append("username=");
173:                buff.append(username);
174:                buff.append(", url=");
175:                buff.append(url);
176:                buff.append(", name=");
177:                buff.append(name);
178:                buff.append(", args=");
179:                buff.append(StringUtils.stringArrayToCSV(args));
180:                return buff.toString();
181:            }
182:
183:            public boolean execute() {
184:                try {
185:                    assert getName() != null;
186:                    CommandHandler handler = CommandHandlerFactory
187:                            .getHandler(getName());
188:                    boolean result = handler.execute(getHandlerContext());
189:                    Out.println();
190:                    return result;
191:                } catch (InvalidCommandException e) {
192:                    throw new RuntimeException(e);
193:                } catch (ServiceException e) {
194:                    Out.println(e.getMessage());
195:                    Out.println();
196:                    return false;
197:                } catch (UnAuthorizedAccessException e) {
198:                    Out
199:                            .println(e.getMessage()
200:                                    + ", You are not authorized to perform this operation");
201:                    Out.println();
202:                    return false;
203:                }
204:            }
205:
206:            private static String[] toArgs(String str) {
207:                StringTokenizer tokenizer = new StringTokenizer(str, " ");
208:                String[] args = new String[tokenizer.countTokens()];
209:                for (int i = 0; i < args.length; i++) {
210:                    args[i] = tokenizer.nextToken();
211:                }
212:                return args;
213:            }
214:
215:            private HandlerContext getHandlerContext() {
216:                return new HandlerContext(this, isAuthRequired());
217:            }
218:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.