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.creadur.tentacles;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.util.HashSet;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  
30  public class Archive {
31  
32      private final Layout layout;
33      private final FileSystem fileSystem;
34      private final URI uri;
35      private final File file;
36      private final Map<URI, URI> map;
37  
38      private final Set<License> licenses = new HashSet<License>();
39      private final Set<Notice> notices = new HashSet<Notice>();
40  
41      private final Set<License> declaredLicenses = new HashSet<License>();
42      private final Set<Notice> declaredNotices = new HashSet<Notice>();
43  
44      private final Set<License> otherLicenses = new HashSet<License>();
45      private final Set<Notice> otherNotices = new HashSet<Notice>();
46      private Map<URI, URI> others;
47  
48      public Archive(final File file, final FileSystem fileSystem,
49              final Layout layout) {
50          this.fileSystem = fileSystem;
51          this.layout = layout;
52          this.uri =
53                  layout.getRepositoryDirectory().toURI()
54                          .relativize(file.toURI());
55          this.file = file;
56          this.map = map();
57      }
58  
59      public Set<License> getDeclaredLicenses() {
60          return this.declaredLicenses;
61      }
62  
63      public Set<Notice> getDeclaredNotices() {
64          return this.declaredNotices;
65      }
66  
67      public Set<License> getOtherLicenses() {
68          return this.otherLicenses;
69      }
70  
71      public Set<Notice> getOtherNotices() {
72          return this.otherNotices;
73      }
74  
75      public Set<License> getLicenses() {
76          return this.licenses;
77      }
78  
79      public Set<Notice> getNotices() {
80          return this.notices;
81      }
82  
83      public URI getUri() {
84          return this.uri;
85      }
86  
87      public File getFile() {
88          return this.file;
89      }
90  
91      public Map<URI, URI> getLegal() {
92          return this.map;
93      }
94  
95      public Map<URI, URI> getOtherLegal() {
96          if (this.others == null) {
97              this.others = mapOther();
98          }
99          return this.others;
100     }
101 
102     private Map<URI, URI> mapOther() {
103         final File jarContents = contentsDirectory();
104         final List<File> legal =
105                 this.fileSystem.legalDocumentsUndeclaredIn(jarContents);
106 
107         return buildMapFrom(jarContents, legal);
108     }
109 
110     private Map<URI, URI> buildMapFrom(final File jarContents,
111             final List<File> legal) {
112         final Map<URI, URI> map = new LinkedHashMap<URI, URI>();
113         for (final File file : legal) {
114             final URI name = jarContents.toURI().relativize(file.toURI());
115             final URI link =
116                     this.layout.getLocalRootDirectory().toURI()
117                             .relativize(file.toURI());
118 
119             map.put(name, link);
120         }
121         return map;
122     }
123 
124     private Map<URI, URI> map() {
125         final File jarContents = contentsDirectory();
126         final List<File> legal =
127                 this.fileSystem.legalDocumentsDeclaredIn(jarContents);
128 
129         return buildMapFrom(jarContents, legal);
130     }
131 
132     public File contentsDirectory() {
133         final File archiveDocument = getFile();
134         String path =
135                 archiveDocument.getAbsolutePath().substring(
136                         this.layout.getLocalRootDirectory().getAbsolutePath()
137                                 .length() + 1);
138 
139         if (path.startsWith("repo/")) {
140             path = path.substring("repo/".length());
141         }
142         if (path.startsWith("content/")) {
143             path = path.substring("content/".length());
144         }
145 
146         final File contents =
147                 new File(this.layout.getContentRootDirectory(), path
148                         + ".contents");
149         this.fileSystem.mkdirs(contents);
150         return contents;
151     }
152 
153     public URI contentsURI() {
154         return contentsDirectory().toURI();
155     }
156 }