You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2022/12/16 15:13:26 UTC

[myfaces-tobago] branch main updated: demo(refactor): prepare for Spring Boot (#3563)

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

lofwyr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/main by this push:
     new f833b3cb86 demo(refactor): prepare for Spring Boot (#3563)
f833b3cb86 is described below

commit f833b3cb86890f7e1111583b470c6b04b0796960
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Fri Dec 16 16:13:20 2022 +0100

    demo(refactor): prepare for Spring Boot (#3563)
    
    * refactor
    * remove @Observes/Events
    
    issue: TOBAGO-2184
---
 .../tobago/example/demo/BundleController.java      | 13 ++---
 .../tobago/example/demo/LocaleController.java      | 23 +++-----
 .../tobago/example/demo/NavigationNode.java        |  5 --
 .../tobago/example/demo/NavigationState.java       | 19 +++---
 .../tobago/example/demo/NavigationTree.java        | 51 +++++++---------
 .../demo/SynchronizeNavigationPhaseListener.java   | 68 ----------------------
 .../example/demo/info/ActivityPhaseListener.java   | 35 +++++------
 .../src/main/webapp/WEB-INF/faces-config.xml       |  1 -
 .../src/main/webapp/css/demo.css                   |  2 +
 .../tobago-example-demo/src/main/webapp/main.xhtml |  1 +
 10 files changed, 66 insertions(+), 152 deletions(-)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java
index be5c24853e..3d0b1a582c 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java
@@ -20,10 +20,10 @@
 package org.apache.myfaces.tobago.example.demo;
 
 import jakarta.enterprise.context.SessionScoped;
-import jakarta.enterprise.event.Observes;
 import jakarta.faces.application.Application;
 import jakarta.faces.context.FacesContext;
 import jakarta.inject.Named;
+import org.apache.myfaces.tobago.util.ResourceUtils;
 
 import java.io.Serializable;
 import java.util.ArrayList;
@@ -32,16 +32,14 @@ import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
-import static org.apache.myfaces.tobago.util.ResourceUtils.TOBAGO_RESOURCE_BUNDLE;
-
 @Named
 @SessionScoped
 public class BundleController implements Serializable {
 
-  private List<BundleEntry> resources = new ArrayList<>();
-  private List<BundleEntry> messages = new ArrayList<>();
+  private final List<BundleEntry> resources = new ArrayList<>();
+  private final List<BundleEntry> messages = new ArrayList<>();
 
-  public void clear(@Observes LocaleChanged event) {
+  public void clear() {
     resources.clear();
     messages.clear();
   }
@@ -50,7 +48,7 @@ public class BundleController implements Serializable {
     if (resources.size() == 0) {
       final FacesContext facesContext = FacesContext.getCurrentInstance();
       final ResourceBundle bundle =
-          facesContext.getApplication().getResourceBundle(facesContext, TOBAGO_RESOURCE_BUNDLE);
+          facesContext.getApplication().getResourceBundle(facesContext, ResourceUtils.TOBAGO_RESOURCE_BUNDLE);
       final Enumeration<String> keys = bundle.getKeys();
       while (keys.hasMoreElements()) {
         final String key = keys.nextElement();
@@ -76,5 +74,4 @@ public class BundleController implements Serializable {
     }
     return messages;
   }
-
 }
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
index 50b2e7e8cd..22086473a9 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
@@ -19,13 +19,7 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
-import org.apache.myfaces.tobago.internal.util.ObjectUtils;
-import org.apache.myfaces.tobago.internal.util.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import jakarta.enterprise.context.SessionScoped;
-import jakarta.enterprise.event.Event;
 import jakarta.faces.application.Application;
 import jakarta.faces.application.FacesMessage;
 import jakarta.faces.component.UIViewRoot;
@@ -34,6 +28,9 @@ import jakarta.faces.model.SelectItem;
 import jakarta.inject.Inject;
 import jakarta.inject.Named;
 import jakarta.servlet.ServletContext;
+import org.apache.myfaces.tobago.internal.util.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.Serializable;
 import java.lang.invoke.MethodHandles;
@@ -53,7 +50,7 @@ public class LocaleController implements Serializable {
   private Locale locale;
 
   @Inject
-  private Event<LocaleChanged> events;
+  private BundleController bundleController;
 
   public LocaleController() {
 
@@ -118,10 +115,6 @@ public class LocaleController implements Serializable {
     }
   }
 
-  public Locale getLocale() {
-    return locale;
-  }
-
   public String getLocalizedLocale() {
     if (locale != null) {
       return locale.getDisplayName(locale);
@@ -150,10 +143,12 @@ public class LocaleController implements Serializable {
     return url;
   }
 
+  public Locale getLocale() {
+    return locale;
+  }
+
   public void setLocale(final Locale locale) {
-    if (!ObjectUtils.equals(this.locale, locale)) {
-      events.fire(new LocaleChanged());
-    }
+    bundleController.clear();
     this.locale = locale;
   }
 
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationNode.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationNode.java
index 668f610399..37b8450431 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationNode.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationNode.java
@@ -63,11 +63,6 @@ public class NavigationNode extends DefaultMutableTreeNode implements Comparable
     return branch.compareTo(other.getBranch());
   }
 
-  public String action() {
-    tree.gotoNode(this);
-    return outcome;
-  }
-
   public void evaluateTreePath() {
     treePath = new TreePath(this);
 
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationState.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationState.java
index 8876d58b0e..d2951d25cf 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationState.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationState.java
@@ -19,6 +19,12 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
+import jakarta.annotation.PostConstruct;
+import jakarta.enterprise.context.SessionScoped;
+import jakarta.faces.context.FacesContext;
+import jakarta.faces.event.ComponentSystemEvent;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
 import org.apache.myfaces.tobago.model.ExpandedState;
 import org.apache.myfaces.tobago.model.SelectedState;
 import org.apache.myfaces.tobago.model.TreePath;
@@ -26,13 +32,6 @@ import org.apache.myfaces.tobago.model.TreeState;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import jakarta.annotation.PostConstruct;
-import jakarta.enterprise.context.SessionScoped;
-import jakarta.enterprise.event.Observes;
-import jakarta.faces.context.FacesContext;
-import jakarta.inject.Inject;
-import jakarta.inject.Named;
-
 import java.io.Serializable;
 import java.lang.invoke.MethodHandles;
 import java.util.Collections;
@@ -56,6 +55,10 @@ public class NavigationState implements Serializable {
   private String searchString;
   private List<NavigationNode> searchResult;
 
+  public void sync(ComponentSystemEvent event) {
+    init();
+  }
+
   public List<NavigationNode> getSearchResult() {
     return searchResult;
   }
@@ -113,7 +116,7 @@ public class NavigationState implements Serializable {
     }
   }
 
-  public String gotoNode(@Observes final NavigationNode node) {
+  public String gotoNode(final NavigationNode node) {
     if (node == null) {
       return gotoFirst();
     } else {
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java
index a0ec7b93e2..ae0849aef4 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NavigationTree.java
@@ -19,18 +19,16 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import jakarta.annotation.PostConstruct;
 import jakarta.enterprise.context.ApplicationScoped;
-import jakarta.enterprise.event.Event;
 import jakarta.faces.context.ExternalContext;
 import jakarta.faces.context.FacesContext;
 import jakarta.inject.Inject;
 import jakarta.inject.Named;
 import jakarta.servlet.ServletContext;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
@@ -57,9 +55,6 @@ public class NavigationTree implements Serializable {
 
   private NavigationNode root;
 
-  @Inject
-  private Event<NavigationNode> events;
-
   @Inject
   private ServletContext servletContext;
 
@@ -72,32 +67,34 @@ public class NavigationTree implements Serializable {
   @PostConstruct
   protected void postConstruct() {
 
-    final List<NavigationNode> nodes = new ArrayList<>();
+    final Map<String, NavigationNode> collectedNodes = new HashMap<>();
 
     final List<String> listWar = locateResourcesInWar("/content", new ArrayList<>());
-    addToResult(listWar, nodes);
+    addToResult(listWar, collectedNodes);
 
     final List<String> listClasspath = getResourcesFromClasspath();
-    addToResult(listClasspath, nodes);
+    addToResult(listClasspath, collectedNodes);
 
-    Collections.sort(nodes);
+    final List<NavigationNode> sortedNodes = new ArrayList<>(collectedNodes.values());
+    Collections.sort(sortedNodes);
 
     // after sorting the first node is the root node.
-    root = nodes.get(0);
+    root = sortedNodes.size() > 0 ? sortedNodes.get(0) : null;
 
-    // (why?)
+    // build the tree from the list
     final Map<String, NavigationNode> map = new HashMap<>();
-    for (final NavigationNode node : nodes) {
-//      LOG.debug("Creating node='{}' branch='{}'", node.getName(), node.getBranch());
-      map.put(node.getBranch(), node);
-      final String parent = node.getBranch().substring(0, node.getBranch().lastIndexOf('/'));
+    for (final NavigationNode node : sortedNodes) {
+      final String branch = node.getBranch();
+//      LOG.debug("Creating node='{}' branch='{}'", node.getName(), branch);
+      map.put(branch, node);
+      final String parent = branch.substring(0, branch.lastIndexOf('/'));
       if (!parent.equals("")) { // is root
         map.get(parent).add(node);
       }
       node.evaluateTreePath();
     }
 
-    for (final NavigationNode node : nodes) {
+    for (final NavigationNode node : sortedNodes) {
       searchIndex.add(node);
     }
   }
@@ -115,13 +112,17 @@ public class NavigationTree implements Serializable {
     return result;
   }
 
-  private void addToResult(List<String> listWar, List<NavigationNode> nodes) {
+  private void addToResult(List<String> listWar, Map<String, NavigationNode> nodes) {
     for (final String path : listWar) {
       if (path.contains("/x-") || !path.contains(".xhtml")) {
         // ignoring excluded files
         continue;
       }
-      nodes.add(new NavigationNode(path, this));
+      if (nodes.containsKey(path)) {
+        // ignoring duplicate
+        continue;
+      }
+      nodes.put(path, new NavigationNode(path, this));
     }
   }
 
@@ -163,14 +164,6 @@ public class NavigationTree implements Serializable {
     return root;
   }
 
-  public void gotoNode(final NavigationNode node) {
-    if (node != null) {
-      events.fire(node);
-    } else {
-      events.fire(root);
-    }
-  }
-
   public String getSource() {
     final FacesContext facesContext = FacesContext.getCurrentInstance();
     final ExternalContext externalContext = facesContext.getExternalContext();
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SynchronizeNavigationPhaseListener.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SynchronizeNavigationPhaseListener.java
deleted file mode 100644
index 99e96ad364..0000000000
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SynchronizeNavigationPhaseListener.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.myfaces.tobago.example.demo;
-
-import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
-import org.apache.myfaces.tobago.util.ComponentUtils;
-import org.apache.myfaces.tobago.util.VariableResolverUtils;
-
-import jakarta.faces.component.UIViewRoot;
-import jakarta.faces.context.FacesContext;
-import jakarta.faces.event.PhaseEvent;
-import jakarta.faces.event.PhaseId;
-import jakarta.faces.event.PhaseListener;
-
-//todo @JsfPhaseListener
-public class SynchronizeNavigationPhaseListener implements PhaseListener {
-
-  @Override
-  public void beforePhase(final PhaseEvent event) {
-    if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
-      synchronizeState();
-    }
-  }
-
-  @Override
-  public void afterPhase(final PhaseEvent event) {
-    if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) {
-      synchronizeState();
-    }
-  }
-
-  @Override
-  public PhaseId getPhaseId() {
-    return PhaseId.ANY_PHASE;
-  }
-
-  private void synchronizeState() {
-    // synchronizing current site with
-    final FacesContext facesContext = FacesContext.getCurrentInstance();
-    final UIViewRoot viewRoot = facesContext.getViewRoot();
-    // in case of direct links the ViewRoot is empty after "restore view".
-    if (viewRoot != null && ComponentUtils.findChild(viewRoot, AbstractUIPage.class) == null) {
-      final String viewId = viewRoot.getViewId();
-      if (viewId != null && viewId.startsWith("/content")) {
-        final NavigationTree navigation
-            = (NavigationTree) VariableResolverUtils.resolveVariable(facesContext, "navigationTree");
-        navigation.gotoNode(navigation.findByViewId(viewId));
-      }
-    }
-  }
-}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/info/ActivityPhaseListener.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/info/ActivityPhaseListener.java
index 2b9e5e09a5..7a14672ce7 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/info/ActivityPhaseListener.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/info/ActivityPhaseListener.java
@@ -19,44 +19,41 @@
 
 package org.apache.myfaces.tobago.example.demo.info;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import jakarta.enterprise.inject.spi.CDI;
 import jakarta.faces.context.FacesContext;
 import jakarta.faces.event.PhaseEvent;
 import jakarta.faces.event.PhaseId;
 import jakarta.faces.event.PhaseListener;
-import jakarta.inject.Inject;
 import jakarta.servlet.http.HttpSession;
+import org.apache.myfaces.tobago.util.VariableResolverUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.lang.invoke.MethodHandles;
 
-// XXX @Inject in PhaseListener doesn't work with Quarkus
 public class ActivityPhaseListener implements PhaseListener {
 
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-  @Inject
-  private ActivityList activityList;
-
   @Override
   public void beforePhase(final PhaseEvent event) {
 
-    if (activityList == null) {
-      LOG.warn("The activityList was not set by CDI");
-      activityList = CDI.current().select(ActivityList.class).get();
-    }
-
     final FacesContext facesContext = event.getFacesContext();
+
+    final ActivityList activityList
+        = (ActivityList) VariableResolverUtils.resolveVariable(facesContext, "activityList");
+
     final String sessionId = ((HttpSession) facesContext.getExternalContext().getSession(true)).getId();
 
-    if (facesContext.getPartialViewContext().isAjaxRequest()) {
-      LOG.debug("AJAX request for sessionId='{}'", sessionId);
-      activityList.executeAjaxRequest(sessionId);
+    if (activityList == null) {
+      LOG.warn("Bean activityList not found!");
     } else {
-      LOG.debug("Normal request for sessionId='{}'", sessionId);
-      activityList.executeFacesRequest(sessionId);
+      if (facesContext.getPartialViewContext().isAjaxRequest()) {
+        LOG.debug("AJAX request for sessionId='{}'", sessionId);
+        activityList.executeAjaxRequest(sessionId);
+      } else {
+        LOG.debug("Normal request for sessionId='{}'", sessionId);
+        activityList.executeFacesRequest(sessionId);
+      }
     }
   }
 
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
index ca642c9101..e86832fcde 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
@@ -66,7 +66,6 @@
   </application>
 
   <lifecycle>
-    <phase-listener>org.apache.myfaces.tobago.example.demo.SynchronizeNavigationPhaseListener</phase-listener>
     <phase-listener>org.apache.myfaces.tobago.example.demo.info.ActivityPhaseListener</phase-listener>
     <!--<phase-listener>org.apache.myfaces.tobago.util.DebugPhaseListener</phase-listener>-->
   </lifecycle>
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/css/demo.css b/tobago-example/tobago-example-demo/src/main/webapp/css/demo.css
index 4e65edaffe..a57edcffa9 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/css/demo.css
+++ b/tobago-example/tobago-example-demo/src/main/webapp/css/demo.css
@@ -89,6 +89,8 @@ tobago-tree .tobago-selected {
 }
 tobago-tree .tobago-selected a {
   background-color: #ceecf5;
+  padding-left: 1px;
+  padding-right: 4px;
 }
 
 .testframe-wrapper {
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml
index 23f28c1d50..b82cebd07a 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml
@@ -55,6 +55,7 @@
         <tc:style customClass="#{navigationState.viewSource ? '' : 'container-lg'}"/>
 
         <tc:segmentLayout small="4seg 8seg" medium="3seg 9seg" large="2seg 10seg">
+          <f:event type="preRenderView" listener="#{navigationState.sync}"/>
           <tc:panel>
             <ui:include src="/navigation.xhtml"/>
           </tc:panel>