View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.common.util.execute;
17  
18  import org.kuali.common.util.Assert;
19  import org.kuali.common.util.CollectionUtils;
20  import org.kuali.common.util.FormatUtils;
21  import org.kuali.common.util.ScmUtils;
22  import org.kuali.common.util.scm.ScmRequest;
23  import org.kuali.common.util.service.ScmService;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  public class ScmExecutable implements Executable {
28  
29  	private static final Logger logger = LoggerFactory.getLogger(ScmExecutable.class);
30  
31  	boolean skip;
32  	boolean logConfiguration;
33  	ScmService service;
34  	ScmRequest request;
35  
36  	@Override
37  	public void execute() {
38  
39  		// Show our current configuration
40  		if (logConfiguration) {
41  			log(this);
42  		}
43  
44  		// They have explicitly asked that execution be skipped
45  		if (skip) {
46  			logger.info("Skipping execution");
47  			return;
48  		}
49  
50  		// The SCM request is not allowed to be null here
51  		Assert.notNull(request);
52  
53  		// There are no files to add/delete/commit, no point in going further.
54  		if (isEmpty(request)) {
55  			logger.info("Skipping execution.  Nothing to do!");
56  			return;
57  		}
58  
59  		// Make sure we are configured correctly
60  		validateConfiguration(service, request);
61  
62  		// Add files as needed
63  		if (!CollectionUtils.isEmpty(request.getAdds())) {
64  			service.add(request.getAdds());
65  		}
66  
67  		// Delete files as needed
68  		if (!CollectionUtils.isEmpty(request.getDeletes())) {
69  			service.delete(request.getDeletes());
70  		}
71  
72  		// Commit files as needed
73  		if (!CollectionUtils.isEmpty(request.getCommits())) {
74  			service.commit(request.getCommits(), request.getCommitMessage());
75  		}
76  	}
77  
78  	protected void validateConfiguration(ScmService service, ScmRequest request) {
79  		// Make sure we are configured correctly
80  		Assert.notNull(service, "service is null");
81  		if (!CollectionUtils.isEmpty(request.getCommits())) {
82  			Assert.hasText(request.getCommitMessage(), "commitMessage has no text");
83  		}
84  	}
85  
86  	protected void log(ScmExecutable exec) {
87  		ScmRequest request = ScmUtils.cloneOrNew(exec.getRequest());
88  		String adds = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getAdds()).size());
89  		String deletes = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getDeletes()).size());
90  		String commits = FormatUtils.getCount(CollectionUtils.toEmptyList(request.getCommits()).size());
91  
92  		logger.info(" -- SCM --");
93  		logger.info("Adds: {}", adds);
94  		logger.info("Deletes: {}", deletes);
95  		logger.info("Commits: {}", commits);
96  		logger.info("Skip: {}", skip);
97  	}
98  
99  	public boolean isEmpty(ScmRequest request) {
100 		if (!CollectionUtils.isEmpty(request.getAdds())) {
101 			return false;
102 		}
103 		if (!CollectionUtils.isEmpty(request.getDeletes())) {
104 			return false;
105 		}
106 		if (!CollectionUtils.isEmpty(request.getCommits())) {
107 			return false;
108 		}
109 		return true;
110 	}
111 
112 	public ScmService getService() {
113 		return service;
114 	}
115 
116 	public void setService(ScmService service) {
117 		this.service = service;
118 	}
119 
120 	public boolean isSkip() {
121 		return skip;
122 	}
123 
124 	public void setSkip(boolean skip) {
125 		this.skip = skip;
126 	}
127 
128 	public ScmRequest getRequest() {
129 		return request;
130 	}
131 
132 	public void setRequest(ScmRequest request) {
133 		this.request = request;
134 	}
135 
136 	public boolean isLogConfiguration() {
137 		return logConfiguration;
138 	}
139 
140 	public void setLogConfiguration(boolean logConfiguration) {
141 		this.logConfiguration = logConfiguration;
142 	}
143 
144 }