You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lp...@apache.org on 2024/10/17 05:53:34 UTC

(shiro) branch revert-1581-feature_quartzmoduleremove created (now a2dffd63e)

This is an automated email from the ASF dual-hosted git repository.

lprimak pushed a change to branch revert-1581-feature_quartzmoduleremove
in repository https://gitbox.apache.org/repos/asf/shiro.git


      at a2dffd63e Revert "[#1499] - Deleted Quartz module."

This branch includes the following new commits:

     new a2dffd63e Revert "[#1499] - Deleted Quartz module."

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(shiro) 01/01: Revert "[#1499] - Deleted Quartz module."

Posted by lp...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lprimak pushed a commit to branch revert-1581-feature_quartzmoduleremove
in repository https://gitbox.apache.org/repos/asf/shiro.git

commit a2dffd63e434017444ca506b4b3c3f609e54579a
Author: Lenny Primak <le...@flowlogix.com>
AuthorDate: Thu Oct 17 00:53:29 2024 -0500

    Revert "[#1499] - Deleted Quartz module."
---
 support/pom.xml                                    |   1 +
 .../mgt/quartz/QuartzSessionValidationJob.java     |  86 +++++++
 .../quartz/QuartzSessionValidationScheduler.java   | 247 +++++++++++++++++++++
 .../shiro/session/mgt/quartz/package-info.java     |  24 ++
 support/quartz/src/main/resources/META-INF/NOTICE  |  15 ++
 test-coverage/pom.xml                              |   7 +-
 6 files changed, 379 insertions(+), 1 deletion(-)

diff --git a/support/pom.xml b/support/pom.xml
index 16b42a1b7..e9c9ee68a 100644
--- a/support/pom.xml
+++ b/support/pom.xml
@@ -36,6 +36,7 @@
         <module>jcache</module>
         <module>ehcache</module>
         <module>hazelcast</module>
+        <module>quartz</module>
         <module>spring</module>
         <module>guice</module>
         <module>spring-boot</module>
diff --git a/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationJob.java b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationJob.java
new file mode 100644
index 000000000..5e5af4ae0
--- /dev/null
+++ b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationJob.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ * 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 org.apache.shiro.session.mgt.quartz;
+
+import org.quartz.Job;
+import org.quartz.JobDataMap;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.shiro.session.mgt.ValidatingSessionManager;
+
+/**
+ * A quartz job that basically just calls the {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions()}
+ * method on a configured session manager.  The session manager will automatically be injected by the
+ * superclass if it is in the job data map or the scheduler map.
+ *
+ * @since 0.1
+ */
+public class QuartzSessionValidationJob implements Job {
+
+    /*--------------------------------------------
+    |             C O N S T A N T S             |
+    ============================================*/
+    /**
+     * Key used to store the session manager in the job data map for this job.
+     */
+    static final String SESSION_MANAGER_KEY = "sessionManager";
+
+    /*--------------------------------------------
+    |    I N S T A N C E   V A R I A B L E S    |
+    ============================================*/
+    private static final Logger LOGGER = LoggerFactory.getLogger(QuartzSessionValidationJob.class);
+
+    /*--------------------------------------------
+    |         C O N S T R U C T O R S           |
+    ============================================*/
+
+    /*--------------------------------------------
+    |  A C C E S S O R S / M O D I F I E R S    |
+    ============================================*/
+
+    /*--------------------------------------------
+    |               M E T H O D S               |
+    ============================================*/
+
+    /**
+     * Called when the job is executed by quartz.  This method delegates to the
+     * <tt>validateSessions()</tt> method on the associated session manager.
+     *
+     * @param context the Quartz job execution context for this execution.
+     */
+    public void execute(JobExecutionContext context) throws JobExecutionException {
+
+        JobDataMap jobDataMap = context.getMergedJobDataMap();
+        ValidatingSessionManager sessionManager = (ValidatingSessionManager) jobDataMap.get(SESSION_MANAGER_KEY);
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Executing session validation Quartz job...");
+        }
+
+        sessionManager.validateSessions();
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Session validation Quartz job complete.");
+        }
+    }
+}
diff --git a/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationScheduler.java b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationScheduler.java
new file mode 100644
index 000000000..1a76886f4
--- /dev/null
+++ b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/QuartzSessionValidationScheduler.java
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ * 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 org.apache.shiro.session.mgt.quartz;
+
+import org.quartz.JobBuilder;
+import org.quartz.JobDetail;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+import org.quartz.SimpleTrigger;
+import org.quartz.TriggerBuilder;
+import org.quartz.TriggerKey;
+import org.quartz.impl.StdSchedulerFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.shiro.session.mgt.DefaultSessionManager;
+import org.apache.shiro.session.mgt.SessionValidationScheduler;
+import org.apache.shiro.session.mgt.ValidatingSessionManager;
+
+import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
+
+/**
+ * An implementation of the {@link org.apache.shiro.session.mgt.SessionValidationScheduler SessionValidationScheduler}
+ * that uses Quartz to schedule a job to call {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions()} on
+ * a regular basis.
+ *
+ * @since 0.1
+ */
+public class QuartzSessionValidationScheduler implements SessionValidationScheduler {
+
+    //TODO - complete JavaDoc
+
+    /*--------------------------------------------
+    |             C O N S T A N T S             |
+    ============================================*/
+    /**
+     * The default interval at which sessions will be validated (1 hour);
+     * This can be overridden by calling {@link #setSessionValidationInterval(long)}
+     */
+    public static final long DEFAULT_SESSION_VALIDATION_INTERVAL = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL;
+
+    /**
+     * The name assigned to the quartz job.
+     */
+    private static final String JOB_NAME = "SessionValidationJob";
+
+    /*--------------------------------------------
+    |    I N S T A N C E   V A R I A B L E S    |
+    ============================================*/
+    private static final Logger LOGGER = LoggerFactory.getLogger(QuartzSessionValidationScheduler.class);
+
+    /**
+     * The configured Quartz scheduler to use to schedule the Quartz job.  If no scheduler is
+     * configured, the scheduler will be retrieved by calling {@link StdSchedulerFactory#getDefaultScheduler()}
+     */
+    private Scheduler scheduler;
+
+    private boolean schedulerImplicitlyCreated;
+
+    private boolean enabled;
+
+    /**
+     * The session manager used to validate sessions.
+     */
+    private ValidatingSessionManager sessionManager;
+
+    /**
+     * The session validation interval in milliseconds.
+     */
+    private long sessionValidationInterval = DEFAULT_SESSION_VALIDATION_INTERVAL;
+
+    /*--------------------------------------------
+    |         C O N S T R U C T O R S           |
+    ============================================*/
+
+    /**
+     * Default constructor.
+     */
+    public QuartzSessionValidationScheduler() {
+    }
+
+    /**
+     * Constructor that specifies the session manager that should be used for validating sessions.
+     *
+     * @param sessionManager the <tt>SessionManager</tt> that should be used to validate sessions.
+     */
+    public QuartzSessionValidationScheduler(ValidatingSessionManager sessionManager) {
+        this.sessionManager = sessionManager;
+    }
+
+    /*--------------------------------------------
+    |  A C C E S S O R S / M O D I F I E R S    |
+    ============================================*/
+
+    protected Scheduler getScheduler() throws SchedulerException {
+        if (scheduler == null) {
+            scheduler = StdSchedulerFactory.getDefaultScheduler();
+            schedulerImplicitlyCreated = true;
+        }
+        return scheduler;
+    }
+
+    public void setScheduler(Scheduler scheduler) {
+        this.scheduler = scheduler;
+    }
+
+    public void setSessionManager(ValidatingSessionManager sessionManager) {
+        this.sessionManager = sessionManager;
+    }
+
+    public boolean isEnabled() {
+        return this.enabled;
+    }
+
+    /**
+     * Specifies how frequently (in milliseconds) this Scheduler will call the
+     * {@link ValidatingSessionManager#validateSessions() ValidatingSessionManager#validateSessions()} method.
+     *
+     * <p>Unless this method is called, the default value is {@link #DEFAULT_SESSION_VALIDATION_INTERVAL}.
+     *
+     * @param sessionValidationInterval
+     */
+    public void setSessionValidationInterval(long sessionValidationInterval) {
+        this.sessionValidationInterval = sessionValidationInterval;
+    }
+
+    /*--------------------------------------------
+    |               M E T H O D S               |
+    ============================================*/
+
+    /**
+     * Starts session validation by creating a Quartz simple trigger, linking it to
+     * the {@link QuartzSessionValidationJob}, and scheduling it with the Quartz scheduler.
+     */
+    public void enableSessionValidation() {
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Scheduling session validation job using Quartz with "
+                    + "session validation interval of [" + sessionValidationInterval + "]ms...");
+        }
+
+        try {
+            TriggerBuilder<SimpleTrigger> triggerBuilder =
+                    TriggerBuilder.newTrigger()
+                            .withIdentity(getClass().getName(), Scheduler.DEFAULT_GROUP)
+                            .withSchedule(simpleSchedule()
+                                    .withIntervalInMilliseconds(sessionValidationInterval)
+                                    .withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY));
+            SimpleTrigger trigger = triggerBuilder.build();
+
+            JobDetail detail = JobBuilder.newJob(QuartzSessionValidationJob.class)
+                    .withIdentity(JOB_NAME, Scheduler.DEFAULT_GROUP).build();
+            detail.getJobDataMap().put(QuartzSessionValidationJob.SESSION_MANAGER_KEY, sessionManager);
+
+            Scheduler scheduler = getScheduler();
+
+            scheduler.scheduleJob(detail, trigger);
+            if (schedulerImplicitlyCreated) {
+                scheduler.start();
+                if (LOGGER.isDebugEnabled()) {
+                    LOGGER.debug("Successfully started implicitly created Quartz Scheduler instance.");
+                }
+            }
+            this.enabled = true;
+
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Session validation job successfully scheduled with Quartz.");
+            }
+
+        } catch (SchedulerException e) {
+            if (LOGGER.isErrorEnabled()) {
+                LOGGER.error("Error starting the Quartz session validation job.  Session validation may not occur.", e);
+            }
+        }
+    }
+
+    @SuppressWarnings("checkstyle:NPathComplexity")
+    public void disableSessionValidation() {
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Stopping Quartz session validation job...");
+        }
+
+        Scheduler scheduler;
+        try {
+            scheduler = getScheduler();
+            if (scheduler == null) {
+                if (LOGGER.isWarnEnabled()) {
+                    LOGGER.warn("getScheduler() method returned a null Quartz scheduler, which is unexpected.  Please "
+                            + "check your configuration and/or implementation.  Returning quietly since there is no "
+                            + "validation job to remove (scheduler does not exist).");
+                }
+                return;
+            }
+        } catch (SchedulerException e) {
+            if (LOGGER.isWarnEnabled()) {
+                LOGGER.warn("Unable to acquire Quartz Scheduler.  Ignoring and returning (already stopped?)", e);
+            }
+            return;
+        }
+
+        try {
+            scheduler.unscheduleJob(TriggerKey.triggerKey(JOB_NAME, Scheduler.DEFAULT_GROUP));
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Quartz session validation job stopped successfully.");
+            }
+        } catch (SchedulerException e) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Could not cleanly remove SessionValidationJob from Quartz scheduler.  "
+                        + "Ignoring and stopping.", e);
+            }
+        }
+
+        this.enabled = false;
+
+        if (schedulerImplicitlyCreated) {
+            try {
+                scheduler.shutdown();
+            } catch (SchedulerException e) {
+                if (LOGGER.isWarnEnabled()) {
+                    LOGGER.warn("Unable to cleanly shutdown implicitly created Quartz Scheduler instance.", e);
+                }
+            } finally {
+                setScheduler(null);
+                schedulerImplicitlyCreated = false;
+            }
+        }
+
+
+    }
+}
diff --git a/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/package-info.java b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/package-info.java
new file mode 100644
index 000000000..e73918a98
--- /dev/null
+++ b/support/quartz/src/main/java/org/apache/shiro/session/mgt/quartz/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ * 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.
+ */
+/**
+ * <a href="http://www.opensymphony.com/quartz/" target="_top">Quartz</a>-based implementations of
+ * components that help <tt>SessionManager</tt> implementations maintain sessions (timed expiration, orphan cleanup,
+ * etc.).
+ */
+package org.apache.shiro.session.mgt.quartz;
diff --git a/support/quartz/src/main/resources/META-INF/NOTICE b/support/quartz/src/main/resources/META-INF/NOTICE
new file mode 100644
index 000000000..9d26a95ff
--- /dev/null
+++ b/support/quartz/src/main/resources/META-INF/NOTICE
@@ -0,0 +1,15 @@
+Apache Shiro
+Copyright 2008-2020 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
+The implementation for org.apache.shiro.util.SoftHashMap is based 
+on initial ideas from Dr. Heinz Kabutz's publicly posted version 
+available at http://www.javaspecialists.eu/archive/Issue015.html,
+with continued modifications.  
+
+Certain parts (StringUtils, IpAddressMatcher, etc.) of the source
+code for this  product was copied for simplicity and to reduce
+dependencies  from the source code developed by the Spring Framework
+Project  (http://www.springframework.org).
diff --git a/test-coverage/pom.xml b/test-coverage/pom.xml
index 852b1e910..eab36c7b6 100644
--- a/test-coverage/pom.xml
+++ b/test-coverage/pom.xml
@@ -89,11 +89,16 @@
             <groupId>org.apache.shiro</groupId>
             <artifactId>shiro-hazelcast</artifactId>
         </dependency>
+
         <dependency>
             <groupId>org.apache.shiro</groupId>
             <artifactId>shiro-guice</artifactId>
         </dependency>
-		
+        <dependency>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-quartz</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.shiro</groupId>
             <artifactId>shiro-spring</artifactId>