View Javadoc
1   package org.eclipse.aether.internal.impl.filter;
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 java.io.IOException;
23  import java.io.UncheckedIOException;
24  import java.nio.file.Path;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
28  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
29  import org.eclipse.aether.util.ConfigUtils;
30  import org.eclipse.aether.util.DirectoryUtils;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * Support class for {@link RemoteRepositoryFilterSource} implementations.
36   * <p>
37   * Support class for implementing {@link RemoteRepositoryFilterSource}. It implements basic support
38   * like optional "basedir" calculation, handling of "enabled" flag.
39   * <p>
40   * The configuration keys supported:
41   * <ul>
42   *     <li><pre>aether.remoteRepositoryFilter.${id}.enabled</pre> (boolean) must be explicitly set to "true"
43   *     to become enabled</li>
44   *     <li><pre>aether.remoteRepositoryFilter.${id}.basedir</pre> (string, path) directory from where implementation
45   *     can use files. If unset, default value is ".remoteRepositoryFilters/${id}" and is resolved from local
46   *     repository basedir.</li>
47   * </ul>
48   *
49   * @since 1.9.0
50   */
51  public abstract class RemoteRepositoryFilterSourceSupport
52          implements RemoteRepositoryFilterSource
53  {
54      private static final String CONFIG_PROP_PREFIX = "aether.remoteRepositoryFilter.";
55  
56      private static final String CONF_NAME_BASEDIR = "basedir";
57  
58      static final String LOCAL_REPO_PREFIX_DIR = ".remoteRepositoryFilters";
59  
60      private final String name;
61  
62      protected RemoteRepositoryFilterSourceSupport( String name )
63      {
64          this.name = requireNonNull( name );
65      }
66  
67      /**
68       * Utility method to create scoped configuration property key of given name.
69       */
70      protected String configPropKey( String name )
71      {
72          return CONFIG_PROP_PREFIX + this.name + "." + name;
73      }
74  
75      /**
76       * Returns {@code true} if session configuration contains this name set to {@code true}.
77       * <p>
78       * Default is {@code false}.
79       */
80      protected boolean isEnabled( RepositorySystemSession session )
81      {
82          return ConfigUtils.getBoolean( session, false, CONFIG_PROP_PREFIX + this.name );
83      }
84  
85      /**
86       * Uses common {@link DirectoryUtils#resolveDirectory(RepositorySystemSession, String, String, boolean)} to
87       * calculate (and maybe create) basedir for this implementation, never returns {@code null}. The returned
88       * {@link Path} may not exists, if invoked with {@code mayCreate} being {@code false}.
89       * <p>
90       * Default value is {@code ${LOCAL_REPOSITORY}/.checksums}.
91       *
92       * @return The {@link Path} of basedir, never {@code null}.
93       */
94      protected Path getBasedir( RepositorySystemSession session, boolean mayCreate )
95      {
96          try
97          {
98              return DirectoryUtils.resolveDirectory(
99                      session, LOCAL_REPO_PREFIX_DIR, configPropKey( CONF_NAME_BASEDIR ), mayCreate );
100         }
101         catch ( IOException e )
102         {
103             throw new UncheckedIOException( e );
104         }
105     }
106 
107     /**
108      * Simple {@link RemoteRepositoryFilter.Result} immutable implementation.
109      */
110     protected static class SimpleResult implements RemoteRepositoryFilter.Result
111     {
112         private final boolean accepted;
113 
114         private final String reasoning;
115 
116         public SimpleResult( boolean accepted, String reasoning )
117         {
118             this.accepted = accepted;
119             this.reasoning = requireNonNull( reasoning );
120         }
121 
122         @Override
123         public boolean isAccepted()
124         {
125             return accepted;
126         }
127 
128         @Override
129         public String reasoning()
130         {
131             return reasoning;
132         }
133     }
134 }