001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.maven.plugin.xsd;
018
019 import java.io.BufferedReader;
020 import java.io.BufferedWriter;
021 import java.io.File;
022 import java.io.FileNotFoundException;
023 import java.io.FileReader;
024 import java.io.FileWriter;
025 import java.io.IOException;
026 import java.io.PrintWriter;
027 import java.util.ArrayList;
028 import java.util.List;
029
030 import org.apache.maven.plugin.AbstractMojo;
031 import org.apache.maven.plugin.MojoExecutionException;
032 import org.apache.maven.plugin.MojoFailureException;
033 import org.apache.maven.project.MavenProject;
034
035 /**
036 * Mojo to add schema location mapping for earlier versions to the
037 * spring.schemas file
038 *
039 * @goal spring.schemas
040 * @phase process-resources
041 */
042 public class SpringSchemasMojo extends AbstractMojo {
043
044 /**
045 * Maven project property containing previous releases
046 */
047 private static final String PREVIOUS_RELEASES = "previous.releases";
048
049 /**
050 * Previously released versions of ServiceMix
051 */
052 private String[] previous = {};
053
054 /**
055 * Location URIs
056 */
057 @SuppressWarnings("serial")
058 private List<String> locations = new ArrayList<String>() {
059 @Override
060 public boolean add(String element) {
061 getLog().info("Adding location " + element);
062 return super.add(element);
063 }
064 };
065
066 /**
067 * Name of the XSD file
068 */
069 private String schema;
070
071 /**
072 * A reference to the Maven project
073 *
074 * @parameter expression="${project}"
075 * @required
076 * @readonly
077 */
078 protected MavenProject project;
079
080 /**
081 * {@inheritDoc}
082 */
083 public void execute() throws MojoExecutionException, MojoFailureException {
084 if (!getSpringSchemas().exists()) {
085 getLog().info("Skipping - spring.schemas file does not exist in " + project.getArtifactId());
086 return;
087 }
088 readOriginalSpringSchemas();
089 getLog().info("Adding spring.schemas entries for earlier versions");
090 if (project.getProperties().containsKey(PREVIOUS_RELEASES)) {
091 previous = ((String) project.getProperties().get(PREVIOUS_RELEASES)).split(",");
092 } else {
093 getLog().warn("No previous version information found");
094 for (Object key : project.getProperties().keySet()) {
095 getLog().info("'" + key.toString() + "'" + " = " + project.getProperties().get(key));
096 }
097 }
098 for (String version : previous) {
099 addVersion(version);
100 }
101 getLog().info("Adding spring.schemas entry for this version");
102 addVersion(project.getVersion());
103 writeNewSpringSchemas();
104 }
105
106 /**
107 * Write the new spring.schemas file
108 *
109 * @throws MojoExecutionException
110 */
111 private void writeNewSpringSchemas() throws MojoExecutionException {
112 getLog().info("Writing new spring.schemas file");
113 PrintWriter writer = null;
114 try {
115 writer = new PrintWriter(new BufferedWriter(new FileWriter(getSpringSchemas())));
116 for (String location : locations) {
117 writer.println(location + "=" + schema);
118 }
119 writer.flush();
120 } catch (IOException e) {
121 throw new MojoExecutionException("Unable to read spring.schemas file", e);
122 } finally {
123 if (writer != null) {
124 writer.close();
125 }
126 }
127 }
128
129 /**
130 * Read the original spring.schemas file
131 *
132 * @throws MojoExecutionException
133 */
134 private void readOriginalSpringSchemas() throws MojoExecutionException {
135 getLog().info("Reading information from spring.schemas");
136 BufferedReader reader = null;
137 try {
138 reader = new BufferedReader(new FileReader(getSpringSchemas()));
139 String line = reader.readLine();
140 while (line != null) {
141 //skip any lines that don't contain a mapping
142 if (line.contains("=")) {
143 String[] info = line.split("=");
144 if (schema == null) {
145 getLog().info("Schema name is " + info[1]);
146 schema = info[1];
147 }
148 locations.add(info[0]);
149 }
150 line = reader.readLine();
151 }
152 } catch (FileNotFoundException e) {
153 throw new MojoExecutionException("Unable to read spring.schemas file", e);
154 } catch (IOException e) {
155 throw new MojoExecutionException("Unable to read spring.schemas file", e);
156 } finally {
157 if (reader != null) {
158 try {
159 reader.close();
160 } catch (IOException e) {
161 throw new MojoExecutionException("Unable to close file reader", e);
162 }
163 }
164 }
165 }
166
167 /**
168 * @return the spring.schemas file generated by xbean
169 */
170 private File getSpringSchemas() {
171 return new File(project.getBasedir().toString() + File.separatorChar + "target" + File.separatorChar + "classes" + File.separatorChar + "META-INF", "spring.schemas");
172 }
173
174 /**
175 * Add a version of the XSD to the spring.schemas file
176 *
177 * @param version the version to be added
178 */
179 private void addVersion(String version) {
180 locations.add("http\\://servicemix.apache.org/schema/" + project.getArtifactId() + "-" + version + ".xsd");
181 }
182
183 }