Read lease check if it is still expired. If so, then update lease and proceed to peer recover
if not still expired (someone else has grabbed it) then bypass peer recover.

@param recoveryIdentityToRecover
@param myRecoveryIdentity
@throws Exception
321546
@Override
    public synchronized boolean claimPeerLeaseForRecovery(String recoveryIdentityToRecover, String myRecoveryIdentity, LeaseInfo leaseInfo) throws Exception
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "claimPeerLeaseForRecovery", new java.lang.Object[] { recoveryIdentityToRecover, myRecoveryIdentity, leaseInfo, this });

        boolean peerClaimed = false;
        Connection conn = null;

        Statement lockingStmt = null;
        PreparedStatement updateStmt = null;
        ResultSet lockingRS = null;

        if (tc.isDebugEnabled())
            Tr.debug(tc, "Recovering server with recoveryIdentity - ", recoveryIdentityToRecover);

        try
        {
            // Have we acquired the reference to the DataSource yet?
            if (_theDS == null)
            {
                _theDS = getDataSourceFromProperties();
                // We've looked up the DS, so now we can get a JDBC connection
                if (_theDS != null)
                {
                    conn = getConnection(_theDS);
                }
            }
            else
            {
                // Try and get a new connection
                conn = _theDS.getConnection();
            }

            // If we were unable to get a connection, throw an exception
            if (conn == null)
            {
                if (tc.isEntryEnabled())
                    Tr.exit(tc, "claimPeerLeaseForRecovery", "Null connection InternalLogException");
                throw new InternalLogException("Failed to get JDBC Connection", null);
            }

            if (tc.isDebugEnabled())
                Tr.debug(tc, "Set autocommit FALSE on the connection");
            conn.setAutoCommit(false);

            // We can go ahead and access the Database
            lockingStmt = conn.createStatement();

            try
            {
                String queryString = "SELECT LEASE_TIME" +
                                     " FROM " + _leaseTableName +
                                     " WHERE SERVER_IDENTITY='" + recoveryIdentityToRecover + "' FOR UPDATE OF LEASE_TIME";
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Attempt to select the row for UPDATE using - " + queryString);
                lockingRS = lockingStmt.executeQuery(queryString);
            } catch (Exception e)
            {
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Query failed with exception: " + e);
                throw new InternalLogException("Failed to query the lease time, exc: " + e, null);
            } // eof Exception e block

            // see if we acquired the row
            if (lockingRS.next())
            {
                // We found the server row
                long storedLease = lockingRS.getLong(1);
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Acquired server row, stored lease value is: " + storedLease);

                // Has the lease expired?
                PeerLeaseData pld = new PeerLeaseData(recoveryIdentityToRecover, storedLease, _leaseTimeout);
                if (pld.isExpired())
                {
                    // Lease has expired, this means that this server is attempting peer recovery and 
                    // it has acquired the lock. If any other server is also trying to peer recover then
                    // they can fail. To make this happen, we'll update the lease time ourselves.

                    // Construct the UPDATE string
                    String updateString = "UPDATE " + _leaseTableName +
                                          " SET LEASE_TIME = ?, LEASE_OWNER = ?" +
                                          " WHERE SERVER_IDENTITY='" + recoveryIdentityToRecover + "'";
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "update lease for " + recoveryIdentityToRecover);

                    updateStmt = conn.prepareStatement(updateString);

                    // Set the Lease_time to the current time
                    //TODO:
                    long fir1 = System.currentTimeMillis();
                    updateStmt.setLong(1, fir1);
                    updateStmt.setString(2, myRecoveryIdentity);
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Ready to UPDATE using string - " + updateString + " and time: " + fir1);

                    int ret = updateStmt.executeUpdate();

                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Have updated server row with return: " + ret);
                    peerClaimed = true;
                }
            }
            else
            {
                // We didn't find the row in the table
                if (tc.isDebugEnabled())
                    Tr.debug(tc, "Could not find row");
                throw new InternalLogException("Could not find lease", null);
            }

            if (tc.isDebugEnabled())
                Tr.debug(tc, "COMMIT the change");
            conn.commit();
        }
        // Catch and report an SQLException. 
        catch (SQLException sqlex)
        {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Lease update failed with exception: " + sqlex);
        } finally
        {
            if (lockingRS != null && !lockingRS.isClosed())
                lockingRS.close();
            if (lockingStmt != null && !lockingStmt.isClosed())
                lockingStmt.close();
            if (updateStmt != null && !updateStmt.isClosed())
                updateStmt.close();
            if (conn != null)
                conn.close();
        }

        if (tc.isEntryEnabled())
            Tr.exit(tc, "claimPeerLeaseForRecovery");
        return peerClaimed;
    }
721509
public static void updateWithSelfSignedServerCertificate(KeyStore pKeyStore)
            throws NoSuchProviderException, NoSuchAlgorithmException, IOException,
                   InvalidKeyException, CertificateException, SignatureException, KeyStoreException {

        final Object x500Name;
        final Object[] certAttributes = { "Jolokia Agent " + Version.getAgentVersion(), // CN
                                          "JVM",                                        // OU
                                          "jolokia.org",                                // O
                                          "Pegnitz",                                    // L
                                          "Franconia",                                  // ST
                                          "DE" };
        if (ClassUtil.checkForClass(X500_NAME_SUN)) {
            x500Name = ClassUtil.newInstance(X500_NAME_SUN, certAttributes);
        } else if (ClassUtil.checkForClass(X500_NAME_IBM)) {
            x500Name = ClassUtil.newInstance(X500_NAME_IBM, certAttributes);
        } else {
            throw new IllegalStateException("Neither Sun- nor IBM-style JVM found.");
        }

        // Need to do it via reflection because Java8 moved class to a different package
        Object keypair = createKeyPair();
        PrivateKey privKey = getPrivateKey(keypair);

        X509Certificate[] chain = new X509Certificate[1];
        chain[0] = getSelfCertificate(keypair, x500Name, new Date(), (long) 3650 * 24 * 60 * 60);
        pKeyStore.setKeyEntry("jolokia-agent", privKey, new char[0], chain);
    }
1318674
private void parseKeystorePassword() throws Exception {
        lookahead = st.nextToken();
        if (lookahead == '\"') {
            if (keystorePasswordURL == null) {
                keystorePasswordURL = st.sval;
            }
        } else {
            throw new Exception("ER033: [\"keystore_password\"] expected.");
        }
        lookahead = st.nextToken();
        if (lookahead == ';') {
            return;
        } else {
            throw new Exception("ER034: [;] expected at the end of keystorePasswordURL entry.");
        }
    }
829631
public void performRecovery(ObjectManagerState objectManagerState)
                    throws ObjectManagerException
    {
        if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
            trace.entry(this, cclass,
                        "performRecovery",
                        "objectManagerState=" + objectManagerState + "(ObjectManagerState)");

        if (Tracing.isAnyTracingEnabled() && trace.isDebugEnabled())
            trace.debug(this, cclass,
                        "logicalUnitOfWork=" + logicalUnitOfWork + "(LogicalUnitOfWork)"
                                        + " transactionState=" + transactionState + "(int)"
                                        + " token=" + token + "(token)"
                                        + " managedObjectBytes.length=" + managedObjectBytes.length + "(int)");

        // Recover the ManagedObject from its serialized bytes.
        ManagedObject addedManagedObject = ManagedObject.restoreFromSerializedBytes(managedObjectBytes,
                                                                                    objectManagerState);

        // Redo the add of the ManagedObject using its original Transaction.
        Transaction transactionForRecovery = objectManagerState.getTransaction(logicalUnitOfWork);
        // Reestablish the restored ManagedObject.
        ManagedObject managedObject = token.setManagedObject(addedManagedObject);
        managedObject.state = ManagedObject.stateConstructed;

        transactionForRecovery.add(managedObject); // Redo the add.
        // No need to reset the transaction state because Add can only be executed before
        // the transaction is prepared.
        // transactionForRecovery.internalTransaction.resetState(transactionState);

        if (Tracing.isAnyTracingEnabled() && trace.isEntryEnabled())
            trace.exit(this, cclass,
                       "performRecovery");
    }
203922
@Override
    public void getLeasesForPeers(final PeerLeaseTable peerLeaseTable, String recoveryGroup) throws Exception {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "getLeasesForPeers", new Object[] { peerLeaseTable, recoveryGroup, this });

        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {

                // We'll start by trying to lock the control file, if we can't do it this time around, then so be it, we
                // assume that someone else is either getting peer leases or deleting peer leases.
                FileChannel theChannel = null;
                FileLock lock = null;
                try {
                    theChannel = new RandomAccessFile(_controlFile, "rw").getChannel();
                } catch (FileNotFoundException e) {
                    if (tc.isDebugEnabled())
                        Tr.debug(tc, "Caught FileNotFound exception when trying to lock control file");
                    theChannel = null;
                }

                if (theChannel != null) {
                    // Non blocking attempt to acquire the lock on the control file.
                    try {
                        lock = theChannel.tryLock();
                    } catch (IOException e) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, "Caught IOException when trying to lock control file - " + e);
                        theChannel = null;
                    }

                    if (lock != null) {

                        File thePeerFiles[] = _leaseLogDirectory.listFiles();
                        // Now process through the peers we need to handle
                        if (thePeerFiles != null) {
                            for (File peerFile : thePeerFiles) {
                                if (!peerFile.isDirectory()) {
                                    final String recoveryId = peerFile.getName();
                                    //Skip over the control file
                                    if (!recoveryId.equals(_controlFile.getName())) {
                                        final long leaseTime = peerFile.lastModified();

                                        if (tc.isEventEnabled()) {
                                            Tr.event(tc, "Lease Table: read recoveryId: " + recoveryId);
                                            Tr.event(tc, "Lease Table: read leaseTime: " + leaseTime);
                                        }

                                        PeerLeaseData pld = new PeerLeaseData(recoveryId, leaseTime, _leaseTimeout);

                                        peerLeaseTable.addPeerEntry(pld);
                                    } else {
                                        if (tc.isDebugEnabled())
                                            Tr.debug(tc, "Exclude the control file from the list");
                                    }
                                }
                            }
                        } else {
                            if (tc.isDebugEnabled())
                                Tr.debug(tc, "No peer servers found");
                        }
                    }
                }

                // Tidy up before we leave
                if (lock != null) {
                    try {
                        lock.release();
                    } catch (IOException e) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, "Caught an IOException on lock release");
                    }
                }
                // Close the channel
                if (theChannel != null) {
                    try {
                        theChannel.close();
                    } catch (IOException e) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, "Caught an IOException on channel close");
                    }
                }

                return null;
            }
        });

        if (tc.isEntryEnabled())
            Tr.exit(tc, "getLeasesForPeers", this);

    }
395772
public RemotablePartitionEntity updateRemotablePartitionOnRecovery(PersistenceServiceUnit psu,
                                                                       final RemotablePartitionEntity partition) {
        // TODO Auto-generated method stub
        EntityManager em = psu.createEntityManager();
        try {
            return new TranRequest<RemotablePartitionEntity>(em) {
                @Override
                public RemotablePartitionEntity call() {
                    RemotablePartitionKey key = new RemotablePartitionKey(partition);
                    RemotablePartitionEntity remotablePartition = entityMgr.find(RemotablePartitionEntity.class, key);
                    remotablePartition.setLastUpdated(new Date());
                    return remotablePartition;
                }
            }.runInNewOrExistingGlobalTran();
        } finally {
            em.close();
        }
    }
1281671
@Override
    public void update(IEntityLock lock, java.util.Date newExpiration, Integer newLockType)
            throws LockingException {
        if (find(lock) == null) {
            throw new LockingException("Problem updating " + lock + " : not found in store.");
        }
        primAdd(lock, newExpiration);
    }
567089
protected void restoreWorkspace() throws Throwable
   {
      boolean restored = true;
      RepositoryImpl repository = (RepositoryImpl)repositoryService.getRepository(repositoryName);
      try
      {
         RepositoryEntry reEntry = repository.getConfiguration();
         backupManager.restore(new BackupChainLog(backupChainLogFile), reEntry.getName(), wEntry, false);
      }
      catch (InvalidItemStateException e)
      {
         restored = false;
         throw new WorkspaceRestoreExeption("Workspace '" + "/" + repositoryName + "/" + wEntry.getName()
            + "' can not be restored! There was database error!", e);

      }
      catch (Throwable t) //NOSONAR
      {
         restored = false;
         throw new WorkspaceRestoreExeption("Workspace '" + "/" + repositoryName + "/" + wEntry.getName()
            + "' can not be restored!", t);

      }
      finally
      {
         if (!restored)
         {
            try
            {
               removeWorkspace(repository, wEntry.getName());
            }
            catch (Throwable thr) //NOSONAR
            {
               throw new WorkspaceRestoreExeption("Workspace '" + "/" + repositoryName + "/"
                  + wEntry.getName() + "' can not be restored!", thr);
            }
         }
      }
   }
909963
public Object getObjectByIdentity(Identity id)
            throws PersistenceBrokerException
    {
        checkOpen();
        ObjectEnvelope envelope = objectEnvelopeTable.getByIdentity(id);
        if (envelope != null)
        {
            return (envelope.needsDelete() ? null : envelope.getObject());
        }
        else
        {
            return getBroker().getObjectByIdentity(id);
        }
    }
941567
public Key retrievePrivateSignerKey() throws Exception {
        KeyStore ks = null;
        Key k = null;
        try {
            ks = KeyStore.getInstance(signerType, signerProvider);
            InputStream is = openKeyStore(signerKeyFileLocation);
            ks.load(is, signerPassword.toCharArray());
            k = ks.getKey(signerAlias, signerPassword.toCharArray());
        } catch (java.security.NoSuchProviderException ne) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore: no such provider.", ne.getMessage());
            throw new Exception(ne.getMessage());
        } catch (java.net.MalformedURLException me) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore: malformed URL", me.getMessage());
            throw new Exception(me.getMessage());
        } catch (java.security.KeyStoreException ke) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore.", ke.getMessage());
            throw new Exception(ke.getMessage());
        } catch (java.security.UnrecoverableKeyException uke) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore.", uke.getMessage());
            throw new Exception(uke.getMessage());
        } catch (java.security.NoSuchAlgorithmException ae) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore: no such algorithm", ae.getMessage());
            throw new Exception(ae.getMessage());
        } catch (java.security.cert.CertificateException ce) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception getting certificate.", ce.getMessage());
            throw new Exception(ce.getMessage());
        } catch (java.io.IOException ioe) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception opening keystore.", ioe.getMessage());
            throw new Exception(ioe.getMessage());

        }
        return k;
    }
898399
private void readTrustStore() throws IOException, FormatNotUnderstoodException {
		if (((this.hocr.getSigTruststoreFile() != null) && (!"".equals(this.hocr.getSigTruststoreFile())))) {
			LOG.info("Reading truststore to validate certificate chain for signatures");
			FlinkKeyStoreManager fksm = new FlinkKeyStoreManager();
			try {
				fksm.openKeyStore(new Path(this.hocr.getSigTruststoreFile()), this.hocr.getSigTruststoreType(),
						this.hocr.getSigTruststorePassword());
				this.hocr.setX509CertificateChain(fksm.getAllX509Certificates());
			} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IllegalArgumentException e) {
				LOG.error("Exception: ",e);
				throw new FormatNotUnderstoodException(
						"Cannot read truststore to establish certificate chain for signature validation " + e);
			}

		}
	}
799118
@Override
    public void removeRecoverableUnit(long identity) throws LogClosedException, InvalidRecoverableUnitException, InternalLogException, LogIncompatibleException
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "removeRecoverableUnit", new Object[] { new Long(identity), this });

        _recoveryLog.removeRecoverableUnit(identity);

        if (tc.isEntryEnabled())
            Tr.exit(tc, "removeRecoverableUnit");
    }
260423
private int getPeerLeaseCheckInterval() {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "getPeerLeaseCheckInterval");

        int intToReturn;
        Integer peerLeaseCheckInterval = null;

        try {
            peerLeaseCheckInterval = AccessController.doPrivileged(
                                                                   new PrivilegedExceptionAction<Integer>() {
                                                                       @Override
                                                                       public Integer run() {
                                                                           return Integer.getInteger("com.ibm.tx.jta.impl.PeerLeaseCheckInterval", 20); // Default is 20 seconds
                                                                       }
                                                                   });
        } catch (PrivilegedActionException e) {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Exception setting Peer Lease-Check Interval", e);
            peerLeaseCheckInterval = null;
        }

        if (peerLeaseCheckInterval == null)
            peerLeaseCheckInterval = new Integer(20);
        intToReturn = peerLeaseCheckInterval.intValue();
        if (tc.isEntryEnabled())
            Tr.exit(tc, "getPeerLeaseCheckInterval", intToReturn);
        return intToReturn;
    }
344382
public boolean isExpired()
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "isExpired");
        boolean expired = false;
        long curTime = System.currentTimeMillis();
        //TODO:
        if (curTime - _leaseTime > _leaseTimeout * 1000) //  30 seconds default for timeout
        {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Lease has EXPIRED for " + _recoveryIdentity + ", currenttime: " + curTime + ", storedTime: " + _leaseTime);
            expired = true;
        }
        else
        {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Lease has not expired for " + _recoveryIdentity + ", currenttime: " + curTime + ", storedTime: " + _leaseTime);
        }

        if (tc.isEntryEnabled())
            Tr.exit(tc, "isExpired", expired);
        return expired;
    }
700001
public void initializeKeyStore(boolean reinitialize) throws Exception {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.entry(tc, "initializeKeyStore");

        try {
            String initAtStartup = getProperty(Constants.SSLPROP_KEY_STORE_INITIALIZE_AT_STARTUP);
            boolean createIfMissing = LibertyConstants.DEFAULT_KEYSTORE_REF_ID.equals(getProperty("id"));

            if (Boolean.parseBoolean(initAtStartup) || reinitialize) {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                    Tr.debug(tc, "Initializing keystore at startup.");
                getKeyStore(reinitialize, createIfMissing);
            }
        } catch (Exception e) {
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                Tr.debug(tc, "Exception initializing KeyStore; " + e);
            throw e;
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.exit(tc, "initializeKeyStore");
    }
837963
private void insertNewLease(String recoveryIdentity, String recoveryGroup, Connection conn) throws SQLException
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "insertNewLease", this);

        short serviceId = (short) 1;
        String insertString = "INSERT INTO " +
                              _leaseTableName +
                              " (SERVER_IDENTITY, RECOVERY_GROUP, LEASE_OWNER, LEASE_TIME)" +
                              " VALUES (?,?,?,?)";

        PreparedStatement specStatement = null;
        long fir1 = System.currentTimeMillis();

        Tr.audit(tc, "WTRN0108I: Insert New Lease for server with recovery identity " + recoveryIdentity);
        try
        {
            if (tc.isDebugEnabled())
                Tr.debug(tc, "Need to setup new row using - " + insertString + ", and time: " + fir1);
            specStatement = conn.prepareStatement(insertString);
            specStatement.setString(1, recoveryIdentity);
            specStatement.setString(2, recoveryGroup);
            specStatement.setString(3, recoveryIdentity);
            specStatement.setLong(4, fir1);

            int ret = specStatement.executeUpdate();

            if (tc.isDebugEnabled())
                Tr.debug(tc, "Have inserted Server row with return: " + ret);
        } finally
        {
            if (specStatement != null && !specStatement.isClosed())
                specStatement.close();
        }
        if (tc.isEntryEnabled())
            Tr.exit(tc, "insertNewLease");
    }
1139559
@Override
    public boolean existsInStore(IEntityLock lock) throws LockingException {
        Class entityType = lock.getEntityType();
        String key = lock.getEntityKey();
        Integer lockType = lock.getLockType();
        Date expiration = lock.getExpirationTime();
        String owner = lock.getLockOwner();
        IEntityLock[] lockArray = getLockStore().find(entityType, key, lockType, expiration, owner);

        return (lockArray.length > 0);
    }