This project has retired. For details please refer to its Attic page.
MetadataService xref
View Javadoc
1   package org.apache.archiva.metadata.repository;
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 org.apache.archiva.metadata.model.MetadataFacet;
23  import org.apache.archiva.metadata.model.MetadataFacetFactory;
24  import org.springframework.context.ApplicationContext;
25  import org.springframework.stereotype.Service;
26  
27  import javax.inject.Inject;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  /**
34   * @author Martin Stockhammer <martin_s@apache.org>
35   */
36  
37  @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" )
38  @Service("metadataService")
39  public class MetadataService
40  {
41  
42      private Map<String, MetadataFacetFactory<? extends MetadataFacet>> facetFactories = new HashMap<>( );
43      private Map<Class<? extends MetadataFacet>, MetadataFacetFactory<? extends MetadataFacet>> facetFactoriesByClass = new HashMap<>( );
44      private Map<String, Class<? extends MetadataFacet>> reverseFactoryMap = new HashMap<>( );
45  
46      private MetadataResolver metadataResolver = null;
47  
48      @Inject
49      ApplicationContext applicationContext;
50  
51  
52      @Inject
53      public void setMetadataFacetFactories( List<MetadataFacetFactory> factoryList ) {
54          Map<String, MetadataFacetFactory<? extends MetadataFacet>> facetFactories = new HashMap<>( );
55          Map<Class<? extends MetadataFacet>, MetadataFacetFactory<? extends MetadataFacet>> facetFactoriesByClass = new HashMap<>( );
56          Map<String, Class<? extends MetadataFacet>> reverseFactoryMap = new HashMap<>( );
57          for (MetadataFacetFactory factory : factoryList) {
58              facetFactories.put( factory.getFacetId( ), factory );
59              facetFactoriesByClass.put( factory.getFacetClass( ), factory );
60              reverseFactoryMap.put( factory.getFacetId( ), factory.getFacetClass( ) );
61          }
62          this.facetFactories = facetFactories;
63          this.facetFactoriesByClass = facetFactoriesByClass;
64          this.reverseFactoryMap = reverseFactoryMap;
65      }
66  
67      public <T extends MetadataFacet> MetadataFacetFactory<T> getFactory(Class<T> facetClazz) {
68          return (MetadataFacetFactory<T>) facetFactoriesByClass.get( facetClazz );
69      }
70  
71      public MetadataFacetFactory<?> getFactory(String facetId) {
72          return facetFactories.get( facetId );
73      }
74  
75      public Set<String> getSupportedFacets() {
76          return facetFactories.keySet( );
77      }
78  
79      public boolean supportsFacet(Class<? extends MetadataFacet> facetClazz) {
80          return facetFactoriesByClass.containsKey( facetClazz );
81      }
82  
83      public boolean supportsFacet(String facetId) {
84          return facetFactories.containsKey( facetId );
85      }
86  
87      public Class<? extends MetadataFacet> getFactoryClassForId( String facetId ) {
88          return reverseFactoryMap.get( facetId );
89      }
90  
91      // Lazy evaluation to avoid problems with circular dependencies during initialization
92      public MetadataResolver getMetadataResolver()
93      {
94          if ( this.metadataResolver == null && applicationContext!=null)
95          {
96              this.metadataResolver = applicationContext.getBean( MetadataResolver.class );
97          }
98          return this.metadataResolver;
99      }
100 }