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 2021/07/22 08:47:38 UTC

[isis] branch master updated (4bdbc4e -> e2bc455)

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

ahuber pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git.


    from 4bdbc4e  Merge pull request #679 from apache/ISIS-2813
     new 3e3f3ee  ISIS-2297: sonar: Resources should be closed
     new e2bc455  ISIS-2297: properly throw if InteractionService cannot be provisioned

The 2 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.


Summary of changes:
 .../commons/internal/exceptions/_Exceptions.java   | 99 +++++++++++++---------
 .../core/interaction/scope/InteractionScope.java   | 32 +++++--
 .../identify/ObjectBookmarker_builtinHandlers.java | 40 ++++-----
 3 files changed, 103 insertions(+), 68 deletions(-)

[isis] 02/02: ISIS-2297: properly throw if InteractionService cannot be provisioned

Posted by ah...@apache.org.
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

commit e2bc4559c8a87369462e5b55af0e4f2f02b24137
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jul 22 10:47:26 2021 +0200

    ISIS-2297: properly throw if InteractionService cannot be provisioned
---
 .../commons/internal/exceptions/_Exceptions.java   | 99 +++++++++++++---------
 .../core/interaction/scope/InteractionScope.java   | 32 +++++--
 2 files changed, 82 insertions(+), 49 deletions(-)

diff --git a/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java b/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
index 12735d6..df3c1bd 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/exceptions/_Exceptions.java
@@ -39,6 +39,7 @@ import org.apache.isis.commons.internal.base._With;
 import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.commons.internal.functions._Functions;
 
+import lombok.NonNull;
 import lombok.val;
 
 /**
@@ -64,29 +65,40 @@ public final class _Exceptions {
      * @param _case the unmatched case to be reported
      * @return new IllegalArgumentException
      */
-    public static final IllegalArgumentException unmatchedCase(@Nullable Object _case) {
+    public static final IllegalArgumentException unmatchedCase(@Nullable final Object _case) {
         return new IllegalArgumentException("internal error: unmatched case in switch statement: "+_case);
     }
 
+    // -- ILLEGAL ARGUMENT
+
     /**
      * @param format like in {@link java.lang.String#format(String, Object...)}
      * @param args
      * @return new IllegalArgumentException
      */
     public static final IllegalArgumentException illegalArgument(
-            final String format,
+            final @NonNull String format,
             final @Nullable Object ... args) {
-        _With.requires(format, "format");
         return new IllegalArgumentException(String.format(format, args));
     }
 
+    // -- ILLEGAL STATE
+
     public static IllegalStateException illegalState(
-            final String format,
+            final @NonNull String format,
             final @Nullable Object ... args) {
-        _With.requires(format, "format");
         return new IllegalStateException(String.format(format, args));
     }
 
+    public static IllegalStateException illegalState(
+            final @NonNull Throwable cause,
+            final @NonNull String format,
+            final @Nullable Object ... args) {
+        return new IllegalStateException(String.format(format, args), cause);
+    }
+
+    // -- ILLEGAL ACCESS
+
     public static IllegalAccessException illegalAccess(
             final String format,
             final @Nullable Object ... args) {
@@ -94,42 +106,49 @@ public final class _Exceptions {
         return new IllegalAccessException(String.format(format, args));
     }
 
+    // -- NO SUCH ELEMENT
+
     public static final NoSuchElementException noSuchElement() {
         return new NoSuchElementException();
     }
 
-    public static final NoSuchElementException noSuchElement(String msg) {
+    public static final NoSuchElementException noSuchElement(final String msg) {
         return new NoSuchElementException(msg);
     }
 
-    public static final NoSuchElementException noSuchElement(String format, Object ...args) {
-        _With.requires(format, "format");
+    public static final NoSuchElementException noSuchElement(
+            @NonNull final String format,
+            @Nullable final Object ...args) {
         return noSuchElement(String.format(format, args));
     }
 
+    // -- UNEXPECTED CODE REACH
+
     public static final IllegalStateException unexpectedCodeReach() {
         return new IllegalStateException("internal error: code was reached, that is expected unreachable");
     }
 
+    // -- NOT IMPLEMENTED
+
     public static IllegalStateException notImplemented() {
         return new IllegalStateException("internal error: code was reached, that is not implemented yet");
     }
 
     // -- UNRECOVERABLE
 
-    public static RuntimeException unrecoverable(Throwable cause) {
+    public static RuntimeException unrecoverable(final Throwable cause) {
         return new RuntimeException("unrecoverable error: with cause ...", cause);
     }
 
-    public static RuntimeException unrecoverable(String msg) {
+    public static RuntimeException unrecoverable(final String msg) {
         return new RuntimeException(String.format("unrecoverable error: '%s'", msg));
     }
 
-    public static RuntimeException unrecoverable(String msg, Throwable cause) {
+    public static RuntimeException unrecoverable(final String msg, final Throwable cause) {
         return new RuntimeException(String.format("unrecoverable error: '%s' with cause ...", msg), cause);
     }
 
-    public static RuntimeException unrecoverableFormatted(String format, Object ...args) {
+    public static RuntimeException unrecoverableFormatted(final String format, final Object ...args) {
         return new RuntimeException(String.format("unrecoverable error: '%s'",
                 String.format(format, args)));
     }
@@ -140,23 +159,23 @@ public final class _Exceptions {
         return new UnsupportedOperationException("unrecoverable error: method call not allowed/supported");
     }
 
-    public static UnsupportedOperationException unsupportedOperation(String msg) {
+    public static UnsupportedOperationException unsupportedOperation(final String msg) {
         return new UnsupportedOperationException(msg);
     }
 
-    public static UnsupportedOperationException unsupportedOperation(String format, Object ...args) {
+    public static UnsupportedOperationException unsupportedOperation(final String format, final Object ...args) {
         return new UnsupportedOperationException(String.format(format, args));
     }
 
     // -- ASSERT
 
-    public static AssertionError assertionError(String msg) {
+    public static AssertionError assertionError(final String msg) {
         return new AssertionError(msg);
     }
 
     // -- MESSAGE
 
-    public static String getMessage(Exception ex) {
+    public static String getMessage(final Exception ex) {
         if(ex==null) {
             return "no exception present";
         }
@@ -254,7 +273,7 @@ public final class _Exceptions {
 
     // -- SELECTIVE THROW
 
-    public static <E extends Exception> void throwWhenTrue(E cause, Predicate<E> test) throws E {
+    public static <E extends Exception> void throwWhenTrue(final E cause, final Predicate<E> test) throws E {
         if(test.test(cause)) {
             throw cause;
         }
@@ -262,7 +281,7 @@ public final class _Exceptions {
 
     // -- STACKTRACE UTILITITIES
 
-    public static final Stream<String> streamStacktraceLines(@Nullable Throwable ex, int maxLines) {
+    public static final Stream<String> streamStacktraceLines(@Nullable final Throwable ex, final int maxLines) {
         if(ex==null) {
             return Stream.empty();
         }
@@ -271,16 +290,16 @@ public final class _Exceptions {
                 .limit(maxLines);
     }
 
-    public static final String asStacktrace(@Nullable Throwable ex, int maxLines, String delimiter) {
+    public static final String asStacktrace(@Nullable final Throwable ex, final int maxLines, final String delimiter) {
         return _Exceptions.streamStacktraceLines(ex, maxLines)
                 .collect(Collectors.joining(delimiter));
     }
 
-    public static final String asStacktrace(@Nullable Throwable ex, int maxLines) {
+    public static final String asStacktrace(@Nullable final Throwable ex, final int maxLines) {
         return asStacktrace(ex, maxLines, "\n");
     }
 
-    public static final String asStacktrace(@Nullable Throwable ex) {
+    public static final String asStacktrace(@Nullable final Throwable ex) {
         return asStacktrace(ex, 1000);
     }
 
@@ -290,7 +309,7 @@ public final class _Exceptions {
      * @param skipLines
      * @param maxLines
      */
-    public static void dumpStackTrace(PrintStream writer, int skipLines, int maxLines) {
+    public static void dumpStackTrace(final PrintStream writer, final int skipLines, final int maxLines) {
         _NullSafe.stream(Thread.currentThread().getStackTrace())
         .map(StackTraceElement::toString)
         .skip(skipLines)
@@ -304,7 +323,7 @@ public final class _Exceptions {
 
     // -- CAUSAL CHAIN
 
-    public static List<Throwable> getCausalChain(@Nullable Throwable ex) {
+    public static List<Throwable> getCausalChain(@Nullable final Throwable ex) {
         if(ex==null) {
             return Collections.emptyList();
         }
@@ -317,7 +336,7 @@ public final class _Exceptions {
         return chain;
     }
 
-    public static Stream<Throwable> streamCausalChain(@Nullable Throwable ex) {
+    public static Stream<Throwable> streamCausalChain(@Nullable final Throwable ex) {
         if(ex==null) {
             return Stream.empty();
         }
@@ -325,18 +344,18 @@ public final class _Exceptions {
         return chain.stream();
     }
 
-    public static Throwable getRootCause(@Nullable Throwable ex) {
+    public static Throwable getRootCause(@Nullable final Throwable ex) {
         return _Lists.lastElementIfAny(getCausalChain(ex));
     }
 
     // -- SWALLOW
 
-    public static void silence(Runnable runnable) {
+    public static void silence(final Runnable runnable) {
 
         val currentThread = Thread.currentThread();
         val silencedHandler = currentThread.getUncaughtExceptionHandler();
 
-        currentThread.setUncaughtExceptionHandler((Thread t, Throwable e)->{/*noop*/});
+        currentThread.setUncaughtExceptionHandler((final Thread t, final Throwable e)->{/*noop*/});
 
         try {
             runnable.run();
@@ -376,13 +395,13 @@ public final class _Exceptions {
      */
     public static class FluentException<E extends Exception> {
 
-        public static <E extends Exception> FluentException<E> of(E cause) {
+        public static <E extends Exception> FluentException<E> of(final E cause) {
             return new FluentException<>(cause);
         }
 
         private final E cause;
 
-        private FluentException(E cause) {
+        private FluentException(final E cause) {
             _With.requires(cause, "cause");
             this.cause = cause;
         }
@@ -401,21 +420,21 @@ public final class _Exceptions {
             throw cause;
         }
 
-        public void rethrowIf(Predicate<E> condition) throws E {
+        public void rethrowIf(final Predicate<E> condition) throws E {
             _With.requires(condition, "condition");
             if(condition.test(cause)) {
                 throw cause;
             }
         }
 
-        public void suppressIf(Predicate<E> condition) throws E {
+        public void suppressIf(final Predicate<E> condition) throws E {
             _With.requires(condition, "condition");
             if(!condition.test(cause)) {
                 throw cause;
             }
         }
 
-        public void rethrowIfMessageContains(String string) throws E {
+        public void rethrowIfMessageContains(final String string) throws E {
             _With.requires(string, "string");
             final boolean containsMessage = getMessage().map(msg->msg.contains(string)).orElse(false);
             if(containsMessage) {
@@ -423,7 +442,7 @@ public final class _Exceptions {
             }
         }
 
-        public void suppressIfMessageContains(String string) throws E {
+        public void suppressIfMessageContains(final String string) throws E {
             _With.requires(string, "string");
             final boolean containsMessage = getMessage().map(msg->msg.contains(string)).orElse(false);
             if(!containsMessage) {
@@ -442,37 +461,37 @@ public final class _Exceptions {
 
         private final Function<Exception, ? extends RuntimeException> toUnchecked;
 
-        public TryContext(Function<Exception, ? extends RuntimeException> toUnchecked) {
+        public TryContext(final Function<Exception, ? extends RuntimeException> toUnchecked) {
             this.toUnchecked = toUnchecked;
         }
 
         // -- SHORTCUTS (RUNNABLE)
 
-        public Runnable uncheckedRunnable(_Functions.CheckedRunnable checkedRunnable) {
+        public Runnable uncheckedRunnable(final _Functions.CheckedRunnable checkedRunnable) {
             return checkedRunnable.toUnchecked(toUnchecked);
         }
 
-        public void tryRun(_Functions.CheckedRunnable checkedRunnable) {
+        public void tryRun(final _Functions.CheckedRunnable checkedRunnable) {
             uncheckedRunnable(checkedRunnable).run();
         }
 
         // -- SHORTCUTS (FUNCTION)
 
-        public <T, R> Function<T, R> uncheckedFunction(_Functions.CheckedFunction<T, R> checkedFunction) {
+        public <T, R> Function<T, R> uncheckedFunction(final _Functions.CheckedFunction<T, R> checkedFunction) {
             return checkedFunction.toUnchecked(toUnchecked);
         }
 
-        public <T, R> R tryApply(T obj, _Functions.CheckedFunction<T, R> checkedFunction) {
+        public <T, R> R tryApply(final T obj, final _Functions.CheckedFunction<T, R> checkedFunction) {
             return uncheckedFunction(checkedFunction).apply(obj);
         }
 
         // -- SHORTCUTS (CONSUMER)
 
-        public <T> Consumer<T> uncheckedConsumer(_Functions.CheckedConsumer<T> checkedConsumer) {
+        public <T> Consumer<T> uncheckedConsumer(final _Functions.CheckedConsumer<T> checkedConsumer) {
             return checkedConsumer.toUnchecked(toUnchecked);
         }
 
-        public <T> void tryAccept(T obj, _Functions.CheckedConsumer<T> checkedConsumer) {
+        public <T> void tryAccept(final T obj, final _Functions.CheckedConsumer<T> checkedConsumer) {
             uncheckedConsumer(checkedConsumer).accept(obj);
         }
     }
diff --git a/core/interaction/src/main/java/org/apache/isis/core/interaction/scope/InteractionScope.java b/core/interaction/src/main/java/org/apache/isis/core/interaction/scope/InteractionScope.java
index c895d66..73b6b84 100644
--- a/core/interaction/src/main/java/org/apache/isis/core/interaction/scope/InteractionScope.java
+++ b/core/interaction/src/main/java/org/apache/isis/core/interaction/scope/InteractionScope.java
@@ -21,7 +21,10 @@ package org.apache.isis.core.interaction.scope;
 import java.util.Map;
 import java.util.UUID;
 
+import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
 import org.springframework.beans.factory.ObjectFactory;
 import org.springframework.beans.factory.config.Scope;
 
@@ -45,7 +48,7 @@ implements
 
     private final BeanFactory beanFactory;
 
-    public InteractionScope(BeanFactory beanFactory) {
+    public InteractionScope(final BeanFactory beanFactory) {
         this.beanFactory = beanFactory;
     }
 
@@ -74,19 +77,30 @@ implements
      */
     private ThreadLocal<Map<String, ScopedObject>> scopedObjects = ThreadLocal.withInitial(_Maps::newHashMap);
 
+    /**
+    * @return an instance of the single bean matching the required type (InteractionService)
+    * @throws NoSuchBeanDefinitionException if no bean of the given type was found
+    * @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
+    * @throws BeansException if the bean could not be created
+    */
     private InteractionService interactionService() {
         return beanFactory.getBean(InteractionService.class);
     }
 
     @Override
-    public Object get(String name, ObjectFactory<?> objectFactory) {
-
-        if(interactionService() == null) {
-            throw _Exceptions.illegalState("Creation of bean %s with @InteractionScope requires the "
+    public Object get(final String name, final ObjectFactory<?> objectFactory) {
+
+        final InteractionService interactionService;
+        try {
+            interactionService = interactionService();
+        } catch (Exception cause) {
+            throw _Exceptions.illegalState(
+                    cause,
+                    "Creation of bean %s with @InteractionScope requires the "
                     + "InteractionScopeBeanFactoryPostProcessor registered and initialized.", name);
         }
 
-        if(!interactionService().isInInteraction()) {
+        if(interactionService.isInInteraction()) {
             throw _Exceptions.illegalState("Creation of bean %s with @InteractionScope requires the "
                     + "calling %s to have an open Interaction on the thread-local stack. Running into "
                     + "this issue might be caused by use of ... @Inject MyScopedBean bean ..., instead of "
@@ -123,12 +137,12 @@ implements
     }
 
     @Override
-    public Object remove(String name) {
+    public Object remove(final String name) {
         throw new UnsupportedOperationException("use IsisInteractionScope.removeAll instead");
     }
 
     @Override
-    public void registerDestructionCallback(String name, Runnable callback) {
+    public void registerDestructionCallback(final String name, final Runnable callback) {
         val scopedObject = scopedObjects.get().get(name);
         if(scopedObject!=null) {
             scopedObject.setDestructionCallback(callback);
@@ -137,7 +151,7 @@ implements
     }
 
     @Override
-    public Object resolveContextualObject(String key) {
+    public Object resolveContextualObject(final String key) {
         // null by convention if not supported
         return null;
     }

[isis] 01/02: ISIS-2297: sonar: Resources should be closed

Posted by ah...@apache.org.
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

commit 3e3f3ee55367fe8691d180ab923de0dc7ffdcc5b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jul 22 10:27:20 2021 +0200

    ISIS-2297: sonar: Resources should be closed
---
 .../identify/ObjectBookmarker_builtinHandlers.java | 40 ++++++++++++----------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/identify/ObjectBookmarker_builtinHandlers.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/identify/ObjectBookmarker_builtinHandlers.java
index bc22d18..58a5b89 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/identify/ObjectBookmarker_builtinHandlers.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/identify/ObjectBookmarker_builtinHandlers.java
@@ -44,12 +44,12 @@ class ObjectBookmarker_builtinHandlers {
     static class GuardAgainstOid implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return managedObject.getPojo() instanceof Oid;
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             throw new IllegalArgumentException("Cannot create a Bookmark for pojo, "
                     + "when pojo is instance of Bookmark. You might want to ask "
                     + "ObjectAdapterByIdProvider for an ObjectAdapter instead.");
@@ -60,12 +60,12 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForServices implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return managedObject.getSpecification().isManagedBean();
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             final String identifier = SERVICE_IDENTIFIER;
             return Bookmark.forLogicalTypeAndIdentifier(
                     managedObject.getSpecification().getLogicalType(),
@@ -77,12 +77,12 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForEntities implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return managedObject.getSpecification().isEntity();
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             val spec = managedObject.getSpecification();
             val pojo = managedObject.getPojo();
             if(pojo==null) {
@@ -103,12 +103,12 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForValues implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return managedObject.getSpecification().containsFacet(ValueFacet.class);
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             throw _Exceptions.illegalArgument("cannot 'identify' the value type %s, "
                     + "as values have no identifier",
                     managedObject.getSpecification().getCorrespondingClass().getName());
@@ -119,21 +119,23 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForSerializable implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             val spec = managedObject.getSpecification();
             return spec.isViewModel() && java.io.Serializable.class.isAssignableFrom(spec.getCorrespondingClass());
         }
 
         @SneakyThrows
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             val spec = managedObject.getSpecification();
             val baos = new ByteArrayOutputStream();
-            val oos = new ObjectOutputStream(baos);
-            oos.writeObject(managedObject.getPojo());
-            val identifier = _Strings.ofBytes(_Bytes.asUrlBase64.apply(baos.toByteArray()), StandardCharsets.UTF_8);
-            oos.close();
-            return Bookmark.forLogicalTypeAndIdentifier(spec.getLogicalType(), identifier);
+            try(val oos = new ObjectOutputStream(baos)) {
+                oos.writeObject(managedObject.getPojo());
+                val identifier = _Strings.ofBytes(
+                        _Bytes.asUrlBase64.apply(baos.toByteArray()),
+                        StandardCharsets.UTF_8);
+                return Bookmark.forLogicalTypeAndIdentifier(spec.getLogicalType(), identifier);
+            }
         }
 
     }
@@ -141,12 +143,12 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForViewModels implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return managedObject.getSpecification().containsFacet(ViewModelFacet.class);
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             val spec = managedObject.getSpecification();
             val recreatableObjectFacet = spec.getFacet(ViewModelFacet.class);
             val identifier = recreatableObjectFacet.memento(managedObject.getPojo());
@@ -158,12 +160,12 @@ class ObjectBookmarker_builtinHandlers {
     static class BookmarkForOthers implements Handler {
 
         @Override
-        public boolean isHandling(ManagedObject managedObject) {
+        public boolean isHandling(final ManagedObject managedObject) {
             return true; // try to handle anything
         }
 
         @Override
-        public Bookmark handle(ManagedObject managedObject) {
+        public Bookmark handle(final ManagedObject managedObject) {
             val spec = managedObject.getSpecification();
             val identifier = UUID.randomUUID().toString();
             return Bookmark.forLogicalTypeAndIdentifier(spec.getLogicalType(), identifier);