001 /**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 /**
017 *
018 */
019 package org.kuali.student.common.mojo;
020
021 import java.io.File;
022 import java.io.IOException;
023 import java.util.ArrayList;
024 import java.util.Collection;
025 import java.util.HashSet;
026 import java.util.LinkedHashSet;
027 import java.util.List;
028 import java.util.Set;
029
030 import org.apache.commons.lang.StringEscapeUtils;
031 import org.apache.commons.lang.StringUtils;
032 import org.apache.maven.plugin.AbstractMojo;
033 import org.apache.maven.project.MavenProject;
034 import org.kuali.student.contract.model.ServiceContractModel;
035 import org.kuali.student.contract.model.impl.ServiceContractModelCache;
036 import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
037 import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
038 import org.slf4j.Logger;
039 import org.slf4j.LoggerFactory;
040
041 import com.thoughtworks.qdox.directorywalker.DirectoryScanner;
042 import com.thoughtworks.qdox.directorywalker.FileVisitor;
043 import com.thoughtworks.qdox.directorywalker.SuffixFilter;
044
045 /**
046 *
047 * The basic Kuali Student Mojo that defines things like the source lookup path.
048 *
049 * @author ocleirig
050 *
051 */
052 public abstract class AbstractKSMojo extends AbstractMojo {
053
054 private static final Logger log = LoggerFactory.getLogger(AbstractKSMojo.class);
055
056 /**
057 * @parameter
058 **/
059 private List<String> sourceDirs;
060
061 /**
062 * @parameter
063 */
064 private List<String>contextSourceDirs;
065
066 public AbstractKSMojo() {
067 super();
068 }
069
070 public List<String> getSourceDirs() {
071 return sourceDirs;
072 }
073
074 public void setSourceDirs(List<String> sourceDirs) {
075 this.sourceDirs = sourceDirs;
076 }
077
078
079
080 /**
081 * @return the contextSourceDirs
082 */
083 public List<String> getContextSourceDirs() {
084 return contextSourceDirs;
085 }
086
087 /**
088 * @param contextSourceDirs the contextSourceDirs to set
089 */
090 public void setContextSourceDirs(List<String> contextSourceDirs) {
091 this.contextSourceDirs = contextSourceDirs;
092 }
093
094
095
096 protected final Set<String>localPackages = new HashSet<String>();
097
098
099 protected final ServiceContractModel getModel() {
100
101 Set<String> modelSourceDirectories = new LinkedHashSet<String>();
102
103 MavenProject project = (MavenProject) getPluginContext().get("project");
104 /*
105 * Default to the source directory that the plugin is run from.
106 */
107
108 if (sourceDirs == null) {
109 modelSourceDirectories.add(project.getBuild().getSourceDirectory());
110
111 } else {
112 modelSourceDirectories.addAll(sourceDirs);
113 }
114 if (modelSourceDirectories.size() == 0)
115 throw new RuntimeException("No Source Directories are defined");
116
117 for (String directory : modelSourceDirectories) {
118
119 DirectoryScanner scanner = new DirectoryScanner(new File (directory));
120 scanner.addFilter(new SuffixFilter(".java"));
121 scanner.scan(new FileVisitor() {
122 public void visitFile(File currentFile) {
123
124 String path = currentFile.getPath();
125
126 int startIndex = path.indexOf("org" + File.separator + "kuali");
127
128 if (startIndex != -1) {
129
130 String subPath = path.substring(startIndex);
131
132 String[] parts = subPath.split(StringEscapeUtils.escapeJava(File.separator));
133
134 StringBuilder packageBuilder = new StringBuilder();
135
136 for (int i = 0; i < parts.length-1; i++) {
137 if (i != 0)
138 packageBuilder.append(".");
139
140 packageBuilder.append(parts[i]);
141 }
142
143 String pkg = packageBuilder.toString();
144
145 localPackages.add(pkg);
146 }
147 }
148 });
149 }
150
151 if (contextSourceDirs != null)
152 modelSourceDirectories.addAll(contextSourceDirs);
153
154 ServiceContractModel instance = new ServiceContractModelQDoxLoader(
155 new ArrayList<String>(modelSourceDirectories));
156
157 return new ServiceContractModelCache(instance);
158 }
159
160 protected final boolean validate(ServiceContractModel model) {
161 Collection<String> errors = new ServiceContractModelValidator(model)
162 .validate();
163 if (errors.size() > 0) {
164
165 StringBuilder buf = new StringBuilder();
166 buf.append(errors.size()).append(
167 " errors found while validating the data.");
168 int cnt = 0;
169 for (String msg : errors) {
170 cnt++;
171 buf.append("\n");
172 buf.append("*error*").append(cnt).append(":").append(msg);
173 }
174
175 buf.append(errors.size()).append(
176 " errors found while validating the data.");
177 return false;
178 }
179 return true;
180 }
181
182 }