Java tutorial
package de.fhg.iais.cortex.storage.filesystem; /****************************************************************************** * Copyright 2011 (c) Fraunhofer IAIS Netmedia http://www.iais.fraunhofer.de * * ************************************************************************** * * Licensed under the Apache License, Version 2.0 (the "License"); you may * * not use this file except in compliance with the License. * * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * ******************************************************************************/ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.concurrent.ConcurrentHashMap; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.fhg.iais.commons.annotation.UsedBy; /** * @author fschulz */ @UsedBy("guice") public class ComponentMapper { private static final Logger LOG = LoggerFactory.getLogger(ComponentMapper.class); private final Map<String, ComponentMapping> componentMappingMap; private static final class ComponentMapping { private String componentName; private String xpath; private String storagePath; @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("ComponentMapping"); sb.append("{componentName='").append(componentName).append('\''); sb.append(", xpath='").append(xpath).append('\''); sb.append(", storagePath='").append(storagePath).append('\''); sb.append('}'); return sb.toString(); } } private ComponentMapper() { this.componentMappingMap = new ConcurrentHashMap<String, ComponentMapping>(); } @Inject public ComponentMapper(@Named("ComponentMapperMappingFile") String mappingFile) { this(); FileReader reader = null; try { reader = new FileReader(mappingFile); readMappingFile(reader); } catch (FileNotFoundException e) { LOG.error("", e); } finally { IOUtils.closeQuietly(reader); } } public ComponentMapper(Reader reader) { this(); readMappingFile(reader); } private void readMappingFile(Reader reader) { BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(reader); String line = ""; while ((line = bufferedReader.readLine()) != null) { if (!line.startsWith("#") && !line.trim().isEmpty()) { ComponentMapping mapping = parseLine(line); this.componentMappingMap.put(mapping.componentName, mapping); } } } catch (IOException e) { LOG.error("", e); } finally { IOUtils.closeQuietly(bufferedReader); } } private ComponentMapping parseLine(String line) { ComponentMapping mapping = new ComponentMapping(); StringTokenizer tokenizer = new StringTokenizer(line, ":,", true); mapping.componentName = tokenizer.nextToken().trim(); tokenizer.nextToken(); String tmpToken = tokenizer.nextToken().trim(); if (!",".equals(tmpToken)) { mapping.xpath = tmpToken; } else { mapping.xpath = ""; } tmpToken = tokenizer.nextToken().trim(); if (!",".equals(tmpToken)) { mapping.storagePath = tmpToken; } else { if (!tokenizer.hasMoreTokens()) { mapping.storagePath = ""; } else { mapping.storagePath = tokenizer.nextToken(); } } return mapping; } public String getXpath(String componentName) { ComponentMapping componentMapping = this.componentMappingMap.get(componentName); if (componentMapping == null) { return null; } return componentMapping.xpath; } public String getStoragePath(String componentName) { ComponentMapping componentMapping = this.componentMappingMap.get(componentName); if (componentMapping == null) { return null; } return componentMapping.storagePath; } public Set<String> getComponents() { return Collections.unmodifiableSet(this.componentMappingMap.keySet()); } }