[gatein-commits] gatein SVN: r6997 - in portal/branches/api/component/api-impl/src: main/java/org/gatein/portal/api/impl/id and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Aug 5 12:15:50 EDT 2011


Author: chris.laprun at jboss.com
Date: 2011-08-05 12:15:50 -0400 (Fri, 05 Aug 2011)
New Revision: 6997

Added:
   portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/
   portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationContext.java
   portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationId.java
   portal/branches/api/component/api-impl/src/test/java/org/gatein/portal/api/impl/id/
   portal/branches/api/component/api-impl/src/test/java/org/gatein/portal/api/impl/id/ComplexApplicationContextTestCase.java
Modified:
   portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/GateInImpl.java
Log:
- Added ComplexApplicationContext and ComplexApplicationId since I couldn't get the generic versions to work in a reasonable amount of time with multiple separators. :(

Modified: portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/GateInImpl.java
===================================================================
--- portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/GateInImpl.java	2011-08-05 06:40:35 UTC (rev 6996)
+++ portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/GateInImpl.java	2011-08-05 16:15:50 UTC (rev 6997)
@@ -67,6 +67,8 @@
 import org.gatein.api.util.IterableIdentifiableCollection;
 import org.gatein.api.util.Type;
 import org.gatein.common.util.ParameterValidation;
+import org.gatein.portal.api.impl.id.ComplexApplicationContext;
+import org.gatein.portal.api.impl.id.ComplexApplicationId;
 import org.gatein.portal.api.impl.portal.DashboardSiteImpl;
 import org.gatein.portal.api.impl.portal.GroupSiteImpl;
 import org.gatein.portal.api.impl.portal.PageImpl;
@@ -98,8 +100,10 @@
       .withDefaultSeparator("::").build();
    public static final Context GROUP_CONTEXT = GenericContext.builder().named("Group").requiredUnboundedHierarchicalComponent("group", Identifiable.class, Pattern.compile("(" + GROUP_CHARS + ")+"))
       .withDefaultSeparator("/").requireSeparatorInFirstPosition().build();
-   public static final Context APPLICATION_CONTEXT = GenericContext.builder().named("Application").requiredComponent("application", Identifiable.class, Pattern.compile("\\w+"))
-      .requiredComponent("portlet", Portlet.class, Pattern.compile("\\w+")).withDefaultSeparator("/").build();
+   public static final String APPLICATION_COMPONENT_NAME = "application";
+   public static final String PORTLET_COMPONENT_NAME = "portlet";
+   public static final Context APPLICATION_CONTEXT = GenericContext.builder().named("Application").requiredComponent(APPLICATION_COMPONENT_NAME, Identifiable.class, Pattern.compile("\\w+"))
+      .requiredComponent(PORTLET_COMPONENT_NAME, Portlet.class, Pattern.compile("\\w+")).withDefaultSeparator("/").build();
    private static final Context GADGET_CONTEXT = GenericContext.builder().named("Gadget").requiredComponent("name", Gadget.class, Pattern.compile("\\w+")).build();
    private static final Context USER_CONTEXT = GenericContext.builder().named("User").requiredComponent("name", Identifiable.class, Pattern.compile("[a-zA-Z0-9]+")).build();
    private static final Context CATEGORY_CONTEXT = GenericContext.builder().named("Category").requiredComponent("name", Category.class, Pattern.compile("\\w+")).build();
@@ -465,7 +469,14 @@
 
    public Id<Portlet> parsePortletId(String contentId)
    {
-      return APPLICATION_CONTEXT.parse(contentId, Portlet.class);
+      if (contentId.contains(ComplexApplicationId.START))
+      {
+         return ComplexApplicationContext.INSTANCE.parse(contentId, Portlet.class);
+      }
+      else
+      {
+         return APPLICATION_CONTEXT.parse(contentId, Portlet.class);
+      }
    }
 
    public Id<WSRP> wsrpPortletId(String invoker, String portlet)

Added: portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationContext.java
===================================================================
--- portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationContext.java	                        (rev 0)
+++ portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationContext.java	2011-08-05 16:15:50 UTC (rev 6997)
@@ -0,0 +1,106 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* 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.gatein.portal.api.impl.id;
+
+import org.gatein.api.content.Portlet;
+import org.gatein.api.id.Context;
+import org.gatein.api.id.Id;
+import org.gatein.api.id.Identifiable;
+
+/** @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a> */
+public class ComplexApplicationContext implements Context
+{
+   public static final Context INSTANCE = new ComplexApplicationContext();
+
+   public String getName()
+   {
+      return "Application with prefixed invoker id";
+   }
+
+   public Id create(String rootComponent, String... additionalComponent)
+   {
+      return create(Identifiable.class, rootComponent, additionalComponent);
+   }
+
+   public <T extends Identifiable> Id<T> create(Class<T> type, String rootComponent, String... additionalComponents)
+   {
+      if (!type.isAssignableFrom(Portlet.class))
+      {
+         throw new IllegalArgumentException("ComplexApplicationContext can only create Id<Portlet>. Was asked to create Id<" + type.getSimpleName() + ">");
+      }
+
+      if (additionalComponents != null && additionalComponents.length == 2)
+      {
+         return (Id<T>)new ComplexApplicationId(rootComponent, additionalComponents[0], additionalComponents[1], this);
+      }
+      else
+      {
+         throw new IllegalArgumentException("ComplexApplicationContext can only create Ids with 3 components: category name, application name and portlet name");
+      }
+   }
+
+   public Id parse(String idAsString)
+   {
+      return parse(idAsString, Identifiable.class);
+   }
+
+   public <U extends Identifiable<U>> Id<U> parse(String idAsString, Class<U> expectedType)
+   {
+      final int index = idAsString.indexOf(ComplexApplicationId.START);
+      if (index < 0)
+      {
+         throw new IllegalArgumentException("'" + idAsString + "' cannot be understood by ComplexApplicationContext. Understood format is 'local._applicationName.portletName'");
+      }
+
+      final int slash = idAsString.indexOf('/');
+      String category = idAsString.substring(0, slash);
+
+      final int separator = idAsString.indexOf('.', index + ComplexApplicationId.START.length());
+      String app = idAsString.substring(index + ComplexApplicationId.START.length(), separator);
+      String portlet = idAsString.substring(separator + 1);
+
+      return create(expectedType, category, app, portlet);
+   }
+
+   public String toString(Id id)
+   {
+      if (id instanceof ComplexApplicationId)
+      {
+         return id.toString();
+      }
+      else
+      {
+         throw new IllegalArgumentException("ComplexApplicationContext cannot handle Ids that are not ComplexApplicationIds");
+      }
+   }
+
+   public boolean isComponentRequired(String component)
+   {
+      return true; // all components are required
+   }
+
+   public boolean isComponentUnboundedHierarchical(String component)
+   {
+      return false;  // no components are hierarchical
+   }
+}

Added: portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationId.java
===================================================================
--- portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationId.java	                        (rev 0)
+++ portal/branches/api/component/api-impl/src/main/java/org/gatein/portal/api/impl/id/ComplexApplicationId.java	2011-08-05 16:15:50 UTC (rev 6997)
@@ -0,0 +1,176 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* 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.gatein.portal.api.impl.id;
+
+import org.gatein.api.content.Portlet;
+import org.gatein.api.id.Context;
+import org.gatein.api.id.Id;
+import org.gatein.portal.api.impl.GateInImpl;
+
+/** @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a> */
+public class ComplexApplicationId implements Id<Portlet>
+{
+   public static final String LOCAL = "local";
+   public static final String START = LOCAL + "._";
+   private final String category;
+   private final String app;
+   private final String portlet;
+   private final Context context;
+
+   public static final String INVOKER_COMPONENT = "invoker";
+   public static final String CATEGORY_COMPONENT = "category";
+
+   public ComplexApplicationId(String category, String appName, String portletName, ComplexApplicationContext context)
+   {
+      this.category = category;
+      this.app = appName;
+      this.portlet = portletName;
+      this.context = context;
+   }
+
+   @Override
+   public String toString()
+   {
+      return toString(context);
+   }
+
+   public String toString(Context context)
+   {
+      if (this.context.equals(context))
+      {
+         return category + '/' + START + app + '.' + portlet;
+      }
+      else
+      {
+         throw new IllegalArgumentException("ComplexApplicationIds can only be rendered by ComplexApplicationContext at the moment.");
+      }
+   }
+
+   @Override
+   public boolean equals(Object o)
+   {
+      if (this == o)
+      {
+         return true;
+      }
+      if (o == null || getClass() != o.getClass())
+      {
+         return false;
+      }
+
+      ComplexApplicationId that = (ComplexApplicationId)o;
+
+      if (!app.equals(that.app))
+      {
+         return false;
+      }
+      if (!category.equals(that.category))
+      {
+         return false;
+      }
+      if (!portlet.equals(that.portlet))
+      {
+         return false;
+      }
+
+      return true;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      int result = category.hashCode();
+      result = 31 * result + app.hashCode();
+      result = 31 * result + portlet.hashCode();
+      return result;
+   }
+
+   public Class<Portlet> getIdentifiableType()
+   {
+      return Portlet.class;
+   }
+
+   public Id getIdForChild(String childId)
+   {
+      throw new IllegalArgumentException("ComplexApplicationId doesn't currently allow children Ids.");
+   }
+
+   public String getComponent(String component)
+   {
+      if (GateInImpl.APPLICATION_COMPONENT_NAME.equals(component))
+      {
+         return app;
+      }
+      else if (GateInImpl.PORTLET_COMPONENT_NAME.equals(component))
+      {
+         return portlet;
+      }
+      else if (INVOKER_COMPONENT.equals(component))
+      {
+         return LOCAL;
+      }
+      else if (CATEGORY_COMPONENT.equals(component))
+      {
+         return category;
+      }
+      else
+      {
+         throw new IllegalArgumentException("Unknown component '" + component + "' for ComplexApplicationId");
+      }
+   }
+
+   public Context getOriginalContext()
+   {
+      return context;
+   }
+
+   public int getComponentNumber()
+   {
+      return 4;
+   }
+
+   public String getRootComponent()
+   {
+      return LOCAL;
+   }
+
+   public Id getParent()
+   {
+      return null;
+   }
+
+   public String[] getComponents()
+   {
+      return new String[]{category, LOCAL, app, portlet};
+   }
+
+   public void associateComponentWith(int componentIndex, String name)
+   {
+      // do nothing as it shouldn't be called
+   }
+
+   public int compareTo(Id o)
+   {
+      return toString().compareTo(o.toString());
+   }
+}

Added: portal/branches/api/component/api-impl/src/test/java/org/gatein/portal/api/impl/id/ComplexApplicationContextTestCase.java
===================================================================
--- portal/branches/api/component/api-impl/src/test/java/org/gatein/portal/api/impl/id/ComplexApplicationContextTestCase.java	                        (rev 0)
+++ portal/branches/api/component/api-impl/src/test/java/org/gatein/portal/api/impl/id/ComplexApplicationContextTestCase.java	2011-08-05 16:15:50 UTC (rev 6997)
@@ -0,0 +1,43 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* 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.gatein.portal.api.impl.id;
+
+import org.gatein.api.content.Portlet;
+import org.gatein.api.id.Id;
+import org.gatein.portal.api.impl.GateInImpl;
+import org.testng.annotations.Test;
+
+/** @author <a href="mailto:chris.laprun at jboss.com">Chris Laprun</a> */
+public class ComplexApplicationContextTestCase
+{
+   @Test
+   public void checkParsing()
+   {
+      final Id<Portlet> id = ComplexApplicationContext.INSTANCE.parse("category/local._webapp.portlet", Portlet.class);
+      assert "category".equals(id.getComponent(ComplexApplicationId.CATEGORY_COMPONENT));
+      assert "webapp".equals(id.getComponent(GateInImpl.APPLICATION_COMPONENT_NAME));
+      assert "portlet".equals(id.getComponent(GateInImpl.PORTLET_COMPONENT_NAME));
+
+      assert id.equals(ComplexApplicationContext.INSTANCE.parse(id.toString()));
+   }
+}



More information about the gatein-commits mailing list