Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dependency on java.desktop optional #1182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
LTS releases.
These are 8, 11, 12 and 21 currently, see:
https://github.com/apache/commons-lang/blob/master/.github/workflows/maven.yml

Please ensure your build environment is up-to-date and kindly report any build issues.
</description>

Expand Down Expand Up @@ -111,7 +111,7 @@
<email>ggregory at apache.org</email>
<url>https://www.garygregory.com</url>
<organization>The Apache Software Foundation</organization>
<organizationUrl>https://www.apache.org/</organizationUrl>
<organizationUrl>https://www.apache.org/</organizationUrl>
<roles>
<role>PMC Member</role>
</roles>
Expand Down Expand Up @@ -775,6 +775,38 @@
<excludeFilterFile>${basedir}/src/conf/spotbugs-exclude-filter.xml</excludeFilterFile>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<executions>
<execution>
<id>add-module-infos</id>
<phase>package</phase>
<goals>
<goal>add-module-info</goal>
</goals>
<configuration>
<jvmVersion>${moditect.java.version}</jvmVersion>
<outputDirectory>${project.build.directory}</outputDirectory>
<overwriteExistingFiles>true</overwriteExistingFiles>
<failOnWarning>false</failOnWarning>
<module>
<moduleInfo>
<name>${commons.module.name}</name>
<exports>
*;
</exports>
<requires>
static java.desktop;
*;
</requires>
<addServiceUses>${commons.moditect-maven-plugin.addServiceUses}</addServiceUses>
</moduleInfo>
</module>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -945,10 +977,10 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>org/apache/commons/lang3/time/Java15BugFastDateParserTest.java</exclude>
<exclude>org/apache/commons/lang3/time/Java15BugFastDateParserTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,43 @@ protected static boolean isOpen(final State state) {
/** The current state of this circuit breaker. */
protected final AtomicReference<State> state = new AtomicReference<>(State.CLOSED);

/**
* Monitor for usages of change support to enable lazy initialization of that field.
* This lets us require the {@code java.desktop} module statically, significantly reducing
* the jlink-ed size of all downstream apache commons libraries.
*/
private final Object changeSupportMonitor = new Object();

/** An object for managing change listeners registered at this instance. */
private final PropertyChangeSupport changeSupport;
private PropertyChangeSupport changeSupport;

/**
* Creates an {@link AbstractCircuitBreaker}. It also creates an internal {@link PropertyChangeSupport}.
*/
public AbstractCircuitBreaker() {
changeSupport = new PropertyChangeSupport(this);
changeSupport = null;
}

/**
* Adds a change listener to this circuit breaker. This listener is notified whenever
* the state of this circuit breaker changes. If the listener is
* <strong>null</strong>, it is silently ignored.
*
* <p>
* If the {@code java.desktop} module is not present in the JRE,
* calling this method will throw an exception.
* </p>
*
* @param listener the listener to be added
*/
public void addChangeListener(final PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
synchronized (changeSupportMonitor) {
if (changeSupport == null) {
changeSupport = new PropertyChangeSupport(this);
}

changeSupport.addPropertyChangeListener(listener);
}
}

/**
Expand All @@ -115,7 +133,11 @@ public void addChangeListener(final PropertyChangeListener listener) {
*/
protected void changeState(final State newState) {
if (state.compareAndSet(newState.oppositeState(), newState)) {
changeSupport.firePropertyChange(PROPERTY_NAME, !isOpen(newState), isOpen(newState));
synchronized (changeSupportMonitor) {
if (changeSupport != null) {
changeSupport.firePropertyChange(PROPERTY_NAME, !isOpen(newState), isOpen(newState));
}
}
}
}

Expand Down Expand Up @@ -169,7 +191,11 @@ public void open() {
* @param listener the listener to be removed
*/
public void removeChangeListener(final PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
synchronized (changeSupportMonitor) {
if (changeSupport != null) {
changeSupport.removePropertyChangeListener(listener);
}
}
}

}