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.model.building;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.Reader;
25  import java.util.Map;
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.io.ModelReader;
31  import org.apache.maven.model.locator.ModelLocator;
32  import org.eclipse.sisu.Typed;
33  
34  /**
35   *
36   * Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
37   *
38   * This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
39   * made this component available under all its interfaces then it could end up being injected
40   * into itself leading to a stack overflow.
41   *
42   * A side effect of using @Typed is that it translates to explicit bindings in the container.
43   * So instead of binding the component under a 'wildcard' key it is now bound with an explicit
44   * key. Since this is a default component this will be a plain binding of ModelProcessor to
45   * this implementation type, ie. no hint/name.
46   *
47   * This leads to a second side effect in that any @Inject request for just ModelProcessor in
48   * the same injector is immediately matched to this explicit binding, which means extensions
49   * cannot override this binding. This is because the lookup is always short-circuited in this
50   * specific situation (plain @Inject request, and plain explicit binding for the same type.)
51   *
52   * The simplest solution is to use a custom @Named here so it isn't bound under the plain key.
53   * This is only necessary for default components using @Typed that want to support overriding.
54   *
55   * As a non-default component this now gets a negative priority relative to other implementations
56   * of the same interface. Since we want to allow overriding this doesn't matter in this case.
57   * (if it did we could add @Priority of 0 to match the priority given to default components.)
58   */
59  @Named("core-default")
60  @Singleton
61  @Typed(ModelProcessor.class)
62  public class DefaultModelProcessor implements ModelProcessor {
63  
64      private final ModelLocator locator;
65      private final ModelReader reader;
66  
67      @Inject
68      public DefaultModelProcessor(ModelLocator locator, ModelReader reader) {
69          this.locator = locator;
70          this.reader = reader;
71      }
72  
73      @Override
74      public File locatePom(File projectDirectory) {
75          return locator.locatePom(projectDirectory);
76      }
77  
78      @Override
79      public Model read(File input, Map<String, ?> options) throws IOException {
80          return reader.read(input, options);
81      }
82  
83      @Override
84      public Model read(Reader input, Map<String, ?> options) throws IOException {
85          return reader.read(input, options);
86      }
87  
88      @Override
89      public Model read(InputStream input, Map<String, ?> options) throws IOException {
90          return reader.read(input, options);
91      }
92  }