View Javadoc
1   /**
2    * Copyright 1&1 Internet AG, https://github.com/1and1/
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.oneandone.maven.plugins.billofmaterials;
17  
18  import com.google.common.base.Function;
19  import com.google.common.hash.HashCode;
20  import com.google.common.hash.HashFunction;
21  import com.google.common.io.Files;
22  import java.io.File;
23  import java.io.IOException;
24  
25  /**
26   * Creates a hashsum check for a single artifact.
27   * @author Mirko Friedenhagen
28   */
29  final class ToBomStringFunction implements Function<File, String> {
30      /**
31       * SHA1 algorithm.
32       */
33      private final HashFunction hashFunction;
34  
35      /**
36       * @param hashFunction to use.
37       */
38      ToBomStringFunction(HashFunction hashFunction) {
39          this.hashFunction = hashFunction;
40      }
41  
42      @Override
43      public String apply(final File file) {
44          final HashCode hash;
45          try {
46              hash = Files.hash(file, hashFunction);
47          } catch (IOException e) {
48              throw new IllegalArgumentException("Could not create hash for " + file, e);
49          }
50          return hash + "  " + file.getName();
51      }
52  
53  }