All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unbound.client.kmip.KMIPServer Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.client.kmip;

import com.unbound.common.Log;

import java.net.URL;
import java.security.ProviderException;
import java.time.Clock;
import java.util.*;


class KMIPServer
{
  private static final int CHECK_DISCONNECTED_TIMEOUT = 3000; // milliseconds
  private static final Clock clock = Clock.systemUTC();
  private static KMIPServer[] servers = null;
  private static Random randomPicker = new Random();

  private final URL url;
  private final String name;
  private long checkDisconnectedClock = 0;
  //HashMap> connections = new HashMap<>();

  public URL getUrl() { return url; }

  private KMIPServer(String name)
  {
    Log log = Log.func("KMIPServer").log("Name", name).end(); try
    {
      this.name = name;
      if (name.startsWith("https://"))
      {
        name = name.substring(8);
      }

      URL host = new URL("https://" + name);
      int port = host.getPort();
      if (port<=0) port = 443;
      url = new URL("https", host.getHost(), port, "/kmip", null);
    }
    catch (Exception e) { log.failed(e); throw new ProviderException(e); } finally { log.leave(); }
  }

  static synchronized void initialize(String servers)
  {
    Log log = Log.func("KMIPServer.initialize").log("servers", servers).end(); try
    {
      initialize(servers.split(",")); // ep1,ep2,ep3
    }
    catch (Exception e) { log.failed(e); throw new ProviderException(e); } finally { log.leave(); }
  }

  static synchronized void initialize(String[] serverNames)
  {
    if (serverNames.length==0) throw new IllegalArgumentException("Server list is empty");

    KMIPServer[] temp = new KMIPServer[serverNames.length];

    int index = 0;
    for (String name: serverNames)
    {
      temp[index++] = new KMIPServer(name.trim());
    }

    servers = temp;
  }

  /*private synchronized Connection getConnection(Partition partition)
  {
    Queue queue = connections.get(partition.name);
    if (queue==null) return null;
    return queue.poll();
  }

  private synchronized void releaseConnection(Connection connection)
  {
    String partitionName = connection.partition.name;
    Queue queue = connections.get(partitionName);
    if (queue==null)
    {
      queue = new ArrayDeque<>();
      connections.put(partitionName, queue);
    }
    queue.add(connection);
  }*/

  static synchronized KMIPServer[] getList()
  {
    long now = clock.millis();
    int count = 0;
    for (KMIPServer server : servers)
    {
      if (now < server.checkDisconnectedClock + CHECK_DISCONNECTED_TIMEOUT) continue;
      count++;
    }

    if (count==0)
    {
      KMIPServer server = servers[randomPicker.nextInt(servers.length)];
      KMIPServer[] list = new KMIPServer[1];
      list[0] = server;
      return list;
    }

    KMIPServer[] list = new KMIPServer[count];
    count = 0;
    for (KMIPServer server : servers)
    {
      if (now < server.checkDisconnectedClock + CHECK_DISCONNECTED_TIMEOUT) continue;
      list[count++] = server;
    }

    for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy