KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > administer > CreateAdministrator


1 /*
2  * CreateAdministrator.java
3  *
4  * Version: $Revision: 1.9 $
5  *
6  * Date: $Date: 2005/04/20 14:23:17 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.administer;
41
42 import java.io.BufferedReader JavaDoc;
43 import java.io.InputStreamReader JavaDoc;
44
45 import org.dspace.core.Context;
46 import org.dspace.eperson.EPerson;
47 import org.dspace.eperson.Group;
48
49 /**
50  * A command-line tool for creating an initial administrator for setting up a
51  * DSpace site. Prompts for an e-mail address, last name, first name and
52  * password from standard input. An administrator group is then created and the
53  * data passed in used to create an e-person in that group.
54  * <P>
55  * Takes no arguments.
56  *
57  * @author Robert Tansley
58  * @version $Revision: 1.9 $
59  */

60 public class CreateAdministrator
61 {
62     /**
63      * For invoking via the command line
64      *
65      * @param argv
66      * command-line arguments
67      */

68     public static void main(String JavaDoc[] argv)
69     {
70         Context context = null;
71         BufferedReader JavaDoc input = null;
72
73         try
74         {
75             // For easier reading of typing
76
input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
77
78             context = new Context();
79
80             // Of course we aren't an administrator yet so we need to
81
// circumvent authorisation
82
context.setIgnoreAuthorization(true);
83
84             System.out.println("Creating an initial administrator account");
85
86             boolean dataOK = false;
87
88             String JavaDoc email = null;
89             String JavaDoc firstName = null;
90             String JavaDoc lastName = null;
91             String JavaDoc password1 = null;
92             String JavaDoc password2 = null;
93
94             while (!dataOK)
95             {
96                 System.out.print("E-mail address: ");
97                 System.out.flush();
98
99                 email = input.readLine().trim();
100
101                 System.out.print("First name: ");
102                 System.out.flush();
103
104                 firstName = input.readLine().trim();
105
106                 System.out.print("Last name: ");
107                 System.out.flush();
108
109                 lastName = input.readLine().trim();
110
111                 System.out.println("WARNING: Password will appear on-screen.");
112                 System.out.print("Password: ");
113                 System.out.flush();
114
115                 password1 = input.readLine().trim();
116
117                 System.out.print("Again to confirm: ");
118                 System.out.flush();
119
120                 password2 = input.readLine().trim();
121
122                 if (!password1.equals("") && password1.equals(password2))
123                 {
124                     // password OK
125
System.out.print("Is the above data correct? (y or n): ");
126                     System.out.flush();
127
128                     String JavaDoc s = input.readLine().trim();
129
130                     if (s.toLowerCase().startsWith("y"))
131                     {
132                         dataOK = true;
133                     }
134                 }
135                 else
136                 {
137                     System.out.println("Passwords don't match");
138                 }
139             }
140
141             // Find administrator group
142
Group admins = Group.find(context, 1);
143
144             if (admins == null)
145             {
146                 System.out.println("Error, no admin group (group 1) found");
147                 System.exit(1);
148             }
149
150             // Create the administrator e-person
151
EPerson eperson = EPerson.create(context);
152
153             eperson.setEmail(email);
154             eperson.setLastName(lastName);
155             eperson.setFirstName(firstName);
156             eperson.setPassword(password1);
157             eperson.setCanLogIn(true);
158             eperson.setRequireCertificate(false);
159             eperson.setSelfRegistered(false);
160             eperson.update();
161
162             admins.addMember(eperson);
163             admins.update();
164
165             context.complete();
166
167             System.out.println("Administrator account created");
168
169             System.exit(0);
170         }
171         catch (Exception JavaDoc e)
172         {
173             System.err.println("Exception occurred:" + e);
174             e.printStackTrace();
175
176             if (context != null)
177             {
178                 context.abort();
179             }
180
181             System.exit(1);
182         }
183     }
184 }
185
Popular Tags