Skip to content

Commit

Permalink
#228 verify bundle context to avoid endless exceptions in log (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha authored Jan 7, 2025
1 parent 9b35736 commit 66de7d2
Show file tree
Hide file tree
Showing 25 changed files with 274 additions and 256 deletions.
26 changes: 7 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ repositories {
xp.enonicRepo()
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = sourceCompatibility

configurations {
includeLib { transitive = false }
}
Expand All @@ -32,38 +35,23 @@ dependencies {
testImplementation "com.enonic.xp:testing:${xpVersion}"
}

task copyPropertyFiles(type: Copy) {
def libFile = file(configurations.includeLib.singleFile)
def libFiles = zipTree(libFile)

def propertySpec = copySpec {
from libFiles
include "CronUtilsI18N*.properties"

}

into "${buildDir}/resources/main/properties" with propertySpec

}

task copyClassFiles(type: Copy) {
task copyLibFiles(type: Copy) {
def libFile = file(configurations.includeLib.singleFile)
def libFiles = zipTree(libFile)

def classSpec = copySpec {
from libFiles
include "**/*.class"
include "**/*.**"
exclude "META-INF/**"
}

into "${buildDir}/classes/java/main" with classSpec
}

jar {
from 'CronUtilsI18N_*.properties'
from '*.class'
}

processResources.dependsOn copyPropertyFiles, copyClassFiles
processResources.dependsOn copyLibFiles

jacocoTestReport {
reports {
Expand Down
3 changes: 2 additions & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ The schedule function takes a parameter object with options.
*** `*attributes*` (_object_) Map of context attributes.
*** `*user*` (_object_) User credentials.
**** `*login*` (_string_) User login.
**** `*userStore*` (_string_) Name of userStore.
**** `*userStore*` (_string_) Deprecated. Use idProvider instead.
**** `*idProvider*` (_string_) Name of idProvider.

=== `Unschedule`

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
26 changes: 16 additions & 10 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -83,7 +85,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -130,26 +133,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -198,11 +204,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/enonic/lib/cron/context/ContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private Context parseContext( final ContextParams params )
final ContextBuilder builder = ContextBuilder.from( this.defaultContext );

applyRepository( builder, params.getRepository() );
applyAuthInfo( builder, params.getUsername(), params.getUserStore(), params.getPrincipals() );
applyAuthInfo( builder, params.getUsername(), params.getIdProvider(), params.getPrincipals() );
applyBranch( builder, params.getBranch() );
addAttributes( builder, params.getAttributes() );

Expand All @@ -55,13 +55,13 @@ private void applyRepository( final ContextBuilder builder, final String reposit
builder.repositoryId( repository );
}

private void applyAuthInfo( final ContextBuilder builder, final String username, final String userStore,
private void applyAuthInfo( final ContextBuilder builder, final String username, final String idProvider,
final PrincipalKey[] principals )
{
AuthenticationInfo authInfo = this.defaultContext.getAuthInfo();
if ( username != null )
{
authInfo = runAsAuthenticated( () -> getAuthenticationInfo( username, userStore ) );
authInfo = runAsAuthenticated( () -> getAuthenticationInfo( username, idProvider ) );
}
if ( principals != null )
{
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/enonic/lib/cron/handler/LibCronHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.enonic.lib.cron.model.params.ListJobsParams;
import com.enonic.lib.cron.model.params.ScheduleParams;
import com.enonic.lib.cron.provider.CronJobProvider;
import com.enonic.lib.cron.scheduler.JobExecutorService;
import com.enonic.xp.context.Context;
import com.enonic.xp.script.bean.BeanContext;
import com.enonic.xp.script.bean.ScriptBean;
Expand All @@ -18,8 +19,9 @@ public final class LibCronHandler
@Override
public void initialize( final BeanContext context )
{
this.cronJobProvider = new CronJobProvider( context.getApplicationKey(), context.getBinding( Context.class ).get(),
context.getService( SecurityService.class ).get() );
this.cronJobProvider =
new CronJobProvider( context.getBinding( Context.class ).get(), context.getService( SecurityService.class ).get(),
context.getService( JobExecutorService.class ).get() );
}

public void schedule( final ScheduleParams params )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public JobDescriptorsMapper( final List<JobDescriptor> jobDescriptors )
public void serialize( final MapGenerator gen )
{
gen.array( "jobs" );
if ( jobDescriptors != null && jobDescriptors.size() > 0 )
if ( jobDescriptors != null && !jobDescriptors.isEmpty() )
{
jobDescriptors.forEach( jobDescriptor -> {
gen.map();
Expand All @@ -29,6 +29,5 @@ public void serialize( final MapGenerator gen )
} );
}
gen.end();

}
}
8 changes: 4 additions & 4 deletions src/main/java/com/enonic/lib/cron/model/CronTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

final class CronTrigger
{
private final static CronDefinition DEFINITION = CronDefinitionBuilder.instanceDefinitionFor( CronType.UNIX );
private static final CronDefinition DEFINITION = CronDefinitionBuilder.instanceDefinitionFor( CronType.UNIX );

private final static CronParser PARSER = new CronParser( DEFINITION );
private static final CronParser PARSER = new CronParser( DEFINITION );

private final static CronDescriptor DESCRIPTOR =
new CronDescriptor( ResourceBundle.getBundle( "properties/CronUtilsI18N", Locale.UK ) );
private static final CronDescriptor DESCRIPTOR =
new CronDescriptor( ResourceBundle.getBundle( "com/cronutils/CronUtilsI18N", Locale.ROOT ) );

private final Cron cron;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getDescription()
}
else
{
return this.name + " @ " + " delay: " + delay + "ms, fixedDelay: " + this.fixedDelay + "ms";
return this.name + " @ " + " delay: " + this.delay + "ms, fixedDelay: " + this.fixedDelay + "ms";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class ContextParams

protected String username;

protected String userStore;
protected String idProvider;

protected PrincipalKey[] principals;

Expand All @@ -34,9 +34,9 @@ public void setUsername( final String username )
this.username = username;
}

public void setUserStore( final String userStore )
public void setIdProvider( final String idProvider )
{
this.userStore = userStore;
this.idProvider = idProvider;
}

public void setPrincipals( final String[] principals )
Expand Down Expand Up @@ -75,9 +75,9 @@ public String getUsername()
return username;
}

public String getUserStore()
public String getIdProvider()
{
return userStore;
return idProvider;
}

public PrincipalKey[] getPrincipals()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

public class ListJobsParams
{
protected String pattern;

public ListJobsParams()
{
}
private String pattern;

public String getPattern()
{
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/enonic/lib/cron/provider/CronJobProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.enonic.lib.cron.model.JobDescriptorFactory;
import com.enonic.lib.cron.model.params.ListJobsParams;
import com.enonic.lib.cron.model.params.ScheduleParams;
import com.enonic.lib.cron.scheduler.JobExecutorService;
import com.enonic.lib.cron.scheduler.JobScheduler;
import com.enonic.xp.app.ApplicationKey;
import com.enonic.xp.context.Context;
import com.enonic.xp.security.SecurityService;

Expand All @@ -19,21 +19,15 @@ public class CronJobProvider

private final JobScheduler jobScheduler;

public CronJobProvider( final ApplicationKey applicationKey, final Context context, final SecurityService securityService )
public CronJobProvider( final Context context, final SecurityService securityService, final JobExecutorService jobExecutorService )
{
this.jobDescriptorFactory = new JobDescriptorFactory( securityService, context );
this.jobScheduler = new JobScheduler( applicationKey );
this.jobScheduler = new JobScheduler( jobExecutorService );
}

public void schedule( final ScheduleParams params )
{
final JobDescriptor jobDescriptor = jobDescriptorFactory.create( params );

if ( jobDescriptor == null )
{
throw new RuntimeException( String.format( "Cannot create a job 'name: %s, cron:%s'", params.getName(), params.getCron() ) );
}

jobScheduler.schedule( jobDescriptor );
}

Expand All @@ -50,7 +44,6 @@ public void deactivate()
public JobDescriptorMapper get( final String jobName )
{
final JobDescriptor jobDescriptor = this.jobScheduler.get( jobName );

return jobDescriptor != null ? new JobDescriptorMapper( jobDescriptor ) : null;
}

Expand All @@ -59,5 +52,4 @@ public JobDescriptorsMapper list( final ListJobsParams params )
final List<JobDescriptor> jobDescriptors = this.jobScheduler.list( params.getPattern() );
return new JobDescriptorsMapper( jobDescriptors );
}

}
Loading

0 comments on commit 66de7d2

Please sign in to comment.