1 | /*
|
---|
2 | * AddGreenstoneUserToDatabase.java
|
---|
3 | * A task to add a user to the Greenstone User Database
|
---|
4 | *
|
---|
5 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 | package org.greenstone.anttasks;
|
---|
22 |
|
---|
23 | import org.apache.tools.ant.*;
|
---|
24 | import org.apache.tools.ant.taskdefs.*;
|
---|
25 | import java.io.File;
|
---|
26 | import java.io.BufferedWriter;
|
---|
27 | import java.io.OutputStreamWriter;
|
---|
28 | import lib.Crypt;
|
---|
29 |
|
---|
30 | public class AddGreenstoneUserToDatabase extends Task {
|
---|
31 |
|
---|
32 | private File txt2db = null;
|
---|
33 | private File usersDb = null;
|
---|
34 | private String username = null;
|
---|
35 | private String password = null;
|
---|
36 | private String groups = null;
|
---|
37 | private String comment = null;
|
---|
38 |
|
---|
39 | public static void main( String[] args ) {
|
---|
40 | AddGreenstoneUserToDatabase agu = new AddGreenstoneUserToDatabase();
|
---|
41 | agu.setTxt2db( new File(args[0]) );
|
---|
42 | agu.setUsersDb( new File(args[1]) );
|
---|
43 | agu.setUsername( args[2] );
|
---|
44 | agu.setPassword( args[3] );
|
---|
45 | agu.setGroups( args[4] );
|
---|
46 | agu.setComment( args[5] );
|
---|
47 | agu.execute();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void execute() {
|
---|
51 |
|
---|
52 | if ( txt2db == null ) { throw new BuildException( "Error - txt2db not specified!!" ); }
|
---|
53 | if ( usersDb == null ) { throw new BuildException( "Error - usersDb not specified!!" ); }
|
---|
54 | if ( username == null ) { throw new BuildException( "Error - username not specified!!" ); }
|
---|
55 | if ( password == null ) { throw new BuildException( "Error - password not specified!!" ); }
|
---|
56 | if ( groups == null ) { throw new BuildException( "Error - groups not specified!!" ); }
|
---|
57 | if ( comment == null ) { comment = ""; }
|
---|
58 |
|
---|
59 | String encryptedPassword = Crypt.crypt("Tp", password);
|
---|
60 |
|
---|
61 | long usersDbFileLastModified = 0;
|
---|
62 | try {
|
---|
63 | // Get the users.db file's last modification date so it can be reset afterwards
|
---|
64 | usersDbFileLastModified = usersDb.lastModified();
|
---|
65 | } catch (Exception exception) {
|
---|
66 | throw new BuildException( "Failed to get the last modified time of the users db" );
|
---|
67 | }
|
---|
68 |
|
---|
69 | Process txt2dbProcess = null;
|
---|
70 | try {
|
---|
71 | // Write directly to the txt2db process to append to the users.db file
|
---|
72 | String[] cmdAndArgs = { txt2db.getAbsolutePath(), "-append", usersDb.getAbsolutePath() };
|
---|
73 | txt2dbProcess = Runtime.getRuntime().exec( cmdAndArgs );
|
---|
74 | } catch ( Exception e ) {
|
---|
75 | System.err.println( e.getMessage() );
|
---|
76 | e.printStackTrace();
|
---|
77 | throw new BuildException( "Failed to execute the txt2db process" );
|
---|
78 | }
|
---|
79 |
|
---|
80 | BufferedWriter txt2dbIn = null;
|
---|
81 | try {
|
---|
82 | txt2dbIn = new BufferedWriter(new OutputStreamWriter(txt2dbProcess.getOutputStream()));
|
---|
83 | } catch (Exception exception) {
|
---|
84 | throw new BuildException( "Failed to get an input stream" );
|
---|
85 | }
|
---|
86 |
|
---|
87 | try {
|
---|
88 | txt2dbIn.write("[" + username + "]"); txt2dbIn.newLine();
|
---|
89 | txt2dbIn.write("<comment>" + comment); txt2dbIn.newLine();
|
---|
90 | txt2dbIn.write("<enabled>true"); txt2dbIn.newLine();
|
---|
91 | txt2dbIn.write("<groups>" + groups); txt2dbIn.newLine();
|
---|
92 | txt2dbIn.write("<password>" + encryptedPassword); txt2dbIn.newLine();
|
---|
93 | txt2dbIn.write("<username>" + username); txt2dbIn.newLine();
|
---|
94 | txt2dbIn.write("----------------------------------------------------------------------"); txt2dbIn.newLine();
|
---|
95 |
|
---|
96 | txt2dbIn.flush();
|
---|
97 | txt2dbIn.close();
|
---|
98 | txt2dbProcess.waitFor();
|
---|
99 |
|
---|
100 | } catch (Exception exception) {
|
---|
101 | throw new BuildException( "Failed to add the user to the user database" );
|
---|
102 | }
|
---|
103 |
|
---|
104 | try {
|
---|
105 | usersDb.setLastModified(usersDbFileLastModified);
|
---|
106 | } catch (Exception exception) {
|
---|
107 | throw new BuildException( "Failed to reset the last modified time of the users db" );
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void setTxt2db( File txt2db ) { this.txt2db = txt2db; }
|
---|
113 | public void setUsersDb( File usersDb ) { this.usersDb = usersDb; }
|
---|
114 | public void setUsername( String username ) { this.username = username; }
|
---|
115 | public void setPassword( String password ) { this.password = password; }
|
---|
116 | public void setGroups( String groups ) { this.groups = groups; }
|
---|
117 | public void setComment( String comment ) { this.comment = comment; }
|
---|
118 |
|
---|
119 | }
|
---|