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.shared.jar;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.jar.Attributes;
25  import java.util.jar.Attributes.Name;
26  import java.util.jar.JarEntry;
27  import java.util.jar.Manifest;
28  
29  import org.apache.maven.shared.jar.classes.JarClasses;
30  import org.apache.maven.shared.jar.classes.JarVersionedRuntimes;
31  import org.apache.maven.shared.jar.identification.JarIdentification;
32  
33  /**
34   * Class that contains details of a single JAR file and it's entries.
35   */
36  public final class JarData {
37  
38      private static final Name ATTR_MULTI_RELEASE = new Attributes.Name("Multi-Release");
39  
40      /**
41       * The JAR file.
42       */
43      private final File file;
44  
45      /**
46       * Whether the JAR file is sealed.
47       */
48      private final boolean aSealed;
49  
50      /**
51       * Whether the JAR file is Multi-Release.
52       */
53      private boolean multiRelease;
54  
55      /**
56       * The hashcode for the entire file's contents.
57       */
58      private String fileHash;
59  
60      /**
61       * The hashcode for the file's class data contents.
62       */
63      private String bytecodeHash;
64  
65      /**
66       * The JAR's manifest.
67       */
68      private final Manifest manifest;
69  
70      /**
71       * Information about the JAR's classes.
72       */
73      private JarClasses jarClasses;
74  
75      /**
76       * The JAR entries.
77       */
78      private final List<JarEntry> entries;
79  
80      /**
81       * The JAR entries of the root content, when it is a multi-release JAR
82       */
83      private List<JarEntry> rootEntries;
84  
85      /**
86       * Information about the JAR's identifying features.
87       */
88      private JarIdentification jarIdentification;
89  
90      /**
91       * Information about the JAR's Multi-Release entries
92       */
93      private JarVersionedRuntimes versionedRuntimes;
94  
95      /**
96       * Constructor.
97       *
98       * @param file     the JAR file
99       * @param manifest the JAR manifest
100      * @param entries  the JAR entries
101      */
102     public JarData(File file, Manifest manifest, List<JarEntry> entries) {
103         this.file = file;
104 
105         this.manifest = manifest;
106 
107         this.entries = Collections.unmodifiableList(entries);
108 
109         this.aSealed = isAttributePresent(Attributes.Name.SEALED);
110         this.multiRelease = isAttributePresent(ATTR_MULTI_RELEASE);
111     }
112 
113     public List<JarEntry> getEntries() {
114         return entries;
115     }
116 
117     public List<JarEntry> getRootEntries() {
118         return rootEntries;
119     }
120 
121     public void setRootEntries(List<JarEntry> rootEntries) {
122         this.rootEntries = rootEntries;
123     }
124 
125     public Manifest getManifest() {
126         return manifest;
127     }
128 
129     public File getFile() {
130         return file;
131     }
132 
133     public boolean isSealed() {
134         return aSealed;
135     }
136 
137     public boolean isMultiRelease() {
138         return multiRelease;
139     }
140 
141     public void setFileHash(String fileHash) {
142         this.fileHash = fileHash;
143     }
144 
145     public String getFileHash() {
146         return fileHash;
147     }
148 
149     public void setBytecodeHash(String bytecodeHash) {
150         this.bytecodeHash = bytecodeHash;
151     }
152 
153     public String getBytecodeHash() {
154         return bytecodeHash;
155     }
156 
157     public boolean isDebugPresent() {
158         return jarClasses.isDebugPresent();
159     }
160 
161     public void setJarClasses(JarClasses jarClasses) {
162         this.jarClasses = jarClasses;
163     }
164 
165     public int getNumEntries() {
166         return entries.size();
167     }
168 
169     public int getNumRootEntries() {
170         return rootEntries.size();
171     }
172 
173     public int getNumClasses() {
174         return jarClasses.getClassNames().size();
175     }
176 
177     public int getNumPackages() {
178         return jarClasses.getPackages().size();
179     }
180 
181     public String getJdkRevision() {
182         return jarClasses.getJdkRevision();
183     }
184 
185     public void setJarIdentification(JarIdentification jarIdentification) {
186         this.jarIdentification = jarIdentification;
187     }
188 
189     public JarIdentification getJarIdentification() {
190         return jarIdentification;
191     }
192 
193     public JarClasses getJarClasses() {
194         return jarClasses;
195     }
196 
197     public void setVersionedRuntimes(JarVersionedRuntimes versionedRuntimes) {
198         this.versionedRuntimes = versionedRuntimes;
199     }
200 
201     public JarVersionedRuntimes getVersionedRuntimes() {
202         return this.versionedRuntimes;
203     }
204 
205     private boolean isAttributePresent(Attributes.Name attrName) {
206         if (this.manifest != null) {
207             String sval = this.manifest.getMainAttributes().getValue(attrName);
208             return sval != null && "true".equalsIgnoreCase(sval.trim());
209         }
210         return false;
211     }
212 }