1 package fr.jayasoft.ivy.repository.ssh; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.URI ; 6 import java.net.URISyntaxException ; 7 8 import com.jcraft.jsch.Session; 9 10 import fr.jayasoft.ivy.repository.AbstractRepository; 11 import fr.jayasoft.ivy.util.Message; 12 13 public abstract class AbstractSshBasedRepository extends AbstractRepository { 14 15 private File keyFile = null; 16 private File passFile = null; 17 private String userPassword = null; 18 private String keyFilePassword = null; 19 private String user = "guest"; 20 private String host = null; 21 private int port = -1; 22 23 public AbstractSshBasedRepository() { 24 super(); 25 } 26 27 34 protected Session getSession(String pathOrUri) throws IOException { 35 URI uri = parseURI(pathOrUri); 36 String host = getHost(); 37 int port = getPort(); 38 String user = getUser(); 39 String userPassword = getUserPassword(); 40 if(uri != null && uri.getScheme() != null) { 41 if(uri.getHost() != null ) 42 host = uri.getHost(); 43 if(uri.getPort() != -1) { 44 port = uri.getPort(); 45 } 46 if(uri.getUserInfo() != null) { 47 String userInfo = uri.getUserInfo(); 48 if(userInfo.indexOf(":") == -1) { 49 user = userInfo; 50 } else { 51 user = userInfo.substring(0, userInfo.indexOf(":")); 52 userPassword = userInfo.substring(userInfo.indexOf(":")+1); 53 } 54 } 55 } 56 return SshCache.getInstance().getSession(host, 57 port, 58 user, 59 userPassword, 60 getKeyFile(), 61 getKeyFilePassword(), 62 getPassFile()); 63 } 64 65 70 private URI parseURI(String source) { 71 try { 72 URI uri = new URI (source); 73 if(uri.getScheme() != null && !uri.getScheme().equalsIgnoreCase(getRepositoryScheme())) 74 throw new URISyntaxException (source,"Wrong scheme in URI. Expected "+getRepositoryScheme()+" as scheme!"); 75 if(uri.getHost() == null && getHost() == null) 76 throw new URISyntaxException (source,"Missing host in URI or in resolver"); 77 if(uri.getPath() == null) 78 throw new URISyntaxException (source,"Missing path in URI"); 79 if(uri.getUserInfo() == null && getUser() == null) 80 throw new URISyntaxException (source,"Missing username in URI or in resolver"); 81 return uri; 82 } catch (URISyntaxException e) { 83 Message.error(e.getMessage()); 84 Message.error("The uri is in the wrong format."); 85 Message.error("Please use scheme://user:pass@hostname/path/to/repository"); 86 return null; 87 } 88 } 89 90 95 protected void releaseSession(Session session,String pathOrUri) { 96 session.disconnect(); 97 SshCache.getInstance().clearSession(session); 98 } 99 100 104 public void setUser(String user) { 105 this.user = user; 106 } 107 108 111 public String getUser() { 112 return user; 113 } 114 115 119 public void setKeyFile(File filePath) { 120 this.keyFile = filePath; 121 if(!keyFile.exists()) { 122 Message.warn("Pemfile "+keyFile.getAbsolutePath()+" doesn't exist."); 123 keyFile = null; 124 } else if(!keyFile.canRead()) { 125 Message.warn("Pemfile "+keyFile.getAbsolutePath()+" not readable."); 126 keyFile = null; 127 } else { 128 Message.debug("Using "+keyFile.getAbsolutePath()+" as keyfile."); 129 } 130 } 131 132 135 public File getKeyFile() { 136 return keyFile; 137 } 138 139 142 public void setUserPassword(String password) { 143 this.userPassword = password; 144 } 145 146 149 public String getKeyFilePassword() { 150 return keyFilePassword; 151 } 152 153 156 public void setKeyFilePassword(String keyFilePassword) { 157 this.keyFilePassword = keyFilePassword; 158 } 159 162 public String getUserPassword() { 163 return userPassword; 164 } 165 166 169 public String getHost() { 170 return host; 171 } 172 175 public void setHost(String host) { 176 this.host = host; 177 } 178 179 182 public int getPort() { 183 return port; 184 } 185 186 189 public void setPort(int port) { 190 this.port = port; 191 } 192 193 196 public void setPassFile(File passFile) { 197 this.passFile = passFile; 198 } 199 200 203 public File getPassFile() { 204 return passFile; 205 } 206 207 protected abstract String getRepositoryScheme(); 208 209 } | Popular Tags |