Subversion Repositories XServices

Rev

Rev 152 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 brianR 1
/*
2
 *   Copyright 2013 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.File;
20
import java.io.IOException;
21
import java.io.InputStreamReader;
22
import java.util.ArrayList;
23
import java.util.Date;
24
import java.util.List;
152 brianR 25
import java.util.Map;
149 brianR 26
import java.util.StringTokenizer;
27
 
28
import net.brutex.svn.SVNCommitInfo.ChangeType;
29
 
30
import org.apache.log4j.Logger;
31
 
150 brianR 32
/* Executes the svnlook utility
149 brianR 33
 *
34
 * @author Brian Rosenberger bru(at)brutex.de
35
 * @since 0.1
36
 
37
 */
38
public class SVNLookExecutor {
39
 
40
	private Logger logger = Logger.getLogger(SVNLookExecutor.class);
41
	private final File svnlook;
42
	private final String repos;
43
	private String TXN = null;
44
	private String rev = null;
152 brianR 45
	private String locale = "de_DE.UTF-8";
46
	private String encoding = "UTF-8";
149 brianR 47
 
48
	/**
49
	 * Instantiates a new sVN look executor.
50
	 *
51
	 * @param svnlook the svnlook
52
	 * @param repos the repos
53
	 */
54
	public SVNLookExecutor(File svnlook, String repos) {
55
		if(! svnlook.exists() ) throw new IllegalArgumentException( String.format("The svnlook executable at '%s' does not exist.", svnlook.toString()));
56
		if(! svnlook.isFile() ) throw new IllegalArgumentException( String.format("The svnlook utility at'%s' is not a file.", svnlook.toString()));
57
		logger.debug(String.format("Instantiating '%s' with svnlook at '%s'.", this.getClass().getCanonicalName(), svnlook.toString()));
58
		this.svnlook = svnlook;
59
		logger.debug(String.format("Working against svn repository at '%s'.", repos));
60
		this.repos = repos;
61
	}
62
 
63
 
64
	/**
65
	 * Execute svn look.
66
	 *
67
	 * @param command the command
68
	 * @return the string
69
	 */
70
	private String executeSVNLook(SVNLookCommand command) {
71
		StringBuilder sb = new StringBuilder();
72
		StringBuilder sberr = new StringBuilder();
73
 
74
		//This throws an IllegalArgumentException when neither TXN nor REV is valid
75
		String[] params;
76
		try {
77
			params = getTargetParam();
78
		} catch (IllegalArgumentException e) {
79
			logger.error(e.getMessage());
80
			throw e;
81
		}
82
		try {
83
			List<String> cmdline =  new ArrayList<String>(5);
84
			cmdline.add(svnlook.toString());
85
			cmdline.add(command.getValue());
86
			cmdline.add(params[0]);
87
			cmdline.add(params[1]);
88
			cmdline.add(repos);
89
 
90
			ProcessBuilder pf = new ProcessBuilder(cmdline);
91
			logger.debug(String.format("Executing svnlook with commandline '%s'.", pf.command()));
152 brianR 92
			Map<String, String> env = pf.environment();
93
			env.put("LANG", locale);
149 brianR 94
			Process svnprocess = pf.start();
152 brianR 95
			BufferedReader stdin = new BufferedReader(new InputStreamReader(svnprocess.getInputStream(), encoding));
96
			BufferedReader stderr = new BufferedReader(new InputStreamReader(svnprocess.getErrorStream(), encoding));
149 brianR 97
			String s;
98
			 while( (s = stdin.readLine()) != null ) {
99
				 sb.append(s + '\n');
100
			 }
101
			 while( (s = stderr.readLine()) != null ) {
102
				 sberr.append(s + '\n');
103
			 }
104
			 stdin.close(); stderr.close();
105
		} catch (IOException e) {
106
			logger.error( String.format( "Error calling the svnlook utility: '%s'", e.getMessage()));
107
			e.printStackTrace();
108
		}
109
 
110
		String error = sberr.toString();
111
		if( error.length()>0 ) {
112
			error = "Failed to call svnlook. The STDERR was '"+error+"'";
113
			logger.error(error);
114
			throw new IllegalArgumentException(error);
115
		}
116
		String output = sb.toString().trim();
117
		logger.debug(String.format("Svnlook output was '%s'", output));
118
		return output;
119
	}
120
 
121
	/*
122
	 * Returns either -t TXN oder -rev REVISION
123
	 *
124
	 * @returns
125
	 */
126
	private String[] getTargetParam() {
127
		String[] result = new String[2];
128
		if( (TXN==null || TXN.length()<=0) && (rev==null || rev.length()<=0) ) throw new IllegalArgumentException("Either TXN or revision must be provided.");
129
		if( TXN!=null && TXN.length()>0 && rev!=null && rev.length()>0 ) throw new IllegalArgumentException("Both, TXN and revision are given. Don't know what to use.");
130
		if( TXN!=null && TXN.length()>0) {
131
			result[0] = "-t";
132
			result[1] = TXN;
133
			return result;
134
		}
135
		if( rev!=null && rev.length()>0) {
136
			result[0] = "-r";
137
			result[1] = rev;
138
			return result;
139
		}
140
		throw new IllegalArgumentException("Either TXN or revision must be provided.");
141
	}
142
 
143
	public void setTXN(String TXN) {
144
		if(TXN==null || TXN.length()<=0) throw new IllegalArgumentException("TXN cannot be null or empty.");
145
		this.TXN = TXN;
146
	}
147
 
148
	public void setRev(String rev) {
149
		if(rev==null || rev.length()<=0) throw new IllegalArgumentException("Revision cannot be null or empty.");
150
		this.rev = rev;
151
	}
152
 
153
	public SVNCommitInfo getCommitInfo() {
154
		String author = executeSVNLook(SVNLookCommand.AUTHOR);
155
		String logmessage = executeSVNLook(SVNLookCommand.LOG);
156
		String files = executeSVNLook(SVNLookCommand.CHANGED);
157
 
158
		SVNCommitInfo result = new SVNCommitInfo(author,
159
												new Date(),
160
												logmessage);
161
 
162
 
163
		files += "\n";
164
		StringTokenizer tokenizer = new StringTokenizer(files, "\n");
165
		String s;
166
		while( tokenizer.hasMoreTokens()) {
167
			s=tokenizer.nextToken();
168
			logger.debug(String.format("Tokenizing file list. Token '%s'.", s));
169
			if(s.startsWith("A")) { result.addFileInfo(ChangeType.ADDED, s.substring(1).trim()); continue; }
170
			if(s.startsWith("D")) { result.addFileInfo(ChangeType.DELETED, s.substring(1).trim()); continue; }
171
			if(s.startsWith("UU")) { result.addFileInfo(ChangeType.BOTHUPDATE, s.substring(2).trim()); continue; }
172
			if(s.startsWith("_U")) { result.addFileInfo(ChangeType.METAUPDATE, s.substring(2).trim()); continue; }
173
			if(s.startsWith("U")) { result.addFileInfo(ChangeType.UPDATED, s.substring(1).trim()); continue; }
174
		}
175
		result.setTxn(TXN);
176
		result.setRev(rev);
177
 
178
		return result;
179
	}
152 brianR 180
 
181
	public void setEncoding(String encoding) {
182
		this.encoding = encoding;
183
	}
184
	 /**
185
	  * Set the locale that will be pushed into
186
	  * 'LANG' environment variable.
187
	  *
188
	  * @param locale i.e. de_DE.UTF-8
189
	  */
190
	public void setLocale(String locale) {
191
		this.locale = locale;
192
	}
149 brianR 193
 
194
}