001package org.apache.archiva.metadata.repository; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.model.MetadataFacet; 023import org.apache.archiva.metadata.model.MetadataFacetFactory; 024import org.springframework.context.ApplicationContext; 025import org.springframework.stereotype.Service; 026 027import javax.inject.Inject; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031import java.util.Set; 032 033/** 034 * @author Martin Stockhammer <martin_s@apache.org> 035 */ 036 037@SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" ) 038@Service("metadataService") 039public class MetadataService 040{ 041 042 private Map<String, MetadataFacetFactory<? extends MetadataFacet>> facetFactories = new HashMap<>( ); 043 private Map<Class<? extends MetadataFacet>, MetadataFacetFactory<? extends MetadataFacet>> facetFactoriesByClass = new HashMap<>( ); 044 private Map<String, Class<? extends MetadataFacet>> reverseFactoryMap = new HashMap<>( ); 045 046 private MetadataResolver metadataResolver = null; 047 048 @Inject 049 ApplicationContext applicationContext; 050 051 052 @Inject 053 public void setMetadataFacetFactories( List<MetadataFacetFactory> factoryList ) { 054 Map<String, MetadataFacetFactory<? extends MetadataFacet>> facetFactories = new HashMap<>( ); 055 Map<Class<? extends MetadataFacet>, MetadataFacetFactory<? extends MetadataFacet>> facetFactoriesByClass = new HashMap<>( ); 056 Map<String, Class<? extends MetadataFacet>> reverseFactoryMap = new HashMap<>( ); 057 for (MetadataFacetFactory factory : factoryList) { 058 facetFactories.put( factory.getFacetId( ), factory ); 059 facetFactoriesByClass.put( factory.getFacetClass( ), factory ); 060 reverseFactoryMap.put( factory.getFacetId( ), factory.getFacetClass( ) ); 061 } 062 this.facetFactories = facetFactories; 063 this.facetFactoriesByClass = facetFactoriesByClass; 064 this.reverseFactoryMap = reverseFactoryMap; 065 } 066 067 public <T extends MetadataFacet> MetadataFacetFactory<T> getFactory(Class<T> facetClazz) { 068 return (MetadataFacetFactory<T>) facetFactoriesByClass.get( facetClazz ); 069 } 070 071 public MetadataFacetFactory<?> getFactory(String facetId) { 072 return facetFactories.get( facetId ); 073 } 074 075 public Set<String> getSupportedFacets() { 076 return facetFactories.keySet( ); 077 } 078 079 public boolean supportsFacet(Class<? extends MetadataFacet> facetClazz) { 080 return facetFactoriesByClass.containsKey( facetClazz ); 081 } 082 083 public boolean supportsFacet(String facetId) { 084 return facetFactories.containsKey( facetId ); 085 } 086 087 public Class<? extends MetadataFacet> getFactoryClassForId( String facetId ) { 088 return reverseFactoryMap.get( facetId ); 089 } 090 091 // Lazy evaluation to avoid problems with circular dependencies during initialization 092 public MetadataResolver getMetadataResolver() 093 { 094 if ( this.metadataResolver == null && applicationContext!=null) 095 { 096 this.metadataResolver = applicationContext.getBean( MetadataResolver.class ); 097 } 098 return this.metadataResolver; 099 } 100}