CF::File_ptr FileManager_impl::open (const char* fileName, CORBA::Boolean read_Only)
    throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException)
{
    TRACE_ENTER(FileManager_impl);

    if (!ossie::isValidFileName(fileName)) {
        throw CF::InvalidFileName(CF::CF_EINVAL, "Invalid file name");
    }

    LOG_TRACE(FileManager_impl, "Opening file " << fileName << std::string((read_Only)?" readonly":" readwrite"));

    // Lock the mount table shared to allow others to access the file system,
    // but prevent changes to the mount table itself.
    boost::shared_lock<boost::shared_mutex> lock(mountsLock);

    MountList::iterator mount = getMountForPath(fileName);
    CF::File_var file;
    if (mount == mountedFileSystems.end()) {
        LOG_TRACE(FileManager_impl, "Opening local file");
        file = FileSystem_impl::open(fileName, read_Only);
    } else {
        const std::string filePath = mount->getRelativePath(fileName);
        LOG_TRACE(FileManager_impl, "Opening " << filePath << " on remote file system mounted at " << mount->path);
        file = mount->fs->open(filePath.c_str(), read_Only);
    }

    TRACE_EXIT(FileManager_impl);
    return file._retn();
}
static std::string getLogConfig(std::string uri)
{
    std::string localPath;

    std::string::size_type fsPos = uri.find("?fs=");
    if (std::string::npos == fsPos) {
        return localPath;
    }

    std::string IOR = uri.substr(fsPos + 4);
    CORBA::Object_var obj = ossie::corba::stringToObject(IOR);
    if (CORBA::is_nil(obj)) {
        return localPath;
    }

    CF::FileSystem_var fileSystem = CF::FileSystem::_narrow(obj);
    if (CORBA::is_nil(fileSystem)) {
        return localPath;
    }

    std::string remotePath = uri.substr(0, fsPos);
    CF::OctetSequence_var data;
    try {
        CF::File_var remoteFile = fileSystem->open(remotePath.c_str(), true);
        CORBA::ULong size = remoteFile->sizeOf();
        remoteFile->read(data, size);
    } catch (...) {
        return localPath;
    }

    std::string tempPath = remotePath;
    std::string::size_type slashPos = remotePath.find_last_of('/');
    if (std::string::npos != slashPos) {
        tempPath.erase(0, slashPos + 1);
    }
    std::fstream localFile(tempPath.c_str(), std::ios::out|std::ios::trunc);
    if (!localFile) {
        return localPath;
    }

    if (localFile.write((const char*)data->get_buffer(), data->length())) {
        localPath = tempPath;
    }
    localFile.close();

    return localPath;
}
void FileManager_impl::copy (const char* sourceFileName, const char* destinationFileName)
    throw (CORBA::SystemException, CF::InvalidFileName, CF::FileException)
{
    TRACE_ENTER(FileManager_impl);

    // Validate absolute file names
    if (sourceFileName[0] != '/' || !ossie::isValidFileName(sourceFileName)) {
        throw CF::InvalidFileName(CF::CF_EINVAL, "Invalid source file name");
    } else if (destinationFileName[0] != '/' || !ossie::isValidFileName(destinationFileName)) {
        throw CF::InvalidFileName(CF::CF_EINVAL, "Invalid destination file name");
    } else if (strcmp(sourceFileName, destinationFileName) == 0) {
        throw CF::InvalidFileName(CF::CF_EINVAL, "Destination file name is identical to source file name");
    }

    LOG_TRACE(FileManager_impl, "Copy " << sourceFileName << " to " << destinationFileName);

    // Lock the mount table shared to allow others to access the file system,
    // but prevent changes to the mount table itself.
    boost::shared_lock<boost::shared_mutex> lock(mountsLock);

    MountList::iterator sourceMount = getMountForPath(sourceFileName);
    MountList::iterator destMount = getMountForPath(destinationFileName);

    if (sourceMount == destMount) {
        // Source and destination are on the same file system...
        if (sourceMount == mountedFileSystems.end()) {
            // ...which is also the local file system.
            LOG_TRACE(FileManager_impl, "Copying locally");
            FileSystem_impl::copy(sourceFileName, destinationFileName);
        } else {
            // ...which is a remote file system.
            LOG_TRACE(FileManager_impl, "Copying locally on remote file system");
            const std::string srcPath = sourceMount->getRelativePath(sourceFileName);
            const std::string dstPath = destMount->getRelativePath(destinationFileName);
            sourceMount->fs->copy(srcPath.c_str(), dstPath.c_str());
        }
        return;
    }

    LOG_TRACE(FileManager_impl, "Copying between filesystems");

    // Open the source file (may be local).
    CF::File_var srcFile;
    if (sourceMount == mountedFileSystems.end()) {
        srcFile = FileSystem_impl::open(sourceFileName, true);
    } else {
        const std::string srcPath = sourceMount->getRelativePath(sourceFileName);
        srcFile = sourceMount->fs->open(srcPath.c_str(), true);
    }

    // Open the destination file (may be local).
    CF::File_var dstFile;
    if (destMount == mountedFileSystems.end()) {
        if (FileSystem_impl::exists(destinationFileName)) {
            FileSystem_impl::remove(destinationFileName);
        }
        dstFile = FileSystem_impl::create(destinationFileName);
    } else {
        const std::string dstPath = destMount->getRelativePath(destinationFileName);
        CF::FileSystem_ptr destFS = destMount->fs;
        if (destFS->exists(dstPath.c_str())) {
            destFS->remove(dstPath.c_str());
        }
        dstFile = destFS->create(dstPath.c_str());
    }

    std::ostringstream eout;
    bool fe=false;
    try {
      // Read the data
      CF::OctetSequence_var data;
      CORBA::ULong bytes = srcFile->sizeOf();
      const CORBA::ULong DEFAULT_CHUNK_SIZE =  ossie::corba::giopMaxMsgSize() * 0.95;
      while (bytes > 0) {
        // Read the file data in 1MB chunks. If omniORB uses the GIOP protocol to talk to the source filesystem
        // (i.e. it is in a different ORB), reads of more than about 2MB will exceed the maximum GIOP packet
        // size and raise a MARSHAL exception.
        CORBA::ULong chunkSize = std::min(bytes, DEFAULT_CHUNK_SIZE);
        bytes -= chunkSize;
        
        try {
          srcFile->read(data, chunkSize);
        } catch ( std::exception& ex ) {
          eout << "The following standard exception occurred: "<<ex.what()<<" While \"srcFile->read\"";
          throw(CF::FileException());
        } catch ( CF::FileException& ex ) {
          eout << "File Exception occured,  While \"srcFile->read\"";
          throw;
        } catch ( CF::File::IOException& ex ) {
          eout << "File IOException occured,  While \"srcFile->read\"";
          throw;
        } catch ( CORBA::Exception& ex ) {
          eout << "The following CORBA exception occurred: "<<ex._name()<<" While \"srcFile->read\"";
          throw(CF::FileException());
        } catch( ... ) {
          eout << "[FileManager::copy] \"srcFile->read\" failed with Unknown Exception\n";
          throw(CF::FileException());
        }

        // write the data
        try {
          dstFile->write(data);
        } catch ( std::exception& ex ) {
          std::ostringstream eout;
          eout << "The following standard exception occurred: "<<ex.what()<<" While \"dstFile->write\"";
          throw(CF::FileException());
        } catch ( CF::FileException& ex ) {
          eout << "File Exception occurred, during \"dstFile->write\"";
          throw;
        } catch ( CF::File::IOException& ex ) {
          eout << "File IOException occurred, during \"dstFile->write\"";
          throw;
        } catch ( CORBA::Exception& ex ) {
          eout << "The following CORBA exception occurred: "<<ex._name()<<" While \"dstFile->write\"";
          throw(CF::FileException());
        } catch( ... ) {
          eout << "[FileManager::copy] \"dstFile->write\" failed with Unknown Exception\n";
          throw(CF::FileException());
        }

      }
    }
    catch(...) {
      LOG_ERROR(FileManager_impl, eout.str());
      fe = true;
    }

   // close the files
    try {
      try {
        srcFile->close();
      } catch ( std::exception& ex ) {
        eout << "The following standard exception occurred: "<<ex.what()<<" While \"srcFile->close\"";
        throw(CF::FileException());
      } catch ( CF::FileException& ex ) {
        eout << "File Exception occured, during \"srcFile->close\"";
        throw;
      } catch ( CORBA::Exception& ex ) {
        eout << "The following CORBA exception occurred: "<<ex._name()<<" While \"srcFile->close\"";
        throw(CF::FileException());
      } catch( ... ) {
        eout << "[FileManager::copy] \"srcFile->close\" failed with Unknown Exception\n";
        throw(CF::FileException());
      }
    } catch(...) {
      LOG_ERROR(FileManager_impl, eout.str());
      fe = true;
    }


    try {
      try {
        dstFile->close();
      } catch ( std::exception& ex ) {
        eout << "The following standard exception occurred: "<<ex.what()<<" While \"dstFile->close\"";
        throw(CF::FileException());
      } catch ( CF::FileException& ex ) {
        eout << "File Exception occured, during \"srcFile->close\"";
        throw;
      } catch ( CORBA::Exception& ex ) {
        eout << "The following CORBA exception occurred: "<<ex._name()<<" While \"dstFile->close\"";
        throw(CF::FileException());
      } catch( ... ) {
        eout << "[FileManager::copy] \"dstFile->close\" failed with Unknown Exception\n";
        throw(CF::FileException());
      }
    } catch(...) {
      LOG_ERROR(FileManager_impl, eout.str());
      fe = true;
    }

    if ( fe ) {
        throw(CF::FileException());
    }

    TRACE_EXIT(FileManager_impl);
}