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

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit d18dc3ac6a2392752795e5ccb63ffff4d7d34b18
Author: Juan Pablo Santos Rodríguez <[email protected]>
AuthorDate: Sat Dec 2 20:54:55 2023 +0100

    Introduce CustomWikiEventListeners, as an easy way to register 3rd party 
listeners
    
    cfr. with 
https://jspwiki-wiki.apache.org/Wiki.jsp?page=HowToWriteACustomWikiEventListener
---
 .../main/java/org/apache/wiki/api/core/Engine.java | 14 +++--
 .../wiki/api/events/CustomWikiEventListener.java   | 46 +++++++++++++++++
 .../java/org/apache/wiki/api/events/package.html   | 34 ++++++++++++
 .../api/events/CustomWikiEventListenerTest.java    | 60 ++++++++++++++++++++++
 .../events/CustomWikiEventListenerTestImpl.java    | 50 ++++++++++++++++++
 ....apache.wiki.api.events.CustomWikiEventListener |  1 +
 6 files changed, 202 insertions(+), 3 deletions(-)

diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/core/Engine.java 
b/jspwiki-api/src/main/java/org/apache/wiki/api/core/Engine.java
index 95349be7e..49a0ef716 100644
--- a/jspwiki-api/src/main/java/org/apache/wiki/api/core/Engine.java
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/core/Engine.java
@@ -20,9 +20,11 @@ package org.apache.wiki.api.core;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.wiki.api.engine.EngineLifecycleExtension;
+import org.apache.wiki.api.events.CustomWikiEventListener;
 import org.apache.wiki.api.exceptions.ProviderException;
 import org.apache.wiki.api.exceptions.WikiException;
 import org.apache.wiki.event.WikiEventListener;
+import org.apache.wiki.event.WikiEventManager;
 import org.apache.wiki.util.TextUtil;
 
 import javax.servlet.ServletContext;
@@ -425,14 +427,20 @@ public interface Engine {
      * @throws WikiException if something happens while setting up the {@code 
Engine}.
      */
     default void start( final Properties properties ) throws WikiException {
-        final ServiceLoader< EngineLifecycleExtension > loader = 
ServiceLoader.load( EngineLifecycleExtension.class );
-        for( final EngineLifecycleExtension extension : loader ) {
+        final var loader = ServiceLoader.load( EngineLifecycleExtension.class 
);
+        for( final var extension : loader ) {
             extension.onInit( properties );
         }
         initialize( properties );
-        for( final EngineLifecycleExtension extension : loader ) {
+        for( final var extension : loader ) {
             extension.onStart( this, properties );
         }
+        final var events = ServiceLoader.load( CustomWikiEventListener.class );
+        for( final var event : events ) {
+            CustomWikiEventListener.LISTENERS.add( event );
+            event.initialize( this, getWikiProperties() );
+            WikiEventManager.addWikiEventListener( event.client(), event );
+        }
     }
 
     /**
diff --git 
a/jspwiki-api/src/main/java/org/apache/wiki/api/events/CustomWikiEventListener.java
 
b/jspwiki-api/src/main/java/org/apache/wiki/api/events/CustomWikiEventListener.java
new file mode 100644
index 000000000..3109c1513
--- /dev/null
+++ 
b/jspwiki-api/src/main/java/org/apache/wiki/api/events/CustomWikiEventListener.java
@@ -0,0 +1,46 @@
+/*
+    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.wiki.api.events;
+
+import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.engine.Initializable;
+import org.apache.wiki.event.WikiEventListener;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ *
+ * @param  <T> the type of the events' source.
+ */
+public interface CustomWikiEventListener< T > extends WikiEventListener, 
Initializable {
+
+    /** {@link WikiEventListener}s are stored on a {@code List< WeaKReference 
>} so we use this List to strong reference custom event listeners. */
+    List< CustomWikiEventListener< ? > > LISTENERS = new ArrayList<>();
+
+    /**
+     * Returns the object of the events' source. Typically, it will be 
obtained on the
+     * {@link Initializable#initialize(Engine, Properties)} method, and 
returned here.
+     *
+     * @return the object of the events' source.
+     */
+    T client();
+
+}
diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/events/package.html 
b/jspwiki-api/src/main/java/org/apache/wiki/api/events/package.html
new file mode 100644
index 000000000..4fd2737ba
--- /dev/null
+++ b/jspwiki-api/src/main/java/org/apache/wiki/api/events/package.html
@@ -0,0 +1,34 @@
+<!--
+    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.
+-->
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
+<html lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>Engine Events Components</title>
+</head>
+<body>
+JSPWiki's Engine events component related classes.
+
+<h3>Package Specification</h3>
+
+<h3>Related Documentation</h3>
+
+</body>
+</html>
\ No newline at end of file
diff --git 
a/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTest.java
 
b/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTest.java
new file mode 100644
index 000000000..9fc93e877
--- /dev/null
+++ 
b/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTest.java
@@ -0,0 +1,60 @@
+/*
+    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.wiki.api.events;
+
+import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.event.WikiEngineEvent;
+import org.apache.wiki.event.WikiEventManager;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Properties;
+
+@ExtendWith( MockitoExtension.class )
+class CustomWikiEventListenerTest {
+
+    @Mock
+    Engine engine;
+
+    @BeforeEach
+    void setUp() {
+        CustomWikiEventListener.LISTENERS.clear();
+    }
+
+    @Test
+    void shouldRegisterCustomWikiEventListenerTestImpl() throws Exception {
+        final Properties properties = new Properties();
+        Mockito.doReturn( properties ).when( engine ).getWikiProperties();
+        Mockito.doCallRealMethod().when( engine ).start( Mockito.any( 
Properties.class ) );
+
+        engine.start( properties );
+        WikiEventManager.fireEvent( engine, new WikiEngineEvent( engine, 
WikiEngineEvent.INITIALIZED ) );
+
+        Assertions.assertEquals( "initialize", properties.getProperty( "test1" 
) );
+        Assertions.assertEquals( "client", properties.getProperty( "test2" ) );
+        Assertions.assertEquals( "actionPerformed", properties.getProperty( 
"test3" ) );
+        Assertions.assertEquals( 1, CustomWikiEventListener.LISTENERS.size() );
+    }
+
+}
diff --git 
a/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTestImpl.java
 
b/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTestImpl.java
new file mode 100644
index 000000000..57b76ed2a
--- /dev/null
+++ 
b/jspwiki-api/src/test/java/org/apache/wiki/api/events/CustomWikiEventListenerTestImpl.java
@@ -0,0 +1,50 @@
+/*
+    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.wiki.api.events;
+
+import org.apache.wiki.api.core.Engine;
+import org.apache.wiki.api.exceptions.WikiException;
+import org.apache.wiki.event.WikiEvent;
+
+import java.util.Properties;
+
+public class CustomWikiEventListenerTestImpl implements 
CustomWikiEventListener< Engine > {
+
+    Engine e;
+    Properties properties;
+
+    @Override
+    public void initialize( final Engine engine, final Properties properties ) 
throws WikiException {
+        this.e = engine;
+        this.properties = properties;
+        this.properties.put( "test1", "initialize" );
+    }
+
+    @Override
+    public Engine client() {
+        properties.put( "test2", "client" );
+        return e;
+    }
+
+    @Override
+    public void actionPerformed( final WikiEvent event ) {
+        properties.put( "test3", "actionPerformed" );
+    }
+
+}
diff --git 
a/jspwiki-api/src/test/resources/META-INF/services/org.apache.wiki.api.events.CustomWikiEventListener
 
b/jspwiki-api/src/test/resources/META-INF/services/org.apache.wiki.api.events.CustomWikiEventListener
new file mode 100644
index 000000000..bcafe37f6
--- /dev/null
+++ 
b/jspwiki-api/src/test/resources/META-INF/services/org.apache.wiki.api.events.CustomWikiEventListener
@@ -0,0 +1 @@
+org.apache.wiki.api.events.CustomWikiEventListenerTestImpl
\ No newline at end of file

Reply via email to