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