Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
167 brianR 1
/*
2
 *   Copyright 2014 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.svn;
17
 
18
import java.io.BufferedReader;
19
import java.io.BufferedWriter;
20
import java.io.File;
21
import java.io.FileOutputStream;
22
import java.io.IOException;
23
import java.io.InputStreamReader;
24
import java.io.OutputStreamWriter;
25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.Map;
28
 
29
import org.apache.log4j.Logger;
30
 
31
/* Executes the svnadmin utility
32
 *
33
 * @author Brian Rosenberger bru(at)brutex.de
34
 * @since 0.1
35
 
36
 */
37
public class SVNAdminExecutor {
38
 
39
	private Logger logger = Logger.getLogger(SVNAdminExecutor.class);
40
	private final File svnadmin;
41
	private final String repos;
42
	private String rev = null;
43
	private String message = null;
44
	private String locale = "de_DE.UTF-8";
45
	private String encoding = "UTF-8";
46
	private boolean useBypassHooks = true;
47
 
48
	/**
49
	 * Instantiates a new SVN admin executor.
50
	 *
51
	 * @param svnadmin the svnadmin
52
	 * @param repos the repos
53
	 */
54
	public SVNAdminExecutor(File svnadmin, String repos) {
55
		if(! svnadmin.exists() ) throw new IllegalArgumentException( String.format("The svnadmin executable at '%s' does not exist.", svnadmin.toString()));
56
		if(! svnadmin.isFile() ) throw new IllegalArgumentException( String.format("The svnadmin utility at'%s' is not a file.", svnadmin.toString()));
57
		logger.debug(String.format("Instantiating '%s' with svnadmin at '%s'.", this.getClass().getCanonicalName(), svnadmin.toString()));
58
		this.svnadmin = svnadmin;
59
		logger.debug(String.format("Working against svn repository at '%s'.", repos));
60
		this.repos = repos;
61
	}
62
 
63
 
64
	/**
65
	 * Execute svn admin.
66
	 *
67
	 * @param command the command
68
	 * @return the string
69
	 */
70
	public String executeSVNAdmin(SVNAdminCommand command) {
71
		StringBuilder sb = new StringBuilder();
72
		StringBuilder sberr = new StringBuilder();
73
 
74
		if(rev==null) {
75
			logger.error("The revision can not be 'null' for the svnadmin command.");
76
			return null;
77
		}
78
 
79
		if(message==null) {
80
			logger.error("The new log message can not be 'null' for the svnadmin command.");
81
			return null;
82
		}
83
 
84
		File logfile = null;
85
		try {
86
			logfile = File.createTempFile("~msg"+rev+"_", ".tmp");
87
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logfile), encoding));
88
			out.write(message);
89
			out.flush();
90
			out.close();
91
		} catch (IOException e) {
92
			logger.error("Failed to write new log message to temporary file '"+logfile.getAbsolutePath()+"'.", e);
93
			return null;
94
		}
95
 
96
 
97
 
98
		try {
99
			List<String> cmdline =  new ArrayList<String>(5);
100
			cmdline.add(svnadmin.toString());
101
			cmdline.add(command.getValue());
102
			cmdline.add(repos);
103
			if(useBypassHooks) {
104
				cmdline.add("--bypass-hooks");
105
			}
106
			cmdline.add("-r");
107
			cmdline.add(rev);
108
			cmdline.add(logfile.getAbsolutePath());
109
 
110
			ProcessBuilder pf = new ProcessBuilder(cmdline);
111
			logger.debug(String.format("Executing svnadmin with commandline '%s'.", pf.command()));
112
			Map<String, String> env = pf.environment();
113
			env.put("LANG", locale);
114
			Process svnprocess = pf.start();
115
			BufferedReader stdin = new BufferedReader(new InputStreamReader(svnprocess.getInputStream(), encoding));
116
			BufferedReader stderr = new BufferedReader(new InputStreamReader(svnprocess.getErrorStream(), encoding));
117
			String s;
118
			 while( (s = stdin.readLine()) != null ) {
119
				 sb.append(s + '\n');
120
			 }
121
			 while( (s = stderr.readLine()) != null ) {
122
				 sberr.append(s + '\n');
123
			 }
124
			 stdin.close(); stderr.close();
125
		} catch (IOException e) {
126
			logger.error( String.format( "Error calling the svnadmin utility: '%s'", e.getMessage()));
127
			e.printStackTrace();
128
		} finally {
129
			logfile.deleteOnExit();
130
		}
131
 
132
		String error = sberr.toString();
133
		if( error.length()>0 ) {
134
			error = "Failed to call svnadmin. The STDERR was '"+error+"'";
135
			logger.error(error);
136
			throw new IllegalArgumentException(error);
137
		}
138
		String output = sb.toString().trim();
139
		logger.debug(String.format("Svnadmin output was '%s'", output));
140
		return output;
141
	}
142
 
143
 
144
	public void setRev(String rev) {
145
		if(rev==null || rev.length()<=0) throw new IllegalArgumentException("Revision cannot be null or empty.");
146
		this.rev = rev;
147
	}
148
 
149
	public void setEncoding(String encoding) {
150
		this.encoding = encoding;
151
	}
152
	 /**
153
	  * Set the locale that will be pushed into
154
	  * 'LANG' environment variable.
155
	  *
156
	  * @param locale i.e. de_DE.UTF-8
157
	  */
158
	public void setLocale(String locale) {
159
		this.locale = locale;
160
	}
161
 
162
	/**
163
	 * Set the new log message.
164
	 *
165
	 * @param message new message
166
	 */
167
	public void setMessage(String message) {
168
		this.message = message;
169
	}
170
 
171
	/**
172
	 * Set this to true to bypass the pre-revprop-change and post-revprop-change
173
	 * hooks.
174
	 *
175
	 * @param useBypassHooks defaults to true
176
	 */
177
	public void setUseBypassHooks(boolean useBypassHooks) {
178
		this.useBypassHooks = useBypassHooks;
179
	}
180
 
181
}