View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.buildcache.xml;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.nio.file.Files;
30  
31  import org.apache.maven.buildcache.xml.build.Build;
32  import org.apache.maven.buildcache.xml.build.io.xpp3.BuildCacheBuildXpp3Reader;
33  import org.apache.maven.buildcache.xml.build.io.xpp3.BuildCacheBuildXpp3Writer;
34  import org.apache.maven.buildcache.xml.config.CacheConfig;
35  import org.apache.maven.buildcache.xml.config.io.xpp3.BuildCacheConfigXpp3Reader;
36  import org.apache.maven.buildcache.xml.config.io.xpp3.BuildCacheConfigXpp3Writer;
37  import org.apache.maven.buildcache.xml.diff.Diff;
38  import org.apache.maven.buildcache.xml.diff.io.xpp3.BuildCacheDiffXpp3Reader;
39  import org.apache.maven.buildcache.xml.diff.io.xpp3.BuildCacheDiffXpp3Writer;
40  import org.apache.maven.buildcache.xml.report.CacheReport;
41  import org.apache.maven.buildcache.xml.report.io.xpp3.BuildCacheReportXpp3Reader;
42  import org.apache.maven.buildcache.xml.report.io.xpp3.BuildCacheReportXpp3Writer;
43  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44  
45  /**
46   * XmlService
47   */
48  @Singleton
49  @Named
50  public class XmlService {
51  
52      public byte[] toBytes(CacheConfig cache) throws IOException {
53          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
54              new BuildCacheConfigXpp3Writer().write(baos, cache);
55              return baos.toByteArray();
56          }
57      }
58  
59      public byte[] toBytes(Build build) throws IOException {
60          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
61              new BuildCacheBuildXpp3Writer().write(baos, build);
62              return baos.toByteArray();
63          }
64      }
65  
66      public byte[] toBytes(Diff diff) throws IOException {
67          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
68              new BuildCacheDiffXpp3Writer().write(baos, diff);
69              return baos.toByteArray();
70          }
71      }
72  
73      public byte[] toBytes(CacheReport cacheReportType) throws IOException {
74          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
75              new BuildCacheReportXpp3Writer().write(baos, cacheReportType);
76              return baos.toByteArray();
77          }
78      }
79  
80      public Build loadBuild(File file) throws IOException {
81          return fromFile(Build.class, file);
82      }
83  
84      public Build loadBuild(byte[] bytes) {
85          return fromBytes(Build.class, bytes);
86      }
87  
88      public Build loadBuild(InputStream inputStream) {
89          return fromInputStream(Build.class, inputStream);
90      }
91  
92      public CacheConfig loadCacheConfig(File file) throws IOException {
93          return fromFile(CacheConfig.class, file);
94      }
95  
96      public CacheConfig loadCacheConfig(byte[] bytes) {
97          return fromBytes(CacheConfig.class, bytes);
98      }
99  
100     public CacheConfig loadCacheConfig(InputStream inputStream) {
101         return fromInputStream(CacheConfig.class, inputStream);
102     }
103 
104     public CacheReport loadCacheReport(File file) throws IOException {
105         return fromFile(CacheReport.class, file);
106     }
107 
108     public CacheReport loadCacheReport(byte[] bytes) {
109         return fromBytes(CacheReport.class, bytes);
110     }
111 
112     public CacheReport loadCacheReport(InputStream inputStream) {
113         return fromInputStream(CacheReport.class, inputStream);
114     }
115 
116     public Diff loadDiff(File file) throws IOException {
117         return fromFile(Diff.class, file);
118     }
119 
120     public Diff loadDiff(byte[] bytes) {
121         return fromBytes(Diff.class, bytes);
122     }
123 
124     public Diff loadDiff(InputStream inputStream) {
125         return fromInputStream(Diff.class, inputStream);
126     }
127 
128     private <T> T fromFile(Class<T> clazz, File file) throws IOException {
129         return fromInputStream(clazz, Files.newInputStream(file.toPath()));
130     }
131 
132     private <T> T fromBytes(Class<T> clazz, byte[] bytes) {
133         return fromInputStream(clazz, new ByteArrayInputStream(bytes));
134     }
135 
136     private <T> T fromInputStream(Class<T> clazz, InputStream inputStream) {
137         try {
138             if (clazz == Build.class) {
139                 return clazz.cast(new BuildCacheBuildXpp3Reader().read(inputStream));
140             } else if (clazz == CacheConfig.class) {
141                 return clazz.cast(new BuildCacheConfigXpp3Reader().read(inputStream));
142             } else if (clazz == Diff.class) {
143                 return clazz.cast(new BuildCacheDiffXpp3Reader().read(inputStream));
144             } else if (clazz == CacheReport.class) {
145                 return clazz.cast(new BuildCacheReportXpp3Reader().read(inputStream));
146             } else {
147                 throw new IllegalArgumentException("Unsupported type " + clazz);
148             }
149         } catch (IOException | XmlPullParserException e) {
150             throw new RuntimeException("Unable to parse cache xml element", e);
151         } finally {
152             closeQuietly(inputStream);
153         }
154     }
155 
156     private void closeQuietly(InputStream inputStream) {
157         try {
158             inputStream.close();
159         } catch (IOException e) {
160             // ignore
161         }
162     }
163 }