View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.repository.RemoteRepository;
27  import org.eclipse.aether.repository.RepositoryPolicy;
28  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
29  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
30  import org.eclipse.aether.spi.locator.Service;
31  import org.eclipse.aether.spi.locator.ServiceLocator;
32  import org.eclipse.aether.spi.log.LoggerFactory;
33  import org.eclipse.aether.spi.log.NullLoggerFactory;
34  import org.eclipse.aether.transfer.TransferResource;
35  
36  /**
37   */
38  @Named
39  public final class DefaultChecksumPolicyProvider
40      implements ChecksumPolicyProvider, Service
41  {
42  
43      private static final int ORDINAL_IGNORE = 0;
44  
45      private static final int ORDINAL_WARN = 1;
46  
47      private static final int ORDINAL_FAIL = 2;
48  
49      private LoggerFactory loggerFactory = NullLoggerFactory.INSTANCE;
50  
51      public DefaultChecksumPolicyProvider()
52      {
53          // enables default constructor
54      }
55  
56      @Inject
57      DefaultChecksumPolicyProvider( LoggerFactory loggerFactory )
58      {
59          setLoggerFactory( loggerFactory );
60      }
61  
62      public void initService( ServiceLocator locator )
63      {
64          setLoggerFactory( locator.getService( LoggerFactory.class ) );
65      }
66  
67      public DefaultChecksumPolicyProvider setLoggerFactory( LoggerFactory loggerFactory )
68      {
69          this.loggerFactory = loggerFactory;
70          return this;
71      }
72  
73      public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
74                                               TransferResource resource, String policy )
75      {
76          if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
77          {
78              return null;
79          }
80          if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
81          {
82              return new FailChecksumPolicy( loggerFactory, resource );
83          }
84          return new WarnChecksumPolicy( loggerFactory, resource );
85      }
86  
87      public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
88      {
89          if ( policy1 != null && policy1.equals( policy2 ) )
90          {
91              return policy1;
92          }
93          int ordinal1 = ordinalOfPolicy( policy1 );
94          int ordinal2 = ordinalOfPolicy( policy2 );
95          if ( ordinal2 < ordinal1 )
96          {
97              return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
98          }
99          else
100         {
101             return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
102         }
103     }
104 
105     private static int ordinalOfPolicy( String policy )
106     {
107         if ( RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
108         {
109             return ORDINAL_FAIL;
110         }
111         else if ( RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
112         {
113             return ORDINAL_IGNORE;
114         }
115         else
116         {
117             return ORDINAL_WARN;
118         }
119     }
120 
121 }