PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/JIST/src/jist/io/WiserFileSystem.java

http://webscheme.googlecode.com/
Java | 364 lines | 231 code | 53 blank | 80 comment | 11 complexity | 9608a956fefae7d8d9c2d9e3a52d6ecc MD5 | raw file
  1. package jist.io;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.net.URL;
  6. import java.util.Vector;
  7. import java.util.zip.CRC32;
  8. import org.apache.xmlrpc.Base64;
  9. import org.apache.xmlrpc.XmlRpcClient;
  10. /**
  11. * Abstraction of UC-WISE file system
  12. *
  13. * @author Turadg
  14. */
  15. public class WiserFileSystem {
  16. public static final char wiserSeparatorChar = '/';
  17. public static final String wiserSeparator = "" + wiserSeparatorChar;
  18. XmlRpcClient xmlrpc;
  19. public WiserFileSystem(URL codeBase) throws Exception {
  20. // Create XML RPC Client object, bind it to Wiser endpoint
  21. URL rpcUrl = new URL(codeBase, "/fm/xmlrpc.php");
  22. System.out.println("rpcUrl= " + rpcUrl);
  23. xmlrpc = new XmlRpcClient(rpcUrl);
  24. }
  25. public void put(File file, String value) throws IOException {
  26. System.out.println("put string ( " + file + " ) aka '"
  27. + file.getAbsolutePath() + "'");
  28. CRC32 checksum = new CRC32();
  29. checksum.update(value.getBytes());
  30. long crc = checksum.getValue();
  31. Vector params = new Vector();
  32. try {
  33. params.addElement(file.getAbsolutePath());
  34. } catch (Exception ex) {
  35. System.err.println("couldn't add param for : " + file);
  36. }
  37. // add data to payload
  38. params.addElement(value);
  39. Object result = null;
  40. try {
  41. // writeString should return an Integer
  42. result = xmlrpc.execute("writeString", params);
  43. } catch (Exception ex) {
  44. System.err.println("writeString failed :: " + ex);
  45. ex.printStackTrace();
  46. System.err.println();
  47. }
  48. int replyCRC = 0;
  49. try {
  50. replyCRC = ((Integer) result).intValue();
  51. } catch (Exception ex) {
  52. System.out.println("replyCRC: " + ex);
  53. }
  54. // PHP's CRC function returns a signed integer
  55. // so we need to convert Java's long to compare
  56. boolean success = (replyCRC == (int) crc);
  57. if (!success)
  58. throw new IOException("unmatched CRCs (" + replyCRC + "!=" + crc
  59. + ") for " + result + "(" + result.getClass() + ")");
  60. }
  61. public void put(File file, byte[] data) throws IOException {
  62. System.out.println("put binary ( " + file + " ) aka '"
  63. + file.getAbsolutePath() + "'");
  64. CRC32 checksum = new CRC32();
  65. checksum.update(data);
  66. long crc = checksum.getValue();
  67. Vector params = new Vector();
  68. try {
  69. params.addElement(file.getAbsolutePath());
  70. } catch (Exception ex) {
  71. System.err.println("couldn't add param for : " + file);
  72. }
  73. // add data to payload as base64
  74. params.addElement(Base64.encode(data));
  75. Object result = null;
  76. try {
  77. // writeBinary should return an Integer
  78. result = xmlrpc.execute("writeBinary", params);
  79. } catch (Exception ex) {
  80. System.err.println("writeBinary failed :: " + ex);
  81. ex.printStackTrace();
  82. System.err.println();
  83. }
  84. int replyCRC = 0;
  85. try {
  86. replyCRC = ((Integer) result).intValue();
  87. } catch (Exception ex) {
  88. System.out.println("replyCRC failed: " + ex);
  89. }
  90. // PHP's CRC function returns a signed integer
  91. // so we need to convert Java's long to compare
  92. boolean success = (replyCRC == (int) crc);
  93. if (!success)
  94. throw new IOException(
  95. "unmatched CRCs ("
  96. + replyCRC
  97. + "!="
  98. + crc
  99. + ") for "
  100. + result
  101. + "("
  102. + ((result == null) ? "null" : result.getClass()
  103. .toString()) + ")");
  104. }
  105. public String get(File file) throws FileNotFoundException {
  106. System.out.println("get( " + file + " )");
  107. System.out.println(" .getName(): " + file.getName());
  108. System.out.println(" .getAbsolutePath(): " + file.getAbsolutePath());
  109. Vector params = new Vector();
  110. params.addElement(file.getAbsolutePath());
  111. String result = "failed";
  112. try {
  113. Object tmp = xmlrpc.execute("readString", params);
  114. System.out.println("readString returned " + tmp);
  115. result = (String) tmp;
  116. } catch (Exception ex) {
  117. System.err.println("readString failed :: " + ex);
  118. throw new FileNotFoundException("error getting " + file + "::" + ex);
  119. }
  120. if (result == null)
  121. throw new FileNotFoundException(file + " does not exist");
  122. else
  123. return result;
  124. }
  125. public Vector listDirs(File file) {
  126. System.out.println("listDirs( " + file + " ) aka '"
  127. + file.getAbsolutePath() + "'");
  128. Vector params = new Vector();
  129. params.addElement(file.getAbsolutePath());
  130. Vector result = null;
  131. try {
  132. result = (Vector) xmlrpc.execute("listDirs", params);
  133. } catch (Exception ex) {
  134. System.err.println("listDirs failed :: " + ex);
  135. }
  136. System.out.println("listDirs returning: " + result);
  137. System.out
  138. .println("*OVERRIDE: return empty list until subdir saving works");
  139. if (result != null)
  140. return new Vector();
  141. return result;
  142. }
  143. public Vector listFiles(WiserFile file) {
  144. System.out.println("listFiles( " + file + " ) aka '"
  145. + file.getAbsolutePath() + "'");
  146. Vector params = new Vector();
  147. params.addElement(file.getAbsolutePath());
  148. Vector result = null;
  149. try {
  150. result = (Vector) xmlrpc.execute("listFiles", params);
  151. } catch (Exception ex) {
  152. System.err.println("listFiles failed :: " + ex);
  153. }
  154. System.out.println("listFiles returning: " + result);
  155. return result;
  156. }
  157. public boolean delete(WiserFile file) {
  158. System.out.println("delete( " + file + " ) aka '"
  159. + file.getAbsolutePath() + "'");
  160. Vector params = new Vector();
  161. params.addElement(file.getAbsolutePath());
  162. Boolean success = new Boolean(false);
  163. try {
  164. success = (Boolean) xmlrpc.execute("delete", params);
  165. } catch (Exception ex) {
  166. System.err.println("delete failed :: " + ex);
  167. }
  168. return success.booleanValue();
  169. }
  170. public boolean exists(WiserFile file) {
  171. Vector params = new Vector();
  172. params.addElement(file.getAbsolutePath());
  173. Boolean success = new Boolean(false);
  174. try {
  175. success = (Boolean) xmlrpc.execute("exists", params);
  176. } catch (Exception ex) {
  177. System.err.println("exists failed :: " + ex);
  178. }
  179. System.out.println("exists( " + file + " ) is " + success);
  180. return success.booleanValue();
  181. }
  182. public static void main(String args[]) throws Exception {
  183. WiserFileSystem fs = new WiserFileSystem(new URL(
  184. "http://turadg.ucdev.org/"));
  185. String data = "This_is_a_longish_short_string";
  186. String result;
  187. boolean success;
  188. WiserFile file = new WiserFile(fs, "test");
  189. System.out.println("putting bytes...");
  190. fs.put(file, data.getBytes());
  191. result = fs.get(file);
  192. System.out.println("OUT: " + data);
  193. System.out.println("IN: " + result);
  194. System.out.println("putting string...");
  195. fs.put(file, data);
  196. result = fs.get(file);
  197. System.out.println("OUT: " + data);
  198. System.out.println("IN: " + result);
  199. System.out.println("checking existance...");
  200. success = fs.exists(file);
  201. System.out.println("OUT: " + data);
  202. System.out.println("IN: " + success);
  203. System.out.println("deleting...");
  204. success = fs.delete(file);
  205. System.out.println("OUT: " + data);
  206. System.out.println("IN: " + success);
  207. System.out.println("checking existance again...");
  208. success = fs.exists(file);
  209. System.out.println("OUT: " + data);
  210. System.out.println("IN: " + success);
  211. }
  212. /* -- Normalization and construction -- */
  213. /**
  214. * Return the local filesystem's name-separator character.
  215. */
  216. public char getSeparator() {
  217. return '/';
  218. }
  219. /**
  220. * Return the local filesystem's path-separator character.
  221. */
  222. public char getPathSeparator() {
  223. return ':';
  224. }
  225. /**
  226. * Convert the given pathname string to normal form. If the string is
  227. * already in normal form then it is simply returned.
  228. */
  229. public String normalize(String path) {
  230. // FIX
  231. return path;
  232. }
  233. /**
  234. * Compute the length of this pathname string's prefix. The pathname string
  235. * must be in normal form.
  236. */
  237. public int prefixLength(String path) {
  238. // FIX ??
  239. return path.lastIndexOf(wiserSeparatorChar) + 1;
  240. }
  241. /**
  242. * Resolve the child pathname string against the parent. Both strings must
  243. * be in normal form, and the result will be in normal form.
  244. */
  245. public String resolve(String parent, String child) {
  246. // FIX handle .. and such
  247. return parent + getSeparator() + child;
  248. }
  249. /**
  250. * Return the parent pathname string to be used when the parent-directory
  251. * argument in one of the two-argument File constructors is the empty
  252. * pathname.
  253. */
  254. public String getDefaultParent() {
  255. // FIX
  256. return "" + getSeparator();
  257. }
  258. /**
  259. * Post-process the given URI path string if necessary. This is used on
  260. * win32, e.g., to transform "/c:/foo" into "c:/foo". The path string still
  261. * has slash separators; code in the File class will translate them after
  262. * this method returns.
  263. */
  264. public String fromURIPath(String path) {
  265. return path;
  266. }
  267. /* -- Path operations -- */
  268. /**
  269. * Tell whether or not the given abstract pathname is absolute.
  270. */
  271. public boolean isAbsolute(WiserFile f) {
  272. return f.getPath().charAt(0) == '/';
  273. }
  274. /**
  275. * Resolve the given abstract pathname into absolute form. Invoked by the
  276. * getAbsolutePath and getCanonicalPath methods in the File class.
  277. */
  278. public String resolve(WiserFile f) {
  279. // FIX
  280. if (isAbsolute(f))
  281. return f.getPath();
  282. else
  283. return "/" + f.getPath();
  284. }
  285. public String canonicalize(String path) {
  286. // TODO implement
  287. return path;
  288. }
  289. }
  290. /*
  291. * Copyright (c) 2004 Regents of the University of California (Regents). Created
  292. * by Graduate School of Education, University of California at Berkeley.
  293. *
  294. * This software is distributed under the GNU General Public License, v2.
  295. *
  296. * Permission is hereby granted, without written agreement and without license
  297. * or royalty fees, to use, copy, modify, and distribute this software and its
  298. * documentation for any purpose, provided that the above copyright notice and
  299. * the following two paragraphs appear in all copies of this software.
  300. *
  301. * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  302. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  303. * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  304. * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  305. * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  306. *
  307. * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  308. * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  309. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  310. * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  311. */