@Override
  public NetworkFile createFile(SrvSession sess, TreeConnection tree, FileOpenParams params)
      throws IOException {
    ContentContext tctx = (ContentContext) tree.getContext();

    FileAccessToken token = null;

    if (tctx.hasStateCache()) {
      FileStateCache cache = tctx.getStateCache();
      FileState fstate = tctx.getStateCache().findFileState(params.getPath(), true);
      token = cache.grantFileAccess(params, fstate, FileStatus.NotExist);
      if (logger.isDebugEnabled()) {
        logger.debug("create file created lock token:" + token);
      }
    }

    try {
      NetworkFile newFile = diskInterface.createFile(sess, tree, params);

      if (tctx.hasStateCache()) {
        FileState fstate = tctx.getStateCache().findFileState(params.getPath(), true);
        fstate.setProcessId(params.getProcessId());
        fstate.setSharedAccess(params.getSharedAccess());

        // Indicate that the file is open
        fstate.setFileStatus(
            newFile.isDirectory() ? FileStatus.DirectoryExists : FileStatus.FileExists);
        fstate.setAllocationSize(params.getAllocationSize());

        if (newFile instanceof NodeRefNetworkFile) {
          NodeRefNetworkFile x = (NodeRefNetworkFile) newFile;
          x.setFileState(fstate);
        }

        if (newFile instanceof TempNetworkFile) {
          TempNetworkFile x = (TempNetworkFile) newFile;
          x.setFileState(fstate);
        }
      }

      if (newFile instanceof NodeRefNetworkFile) {
        NodeRefNetworkFile x = (NodeRefNetworkFile) newFile;
        x.setProcessId(params.getProcessId());
        x.setAccessToken(token);
      }

      if (newFile instanceof TempNetworkFile) {
        TempNetworkFile x = (TempNetworkFile) newFile;
        x.setAccessToken(token);
      }

      return newFile;

    } catch (IOException ie) {
      if (logger.isDebugEnabled()) {
        logger.debug("create file exception caught", ie);
      }
      if (tctx.hasStateCache() && token != null) {
        FileStateCache cache = tctx.getStateCache();
        FileState fstate = tctx.getStateCache().findFileState(params.getPath(), false);
        if (fstate != null && token != null) {
          if (logger.isDebugEnabled()) {
            logger.debug("create file release lock token:" + token);
          }
          cache.releaseFileAccess(fstate, token);
        }
      }
      throw ie;
    }
  }
  @Override
  public NetworkFile openFile(SrvSession sess, TreeConnection tree, FileOpenParams params)
      throws IOException {
    ContentContext tctx = (ContentContext) tree.getContext();
    String path = params.getPath();

    FileAccessToken token = null;

    if (tctx.hasStateCache()) {
      if (!params.isDirectory()) {
        FileStateCache cache = tctx.getStateCache();
        FileState fstate = tctx.getStateCache().findFileState(params.getPath(), true);
        token = cache.grantFileAccess(params, fstate, FileStatus.Unknown);
        if (logger.isDebugEnabled()) {
          logger.debug("open file created lock token:" + token);
        }
      }
    }

    try {
      NetworkFile openFile = diskInterface.openFile(sess, tree, params);

      if (openFile instanceof ContentNetworkFile) {
        ContentNetworkFile x = (ContentNetworkFile) openFile;
        x.setProcessId(params.getProcessId());
        x.setAccessToken(token);
        if (tctx.hasStateCache()) {
          FileState fstate = tctx.getStateCache().findFileState(path, true);
          x.setFileState(fstate);
          fstate.setProcessId(params.getProcessId());
          fstate.setSharedAccess(params.getSharedAccess());
          fstate.setFileStatus(FileStatus.FileExists);
        }
      }

      if (openFile instanceof TempNetworkFile) {
        TempNetworkFile x = (TempNetworkFile) openFile;
        x.setAccessToken(token);
        // x.setProcessId( params.getProcessId());
        if (tctx.hasStateCache()) {
          FileState fstate = tctx.getStateCache().findFileState(path, true);
          x.setFileState(fstate);
          fstate.setFileStatus(FileStatus.FileExists);
          fstate.setProcessId(params.getProcessId());
          fstate.setSharedAccess(params.getSharedAccess());
        }
      }

      if (openFile instanceof AlfrescoFolder) {
        AlfrescoFolder x = (AlfrescoFolder) openFile;
        // x.setProcessId( param.getProcessId());
        if (tctx.hasStateCache()) {
          FileState fstate = tctx.getStateCache().findFileState(path, true);
          x.setFileState(fstate);
          fstate.setFileStatus(FileStatus.DirectoryExists);
          fstate.setProcessId(params.getProcessId());
          fstate.setSharedAccess(params.getSharedAccess());
        }
      }

      return openFile;
    } catch (IOException ie) {
      if (logger.isDebugEnabled()) {
        logger.debug("open file exception caught", ie);
      }
      if (tctx.hasStateCache() && token != null) {
        FileStateCache cache = tctx.getStateCache();
        FileState fstate = tctx.getStateCache().findFileState(params.getPath(), false);
        if (fstate != null) {
          if (logger.isDebugEnabled()) {
            logger.debug("open file release lock token:" + token);
          }
          cache.releaseFileAccess(fstate, token);
        }
      }
      throw ie;
    }
  }