/*
 *  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.core.interaction.session;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

import org.apache.isis.commons.collections.Can;
import org.apache.isis.commons.internal.collections._Lists;

import lombok.val;

public class MessageBroker implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private final List<String> messages = _Lists.newArrayList();
    private final List<String> warnings = _Lists.newArrayList();
    private String applicationError;
    
    public MessageBroker() {
    }

    // -- RESET

    public void reset() {
        warnings.clear();
        messages.clear();
        applicationError = null;
    }

    // -- MESSAGES

    public Can<String> drainMessages() {
        return copyAndClear(messages);
    }

    public void addMessage(final String message) {
        messages.add(message);
    }

    // -- WARNINGS

    public Can<String> drainWarnings() {
        return copyAndClear(warnings);
    }

    public void addWarning(final String message) {
        if(warnings.contains(message)) {
            // just ignore it...
            return;
        }
        warnings.add(message);
    }

    // -- APPLICATION ERROR
    
    public Optional<String> drainApplicationError() {
        final String error = applicationError;
        setApplicationError(null);
        return Optional.ofNullable(error);
    }

    public void setApplicationError(String applicationError) {
        this.applicationError = applicationError;
    }


    // -- HELPERS

    private Can<String> copyAndClear(final List<String> messages) {
        val copy = Can.ofCollection(messages);
        messages.clear();
        return copy;
    }

}

@@ -1080,16 +1080,182 @@
 k.val;%0A%0A
+/**        %0A * Holder of queued up messages, to be accessed/drained cross interaction boundary.%0A * %0A * @implNote Serializable and thread-safe %0A *  %0A * @since 1.x%0A */%0A
 public c
@@ -1352,16 +1352,90 @@
 ID = 1L;
+%0A%0A    // serializable lock%0A    private final Object $lock = new Object%5B0%5D;
 %0A    %0A  
@@ -1679,24 +1679,59 @@
 d reset() %7B%0A
+        synchronized ($lock) %7B%0A    
         warn
@@ -1744,32 +1744,36 @@
 lear();%0A        
+ 
+   
 messages.clear()
@@ -1766,32 +1766,36 @@
 ssages.clear();%0A
+    
         applicat
@@ -1810,16 +1810,30 @@
  = null;
+    %0A        %7D
 %0A    %7D%0A%0A
@@ -1982,32 +1982,67 @@
 ring message) %7B%0A
+        synchronized ($lock) %7B%0A    
         messages
@@ -2048,32 +2048,42 @@
 s.add(message);%0A
+        %7D%0A
     %7D%0A%0A    // --
@@ -2223,32 +2223,67 @@
 ring message) %7B%0A
+        synchronized ($lock) %7B%0A    
         if(warni
@@ -2319,16 +2319,20 @@
         
+    
 // just 
@@ -2352,24 +2352,28 @@
             
+ 
+   
 return;%0A    
@@ -2372,26 +2372,34 @@
 rn;%0A        
-%7D%0A
+    %7D%0A    
         warn
@@ -2413,24 +2413,34 @@
 d(message);%0A
+        %7D%0A
     %7D%0A%0A    /
@@ -2517,24 +2517,59 @@
 onError() %7B%0A
+        synchronized ($lock) %7B%0A    
         fina
@@ -2599,24 +2599,28 @@
 ationError;%0A
+    
         setA
@@ -2646,24 +2646,28 @@
 l);%0A        
+    
 return Optio
@@ -2689,16 +2689,26 @@
 error);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2765,24 +2765,59 @@
 ionError) %7B%0A
+        synchronized ($lock) %7B%0A    
         this
@@ -2850,24 +2850,34 @@
 ationError;%0A
+        %7D%0A
     %7D%0A%0A%0A    
@@ -2955,24 +2955,59 @@
 messages) %7B%0A
+        synchronized ($lock) %7B%0A    
         val 
@@ -3041,32 +3041,36 @@
 sages);%0A        
+    
 messages.clear()
@@ -3071,32 +3071,36 @@
 lear();%0A        
+ 
+   
 return copy;%0A   
@@ -3096,16 +3096,26 @@
 n copy;%0A
+        %7D%0A
     %7D%0A%0A%7D

make MessageBroker thread-safe