001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.serialization; 017 018import org.slf4j.Logger; 019import org.springframework.context.ApplicationContext; 020import org.springframework.context.ApplicationContextAware; 021import org.springframework.stereotype.Component; 022 023import javax.annotation.PostConstruct; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.Set; 027 028import static org.slf4j.LoggerFactory.getLogger; 029 030/** 031 * Helper service that aggregates serializers and makes them accessible by key 032 * 033 * @author cbeer 034 */ 035@Component 036public class SerializerUtil implements ApplicationContextAware { 037 038 private static final Logger LOGGER = getLogger(SerializerUtil.class); 039 040 private ApplicationContext applicationContext; 041 042 private Map<String, FedoraObjectSerializer> serializerMap; 043 044 @Override 045 public void setApplicationContext(final ApplicationContext applicationContext) { 046 this.applicationContext = applicationContext; 047 } 048 049 /** 050 * Get the list of Fedora serializer keys 051 * @return the list of Fedora serializer keys 052 */ 053 public Set<String> keySet() { 054 return getFedoraObjectSerializers().keySet(); 055 } 056 057 /** 058 * Get a Fedora Object Serializer by its key 059 * @param format the format 060 * @return FedoraObjectSerializer for the given format 061 */ 062 public FedoraObjectSerializer getSerializer(final String format) { 063 return getFedoraObjectSerializers().get(format); 064 } 065 066 /** 067 * Get the whole list of FedoraObjectSerializers 068 * @return map of all serializers with format as the key 069 */ 070 public Map<String, FedoraObjectSerializer> getFedoraObjectSerializers() { 071 return serializerMap; 072 } 073 074 /** 075 * Hook into Spring to get the list of all FedoraObjectSerializers that 076 * were (supposedly) component scanned, and register them in our own 077 * map. 078 */ 079 @PostConstruct 080 public void buildFedoraObjectSerializersMap() { 081 final Map<String, FedoraObjectSerializer> beans = 082 applicationContext.getBeansOfType(FedoraObjectSerializer.class); 083 084 final Map<String, FedoraObjectSerializer> m = new HashMap<>(); 085 086 for (Map.Entry<String, FedoraObjectSerializer> e : beans.entrySet()) { 087 final FedoraObjectSerializer serializer = e.getValue(); 088 LOGGER.info("Registering serializer {} for format {}", serializer, 089 serializer.getKey()); 090 m.put(serializer.getKey(), serializer); 091 } 092 093 serializerMap = m; 094 095 } 096}