FIF's 코딩팩토리

[Java]어댑터 패턴(Adapter pattern)을 이용한 통합 FTP,SFTP프로그램 본문

Back-End/Java(자바)

[Java]어댑터 패턴(Adapter pattern)을 이용한 통합 FTP,SFTP프로그램

FIF 2019. 5. 14. 15:38
반응형

Adapter를 우리말로 바꿔보면 변환기? 정도입니다.

무엇을 변환할 것인가?

핸드폰을 예를들어 봅시다.

핸드폰을 콘센트에 직접 접속시켜 충전하지 않고 충전기라는 어댑터를 통해 핸드폰과 콘센트를 연결해 충전합니다.

 

왜 이런 어댑터를 사용할까요?

바로 기존에 이미 잘 구축되어있는 것을 새로운 어떤 것이 사용할 때, 양쪽간의 호환성을 유지해주기 위함입니다.

자바에서도 직접적으로 메소드를 호출하지 않고 중간에 어댑터를 거쳐 메소드를 호줄하도록 하는 패턴이 바로 어댑터 패턴 입니다.

 

프로그램을 보면 직접 호출하지 않고 왜 저렇게 거치고 거쳐서 호출하는 걸까라는 생각이 들 수있습니다. 만약 직접 호출이 안되는 경우이거나, 기존에 있는 것을 변환해서 호출해야 하는 경우를 생각하면 기존에 있는 Protocol를 수정하지 않은 상태에서 interface와 Adapter만 수정하여 Main에서 원하는 메소드로 변화 시켜 호출 시켜 주는 것이라 생각하면 이해에 도움이 될것입니다.

 

 

Connectable.java

package program;

public interface Connectable {
	public abstract void myFTPClient();

	public abstract void mySFTPClient();
}

 

 

AdapterProtocolByObject.java

package program;
public class AdapterProtocolByObject implements Connectable {
	Protocol protocol = new Protocol();
	@Override
	public void myFTPClient() {
		protocol.myFTPClient();
	}

	@Override
	public void mySFTPClient() {
		protocol.mySFTPClient();
	}
}


 

 

Protocol.java

package program;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Scanner;
import java.util.Vector;

import javax.crypto.KeyAgreement;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class Protocol {

	void myFTPClient() {
		FTPClient ftpClient = new FTPClient();
		// 서버접속
		Scanner scanner = new Scanner(System.in);
		System.out.print("호스트 주소 입력: ");
		String server = scanner.nextLine();
		try {
			ftpClient.connect(server, 21);
			if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
				System.out.println(server + "에 연결됐습니다.");
				System.out.println(ftpClient.getReplyCode() + " SUCCESS CONNECTION.");
			}
		} catch (Exception e) {
			System.out.println("서버 연결 실패");
			System.exit(0);
		}
		// 계정입력
		System.out.print("계정 입력: ");
		String user = scanner.nextLine();
		System.out.print("비밀번호 입력: ");
		String password = scanner.nextLine();
		try {
			ftpClient.login(user, password);
			System.out.println(ftpClient.getReplyCode() + " Login successful.");
		} catch (IOException e) {
			System.out.println(ftpClient.getReplyCode() + " Login incorrect.");
			System.exit(0);
		}

		// 명령어시작
		while (true) {
			System.out.print("ftp> ");
			String str = "";
			str = scanner.nextLine();
			String[] params = str.split(" ");
			String command = params[0];
			// 명령어 시작
			if (command.equals("cd")) {
				String path = params[1];
				try {
					ftpClient.changeWorkingDirectory(path);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end cd
			else if (command.equals("mkdir")) {
				String directory = params[1];
				try {
					ftpClient.makeDirectory(directory);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end mkdir
			else if (command.equals("rmdir")) {
				String directory = params[1];
				try {
					ftpClient.removeDirectory(directory);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end rmdir
			else if (command.equals("binary")) {
				try {
					ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
					System.out.println(ftpClient.getReplyCode() + " Switching to Binary mode.");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end binary
			else if (command.equals("ascii")) {
				try {
					ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
					System.out.println(ftpClient.getReplyCode() + "  Switching to ASCII mode.");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end ascii
			else if (command.equals("pwd")) {
				try {
					System.out.println(ftpClient.printWorkingDirectory());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end pwd
			else if (command.equals("quit")) {
				try {
					ftpClient.logout();
					System.out.println(ftpClient.getReplyCode() + " Goodbye.");
					break;
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end quit
			else if (command.equals("put")) {
				String p1 = str.split(" ")[1];
				String p2 = str.split(" ")[2];

				File putFile = new File(p2);
				InputStream inputStream = null;

				try {
					inputStream = new FileInputStream(putFile);
					boolean result = ftpClient.storeFile(p1, inputStream);
					if (result == true) {
						System.out.println("업로드 성공");
					} else {
						System.out.println("업로드 실패");
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (inputStream != null) {
						try {
							inputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			} // end put
			else if (command.equals("get")) {
				String p1 = str.split(" ")[1];
				String p2 = str.split(" ")[2];

				File getFile = new File(p2);
				OutputStream outputStream = null;

				try {
					outputStream = new FileOutputStream(getFile);
					boolean result = ftpClient.retrieveFile(p1, outputStream);
					if (result == true) {
						System.out.println("다운로드 성공");
					} else {
						System.out.println("다운로드 실패");
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					if (outputStream != null) {
						try {
							outputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			} // end get
			else if (command.equals("delete")) {
				String file = str.split(" ")[1];
				try {
					ftpClient.deleteFile(file);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end rm
			else if (command.equals("ls")) {
				String[] files = null;
				try {
					files = ftpClient.listNames();
					for (int i = 0; i < files.length; i++) {
						System.out.println(files[i]);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end ls
			else if (command.equals("dir")) {
				FTPFile[] files = null;
				try {
					files = ftpClient.listFiles();
					for (int i = 0; i < files.length; i++) {
						System.out.println(files[i]);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			} // end dir
		} // end while
		try {
			ftpClient.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			scanner.close();
		}
		System.exit(0);

	}

	void mySFTPClient() {
		// https://lahuman.jabsiri.co.kr/152
		// DH알고리즘을 쓰기위한 코드
		Security.addProvider(new BouncyCastleProvider());
		try {
			KeyPairGenerator.getInstance("DH");
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		}
		try {
			KeyAgreement.getInstance("DH");
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		}

		Session session = null;
		Channel channel = null;
		JSch jsch = new JSch();

		Scanner scanner = new Scanner(System.in);
		System.out.print("계정 입력: ");
		String username = scanner.nextLine();
		System.out.print("호스트 주소 입력: ");
		String host = scanner.nextLine();
		System.out.print("비밀번호 입력: ");
		String password = scanner.nextLine();

		try {
			// 세션 객체 생성
			session = jsch.getSession(username, host, 22);
			// 비밀번호설정
			session.setPassword(password);
			// 호스트 정보를 검사하지 않음
			session.setConfig("StrictHostKeyChecking", "no");
			// 세션접속
			session.connect();
			// sftp채널열기
			channel = session.openChannel("sftp");
			// 채널접속
			channel.connect();
			System.out.println("Connected to user@" + host);
		} catch (JSchException e) {
			e.printStackTrace();
			System.out.println("접속에 실패했습니다.");
			// 실패시 시스템 종료
			System.exit(0);
		}
		ChannelSftp channelSftp = (ChannelSftp) channel;
		while (true) {
			System.out.print("sftp> ");
			String str = "";
			str = scanner.nextLine();

			String[] params = str.split(" ");
			String command = params[0];

			if (command.equals("cd")) {
				String p1 = params[1];// 스플릿의 과부하를 줄인다..왜? 변수명으로 바꼈자나
				try {
					channelSftp.cd(p1);
				} catch (SftpException e) {
					System.out.println("Couldn't stat remote file: No such file or directory");
				}
			} // end cd
			else if (command.equals("lcd")) {
				// lcd C:\Users\solulink
				String p1 = params[1];
				try {
					channelSftp.lcd(p1);
				} catch (SftpException e) {

					System.out.println("Couldn't change local directory to " + p1 + ": No such file or directory");
				}
			} // end lcd
			else if (command.equals("pwd")) {
				try {
					System.out.println("Remote working directory: " + channelSftp.pwd());
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} // end pwd
			else if (command.equals("lpwd")) {
				// lpwd
				System.out.println("Local working directory: " + channelSftp.lpwd());
			} // end lpwd
			else if (command.equals("get")) {
				try {
					if (params.length == 2) {
						channelSftp.get(params[1]);
					} else {
						channelSftp.get(params[1], params[2]);
					}
				} catch (SftpException e) {
					System.out.println("Ex)get centos.txt C:\\Users\\solulink");
				}
			} // end get
			else if (command.equals("put")) {
				String p1 = str.split(" ")[1];
				try {
					channelSftp.put(p1);

				} catch (SftpException e) {
					System.out.println("Ex)put window.txt");
				}
			} // end put
			else if (command.equals("ls") || command.equals("dir")) {
				String path = ".";
				try {
					// 가변길이의 배열
					Vector vector = channelSftp.ls(path);
					if (vector != null) {
						for (int i = 0; i < vector.size(); i++) {
							Object obj = vector.elementAt(i);
							if (obj instanceof ChannelSftp.LsEntry) {
								System.out.println(((ChannelSftp.LsEntry) obj).getLongname());
							}
						}
					}
				} catch (SftpException e) {
					System.out.println(e.toString());
				}
			} // end ls
			else if (command.equals("rm")) {
				try {
					String p1 = str.split(" ")[1];
					channelSftp.rm(p1);
				} catch (SftpException e) {
					System.out.println("Couldn't delete file: No such file or directory");
				}
			} // end rm
			else if (command.equals("mkdir")) {
				String p1 = str.split(" ")[1];
				try {
					channelSftp.mkdir(p1);
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} // end mkdir
			else if (command.equals("rmdir")) {
				String p1 = str.split(" ")[1];
				try {
					channelSftp.rmdir(p1);
				} catch (SftpException e) {
					System.out.println("Couldn't remove diretory: No such file or directory");
				}
			} // end rmdir
			else if (command.equals("chmod")) {
				// 접근권한 설정
				// chmod 777 window.txt(rwx:7 x:1 wx:3 r-x:5)
				String p1 = str.split(" ")[1];
				String p2 = str.split(" ")[2];
				try {
					channelSftp.chmod(Integer.parseInt(p1), p2);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} // end chmod
			else if (command.equals("chown")) {
				// 파일소유권변경->일반계정에 root권한 부여(vi /etc/passwd->UID와GID변경-하면안됨)
				// 리눅스에서 cat /etc/passwd
				// jinpyolee : 1000 sftpuser : 1004
				// chown 1000 window.txt
				String p1 = str.split(" ")[1];
				String p2 = str.split(" ")[2];
				try {
					channelSftp.chown(Integer.parseInt(p1), p2);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} // end chown
			else if (command.equals("ln") || (command.equals("symlink"))) {
				// 링크파일 생성(rwxrwxrwx, 리눅스에서 하늘색으로 나옴)
				// ln window.txt win.txt
				String p1 = str.split(" ")[1];
				String p2 = str.split(" ")[2];
				try {
					channelSftp.symlink(p1, p2);
				} catch (SftpException e) {
					e.printStackTrace();
				}
			} // end ln
			else if (command.equals("quit")) {
				channelSftp.quit();
				// 반복문 나가서 종료
				break;
			} // end quit
			else {
				System.out.println("Invalid command.");
			}
		} // end while
			// 연결해제
		channelSftp.disconnect();
		// 스캐너자원반납
		scanner.close();
		// 시스템종료
		System.exit(0);

	}
}

 

Main.java

package program;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Connectable connect = new AdapterProtocol();
		//Connectable connect = new AdapterProtocolByObject();
		Scanner sc = new Scanner(System.in);
		String protocol = sc.nextLine();

		if (protocol.equals("ftp") || protocol.equals("FTP")) {
			connect.myFTPClient();
		} else if (protocol.equals("sftp") || protocol.equals("SFTP")) {
			connect.mySFTPClient();
		}
		sc.close();
	}// end main()
}

 

반응형
Comments