All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dangdang.config.service.file.protocol.Protocols Maven / Gradle / Ivy

There is a newer version: 3.3.2-RELEASE
Show newest version
package com.dangdang.config.service.file.protocol;

import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;

/**
 * Protocol factory
 * 
 * @author Yuxuan Wang
 *
 */
public final class Protocols {

	private Map> protocols;

	private static Protocols INSTANCE = new Protocols();

	private static final String REGISTER_FILE = "META-INF/toolkit/com.dangdang.config.service.file.protocol.Protocol";

	private Protocols() {
		try {
			Properties props = new Properties();

			// Load register file contents
			Enumeration registerFiles = this.getClass().getClassLoader().getResources(REGISTER_FILE);
			URL registerFile = null;
			while (registerFiles.hasMoreElements()) {
				registerFile = registerFiles.nextElement();
				try (InputStream in = registerFile.openStream()) {
					props.load(in);
				}
			}

			// Initialize protocol beans
			protocols = Maps.newHashMap();
			for (Map.Entry entry : props.entrySet()) {
				final String protocolName = ((String) entry.getKey()).toLowerCase();
				@SuppressWarnings("unchecked")
				final Class protocolBeanClazz = (Class) Class.forName((String) entry.getValue());
				protocols.put(protocolName, protocolBeanClazz);
			}
		} catch (Exception e) {
			throw Throwables.propagate(e);
		}
	}

	public static Protocols getInstance() {
		return INSTANCE;
	}

	public Class get(String protocolName) {
		return Preconditions.checkNotNull(protocols.get(protocolName), "Procotol with name {} not registered.", protocolName);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy