Skip to content

Commit

Permalink
Implements getAllResources method
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Jul 24, 2023
1 parent 7012477 commit 9d47193
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,12 +17,12 @@

package com.sun.enterprise.resource.pool.datastructure;

import java.util.ArrayList;

import com.sun.appserv.connectors.internal.api.PoolingException;
import com.sun.enterprise.resource.ResourceHandle;
import com.sun.enterprise.resource.allocator.ResourceAllocator;

import java.util.List;

/**
* Represents a pool datastructure. Helps to plug-in various implementations that the pool can use.<br>
* Datastructure need to synchronize the operations.
Expand Down Expand Up @@ -97,7 +97,7 @@ public interface DataStructure {
* resources (including the ones in use). This is used under special circumstances where there is a need to process all
* resources.
*
* @return ArrayList<ResourceHandle>
* @return List<ResourceHandle>
*/
ArrayList<ResourceHandle> getAllResources();
List<ResourceHandle> getAllResources();
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,15 +17,17 @@

package com.sun.enterprise.resource.pool.datastructure;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

import com.sun.appserv.connectors.internal.api.PoolingException;
import com.sun.enterprise.resource.ResourceHandle;
import com.sun.enterprise.resource.allocator.ResourceAllocator;
import com.sun.enterprise.resource.pool.ResourceHandler;
import com.sun.enterprise.resource.pool.datastructure.strategy.ResourceSelectionStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;


/**
* List based datastructure that can be used by connection pool <br>
*
Expand Down Expand Up @@ -216,7 +218,7 @@ public int getResourcesSize() {
* {@inheritDoc}
*/
@Override
public ArrayList<ResourceHandle> getAllResources() {
public List<ResourceHandle> getAllResources() {
return this.resources;
}

Expand Down
Expand Up @@ -23,9 +23,9 @@
import com.sun.enterprise.resource.pool.ResourceHandler;
import com.sun.logging.LogDomains;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.StampedLock;
import java.util.logging.Level;
Expand Down Expand Up @@ -331,8 +331,26 @@ public synchronized void setMaxSize(int newMaxSize) {
}

@Override
public ArrayList<ResourceHandle> getAllResources() {
throw new UnsupportedOperationException("Not supported yet.");
public List<ResourceHandle> getAllResources() {
long stamp = lock.tryOptimisticRead();
try {
for (;; stamp = lock.readLock()) {
if (stamp == 0L) {
continue;
}

ResourceHandle[] allResources = Arrays.copyOf(resources, size);
if (!lock.validate(stamp)) {
continue;
}

return Arrays.asList(allResources);
}
} finally {
if (StampedLock.isReadLockStamp(stamp)) {
lock.unlockRead(stamp);
}
}
}

/**
Expand Down

0 comments on commit 9d47193

Please sign in to comment.