Skip to content

Commit

Permalink
Merge pull request #991 from arjantijms/4.0.0-prepare
Browse files Browse the repository at this point in the history
Merge master into 4.0.0-prepare
  • Loading branch information
arjantijms committed Mar 5, 2024
2 parents 1718cbf + 3f19e5d commit 2f12674
Show file tree
Hide file tree
Showing 17 changed files with 622 additions and 405 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -19,6 +19,8 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import org.glassfish.hk2.classmodel.reflect.*;

/**
Expand All @@ -27,15 +29,21 @@
*/
public class EnumTypeImpl extends ExtensibleTypeImpl<EnumType> implements EnumType {

private final ReentrantLock lock = new ReentrantLock();
final List<FieldModel> fields = new ArrayList<>();

public EnumTypeImpl(String name, TypeProxy<Type> sink, TypeProxy parent) {
super(name, sink, parent);
}

@Override
synchronized void addField(FieldModel field) {
fields.add(field);
void addField(FieldModel field) {
lock.lock();
try {
fields.add(field);
} finally {
lock.unlock();
}
}

@Override
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,6 +24,8 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -85,7 +87,8 @@ public class ImmediateHelper implements DynamicConfigurationListener, Runnable,

private final HashSet<Long> tidsWithWork = new HashSet<Long>();

private final Object queueLock = new Object();
private final ReentrantLock queueLock = new ReentrantLock();
private final Condition condition = queueLock.newCondition();
private boolean threadAvailable;
private boolean outstandingJob;
private boolean waitingForWork;
Expand Down Expand Up @@ -140,16 +143,19 @@ private void doWorkIfWeHaveSome() {
currentExecutor.execute(this);
}
else if (waitingForWork) {
queueLock.notify();
condition.signal();
}
}

@Override
public void configurationChanged() {
synchronized (queueLock) {
queueLock.lock();
try {
if (currentState.equals(ImmediateServiceState.SUSPENDED)) return;

doWorkIfWeHaveSome();
} finally {
queueLock.unlock();
}
}

Expand All @@ -169,9 +175,11 @@ public void onFailure(ErrorInformation errorInformation)
if (!(ErrorType.DYNAMIC_CONFIGURATION_FAILURE.equals(errorInformation.getErrorType()))) {
// Only interested in dynamic configuration failures
long tid = Thread.currentThread().getId();

synchronized (queueLock) {
queueLock.lock();
try {
tidsWithWork.remove(tid);
} finally {
queueLock.unlock();
}

return;
Expand All @@ -185,8 +193,11 @@ public boolean validate(ValidationInformation info) {
info.getOperation().equals(Operation.UNBIND)) {
long tid = Thread.currentThread().getId();

synchronized (queueLock) {
queueLock.lock();
try {
tidsWithWork.add(tid);
} finally {
queueLock.unlock();
}
}

Expand All @@ -200,7 +211,8 @@ public boolean validate(ValidationInformation info) {
@Override
public void run() {
for(;;) {
synchronized (queueLock) {
queueLock.lock();
try {
long decayTime = this.decayTime;

while (currentState.equals(ImmediateServiceState.RUNNING) &&
Expand All @@ -209,7 +221,7 @@ public void run() {
waitingForWork = true;
long currentTime = System.currentTimeMillis();
try {
queueLock.wait(decayTime);
condition.await(decayTime, TimeUnit.MILLISECONDS);
}
catch (InterruptedException ie) {
threadAvailable = false;
Expand All @@ -228,6 +240,8 @@ public void run() {
}

outstandingJob = false;
} finally {
queueLock.unlock();
}

immediateContext.doWork();
Expand All @@ -240,8 +254,11 @@ public void run() {
*/
@Override
public Executor getExecutor() {
synchronized (queueLock) {
queueLock.lock();
try {
return currentExecutor;
} finally {
queueLock.unlock();
}
}

Expand All @@ -250,12 +267,15 @@ public Executor getExecutor() {
*/
@Override
public void setExecutor(Executor executor) throws IllegalStateException {
synchronized (queueLock) {
queueLock.lock();
try {
if (currentState.equals(ImmediateServiceState.RUNNING)) {
throw new IllegalStateException("ImmediateSerivce attempt made to change executor while in RUNNING state");
}

currentExecutor = (executor == null) ? DEFAULT_EXECUTOR : executor ;
} finally {
queueLock.unlock();
}

}
Expand All @@ -265,8 +285,11 @@ public void setExecutor(Executor executor) throws IllegalStateException {
*/
@Override
public long getThreadInactivityTimeout() {
synchronized (queueLock) {
queueLock.lock();
try {
return decayTime;
} finally {
queueLock.unlock();
}
}

Expand All @@ -276,12 +299,15 @@ public long getThreadInactivityTimeout() {
@Override
public void setThreadInactivityTimeout(long timeInMillis)
throws IllegalStateException {
synchronized (queueLock) {
queueLock.lock();
try {
if (timeInMillis < 0) {
throw new IllegalArgumentException();
}

decayTime = timeInMillis;
} finally {
queueLock.unlock();
}

}
Expand All @@ -291,8 +317,11 @@ public void setThreadInactivityTimeout(long timeInMillis)
*/
@Override
public ImmediateServiceState getImmediateState() {
synchronized (queueLock) {
queueLock.lock();
try {
return currentState;
} finally {
queueLock.unlock();
}
}

Expand All @@ -301,7 +330,8 @@ public ImmediateServiceState getImmediateState() {
*/
@Override
public void setImmediateState(ImmediateServiceState state) {
synchronized (queueLock) {
queueLock.lock();
try {
if (state == null) throw new IllegalArgumentException();

if (state == currentState) return;
Expand All @@ -310,6 +340,8 @@ public void setImmediateState(ImmediateServiceState state) {
if (currentState.equals(ImmediateServiceState.RUNNING)) {
doWorkIfWeHaveSome();
}
} finally {
queueLock.unlock();
}

}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,6 +24,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -48,6 +49,7 @@
*/
@SupportedAnnotationTypes("org.glassfish.hk2.xml.api.annotations.Hk2XmlPreGenerate")
public class Hk2XmlGenerator extends AbstractProcessor {
private final ReentrantLock lock = new ReentrantLock();
private volatile boolean initialized;
private ClassPool defaultClassPool;
private CtClass superClazz;
Expand All @@ -62,8 +64,8 @@ public SourceVersion getSupportedSourceVersion() {

private void initializeHk2XmlGenerator() {
if (initialized) return;

synchronized (this) {
lock.lock();
try {
if (initialized) return;

defaultClassPool = new ClassPool(true);
Expand Down Expand Up @@ -103,6 +105,8 @@ private void initializeHk2XmlGenerator() {
catch (NotFoundException e) {
throw new RuntimeException(e);
}
} finally {
lock.unlock();
}
}

Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

import jakarta.annotation.PreDestroy;
import jakarta.inject.Inject;
Expand All @@ -44,6 +45,7 @@
@Singleton
@Visibility(DescriptorVisibility.LOCAL)
public class Hk2BridgeImpl implements DynamicConfigurationListener {
private final ReentrantLock lock = new ReentrantLock();
private final ServiceLocator local;
private ServiceLocator remote;
private Filter filter;
Expand All @@ -56,46 +58,56 @@ private Hk2BridgeImpl(ServiceLocator local) {

}

public synchronized void setRemote(ServiceLocator remote) {
this.remote = remote;
this.filter = new NoLocalNoRemoteFilter(remote.getLocatorId());

List<ActiveDescriptor<?>> newDescriptors = local.getDescriptors(filter);

handleChange(newDescriptors);
public void setRemote(ServiceLocator remote) {
lock.lock();
try {
this.remote = remote;
this.filter = new NoLocalNoRemoteFilter(remote.getLocatorId());

List<ActiveDescriptor<?>> newDescriptors = local.getDescriptors(filter);

handleChange(newDescriptors);
} finally {
lock.unlock();
}
}

@SuppressWarnings("unchecked")
private synchronized void handleChange(List<ActiveDescriptor<?>> newDescriptors) {
if (remote == null) return;

HashSet<ActiveDescriptor<?>> toRemove = new HashSet<ActiveDescriptor<?>>(mirroredDescriptors);
toRemove.removeAll(newDescriptors);

HashSet<ActiveDescriptor<?>> toAdd = new HashSet<ActiveDescriptor<?>>(newDescriptors);
toAdd.removeAll(mirroredDescriptors);

DynamicConfigurationService remoteDCS = remote.getService(DynamicConfigurationService.class);
DynamicConfiguration config = remoteDCS.createDynamicConfiguration();

boolean dirty = false;
for (ActiveDescriptor<?> removeMe : toRemove) {
Filter removeFilter = new RemoveFilter(removeMe.getLocatorId(), removeMe.getServiceId());
config.addUnbindFilter(removeFilter);
dirty = true;
}

for (ActiveDescriptor<?> addMe : toAdd) {
CrossOverDescriptor<Object> cod = new CrossOverDescriptor<Object>(local, (ActiveDescriptor<Object>) addMe);
config.addActiveDescriptor(cod);
dirty = true;
}

if (dirty) {
config.commit();
private void handleChange(List<ActiveDescriptor<?>> newDescriptors) {
lock.lock();
try {
if (remote == null) return;

HashSet<ActiveDescriptor<?>> toRemove = new HashSet<ActiveDescriptor<?>>(mirroredDescriptors);
toRemove.removeAll(newDescriptors);

HashSet<ActiveDescriptor<?>> toAdd = new HashSet<ActiveDescriptor<?>>(newDescriptors);
toAdd.removeAll(mirroredDescriptors);

DynamicConfigurationService remoteDCS = remote.getService(DynamicConfigurationService.class);
DynamicConfiguration config = remoteDCS.createDynamicConfiguration();

boolean dirty = false;
for (ActiveDescriptor<?> removeMe : toRemove) {
Filter removeFilter = new RemoveFilter(removeMe.getLocatorId(), removeMe.getServiceId());
config.addUnbindFilter(removeFilter);
dirty = true;
}

for (ActiveDescriptor<?> addMe : toAdd) {
CrossOverDescriptor<Object> cod = new CrossOverDescriptor<Object>(local, (ActiveDescriptor<Object>) addMe);
config.addActiveDescriptor(cod);
dirty = true;
}

if (dirty) {
config.commit();
}

mirroredDescriptors = newDescriptors;
} finally {
lock.unlock();
}

mirroredDescriptors = newDescriptors;
}

/* (non-Javadoc)
Expand Down

0 comments on commit 2f12674

Please sign in to comment.