[seam-commits] Seam SVN: r13579 - in modules/faces/trunk: api/src/main/java/org/jboss/seam/faces/viewdata and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Aug 9 05:54:50 EDT 2010
Author: swd847
Date: 2010-08-09 05:54:48 -0400 (Mon, 09 Aug 2010)
New Revision: 13579
Added:
modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/
modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStore.java
modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/
modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/Icon.java
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/IconLiteral.java
modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataStoreTest.java
Log:
Add view data store for storing view specific data
Added: modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStore.java
===================================================================
--- modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStore.java (rev 0)
+++ modules/faces/trunk/api/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStore.java 2010-08-09 09:54:48 UTC (rev 13579)
@@ -0,0 +1,66 @@
+/*
+ * 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.util.List;
+/**
+ * stores data specific to a given view is a heiracial fashion
+ * @author Stuart Douglas
+ *
+ */
+public interface ViewDataStore
+{
+
+ /**
+ * Adds data to the store
+ *
+ * @param viewId The view id to associate the data with. A * at the end of
+ * the view id is considered a wildcard
+ * @param annotation the data to store
+ */
+ public abstract void addData(String viewId, Annotation annotation);
+
+ /**
+ * gets the most specific data for a given viewId
+ *
+ */
+ public abstract <T extends Annotation> T getData(String viewId, Class<T> type);
+
+ /**
+ * gets the most specific data for the current viewId
+ */
+ public abstract <T extends Annotation> T getData(Class<T> type);
+
+ /**
+ * returns all data for a given viewId, with the most specific data at the
+ * start of the list
+ */
+ public abstract <T extends Annotation> List<T> getAllData(String viewId, Class<T> type);
+
+ /**
+ * returns all data for the current viewId, with the most specific data at
+ * the start of the list
+ */
+ public abstract <T extends Annotation> List<T> getAllData(Class<T> type);
+
+}
\ No newline at end of file
Added: 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 (rev 0)
+++ modules/faces/trunk/impl/src/main/java/org/jboss/seam/faces/viewdata/ViewDataStoreImpl.java 2010-08-09 09:54:48 UTC (rev 13579)
@@ -0,0 +1,170 @@
+/*
+ * 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.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.faces.context.FacesContext;
+
+/**
+ * Data store for view specific data.
+ *
+ * @author Stuart Douglas
+ *
+ */
+ at ApplicationScoped
+public class ViewDataStoreImpl implements ViewDataStore
+{
+ /**
+ * cache of viewId to a given data list
+ */
+ private final ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, List<? extends Annotation>>> cache = new ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, List<? extends Annotation>>>();
+
+ private final ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, Annotation>> data = new ConcurrentHashMap<Class<? extends Annotation>, ConcurrentHashMap<String, Annotation>>();
+
+ public synchronized void addData(String viewId, Annotation annotation)
+ {
+ ConcurrentHashMap<String, Annotation> map = data.get(annotation.annotationType());
+ if (map == null)
+ {
+ map = new ConcurrentHashMap<String, Annotation>();
+ data.put(annotation.annotationType(), map);
+ }
+ map.put(viewId, annotation);
+ }
+
+ public <T extends Annotation> T getData(String viewId, Class<T> type)
+ {
+ List<T> data = prepareCache(viewId, type);
+ if (data != null)
+ {
+ return data.get(0);
+ }
+ return null;
+ }
+
+ public <T extends Annotation> T getData(Class<T> type)
+ {
+ return getData(FacesContext.getCurrentInstance().getViewRoot().getViewId(), type);
+ }
+
+ public <T extends Annotation> List<T> getAllData(String viewId, Class<T> type)
+ {
+ List<T> data = prepareCache(viewId, type);
+ if (data != null)
+ {
+ return Collections.unmodifiableList(data);
+ }
+ return null;
+ }
+
+ public <T extends Annotation> List<T> getAllData(Class<T> type)
+ {
+ return getAllData(FacesContext.getCurrentInstance().getViewRoot().getViewId(), type);
+ }
+
+ private <T extends Annotation> List<T> prepareCache(String viewId, Class<T> type)
+ {
+ // we need to synchonise to make sure that no threads see a half completed
+ // list due to instruction re-ordering
+ ConcurrentHashMap<String, List<? extends Annotation>> map = cache.get(type);
+ if (map == null)
+ {
+ ConcurrentHashMap<String, List<? extends Annotation>> newMap = new ConcurrentHashMap<String, List<? extends Annotation>>();
+ map = cache.putIfAbsent(type, newMap);
+ if (map == null)
+ {
+ map = newMap;
+ }
+ }
+ List<? extends Annotation> annotationData = map.get(viewId);
+ if (annotationData == null)
+ {
+ List<Annotation> newList = new ArrayList<Annotation>();
+ Map<String, Annotation> viewData = data.get(type);
+ List<String> resultingViews = new ArrayList<String>();
+ if (viewData != null)
+ {
+ for (Entry<String, Annotation> e : viewData.entrySet())
+ {
+ if (e.getKey().endsWith("*"))
+ {
+ String cutView = e.getKey().substring(0, e.getKey().length() - 1);
+ if (viewId.startsWith(cutView))
+ {
+ resultingViews.add(e.getKey());
+ }
+ }
+ else
+ {
+ if (e.getKey().equals(viewId))
+ {
+ resultingViews.add(e.getKey());
+ }
+ }
+ }
+ // sort the keys by length, longest is the most specific and so
+ // should go first
+ Collections.sort(resultingViews, StringLengthComparator.INSTANCE);
+ for (String i : resultingViews)
+ {
+ newList.add(viewData.get(i));
+ }
+ }
+
+ annotationData = map.putIfAbsent(viewId, newList);
+ if (annotationData == null)
+ {
+ annotationData = newList;
+ }
+ }
+ return (List) annotationData;
+ }
+
+ private static class StringLengthComparator implements Comparator<String>
+ {
+
+ public int compare(String o1, String o2)
+ {
+ if (o1.length() > o2.length())
+ {
+ return -1;
+ }
+ if (o1.length() < o2.length())
+ {
+ return 1;
+ }
+ return 0;
+ }
+
+ public static final StringLengthComparator INSTANCE = new StringLengthComparator();
+
+ }
+}
Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/Icon.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/Icon.java (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/Icon.java 2010-08-09 09:54:48 UTC (rev 13579)
@@ -0,0 +1,31 @@
+/*
+ * 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.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface Icon
+{
+ public String value();
+}
Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/IconLiteral.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/IconLiteral.java (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/IconLiteral.java 2010-08-09 09:54:48 UTC (rev 13579)
@@ -0,0 +1,40 @@
+/*
+ * 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 javax.enterprise.util.AnnotationLiteral;
+
+public class IconLiteral extends AnnotationLiteral<Icon> implements Icon
+{
+ private final String value;
+
+ public IconLiteral(String value)
+ {
+ this.value = value;
+ }
+
+ public String value()
+ {
+ return value;
+ }
+
+}
Added: modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataStoreTest.java
===================================================================
--- modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataStoreTest.java (rev 0)
+++ modules/faces/trunk/impl/src/test/java/org/jboss/seam/faces/viewdata/ViewDataStoreTest.java 2010-08-09 09:54:48 UTC (rev 13579)
@@ -0,0 +1,64 @@
+/*
+ * 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 junit.framework.Assert;
+
+import org.junit.Test;
+
+public class ViewDataStoreTest
+{
+ @Test
+ public void testViewDataStore()
+ {
+ ViewDataStore store = new ViewDataStoreImpl();
+ 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