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

Add Spotless linter #1585

Merged
merged 2 commits into from
Nov 1, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
env:
# Kotlin needs access to java.util
MAVEN_OPTS: --add-opens java.base/java.util=ALL-UNNAMED
run: mvn test javadoc:javadoc
run: mvn test spotless:check javadoc:javadoc
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: always() # always run even if the previous step fails
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package net.javacrumbs.shedlock.cdi;

import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Inherited
public @interface SchedulerLock {
/**
* Lock name.
*/
@Nonbinding String name();
/** Lock name. */
@Nonbinding
String name();

/**
* How long the lock should be kept in case the machine which obtained the lock died before releasing it.
* This is just a fallback, under normal circumstances the lock is released as soon the tasks finishes. Can be any format
* supported by <a href="https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration Conversion</a>
* How long the lock should be kept in case the machine which obtained the lock
* died before releasing it. This is just a fallback, under normal circumstances
* the lock is released as soon the tasks finishes. Can be any format supported
* by <a href=
* "https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration
* Conversion</a>
*
* <p>
*/
@Nonbinding String lockAtMostFor() default "";
@Nonbinding
String lockAtMostFor() default "";

/**
* The lock will be held at least for this period of time. Can be used if you really need to execute the task
* at most once in given period of time. If the duration of the task is shorter than clock difference between nodes, the task can
* be theoretically executed more than once (one node after another). By setting this parameter, you can make sure that the
* lock will be kept at least for given period of time. Can be any format
* supported by <a href="https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration Conversion</a>
* The lock will be held at least for this period of time. Can be used if you
* really need to execute the task at most once in given period of time. If the
* duration of the task is shorter than clock difference between nodes, the task
* can be theoretically executed more than once (one node after another). By
* setting this parameter, you can make sure that the lock will be kept at least
* for given period of time. Can be any format supported by <a href=
* "https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration
* Conversion</a>
*/
@Nonbinding String lockAtLeastFor() default "";
@Nonbinding
String lockAtLeastFor() default "";
}

Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
/**
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.shedlock.cdi.internal;


import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.core.LockConfiguration;
import static java.util.Objects.requireNonNull;
import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;

import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;
import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.core.LockConfiguration;

class CdiLockConfigurationExtractor {
private final Duration defaultLockAtMostFor;
Expand All @@ -36,39 +32,26 @@ class CdiLockConfigurationExtractor {
this.defaultLockAtLeastFor = requireNonNull(defaultLockAtLeastFor);
}


Optional<LockConfiguration> getLockConfiguration(Method method) {
Optional<SchedulerLock> annotation = findAnnotation(method);
return annotation.map(this::getLockConfiguration);
}

private LockConfiguration getLockConfiguration(SchedulerLock annotation) {
return new LockConfiguration(
ClockProvider.now(),
getName(annotation),
getLockAtMostFor(annotation),
getLockAtLeastFor(annotation)
);
ClockProvider.now(), getName(annotation), getLockAtMostFor(annotation), getLockAtLeastFor(annotation));
}

private String getName(SchedulerLock annotation) {
return annotation.name();
}

Duration getLockAtMostFor(SchedulerLock annotation) {
return getValue(
annotation.lockAtMostFor(),
this.defaultLockAtMostFor,
"lockAtMostFor"
);
return getValue(annotation.lockAtMostFor(), this.defaultLockAtMostFor, "lockAtMostFor");
}

Duration getLockAtLeastFor(SchedulerLock annotation) {
return getValue(
annotation.lockAtLeastFor(),
this.defaultLockAtLeastFor,
"lockAtLeastFor"
);
return getValue(annotation.lockAtLeastFor(), this.defaultLockAtLeastFor, "lockAtLeastFor");
}

private Duration getValue(String stringValueFromAnnotation, Duration defaultValue, String paramName) {
Expand All @@ -83,5 +66,3 @@ Optional<SchedulerLock> findAnnotation(Method method) {
return Optional.ofNullable(method.getAnnotation(SchedulerLock.class));
}
}


Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.shedlock.cdi.internal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package net.javacrumbs.shedlock.cdi.internal;

import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.core.LockingTaskExecutor;
import org.eclipse.microprofile.config.ConfigProvider;
import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;

import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;

import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;

import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.core.LockingTaskExecutor;
import org.eclipse.microprofile.config.ConfigProvider;

@SchedulerLock(name = "?")
@Priority(3001)
Expand All @@ -33,9 +31,7 @@ public SchedulerLockInterceptor(LockProvider lockProvider) {
String lockAtLeastFor = getConfigValue("shedlock.defaults.lock-at-least-for");
Objects.requireNonNull(lockAtMostFor, "shedlock.defaults.lock-at-most-for parameter is mandatory");
this.lockConfigurationExtractor = new CdiLockConfigurationExtractor(
parseDuration(lockAtMostFor),
lockAtLeastFor != null ? parseDuration(lockAtLeastFor) : Duration.ZERO
);
parseDuration(lockAtMostFor), lockAtLeastFor != null ? parseDuration(lockAtLeastFor) : Duration.ZERO);
}

private static String getConfigValue(String propertyName) {
Expand All @@ -49,7 +45,8 @@ Object lock(InvocationContext context) throws Throwable {
throw new LockingNotSupportedException();
}

Optional<LockConfiguration> lockConfiguration = lockConfigurationExtractor.getLockConfiguration(context.getMethod());
Optional<LockConfiguration> lockConfiguration =
lockConfigurationExtractor.getLockConfiguration(context.getMethod());
if (lockConfiguration.isPresent()) {
lockingTaskExecutor.executeWithLock((LockingTaskExecutor.Task) context::proceed, lockConfiguration.get());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.enterprise.util.Nonbinding;
import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
Expand All @@ -14,26 +13,33 @@
@Target({ElementType.TYPE, ElementType.METHOD})
@Inherited
public @interface SchedulerLock {
/**
* Lock name.
*/
@Nonbinding String name();
/** Lock name. */
@Nonbinding
String name();

/**
* How long the lock should be kept in case the machine which obtained the lock died before releasing it.
* This is just a fallback, under normal circumstances the lock is released as soon the tasks finishes. Can be any format
* supported by <a href="https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration Conversion</a>
* How long the lock should be kept in case the machine which obtained the lock
* died before releasing it. This is just a fallback, under normal circumstances
* the lock is released as soon the tasks finishes. Can be any format supported
* by <a href=
* "https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration
* Conversion</a>
*
* <p>
*/
@Nonbinding String lockAtMostFor() default "";
@Nonbinding
String lockAtMostFor() default "";

/**
* The lock will be held at least for this period of time. Can be used if you really need to execute the task
* at most once in given period of time. If the duration of the task is shorter than clock difference between nodes, the task can
* be theoretically executed more than once (one node after another). By setting this parameter, you can make sure that the
* lock will be kept at least for given period of time. Can be any format
* supported by <a href="https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration Conversion</a>
* The lock will be held at least for this period of time. Can be used if you
* really need to execute the task at most once in given period of time. If the
* duration of the task is shorter than clock difference between nodes, the task
* can be theoretically executed more than once (one node after another). By
* setting this parameter, you can make sure that the lock will be kept at least
* for given period of time. Can be any format supported by <a href=
* "https://docs.micronaut.io/latest/guide/config.html#_duration_conversion">Duration
* Conversion</a>
*/
@Nonbinding String lockAtLeastFor() default "";
@Nonbinding
String lockAtLeastFor() default "";
}

Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
/**
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.shedlock.cdi.internal;


import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.core.LockConfiguration;
import static java.util.Objects.requireNonNull;
import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;

import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
import static net.javacrumbs.shedlock.cdi.internal.Utils.parseDuration;
import net.javacrumbs.shedlock.cdi.SchedulerLock;
import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.core.LockConfiguration;

class CdiLockConfigurationExtractor {
private final Duration defaultLockAtMostFor;
Expand All @@ -36,39 +32,26 @@ class CdiLockConfigurationExtractor {
this.defaultLockAtLeastFor = requireNonNull(defaultLockAtLeastFor);
}


Optional<LockConfiguration> getLockConfiguration(Method method) {
Optional<SchedulerLock> annotation = findAnnotation(method);
return annotation.map(this::getLockConfiguration);
}

private LockConfiguration getLockConfiguration(SchedulerLock annotation) {
return new LockConfiguration(
ClockProvider.now(),
getName(annotation),
getLockAtMostFor(annotation),
getLockAtLeastFor(annotation)
);
ClockProvider.now(), getName(annotation), getLockAtMostFor(annotation), getLockAtLeastFor(annotation));
}

private String getName(SchedulerLock annotation) {
return annotation.name();
}

Duration getLockAtMostFor(SchedulerLock annotation) {
return getValue(
annotation.lockAtMostFor(),
this.defaultLockAtMostFor,
"lockAtMostFor"
);
return getValue(annotation.lockAtMostFor(), this.defaultLockAtMostFor, "lockAtMostFor");
}

Duration getLockAtLeastFor(SchedulerLock annotation) {
return getValue(
annotation.lockAtLeastFor(),
this.defaultLockAtLeastFor,
"lockAtLeastFor"
);
return getValue(annotation.lockAtLeastFor(), this.defaultLockAtLeastFor, "lockAtLeastFor");
}

private Duration getValue(String stringValueFromAnnotation, Duration defaultValue, String paramName) {
Expand All @@ -83,5 +66,3 @@ Optional<SchedulerLock> findAnnotation(Method method) {
return Optional.ofNullable(method.getAnnotation(SchedulerLock.class));
}
}


Loading