Subversion Repositories XServices

Rev

Rev 150 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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