[seam-commits] Seam SVN: r13580 - in modules/faces/trunk: impl/src/main/java/org/jboss/seam/faces/viewdata and 2 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Aug 9 06:45:31 EDT 2010


Author: swd847
Date: 2010-08-09 06:45:30 -0400 (Mon, 09 Aug 2010)
New Revision: 13580

Added:
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewConfig.java
   modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewData.java
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationExtension.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationEnum.java
   modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationTest.java
Modified:
   modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java
   modules/faces/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
add ability to configure view data from an enum



Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewConfig.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewConfig.java	                        (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewConfig.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.viewdata;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation that is applied to an enum to signify that it contains
+ * configuration for view specific data
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.TYPE)
+ at Documented
+public @interface ViewConfig
+{
+
+}

Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewData.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewData.java	                        (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewData.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.viewdata;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation that is applied to an enum constant to configure view specific
+ * information
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.FIELD)
+ at Documented
+public @interface ViewData
+{
+   String value();
+}

Added: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationExtension.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationExtension.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationExtension.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -0,0 +1,89 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.viewdata;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Extension that scans enums for view specific configuration
+ * 
+ * @author stuart
+ * 
+ */
+public class ViewDataConfigurationExtension implements Extension
+{
+
+   private final Logger log = LoggerFactory.getLogger(ViewDataConfigurationExtension.class);
+
+   private final Map<String, Set<Annotation>> data = new HashMap<String, Set<Annotation>>();
+
+   public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event)
+   {
+      AnnotatedType<T> tp = event.getAnnotatedType();
+      if (tp.isAnnotationPresent(ViewConfig.class))
+      {
+         if (!tp.getJavaClass().isEnum())
+         {
+            log.warn("ViewConfig annotation should only be applied to enums, and {} is not an enum.", tp.getJavaClass());
+         }
+         else
+         {
+            for (Field f : tp.getJavaClass().getDeclaredFields())
+            {
+               if (f.isAnnotationPresent(ViewData.class))
+               {
+                  ViewData viewConfig = f.getAnnotation(ViewData.class);
+                  Set<Annotation> viewData = new HashSet<Annotation>();
+                  data.put(viewConfig.value(), viewData);
+                  for (Annotation a : f.getAnnotations())
+                  {
+                     if (a.annotationType() != ViewData.class)
+                     {
+                        viewData.add(a);
+                     }
+                  }
+               }
+            }
+         }
+      }
+   }
+
+   public Map<String, Set<Annotation>> getData()
+   {
+      return Collections.unmodifiableMap(data);
+   }
+
+}

Modified: modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java
===================================================================
--- modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java	2010-08-09 09:54:48 UTC (rev 13579)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -27,11 +27,13 @@
 import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.faces.context.FacesContext;
+import javax.inject.Inject;
 
 /**
  * Data store for view specific data.
@@ -49,6 +51,26 @@
    
    private final ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, Annotation>> data = new ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, Annotation>>();
 
+   /**
+    * setup the bean with the configuration from the extension
+    * 
+    * It would be better if the extension could do this, but the extension
+    * cannot resolve the bean until after all lifecycle events have been
+    * processed
+    * 
+    */
+   @Inject
+   public void setup(ViewDataConfigurationExtension extension)
+   {
+      for (Entry<String, Set<Annotation>> e : extension.getData().entrySet())
+      {
+         for (Annotation i : e.getValue())
+         {
+            addData(e.getKey(), i);
+         }
+      }
+   }
+
    public synchronized void addData(String viewId, Annotation annotation)
    {
       ConcurrentHashMap<String, Annotation> map = data.get(annotation.annotationType());

Modified: modules/faces/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
--- modules/faces/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-08-09 09:54:48 UTC (rev 13579)
+++ modules/faces/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-08-09 10:45:30 UTC (rev 13580)
@@ -2,3 +2,4 @@
 org.jboss.seam.faces.context.ViewScopedExtension
 org.jboss.seam.faces.context.FlashScopedExtension
 org.jboss.seam.faces.context.FacesAnnotationsAdapterExtension
+org.jboss.seam.faces.viewdata.ViewDataConfigurationExtension

Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationEnum.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationEnum.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationEnum.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.viewdata;
+ at ViewConfig
+public enum ViewDataConfigurationEnum
+{
+   @ViewData("/*")
+   @Icon("default.gif")
+   DEFAULT,
+   @ViewData("/happy/*")
+   @Icon("happy.gif")
+   HAPPY,
+   @ViewData("/sad/*")
+   @Icon("sad.gif")
+   SAD,
+   @ViewData("/happy/done.xhtml")
+   @Icon("finished.gif")
+   HAPPY_DONE;
+}

Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationTest.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationTest.java	                        (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataConfigurationTest.java	2010-08-09 10:45:30 UTC (rev 13580)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.faces.viewdata;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+ at RunWith(Arquillian.class)
+public class ViewDataConfigurationTest
+{
+
+   @Deployment
+   public static Archive<?> createTestArchive()
+   {
+      JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class);
+      archive.addPackage(ViewDataConfigurationTest.class.getPackage());
+      archive.addManifestResource(new ByteArrayAsset(new byte[0]), ArchivePaths.create("beans.xml"));
+      return archive;
+   }
+
+   @Inject
+   ViewDataStore store;
+
+   @Test
+   public void testViewDataStore()
+   {
+
+      store.addData("/*", new IconLiteral("default.gif"));
+      store.addData("/sad/*", new IconLiteral("sad.gif"));
+      store.addData("/happy/*", new IconLiteral("happy.gif"));
+      store.addData("/happy/done.xhtml", new IconLiteral("finished.gif"));
+
+      Icon data;
+      data = store.getData("/happy/done.xhtml", Icon.class);
+      Assert.assertEquals("finished.gif", data.value());
+      data = store.getData("/happy/other.xhtml", Icon.class);
+      Assert.assertEquals("happy.gif", data.value());
+      data = store.getData("/default/news.xhtml", Icon.class);
+      Assert.assertEquals("default.gif", data.value());
+
+      List<Icon> dlist;
+      dlist = store.getAllData("/happy/done.xhtml", Icon.class);
+      Assert.assertEquals(3, dlist.size());
+      Assert.assertEquals("finished.gif", dlist.get(0).value());
+      Assert.assertEquals("happy.gif", dlist.get(1).value());
+      Assert.assertEquals("default.gif", dlist.get(2).value());
+      dlist = store.getAllData("/happy/other.xhtml", Icon.class);
+      Assert.assertEquals(2, dlist.size());
+      Assert.assertEquals("happy.gif", dlist.get(0).value());
+      Assert.assertEquals("default.gif", dlist.get(1).value());
+      dlist = store.getAllData("/default/news.xhtml", Icon.class);
+      Assert.assertEquals(1, dlist.size());
+      Assert.assertEquals("default.gif", dlist.get(0).value());
+
+   }
+}



More information about the seam-commits mailing list