Source Code Cross Referenced for CreateAdministrator.java in  » Content-Management-System » dspace » org » dspace » administer » 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 » Content Management System » dspace » org.dspace.administer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * CreateAdministrator.java
003:         *
004:         * Version: $Revision: 1947 $
005:         *
006:         * Date: $Date: 2007-05-18 08:50:29 -0500 (Fri, 18 May 2007) $
007:         *
008:         * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009:         * Institute of Technology.  All rights reserved.
010:         *
011:         * Redistribution and use in source and binary forms, with or without
012:         * modification, are permitted provided that the following conditions are
013:         * met:
014:         *
015:         * - Redistributions of source code must retain the above copyright
016:         * notice, this list of conditions and the following disclaimer.
017:         *
018:         * - Redistributions in binary form must reproduce the above copyright
019:         * notice, this list of conditions and the following disclaimer in the
020:         * documentation and/or other materials provided with the distribution.
021:         *
022:         * - Neither the name of the Hewlett-Packard Company nor the name of the
023:         * Massachusetts Institute of Technology nor the names of their
024:         * contributors may be used to endorse or promote products derived from
025:         * this software without specific prior written permission.
026:         *
027:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028:         * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030:         * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031:         * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033:         * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034:         * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036:         * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037:         * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038:         * DAMAGE.
039:         */
040:        package org.dspace.administer;
041:
042:        import java.io.BufferedReader;
043:        import java.io.InputStreamReader;
044:        import java.util.Locale;
045:
046:        import org.apache.commons.cli.CommandLine;
047:        import org.apache.commons.cli.CommandLineParser;
048:        import org.apache.commons.cli.Options;
049:        import org.apache.commons.cli.PosixParser;
050:
051:        import org.dspace.core.ConfigurationManager;
052:        import org.dspace.core.Context;
053:        import org.dspace.core.I18nUtil;
054:        import org.dspace.eperson.EPerson;
055:        import org.dspace.eperson.Group;
056:
057:        /**
058:         * A command-line tool for creating an initial administrator for setting up a
059:         * DSpace site. Prompts for an e-mail address, last name, first name and
060:         * password from standard input. An administrator group is then created and the
061:         * data passed in used to create an e-person in that group.
062:         * <P>
063:         * Alternatively, it can be used to take the email, first name, last name and
064:         * desired password as arguments thus:
065:         * 
066:         * CreateAdministrator -e [email] -f [first name] -l [last name] -p [password]
067:         * 
068:         * This is particularly convenient for automated deploy scripts that require an 
069:         * initial administrator, for example, before deployment can be completed
070:         * 
071:         * @author Robert Tansley
072:         * @author Richard Jones
073:         * 
074:         * @version $Revision: 1947 $
075:         */
076:        public class CreateAdministrator {
077:            /** DSpace Context object */
078:            private Context context;
079:
080:            /**
081:             * For invoking via the command line.  If called with no command line arguments,
082:             * it will negotiate with the user for the administrator details
083:             * 
084:             * @param argv
085:             *            command-line arguments
086:             */
087:            public static void main(String[] argv) throws Exception {
088:                CommandLineParser parser = new PosixParser();
089:                Options options = new Options();
090:
091:                CreateAdministrator ca = new CreateAdministrator();
092:
093:                options.addOption("e", "email", true,
094:                        "administrator email address");
095:                options.addOption("f", "first", true,
096:                        "administrator first name");
097:                options
098:                        .addOption("l", "last", true,
099:                                "administrator lastt name");
100:                options.addOption("c", "language", true,
101:                        "administrator language");
102:                options.addOption("p", "password", true,
103:                        "administrator password");
104:
105:                CommandLine line = parser.parse(options, argv);
106:
107:                if (line.hasOption("e") && line.hasOption("f")
108:                        && line.hasOption("l") && line.hasOption("c")
109:                        && line.hasOption("p")) {
110:                    ca.createAdministrator(line.getOptionValue("e"), line
111:                            .getOptionValue("f"), line.getOptionValue("l"),
112:                            line.getOptionValue("c"), line.getOptionValue("p"));
113:                } else {
114:                    ca.negotiateAdministratorDetails();
115:                }
116:            }
117:
118:            /** 
119:             * constructor, which just creates and object with a ready context
120:             * 
121:             * @throws Exception
122:             */
123:            private CreateAdministrator() throws Exception {
124:                context = new Context();
125:            }
126:
127:            /**
128:             * Method which will negotiate with the user via the command line to 
129:             * obtain the administrator's details
130:             * 
131:             * @throws Exception
132:             */
133:            private void negotiateAdministratorDetails() throws Exception {
134:                // For easier reading of typing
135:                BufferedReader input = new BufferedReader(
136:                        new InputStreamReader(System.in));
137:
138:                System.out.println("Creating an initial administrator account");
139:
140:                boolean dataOK = false;
141:
142:                String email = null;
143:                String firstName = null;
144:                String lastName = null;
145:                String password1 = null;
146:                String password2 = null;
147:                String language = I18nUtil.DEFAULTLOCALE.getLanguage();
148:
149:                while (!dataOK) {
150:                    System.out.print("E-mail address: ");
151:                    System.out.flush();
152:
153:                    email = input.readLine().trim();
154:
155:                    System.out.print("First name: ");
156:                    System.out.flush();
157:
158:                    firstName = input.readLine().trim();
159:
160:                    System.out.print("Last name: ");
161:                    System.out.flush();
162:
163:                    lastName = input.readLine().trim();
164:
165:                    if (ConfigurationManager
166:                            .getProperty("webui.supported.locales") != null) {
167:                        System.out
168:                                .println("Select one of the following languages: "
169:                                        + ConfigurationManager
170:                                                .getProperty("webui.supported.locales"));
171:                        System.out.print("Language: ");
172:                        System.out.flush();
173:
174:                        language = input.readLine().trim();
175:                        language = I18nUtil.getSupportedLocale(
176:                                new Locale(language)).getLanguage();
177:                    }
178:
179:                    System.out
180:                            .println("WARNING: Password will appear on-screen.");
181:                    System.out.print("Password: ");
182:                    System.out.flush();
183:
184:                    password1 = input.readLine().trim();
185:
186:                    System.out.print("Again to confirm: ");
187:                    System.out.flush();
188:
189:                    password2 = input.readLine().trim();
190:
191:                    if (!password1.equals("") && password1.equals(password2)) {
192:                        // password OK
193:                        System.out
194:                                .print("Is the above data correct? (y or n): ");
195:                        System.out.flush();
196:
197:                        String s = input.readLine().trim();
198:
199:                        if (s.toLowerCase().startsWith("y")) {
200:                            dataOK = true;
201:                        }
202:                    } else {
203:                        System.out.println("Passwords don't match");
204:                    }
205:                }
206:
207:                // if we make it to here, we are ready to create an administrator
208:                createAdministrator(email, firstName, lastName, language,
209:                        password1);
210:            }
211:
212:            /**
213:             * Create the administrator with the given details.  If the user
214:             * already exists then they are simply upped to administrator status
215:             * 
216:             * @param email	the email for the user
217:             * @param first	user's first name
218:             * @param last	user's last name
219:             * @param ps	desired password
220:             * 
221:             * @throws Exception
222:             */
223:            private void createAdministrator(String email, String first,
224:                    String last, String language, String pw) throws Exception {
225:                // Of course we aren't an administrator yet so we need to
226:                // circumvent authorisation
227:                context.setIgnoreAuthorization(true);
228:
229:                // Find administrator group
230:                Group admins = Group.find(context, 1);
231:
232:                if (admins == null) {
233:                    throw new Exception("Error, no admin group (group 1) found");
234:                }
235:
236:                // Create the administrator e-person
237:                EPerson eperson = EPerson.findByEmail(context, email);
238:
239:                // check if the email belongs to a registered user,
240:                // if not create a new user with this email
241:                if (eperson == null) {
242:                    eperson = EPerson.create(context);
243:                    eperson.setEmail(email);
244:                    eperson.setCanLogIn(true);
245:                    eperson.setRequireCertificate(false);
246:                    eperson.setSelfRegistered(false);
247:                }
248:
249:                eperson.setLastName(last);
250:                eperson.setFirstName(first);
251:                eperson.setLanguage(language);
252:                eperson.setPassword(pw);
253:                eperson.update();
254:
255:                admins.addMember(eperson);
256:                admins.update();
257:
258:                context.complete();
259:
260:                System.out.println("Administrator account created");
261:            }
262:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.