001: /*
002: * This file is part of Thingamablog. ( http://thingamablog.sf.net )
003: *
004: * Copyright (c) 2004, Bob Tantlinger All Rights Reserved.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
019: * USA.
020: */
021: package net.sf.thingamablog.transport;
022:
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.nio.channels.FileChannel;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: /**
032: * Transport for publishing weblogs to a local or network drive
033: *
034: * @author Bob Tantlinger
035: *
036: */
037: public class LocalTransport implements PublishTransport {
038: private static Logger logger = Logger
039: .getLogger("net.sf.thingamablog.transport");
040: private boolean isConnected;
041: private String failure = "";
042:
043: /* (non-Javadoc)
044: * @see net.sf.thingamablog.transport.PublishTransport#connect()
045: */
046: public boolean connect() {
047: failure = "";
048: isConnected = true;
049: return true;
050: }
051:
052: public boolean publishFile(String pubPath, File file,
053: TransportProgress tp) {
054: File dir = new File(pubPath);
055: if (file.isDirectory() || !isConnected || tp.isAborted()) {
056: failure = "Publish aborted";
057: return false;
058: }
059:
060: if (!dir.exists() || !dir.isDirectory()) {
061: boolean result = dir.mkdirs();
062: if (!result)//couldn't access pubPath
063: {
064: failure = "Unable to create the path: "
065: + dir.getAbsolutePath();
066: return false;
067: }
068: }
069:
070: try {
071: // Create channel on the source
072: FileChannel srcChannel = new FileInputStream(file)
073: .getChannel();
074:
075: File dest = new File(dir, file.getName());
076: // Create channel on the destination
077: FileChannel dstChannel = new FileOutputStream(dest)
078: .getChannel();
079:
080: // Copy file contents from source to destination
081: dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
082:
083: // Close the channels
084: srcChannel.close();
085: dstChannel.close();
086:
087: tp.bytesTransferred(file.length());
088:
089: return true;
090: } catch (IOException e) {
091: failure = e.getMessage();
092: logger.log(Level.WARNING, failure, e);
093: }
094:
095: return false;
096: }
097:
098: /* (non-Javadoc)
099: * @see net.sf.thingamablog.transport.PublishTransport#disconnect()
100: */
101: public boolean disconnect() {
102: isConnected = false;
103: return true;
104: }
105:
106: public boolean isConnected() {
107: return isConnected;
108: }
109:
110: public String getFailureReason() {
111: return failure;
112: }
113: }
|