public static synchronized Session getSession(long syncAccountId) {
    Session session = _sessions.get(syncAccountId);

    if (session != null) {
      return session;
    }

    try {
      SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(syncAccountId);

      URL url = new URL(syncAccount.getUrl());

      session =
          new Session(
              url,
              syncAccount.getLogin(),
              Encryptor.decrypt(syncAccount.getPassword()),
              syncAccount.isTrustSelfSigned(),
              syncAccount.getMaxConnections());

      _sessions.put(syncAccountId, session);

      return session;
    } catch (Exception e) {
      _logger.error(e.getMessage(), e);

      return null;
    }
  }
  public static SyncAccount addSyncAccount(
      String filePathName,
      String login,
      int maxConnections,
      String name,
      String password,
      int pollInterval,
      SyncSite[] syncSites,
      boolean trustSelfSigned,
      String url)
      throws Exception {

    // Sync account

    SyncAccount syncAccount = new SyncAccount();

    syncAccount.setFilePathName(filePathName);
    syncAccount.setLogin(login);
    syncAccount.setMaxConnections(maxConnections);
    syncAccount.setName(name);
    syncAccount.setPassword(Encryptor.encrypt(password));
    syncAccount.setPollInterval(pollInterval);
    syncAccount.setTrustSelfSigned(trustSelfSigned);
    syncAccount.setUrl(url);

    _syncAccountPersistence.create(syncAccount);

    // Sync file

    Files.createDirectories(Paths.get(filePathName));

    SyncFileService.addSyncFile(
        null,
        null,
        filePathName,
        FileUtil.getFileKey(filePathName),
        filePathName,
        null,
        filePathName,
        0,
        0,
        syncAccount.getSyncAccountId(),
        SyncFile.TYPE_SYSTEM);

    // Sync sites

    if (syncSites != null) {
      for (SyncSite syncSite : syncSites) {
        String syncSiteName = syncSite.getName();

        if (!FileUtil.isValidFileName(syncSiteName)) {
          syncSiteName = String.valueOf(syncSite.getGroupId());
        }

        syncSite.setFilePathName(syncAccount.getFilePathName() + "/" + syncSiteName);

        syncSite.setSyncAccountId(syncAccount.getSyncAccountId());

        SyncSiteService.update(syncSite);
      }
    }

    return syncAccount;
  }