Subversion Repositories XServices

Rev

Rev 149 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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