KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > ssh > AbstractSshBasedRepository


1 package fr.jayasoft.ivy.repository.ssh;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.net.URI JavaDoc;
6 import java.net.URISyntaxException JavaDoc;
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 JavaDoc keyFile = null;
16     private File JavaDoc passFile = null;
17     private String JavaDoc userPassword = null;
18     private String JavaDoc keyFilePassword = null;
19     private String JavaDoc user = "guest";
20     private String JavaDoc host = null;
21     private int port = -1;
22
23     public AbstractSshBasedRepository() {
24         super();
25     }
26
27     /**
28      * get a new session using the default attributes
29      * if the given String is a full uri, use the data from the uri
30      * instead
31      * @param pathOrUri might be just a path or a full ssh or sftp uri
32      * @return matching Session
33      */

34     protected Session getSession(String JavaDoc pathOrUri) throws IOException JavaDoc {
35         URI JavaDoc uri = parseURI(pathOrUri);
36         String JavaDoc host = getHost();
37         int port = getPort();
38         String JavaDoc user = getUser();
39         String JavaDoc 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 JavaDoc 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     /**
66      * Just check the uri for sanity
67      * @param source String of the uri
68      * @return URI object of the String or null
69      */

70     private URI JavaDoc parseURI(String JavaDoc source) {
71         try {
72             URI JavaDoc uri = new URI JavaDoc(source);
73             if(uri.getScheme() != null && !uri.getScheme().equalsIgnoreCase(getRepositoryScheme()))
74                 throw new URISyntaxException JavaDoc(source,"Wrong scheme in URI. Expected "+getRepositoryScheme()+" as scheme!");
75             if(uri.getHost() == null && getHost() == null)
76                 throw new URISyntaxException JavaDoc(source,"Missing host in URI or in resolver");
77             if(uri.getPath() == null)
78                 throw new URISyntaxException JavaDoc(source,"Missing path in URI");
79             if(uri.getUserInfo() == null && getUser() == null)
80                 throw new URISyntaxException JavaDoc(source,"Missing username in URI or in resolver");
81             return uri;
82         } catch (URISyntaxException JavaDoc 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     /**
91      * closes the session and remove it from the cache (eg. on case of errors)
92      * @param uri key for the cache
93      * @param conn to release
94      */

95     protected void releaseSession(Session session,String JavaDoc pathOrUri) {
96         session.disconnect();
97         SshCache.getInstance().clearSession(session);
98     }
99
100     /**
101      * set the default user to use for the connection if no user is given or a PEM file is used
102      * @param user to use
103      */

104     public void setUser(String JavaDoc user) {
105         this.user = user;
106     }
107
108     /**
109      * @return the user to use for the connection if no user is given or a PEM file is used
110      */

111     public String JavaDoc getUser() {
112         return user;
113     }
114
115     /**
116      * Sets the full file path to use for accessing a PEM key file
117      * @param filePath fully qualified name
118      */

119     public void setKeyFile(File JavaDoc 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     /**
133      * @return the keyFile
134      */

135     public File JavaDoc getKeyFile() {
136         return keyFile;
137     }
138
139     /**
140      * @param user password to use for user/password authentication
141      */

142     public void setUserPassword(String JavaDoc password) {
143         this.userPassword = password;
144     }
145
146     /**
147      * @return the keyFile password for public key based authentication
148      */

149     public String JavaDoc getKeyFilePassword() {
150         return keyFilePassword;
151     }
152     
153     /**
154      * @param keyFilePassword sets password for public key based authentication
155      */

156     public void setKeyFilePassword(String JavaDoc keyFilePassword) {
157         this.keyFilePassword = keyFilePassword;
158     }
159     /**
160      * @return the user password
161      */

162     public String JavaDoc getUserPassword() {
163         return userPassword;
164     }
165
166     /**
167      * @return the host
168      */

169     public String JavaDoc getHost() {
170         return host;
171     }
172     /**
173      * @param host the host to set
174      */

175     public void setHost(String JavaDoc host) {
176         this.host = host;
177     }
178
179     /**
180      * @return the port
181      */

182     public int getPort() {
183         return port;
184     }
185
186     /**
187      * @param port the port to set
188      */

189     public void setPort(int port) {
190         this.port = port;
191     }
192     
193     /**
194      * @param passFile the passfile to set
195      */

196     public void setPassFile(File JavaDoc passFile) {
197         this.passFile = passFile;
198     }
199     
200     /**
201      * @return the passFile
202      */

203     public File JavaDoc getPassFile() {
204         return passFile;
205     }
206
207     protected abstract String JavaDoc getRepositoryScheme();
208
209 }
Popular Tags