/Users/lyon/j4p/src/net/scp/ScpModel.java
|
1 package net.scp;
2
3 import com.jcraft.jsch.UserInfo;
4 import gui.In;
5 import xml.Utils;
6
7 import java.beans.PropertyChangeEvent;
8 import java.beans.PropertyChangeSupport;
9 import java.io.*;
10 import java.util.prefs.Preferences;
11
12 /**
13 * Copyright DocJava, inc. User: lyon
14 * <p/>
15 * Date: Dec 13, 2004
16 * <p/>
17 * Time: 6:31:14 AM
18 */
19 public class ScpModel implements ScpModelChangeEventListener,
20 Serializable {
21 private String user;
22 private String host;
23
24 private String localFile;
25 private String remoteFile;
26 private String password;
27
28 private transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
29 private static String key = "net.ScpModel";
30
31
32 public ScpModel() {
33
34 }
35
36 public void addModelChangeListener(ScpModelChangeEventListener smcl) {
37 pcs.addPropertyChangeListener(smcl);
38 }
39
40 public String getUser() {
41 return user;
42 }
43
44 public void setUser(String user) {
45 this.user = user;
46 }
47
48 public void update() {
49 if (isValidData())
50 pcs.firePropertyChange("ScpModel", "changeIt", this);
51 }
52
53 public String getHost() {
54 return host;
55 }
56
57 public void setHost(String host) {
58 this.host = host;
59
60 }
61
62 public String getLocalFile() {
63 return localFile;
64 }
65
66 public void setLocalFile(String localFile) {
67 this.localFile = localFile;
68
69 }
70
71 public String getRemoteFile() {
72 return remoteFile;
73 }
74
75 public void setRemoteFile(String remoteFile) {
76 this.remoteFile = remoteFile;
77 }
78
79
80 private boolean isNull(Object o) {
81 return o == null;
82 }
83
84 public boolean isValidData() {
85 final boolean rfNull = isNull(remoteFile);
86 final boolean lfNull = isNull(localFile);
87 final boolean uNull = isNull(user);
88 final boolean pwNull = isNull(host);
89 if (
90 rfNull |
91 lfNull |
92 uNull |
93 pwNull)
94 return false;
95 return true;
96 }
97
98 public void setPassword(String password) {
99 this.password = password;
100 }
101
102 public static ScpModel fromXml(String xmlString) {
103 return (ScpModel) Utils.decodeXml(xmlString);
104 }
105
106 public String toXml() {
107 return Utils.toXml(this) + "</java>";
108 }
109
110 public String toString() {
111 return toXml();
112 }
113
114 /**
115 * saves the properties to the Preferences of the userRoot
116 */
117 public void save() {
118 try {
119 Preferences p = Preferences.userRoot();
120 ByteArrayOutputStream baos = new
121 ByteArrayOutputStream();
122 ObjectOutputStream oos = new
123 ObjectOutputStream(baos);
124 oos.writeObject(this);
125 baos.close();
126 p.putByteArray(key, baos.toByteArray());
127 } catch (IOException e) {
128 e.printStackTrace();
129 }
130 }
131 public static void main(String[] args) {
132 ScpModel sm = new ScpModel();
133 sm.setHost("192.168.1.96");
134 sm.setUser("lyon");
135 sm.setPassword("invalid");
136 sm.setRemoteFile("/home/lyon/foo");
137 sm.setLocalFile("/home/lyon/foo");
138 sm.save();
139 sm = ScpModel.restore();
140 System.out.println(sm);
141 }
142
143 /**
144 * restores the properties from the preference in the user root.
145 */
146 public static ScpModel restore() {
147 try {
148 Preferences p = Preferences.userRoot();
149 byte b [] = p.getByteArray(key, null);
150 if (b == null)
151 return new ScpModel();
152 ByteArrayInputStream bais = new
153 ByteArrayInputStream(b);
154 ObjectInputStream ois = new
155 ObjectInputStream(bais);
156 Object o = ois.readObject();
157 bais.close();
158 return (ScpModel) o;
159 } catch (IOException e) {
160 e.printStackTrace();
161 } catch (ClassNotFoundException e) {
162 e.printStackTrace();
163 }
164 return new ScpModel();
165 }
166
167 public void propertyChange(PropertyChangeEvent evt) {
168
169 };
170
171 public void scpModelChanged(ScpModelChangeEvent modelChangedEvent) {
172 ScpModel m = modelChangedEvent.getScpModel();
173 setHost(m.getHost());
174 setLocalFile(m.getLocalFile());
175 setRemoteFile(m.getRemoteFile());
176 setUser(m.getUser());
177 setPassword(m.getPassword());
178 pcs.firePropertyChange(modelChangedEvent);
179 }
180
181 public UserInfo getUserInfo() {
182 return new MyUserInfo();
183 }
184
185 public String getPassword() {
186 return password;
187 }
188
189 public void upload() {
190 ScpTo.scpTo(getLocalFile(),
191 getUser(),
192 getHost(),
193 getRemoteFile(),
194 getUserInfo());
195 }
196
197 private class MyUserInfo implements UserInfo {
198 public String getPassphrase() {
199 return null;
200 }
201
202 public String getPassword() {
203 return password;
204 }
205
206 public boolean promptPassword(String s) {
207 return true;
208 }
209
210 public boolean promptPassphrase(String s) {
211 return true;
212 }
213
214 public boolean promptYesNo(String s) {
215 return true;
216 }
217
218 public void showMessage(String s) {
219 In.message(s);
220 }
221 }
222 }
223