001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.localfilesystem;
020:
021: import java.io.*;
022: import java.net.*;
023: import java.util.*;
024:
025: import org.openharmonise.vfs.*;
026: import org.openharmonise.vfs.authentication.*;
027: import org.openharmonise.vfs.metadata.*;
028: import org.openharmonise.vfs.search.*;
029: import org.openharmonise.vfs.status.*;
030:
031: /**
032: * @author Matthew Large
033: *
034: */
035: public class LocalFileSystem extends AbstractVirtualFileSystem {
036:
037: private VirtualFileCache m_cache = null;
038:
039: private VirtualFileSystemView m_vfView = null;
040:
041: /**
042: * @param uri
043: * @param sPathSeparator
044: */
045: public LocalFileSystem(URI uri) {
046: super (uri);
047: this .setup();
048: }
049:
050: /**
051: * @param uri
052: * @param authInfo
053: * @param sPathSeparator
054: */
055: public LocalFileSystem(URI uri, AuthInfo authInfo) {
056: super (uri, authInfo);
057: this .setup();
058: }
059:
060: /**
061: * @param uri
062: * @param authStore
063: * @param sPathSeparator
064: */
065: public LocalFileSystem(URI uri,
066: AbstractAuthenticationStore authStore) {
067: super (uri, authStore);
068: this .setup();
069: }
070:
071: private void setup() {
072: this .m_sInitialPath = this .m_sInitialPath.substring(1,
073: this .m_sInitialPath.length());
074:
075: this .m_sInitialPath = this .m_sInitialPath.replace('/', '\\');
076: this .m_cache = new VirtualFileCache();
077: }
078:
079: /* (non-Javadoc)
080: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getOptions()
081: */
082: public List getOptions() {
083: return null;
084: }
085:
086: /* (non-Javadoc)
087: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFile(java.lang.String)
088: */
089: public ResourceStatusWrapper getVirtualFile(String sFullPath) {
090: VirtualFile vfFile = null;
091:
092: String sRealPath = this .getInitialPath() + sFullPath;
093: sRealPath = sRealPath.replace('/', File.separatorChar);
094:
095: if (this .m_cache.hasFile(sFullPath)) {
096: vfFile = this .m_cache.getFile(sFullPath);
097: } else {
098: vfFile = new VirtualFile(sFullPath);
099: vfFile.setVFS(this );
100: File fRealFile = new File(sRealPath);
101: this .populateVirtualFile(fRealFile, vfFile);
102: this .m_cache.addFile(vfFile);
103: }
104:
105: if (vfFile == null) {
106: this .fireErrorEvent("FileNotFound", "The file " + sFullPath
107: + " cannot be found.");
108: }
109:
110: return new ResourceStatusWrapper(vfFile, new VFSStatus());
111: }
112:
113: private void populateVirtualFile(File fRealFile, VirtualFile vfFile) {
114: vfFile.setIsDirectory(fRealFile.isDirectory());
115:
116: File[] children = fRealFile.listFiles();
117: if (children != null) {
118: for (int i = 0; i < children.length; i++) {
119: vfFile.addChild(vfFile.getFullPath() + "/"
120: + children[i].getName());
121: }
122: }
123: this .setFileChildrenPopulated(vfFile, true);
124:
125: this .setFileMetadataPopulated(vfFile, true);
126: }
127:
128: /* (non-Javadoc)
129: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#addVirtualFile(java.lang.String, com.simulacramedia.vfs.VirtualFile)
130: */
131: public ResourceStatusWrapper addVirtualFile(String sPath,
132: VirtualFile content) {
133: VirtualFile vfFile = null;
134:
135: String sFullPath = sPath + "/" + content.getFileName();
136:
137: String sRealPath = this .getInitialPath() + sPath + "/"
138: + content.getFileName();
139: sRealPath = sRealPath.replace('/', File.separatorChar);
140:
141: if (this .m_cache.hasFile(sFullPath)) {
142: // NO-OP
143: } else {
144: File fRealFile = new File(sRealPath);
145: try {
146: FileOutputStream fos = new FileOutputStream(fRealFile);
147: fos.write(content.getContent());
148: } catch (FileNotFoundException e) {
149: this
150: .fireErrorEvent(
151: "FileNotAdded",
152: "The file "
153: + sFullPath
154: + " cannot be added here, this might be because a file already exists here with the same name.");
155: e.printStackTrace();
156: } catch (IOException e) {
157: this
158: .fireErrorEvent(
159: "FileNotAdded",
160: "The file "
161: + sFullPath
162: + " cannot be added here, this might be because a file already exists here with the same name.");
163: e.printStackTrace();
164: }
165: }
166:
167: if (vfFile == null) {
168: this
169: .fireErrorEvent(
170: "FileNotAdded",
171: "The file "
172: + sFullPath
173: + " cannot be added here, this might be because a file already exists here with the same name.");
174: }
175:
176: return new ResourceStatusWrapper(vfFile, new VFSStatus());
177: }
178:
179: /* (non-Javadoc)
180: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#moveVirtualFile(java.lang.String, java.lang.String)
181: */
182: public StatusData moveVirtualFile(String sFromFullPath,
183: String sToFullPath) {
184: VFSStatus retnStatus = new VFSStatus();
185:
186: boolean bError = false;
187:
188: String sRealFromPath = this .getInitialPath() + sFromFullPath;
189: sRealFromPath = sRealFromPath.replace('/', File.separatorChar);
190: String sRealToPath = this .getInitialPath() + sToFullPath;
191: sRealToPath = sRealToPath.replace('/', File.separatorChar);
192:
193: VirtualFile vfFromFile = this .getVirtualFile(sFromFullPath)
194: .getResource();
195: if (!vfFromFile.isDirectory()) {
196: byte[] content = vfFromFile.getContent();
197: File from = new File(sRealFromPath);
198: File to = new File(sRealToPath);
199:
200: FileInputStream fi = null;
201: FileOutputStream fo = null;
202:
203: try {
204: Long lLength = new Long(from.length());
205: byte[] bytes = new byte[lLength.intValue()];
206: fi = new FileInputStream(from);
207: fo = new FileOutputStream(to);
208:
209: fi.read(bytes);
210: fo.write(bytes);
211:
212: } catch (Exception e) {
213: e.printStackTrace(System.out);
214: this .fireErrorEvent("FileNotMoved",
215: "The original file " + sFromFullPath
216: + " cannot be removed.");
217: bError = true;
218: } finally {
219: try {
220: fo.close();
221: fi.close();
222: } catch (IOException ioe) {
223: this .fireErrorEvent("FileNotMoved",
224: "The original file " + sFromFullPath
225: + " cannot be removed.");
226: bError = true;
227: ioe.printStackTrace(System.out);
228: }
229: }
230:
231: if (!bError) {
232: from.delete();
233: }
234: }
235:
236: if (bError) {
237: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
238: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
239: }
240:
241: return retnStatus;
242: }
243:
244: /* (non-Javadoc)
245: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#copyVirtualFile(java.lang.String, java.lang.String)
246: */
247: public StatusData copyVirtualFile(String sFromFullPath,
248: String sToFullPath) {
249: VFSStatus retnStatus = new VFSStatus();
250:
251: boolean bError = false;
252:
253: String sRealFromPath = this .getInitialPath() + sFromFullPath;
254: sRealFromPath = sRealFromPath.replace('/', File.separatorChar);
255: String sRealToPath = this .getInitialPath() + sToFullPath;
256: sRealToPath = sRealToPath.replace('/', File.separatorChar);
257:
258: VirtualFile vfFromFile = this .getVirtualFile(sFromFullPath)
259: .getResource();
260: if (!vfFromFile.isDirectory()) {
261: byte[] content = vfFromFile.getContent();
262: File from = new File(sRealFromPath);
263: File to = new File(sRealToPath);
264:
265: FileInputStream fi = null;
266: FileOutputStream fo = null;
267:
268: try {
269: Long lLength = new Long(from.length());
270: byte[] bytes = new byte[lLength.intValue()];
271: fi = new FileInputStream(from);
272: fo = new FileOutputStream(to);
273:
274: fi.read(bytes);
275: fo.write(bytes);
276:
277: } catch (Exception e) {
278: e.printStackTrace(System.out);
279: this
280: .fireErrorEvent(
281: "FileNotCopied",
282: "The file "
283: + sFromFullPath
284: + " cannot be copied here, this might be because a file already exists here with the same name.");
285: bError = true;
286: } finally {
287: try {
288: fo.close();
289: fi.close();
290: } catch (IOException ioe) {
291: this
292: .fireErrorEvent(
293: "FileNotCopied",
294: "The file "
295: + sFromFullPath
296: + " cannot be copied here, this might be because a file already exists here with the same name.");
297: bError = true;
298: ioe.printStackTrace(System.out);
299: }
300: }
301: }
302:
303: if (bError) {
304: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
305: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
306: }
307:
308: return retnStatus;
309: }
310:
311: /* (non-Javadoc)
312: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#deleteVirtualFile(java.lang.String)
313: */
314: public StatusData deleteVirtualFile(String sFullPath) {
315: VFSStatus retnStatus = new VFSStatus();
316:
317: boolean bError = false;
318:
319: String sRealPath = this .getInitialPath() + sFullPath;
320: sRealPath = sRealPath.replace('/', File.separatorChar);
321:
322: File fRealFile = new File(sRealPath);
323:
324: bError = !fRealFile.delete();
325:
326: if (bError) {
327: this .fireErrorEvent("FileNotDeleted", "The file "
328: + sFullPath + " cannot be deleted.");
329: }
330:
331: if (bError) {
332: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
333: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
334: }
335:
336: return retnStatus;
337: }
338:
339: /* (non-Javadoc)
340: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#lockVirtualFile(java.lang.String)
341: */
342: public StatusData lockVirtualFile(String sFullPath) {
343: VFSStatus retnStatus = new VFSStatus();
344:
345: String sRealPath = this .getInitialPath() + sFullPath;
346: sRealPath = sRealPath.replace('/', File.separatorChar);
347: this .fireErrorEvent("FileNotLocked",
348: "This server does not support the locking of files.");
349:
350: return retnStatus;
351: }
352:
353: /* (non-Javadoc)
354: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#unlockVirtualFile(java.lang.String)
355: */
356: public StatusData unlockVirtualFile(String sFullPath) {
357: VFSStatus retnStatus = new VFSStatus();
358:
359: String sRealPath = this .getInitialPath() + sFullPath;
360: sRealPath = sRealPath.replace('/', File.separatorChar);
361: this .fireErrorEvent("FileNotUnlocked",
362: "This server does not support the locking of files.");
363:
364: return retnStatus;
365: }
366:
367: /* (non-Javadoc)
368: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createVirtualDirectory(java.lang.String)
369: */
370: public StatusData createVirtualDirectory(String sFullPath) {
371: VFSStatus retnStatus = new VFSStatus();
372:
373: boolean bError = false;
374:
375: String sRealPath = this .getInitialPath() + sFullPath;
376: sRealPath = sRealPath.replace('/', File.separatorChar);
377:
378: File fRealDir = new File(sRealPath);
379:
380: bError = fRealDir.mkdirs();
381:
382: if (!bError) {
383: this .fireErrorEvent("DirectoryNotCreated", "The directory "
384: + sFullPath + " cannot be created.");
385: }
386:
387: if (bError) {
388: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
389: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
390: }
391:
392: return retnStatus;
393: }
394:
395: /* (non-Javadoc)
396: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createShortcut(java.lang.String, java.lang.String)
397: */
398: public StatusData createShortcut(String sFullPath,
399: String sToFullPath) {
400: VFSStatus retnStatus = new VFSStatus();
401:
402: String sRealPath = this .getInitialPath() + sFullPath;
403: sRealPath = sRealPath.replace('/', File.separatorChar);
404: String sRealToPath = this .getInitialPath() + sToFullPath;
405: sRealToPath = sRealToPath.replace('/', File.separatorChar);
406: this .fireErrorEvent("ShortcutNotCreted",
407: "This server does not support shortcuts.");
408:
409: return retnStatus;
410: }
411:
412: /* (non-Javadoc)
413: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#search(com.simulacramedia.vfs.search.Query)
414: */
415: public ResourceListStatusWrapper search(Query query) {
416: return null;
417: }
418:
419: /* (non-Javadoc)
420: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileSystemView()
421: */
422: public VirtualFileSystemView getVirtualFileSystemView() {
423: if (this .m_vfView == null) {
424: this .m_vfView = new LocalFileSystemView();
425: }
426:
427: return this .m_vfView;
428: }
429:
430: /* (non-Javadoc)
431: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileContent(java.lang.String)
432: */
433: public byte[] getVirtualFileContent(String sFullPath) {
434: boolean bError = false;
435:
436: String sRealPath = this .getInitialPath() + sFullPath;
437: sRealPath = sRealPath.replace('/', File.separatorChar);
438:
439: byte[] content = null;
440:
441: File from = new File(sRealPath);
442: FileInputStream fi = null;
443:
444: try {
445: Long lLength = new Long(from.length());
446: content = new byte[lLength.intValue()];
447: fi = new FileInputStream(from);
448:
449: fi.read(content);
450: this .setFileContentPopulated(this .m_cache
451: .getFile(sFullPath), true);
452:
453: } catch (Exception e) {
454: e.printStackTrace(System.out);
455: this .fireErrorEvent("ContentNotRetrieved",
456: "The content for " + sFullPath
457: + " could not be retrieved.");
458: bError = true;
459: } finally {
460: try {
461: fi.close();
462: } catch (IOException ioe) {
463: this .fireErrorEvent("ContentNotRetrieved",
464: "The content for " + sFullPath
465: + " could not be retrieved.");
466: bError = true;
467: ioe.printStackTrace(System.out);
468: }
469: }
470:
471: return content;
472: }
473:
474: /* (non-Javadoc)
475: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileMetadata(com.simulacramedia.vfs.VirtualFile)
476: */
477: protected void fullyPopulateFileMetadata(VirtualFile vfFile) {
478: // NO-OP
479: }
480:
481: /* (non-Javadoc)
482: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileChildren(com.simulacramedia.vfs.VirtualFile)
483: */
484: protected void fullyPopulateFileChildren(VirtualFile vfFile) {
485: // NO-OP
486: }
487:
488: /* (non-Javadoc)
489: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#synchroniseFile(com.simulacramedia.vfs.VirtualFile)
490: */
491: public StatusData synchroniseFile(VirtualFile vfFile) {
492: VFSStatus retnStatus = new VFSStatus();
493:
494: boolean bError = false;
495:
496: if (vfFile.isChanged()) {
497: String sFullPath = vfFile.getFullPath();
498:
499: String sRealPath = this .getInitialPath() + sFullPath;
500: sRealPath = sRealPath.replace('/', File.separatorChar);
501:
502: FileOutputStream fos = null;
503:
504: try {
505: fos = new FileOutputStream(sRealPath);
506: fos.write(vfFile.getContent());
507: } catch (FileNotFoundException e) {
508: bError = true;
509: e.printStackTrace();
510: } catch (IOException e) {
511: bError = true;
512: e.printStackTrace();
513: } finally {
514: try {
515: fos.close();
516: } catch (IOException e1) {
517: e1.printStackTrace();
518: }
519: }
520: }
521:
522: if (bError) {
523: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
524: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
525: }
526:
527: return retnStatus;
528: }
529:
530: /* (non-Javadoc)
531: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#sunchroniseAllFiles()
532: */
533: public StatusData synchroniseAllFiles() {
534: VFSStatus retnStatus = new VFSStatus();
535:
536: boolean bError = false;
537:
538: Iterator itor = this .m_cache.getPaths().iterator();
539: while (itor.hasNext()) {
540: String sFullPath = (String) itor.next();
541: VirtualFile vfFile = this .m_cache.getFile(sFullPath);
542: if (vfFile.isChanged()) {
543: StatusData status = this .synchroniseFile(vfFile);
544: retnStatus.addStatusData(status);
545: if (!status.isOK()) {
546: bError = true;
547: }
548: }
549: }
550:
551: if (bError) {
552: retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
553: retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
554: }
555:
556: return retnStatus;
557: }
558:
559: /* (non-Javadoc)
560: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#exists(java.lang.String)
561: */
562: public boolean exists(String sFullPath) {
563: boolean bExists = true;
564:
565: String sRealPath = this .getInitialPath() + sFullPath;
566: sRealPath = sRealPath.replace('/', File.separatorChar);
567:
568: File file = new File(sRealPath);
569: bExists = file.exists();
570:
571: return bExists;
572: }
573:
574: /* (non-Javadoc)
575: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#refreshChildren(com.simulacramedia.vfs.VirtualFile)
576: */
577: protected void refreshChildren(VirtualFile vfFile) {
578: }
579:
580: /* (non-Javadoc)
581: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#orderVirtualFileChildren(java.util.List, com.simulacramedia.vfs.VirtualFile)
582: */
583: public StatusData orderVirtualFileChildren(List aPaths,
584: VirtualFile vfDir) {
585: VFSStatus retnStatus = new VFSStatus();
586: return retnStatus;
587: }
588:
589: /* (non-Javadoc)
590: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getNewValueInstance(com.simulacramedia.vfs.metadata.PropertyInstance)
591: */
592: public ValueInstance getNewValueInstance(PropertyInstance propInst) {
593: return null;
594: }
595:
596: /* (non-Javadoc)
597: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#rejectAllChanges()
598: */
599: public boolean rejectAllChanges() {
600: return false;
601: }
602:
603: /* (non-Javadoc)
604: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileAllowedMethods(com.simulacramedia.vfs.VirtualFile)
605: */
606: protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) {
607: }
608:
609: /* (non-Javadoc)
610: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#currentUserResourcePath()
611: */
612: public String currentUserResourcePath(AuthInfo authInfo) {
613: return null;
614: }
615:
616: /* (non-Javadoc)
617: * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getChangedVirtualFiles()
618: */
619: public List getChangedVirtualFiles() {
620: return new ArrayList();
621: }
622:
623: /* (non-Javadoc)
624: * @see org.openharmonise.vfs.AbstractVirtualFileSystem#getPropertyVirtualFile(java.lang.String)
625: */
626: public VirtualFile getPropertyVirtualFile(String sPropPath) {
627: return null;
628: }
629:
630: }
|