You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/01/10 15:21:01 UTC
[isis] branch master updated: ISIS-2158: refactors one-shot idiom
into a 'commons' utility
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 8759334 ISIS-2158: refactors one-shot idiom into a 'commons' utility
8759334 is described below
commit 8759334412b72530ea3a804c50495d22dd745ab6
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Jan 10 16:20:53 2020 +0100
ISIS-2158: refactors one-shot idiom into a 'commons' utility
---
.../isis/commons/internal/base/_Oneshot.java | 64 ++++++++++++++++++++++
.../webappctx/IsisWebAppContextInitializer.java | 15 +++--
2 files changed, 71 insertions(+), 8 deletions(-)
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Oneshot.java b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Oneshot.java
new file mode 100644
index 0000000..c2c006a
--- /dev/null
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/base/_Oneshot.java
@@ -0,0 +1,64 @@
+/*
+ * 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.isis.commons.internal.base;
+
+import java.io.Serializable;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * One-shot idiom helper utility, thread-safe and serializable
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ *
+ * @since 2.0
+ *
+ */
+public final class _Oneshot implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final Object $lock = new Object[0]; // serializable lock
+
+ private int shotCount = 0;
+
+ /**
+ * @return whether the shot actually happened (was allowed)
+ */
+ public boolean shoot() {
+ synchronized ($lock) {
+ if(shotCount==0) {
+ ++ shotCount;
+ return true;
+ }
+ return false;
+ }
+ }
+
+ /**
+ * resets to initial condition, that is it allows one more shot
+ */
+ public void reset() {
+ synchronized ($lock) {
+ shotCount = 0;
+ }
+ }
+
+}
diff --git a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java b/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
index 2a9507b..8324f32 100644
--- a/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
+++ b/core/webapp/src/main/java/org/apache/isis/webapp/webappctx/IsisWebAppContextInitializer.java
@@ -19,9 +19,9 @@
package org.apache.isis.webapp.webappctx;
import java.util.EventListener;
-import java.util.concurrent.atomic.LongAdder;
import javax.inject.Inject;
+import javax.inject.Singleton;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@@ -31,6 +31,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.commons.internal.base._Oneshot;
import org.apache.isis.commons.internal.context._Context;
import org.apache.isis.config.IsisConfiguration;
import org.apache.isis.config.viewer.wicket.WebAppContextPath;
@@ -59,7 +60,7 @@ import lombok.extern.log4j.Log4j2;
@Log4j2
public class IsisWebAppContextInitializer implements ServletContextInitializer {
- private final static LongAdder instanceCount = new LongAdder();
+ private final static _Oneshot oneshot = new _Oneshot();
@Inject private ServiceRegistry serviceRegistry; // this dependency ensures Isis has been initialized/provisioned
@Inject private IsisConfiguration isisConfiguration;
@@ -70,11 +71,9 @@ public class IsisWebAppContextInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
- { // onStartup(...) must be a one shot, otherwise just ignore
- if(instanceCount.intValue() > 0) {
- return;
- }
- instanceCount.increment();
+ // onStartup(...) must be a one shot, otherwise just ignore
+ if(!oneshot.shoot()) {
+ return;
}
if(!isIsisProvisioned()) {
@@ -109,7 +108,7 @@ public class IsisWebAppContextInitializer implements ServletContextInitializer {
log.info("about to destroy the context");
webModuleContext.shutdown(event);
}
- instanceCount.reset();
+ oneshot.reset();
log.info("context destroyed");
}