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.JarEntry;
26  import java.util.jar.Manifest;
27  
28  import org.apache.maven.shared.jar.classes.JarClasses;
29  import org.apache.maven.shared.jar.identification.JarIdentification;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * Class that contains details of a single JAR file and it's entries.
34   */
35  public final class JarData {
36      /**
37       * The JAR file.
38       */
39      private final File file;
40  
41      /**
42       * Whether the JAR file is sealed.
43       */
44      private final boolean aSealed;
45  
46      /**
47       * The hashcode for the entire file's contents.
48       */
49      private String fileHash;
50  
51      /**
52       * The hashcode for the file's class data contents.
53       */
54      private String bytecodeHash;
55  
56      /**
57       * The JAR's manifest.
58       */
59      private final Manifest manifest;
60  
61      /**
62       * Information about the JAR's classes.
63       */
64      private JarClasses jarClasses;
65  
66      /**
67       * The JAR entries.
68       */
69      private final List<JarEntry> entries;
70  
71      /**
72       * Information about the JAR's identifying features.
73       */
74      private JarIdentification jarIdentification;
75  
76      /**
77       * Constructor.
78       *
79       * @param file     the JAR file
80       * @param manifest the JAR manifest
81       * @param entries  the JAR entries
82       */
83      public JarData(File file, Manifest manifest, List<JarEntry> entries) {
84          this.file = file;
85  
86          this.manifest = manifest;
87  
88          this.entries = Collections.unmodifiableList(entries);
89  
90          boolean aSealed = false;
91          if (this.manifest != null) {
92              String sval = this.manifest.getMainAttributes().getValue(Attributes.Name.SEALED);
93              if (StringUtils.isNotEmpty(sval)) {
94                  aSealed = "true".equalsIgnoreCase(sval.trim());
95              }
96          }
97          this.aSealed = aSealed;
98      }
99  
100     public List<JarEntry> getEntries() {
101         return entries;
102     }
103 
104     public Manifest getManifest() {
105         return manifest;
106     }
107 
108     public File getFile() {
109         return file;
110     }
111 
112     public boolean isSealed() {
113         return aSealed;
114     }
115 
116     public void setFileHash(String fileHash) {
117         this.fileHash = fileHash;
118     }
119 
120     public String getFileHash() {
121         return fileHash;
122     }
123 
124     public void setBytecodeHash(String bytecodeHash) {
125         this.bytecodeHash = bytecodeHash;
126     }
127 
128     public String getBytecodeHash() {
129         return bytecodeHash;
130     }
131 
132     public boolean isDebugPresent() {
133         return jarClasses.isDebugPresent();
134     }
135 
136     public void setJarClasses(JarClasses jarClasses) {
137         this.jarClasses = jarClasses;
138     }
139 
140     public int getNumEntries() {
141         return entries.size();
142     }
143 
144     public int getNumClasses() {
145         return jarClasses.getClassNames().size();
146     }
147 
148     public int getNumPackages() {
149         return jarClasses.getPackages().size();
150     }
151 
152     public String getJdkRevision() {
153         return jarClasses.getJdkRevision();
154     }
155 
156     public void setJarIdentification(JarIdentification jarIdentification) {
157         this.jarIdentification = jarIdentification;
158     }
159 
160     public JarIdentification getJarIdentification() {
161         return jarIdentification;
162     }
163 
164     public JarClasses getJarClasses() {
165         return jarClasses;
166     }
167 }