import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
public class Main {
public static void main(String[] args) throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("context.xml"));
DownloadManager dm = (DownloadManager)xbf.getBean("downloadManager");
String uri = "http://www.java2java.com";
dm.download(uri);
}
}
class DownloadManager {
private static final Pattern URI_PATTERN = Pattern.compile("^([^:]*):/(.*)$");
private Map<String, Downloader> downloaders;
void download(String uri) {
Matcher matcher = URI_PATTERN.matcher(uri);
if (matcher.matches()) {
String scheme = matcher.group(1);
String path = matcher.group(2);
Downloader downloader = this.downloaders.get(scheme);
if (downloader != null) {
downloader.download(path);
}
}
}
public void setDownloaders(Map<String, Downloader> downloaders) {
this.downloaders = downloaders;
}
}
@Component
class SftpDownloader implements Downloader {
public byte[] download(String uri) {
System.out.println("SFTP Downloading " + uri);
return new byte[0];
}
}
@Component
class HttpDownloader implements Downloader {
public byte[] download(String uri) {
System.out.println("HTTP Downloading " + uri);
return new byte[0];
}
}
@Component
class FtpDownloader implements Downloader {
public byte[] download(String uri) {
System.out.println("FTP Downloading " + uri);
return new byte[0];
}
}
interface Downloader {
byte[] download(final String uri);
}
|