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       * Information about the JAR's identifying features.
82       */
83      private JarIdentification jarIdentification;
84  
85      /**
86       * Information about the JAR's Multi-Release entries
87       */
88      private JarVersionedRuntimes versionedRuntimes;
89  
90      /**
91       * Constructor.
92       *
93       * @param file     the JAR file
94       * @param manifest the JAR manifest
95       * @param entries  the JAR entries
96       */
97      public JarData(File file, Manifest manifest, List<JarEntry> entries) {
98          this.file = file;
99  
100         this.manifest = manifest;
101 
102         this.entries = Collections.unmodifiableList(entries);
103 
104         this.aSealed = isAttributePresent(Attributes.Name.SEALED);
105         this.multiRelease = isAttributePresent(ATTR_MULTI_RELEASE);
106     }
107 
108     public List<JarEntry> getEntries() {
109         return entries;
110     }
111 
112     public Manifest getManifest() {
113         return manifest;
114     }
115 
116     public File getFile() {
117         return file;
118     }
119 
120     public boolean isSealed() {
121         return aSealed;
122     }
123 
124     public boolean isMultiRelease() {
125         return multiRelease;
126     }
127 
128     public void setFileHash(String fileHash) {
129         this.fileHash = fileHash;
130     }
131 
132     public String getFileHash() {
133         return fileHash;
134     }
135 
136     public void setBytecodeHash(String bytecodeHash) {
137         this.bytecodeHash = bytecodeHash;
138     }
139 
140     public String getBytecodeHash() {
141         return bytecodeHash;
142     }
143 
144     public boolean isDebugPresent() {
145         return jarClasses.isDebugPresent();
146     }
147 
148     public void setJarClasses(JarClasses jarClasses) {
149         this.jarClasses = jarClasses;
150     }
151 
152     public int getNumEntries() {
153         return entries.size();
154     }
155 
156     public int getNumClasses() {
157         return jarClasses.getClassNames().size();
158     }
159 
160     public int getNumPackages() {
161         return jarClasses.getPackages().size();
162     }
163 
164     public String getJdkRevision() {
165         return jarClasses.getJdkRevision();
166     }
167 
168     public void setJarIdentification(JarIdentification jarIdentification) {
169         this.jarIdentification = jarIdentification;
170     }
171 
172     public JarIdentification getJarIdentification() {
173         return jarIdentification;
174     }
175 
176     public JarClasses getJarClasses() {
177         return jarClasses;
178     }
179 
180     public void setVersionedRuntimes(JarVersionedRuntimes versionedRuntimess) {
181         this.versionedRuntimes = versionedRuntimess;
182     }
183 
184     public JarVersionedRuntimes getVersionedRuntimes() {
185         return this.versionedRuntimes;
186     }
187 
188     private boolean isAttributePresent(Attributes.Name attrName) {
189         if (this.manifest != null) {
190             String sval = this.manifest.getMainAttributes().getValue(attrName);
191             return sval != null && "true".equalsIgnoreCase(sval.trim());
192         }
193         return false;
194     }
195 }