[exo-jcr-commits] exo-jcr SVN: r4345 - in ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext: management and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu May 5 08:14:48 EDT 2011


Author: areshetnyak
Date: 2011-05-05 08:14:47 -0400 (Thu, 05 May 2011)
New Revision: 4345

Added:
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ResourceKey.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResource.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethod.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethodParameter.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceProperty.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ValueWrapper.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/GetterInvoker.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/MethodInvoker.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/NoSuchMethodInvoker.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SetterInvoker.java
   ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SimpleMethodInvoker.java
Log:
EXOJCR-769 : The annotation processor of RESTEndPoint was ported in WS ( ws.rest.ext ).

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ResourceKey.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ResourceKey.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ResourceKey.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public final class ResourceKey
+{
+
+   /** . */
+   private final String name;
+
+   public ResourceKey(String name)
+   {
+      if (name == null)
+      {
+         throw new NullPointerException();
+      }
+      this.name = name;
+   }
+
+   @Override
+   public int hashCode()
+   {
+      return name.hashCode();
+   }
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (obj == this)
+      {
+         return true;
+      }
+      if (obj instanceof ResourceKey)
+      {
+         ResourceKey that = (ResourceKey)obj;
+         return name.equals(that.name);
+      }
+      return false;
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResource.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResource.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResource.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+import org.exoplatform.management.annotations.ImpactType;
+import org.exoplatform.management.spi.ManagedMethodMetaData;
+import org.exoplatform.management.spi.ManagedPropertyMetaData;
+import org.exoplatform.management.spi.ManagedResource;
+import org.exoplatform.management.spi.ManagedTypeMetaData;
+import org.exoplatform.services.rest.ext.management.invocation.MethodInvoker;
+import org.exoplatform.services.rest.impl.ApplicationContextImpl;
+import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
+
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RestResource
+{
+
+   /** . */
+   final Map<String, RestResourceProperty> properties;
+
+   /** . */
+   private final List<RestResourceMethod> methods;
+
+   /** . */
+   private final ManagedResource managedResource;
+
+   /** . */
+   private final String name;
+
+   /** . */
+   private final String description;
+
+   public RestResource(String name, ManagedResource managedResource)
+   {
+      ManagedTypeMetaData managedType = managedResource.getMetaData();
+
+      //
+      HashMap<String, RestResourceProperty> properties = new HashMap<String, RestResourceProperty>();
+      for (ManagedPropertyMetaData managedProperty : managedType.getProperties()) {
+         RestResourceProperty resourceProperty = new RestResourceProperty(managedProperty);
+         properties.put(resourceProperty.getName(), resourceProperty);
+      }
+
+      //
+      List<RestResourceMethod> methods = new ArrayList<RestResourceMethod>();
+      for (ManagedMethodMetaData managedMethod : managedType.getMethods()) {
+         RestResourceMethod resourceMethod = new RestResourceMethod(managedMethod);
+         methods.add(resourceMethod);
+      }
+
+      //
+      this.name = name;
+      this.description = managedType.getDescription();
+      this.managedResource = managedResource;
+      this.properties = Collections.unmodifiableMap(properties);
+      this.methods = methods;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+
+   public String getDescription()
+   {
+      return description;
+   }
+
+   public Collection<RestResourceProperty> getProperties()
+   {
+      return properties.values();
+   }
+
+   public Collection<RestResourceMethod> getMethods()
+   {
+      return methods;
+   }
+
+   @GET
+   @Produces(MediaType.APPLICATION_JSON)
+   public RestResource get()
+   {
+      return this;
+   }
+
+   @GET
+   @Path("{name}")
+   @Produces(MediaType.APPLICATION_JSON)
+   public Object get(@Context UriInfo info, @PathParam("name") String name)
+   {
+      MultivaluedMap<String, String> parameters = info.getQueryParameters();
+      
+      // Try first to get a property
+      RestResourceProperty property = properties.get(name);
+      if (property != null)
+      {
+         MethodInvoker getter = property.getGetterInvoker();
+         if (getter != null)
+         {
+            return safeInvoke(getter, parameters);
+         }
+      }
+
+      //
+      return tryInvoke(name, parameters, ImpactType.READ);
+   }
+
+   @PUT
+   @Path("{name}")
+   @Produces(MediaType.APPLICATION_JSON)
+   public Object put(@Context UriInfo info, @PathParam("name") String name)
+   {
+      MultivaluedMap<String, String> parameters = getParameters(info);
+      // Try first to get a property
+      RestResourceProperty property = properties.get(name);
+      if (property != null)
+      {
+         MethodInvoker setter = property.getSetterInvoker();
+         if (setter != null)
+         {
+            return safeInvoke(setter, parameters);
+         }
+      }
+
+      //
+      return tryInvoke(name, parameters, ImpactType.IDEMPOTENT_WRITE);
+   }
+
+   @POST
+   @Path("{name}")
+   @Produces(MediaType.APPLICATION_JSON)
+   public Object post(@Context UriInfo info, @PathParam("name") String name)
+   {
+      return tryInvoke(name, getParameters(info), ImpactType.WRITE);
+   }
+
+   /**
+    * Try to invoke a method with matching parameters from the query string
+    *
+    * @param methodName the method name to invoke
+    * @param info the uri info
+    * @param impact the expected impact
+    * @return a suitable response
+    */
+   private Object tryInvoke(String methodName, MultivaluedMap<String, String> parameters, ImpactType impact)
+   {
+      //
+      RestResourceMethod method = lookupMethod(methodName, parameters.keySet(), impact);
+
+      //
+      if (method != null)
+      {
+         MethodInvoker invoker = method.getMethodInvoker();
+         return safeInvoke(invoker, parameters);
+      }
+
+      //
+      return null;
+   }
+
+   private RestResourceMethod lookupMethod(String methodName, Set<String> argNames, ImpactType impact)
+   {
+      for (RestResourceMethod method : methods)
+      {
+         if (method.getName().equals(methodName) && method.metaData.getImpact() == impact && method.parameterNames.equals(argNames))
+         {
+            return method;
+         }
+      }
+      return null;
+   }
+
+   /**
+    * Invoke safely a method.
+    *
+    * @param invoker the method to invoke
+    * @param argMap the arguments
+    * @return the ok response or an object returned by the method wrapped by {@link ValueWrapper}
+    */
+   private Object safeInvoke(MethodInvoker invoker, Map<String, List<String>> argMap)
+   {
+      Object resource = managedResource.getResource();
+
+      //
+      managedResource.beforeInvoke(resource);
+
+      //
+      try
+      {
+         Object ret = invoker.invoke(resource, argMap);
+         return ret == null ? Response.ok() : ValueWrapper.wrap(ret);
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+         return Response.serverError();
+      }
+      finally
+      {
+         managedResource.afterInvoke(resource);
+      }
+   }
+   
+   @SuppressWarnings("unchecked")
+   private MultivaluedMap<String, String> getParameters(UriInfo info)
+   {
+      MultivaluedMap<String, String> parameters = info.getQueryParameters();
+      ApplicationContextImpl context = (ApplicationContextImpl)info;      
+      
+      Type formType = (ParameterizedType)MultivaluedMapImpl.class.getGenericInterfaces()[0];
+      MediaType contentType = context.getHttpHeaders().getMediaType();
+      if (contentType == null) {
+         contentType = MediaType.APPLICATION_FORM_URLENCODED_TYPE;
+      }      
+
+      MultivaluedMap<String, String> form = new MultivaluedMapImpl();      
+      try {
+         MessageBodyReader reader =
+            context.getProviders().getMessageBodyReader(MultivaluedMap.class, formType, null, contentType);
+         if (reader != null) {
+            form = (MultivaluedMap<String, String>)reader.readFrom(MultivaluedMap.class, formType, null, contentType, context
+               .getHttpHeaders().getRequestHeaders(), context.getContainerRequest().getEntityStream());
+         }
+      } catch (Exception e) {         
+      }
+      
+      parameters.putAll(form);
+      return parameters;
+   }
+   
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethod.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethod.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethod.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+import org.exoplatform.management.spi.ManagedMethodMetaData;
+import org.exoplatform.management.spi.ManagedMethodParameterMetaData;
+import org.exoplatform.services.rest.ext.management.invocation.MethodInvoker;
+import org.exoplatform.services.rest.ext.management.invocation.SimpleMethodInvoker;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RestResourceMethod
+{
+
+   /** . */
+   final ManagedMethodMetaData metaData;
+
+   /** . */
+   final List<RestResourceMethodParameter> parameters;
+
+   /** . */
+   final Set<String> parameterNames;
+
+   /** . */
+   final MethodInvoker methodInvoker;
+
+   public RestResourceMethod(ManagedMethodMetaData metaData)
+   {
+      List<RestResourceMethodParameter> parameters = new ArrayList<RestResourceMethodParameter>();
+      Set<String> parameterNames = new HashSet<String>();
+      for (ManagedMethodParameterMetaData parameterMD : metaData.getParameters())
+      {
+         parameters.add(new RestResourceMethodParameter(parameterMD));
+         parameterNames.add(parameterMD.getName());
+      }
+
+      //
+      this.metaData = metaData;
+      this.parameterNames = Collections.unmodifiableSet(parameterNames);
+      this.parameters = Collections.unmodifiableList(parameters);
+      this.methodInvoker = new SimpleMethodInvoker(metaData.getMethod())
+      {
+         @Override
+         protected String getArgumentName(int index)
+         {
+            RestResourceMethodParameter param = RestResourceMethod.this.parameters.get(index);
+            return param != null ? param.getName() : null;
+         }
+      };
+   }
+
+   public String getName()
+   {
+      return metaData.getName();
+   }
+
+   public String getMethod()
+   {
+      switch (metaData.getImpact())
+      {
+         case READ:
+            return "get";
+         case WRITE:
+            return "post";
+         case IDEMPOTENT_WRITE:
+            return "put";
+         default:
+            throw new AssertionError();
+      }
+   }
+
+   public String getDescription()
+   {
+      return metaData.getDescription();
+   }
+
+   public List<RestResourceMethodParameter> getParameters()
+   {
+      return parameters;
+   }
+
+   // Internal *********************************************************************************************************
+
+   MethodInvoker getMethodInvoker()
+   {
+      return methodInvoker;
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethodParameter.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethodParameter.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceMethodParameter.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+import org.exoplatform.management.spi.ManagedMethodParameterMetaData;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RestResourceMethodParameter
+{
+
+   /** . */
+   final ManagedMethodParameterMetaData metaData;
+
+   public RestResourceMethodParameter(ManagedMethodParameterMetaData metaData)
+   {
+      this.metaData = metaData;
+   }
+
+   public String getName()
+   {
+      return metaData.getName();
+   }
+
+   public String getDescription()
+   {
+      return metaData.getDescription();
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceProperty.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceProperty.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/RestResourceProperty.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+import org.exoplatform.management.spi.ManagedPropertyMetaData;
+import org.exoplatform.services.rest.ext.management.invocation.GetterInvoker;
+import org.exoplatform.services.rest.ext.management.invocation.MethodInvoker;
+import org.exoplatform.services.rest.ext.management.invocation.NoSuchMethodInvoker;
+import org.exoplatform.services.rest.ext.management.invocation.SetterInvoker;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RestResourceProperty
+{
+
+   /** . */
+   final ManagedPropertyMetaData metaData;
+
+   /** . */
+   private final MethodInvoker setterInvoker;
+
+   /** . */
+   private final MethodInvoker getterInvoker;
+
+   public RestResourceProperty(ManagedPropertyMetaData metaData)
+   {
+      Method getter = metaData.getGetter();
+      MethodInvoker getterInvoker = getter != null ? new GetterInvoker(getter) : new NoSuchMethodInvoker();
+
+      //
+      Method setter = metaData.getSetter();
+      MethodInvoker setterInvoker = setter != null ? new SetterInvoker(setter) : new NoSuchMethodInvoker();
+
+      //
+      this.metaData = metaData;
+      this.setterInvoker = setterInvoker;
+      this.getterInvoker = getterInvoker;
+   }
+
+   public String getName()
+   {
+      return metaData.getName();
+   }
+
+   public String getDescription()
+   {
+      return metaData.getDescription();
+   }
+
+   // Internal *********************************************************************************************************
+
+   MethodInvoker getSetterInvoker()
+   {
+      return setterInvoker;
+   }
+
+   MethodInvoker getGetterInvoker()
+   {
+      return getterInvoker;
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ValueWrapper.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ValueWrapper.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/ValueWrapper.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management;
+
+/**
+ * Temporary class until eXo JSON marshaller supports non java bean objects.
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class ValueWrapper<T>
+{
+
+   public static <T> ValueWrapper<T> wrap(T value)
+   {
+      return new ValueWrapper<T>(value);
+   }
+
+   /** . */
+   private final T value;
+
+   public ValueWrapper(T value)
+   {
+      this.value = value;
+   }
+
+   public T getValue()
+   {
+      return value;
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/GetterInvoker.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/GetterInvoker.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/GetterInvoker.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management.invocation;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class GetterInvoker extends SimpleMethodInvoker
+{
+
+   public GetterInvoker(Method method)
+   {
+      super(method);
+   }
+
+   @Override
+   protected String getArgumentName(int index)
+   {
+      throw new IndexOutOfBoundsException();
+   }
+}
\ No newline at end of file

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/MethodInvoker.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/MethodInvoker.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/MethodInvoker.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management.invocation;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public interface MethodInvoker
+{
+
+   Object invoke(Object o, Map<String, List<String>> argMap) throws IllegalAccessException, InvocationTargetException;
+
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/NoSuchMethodInvoker.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/NoSuchMethodInvoker.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/NoSuchMethodInvoker.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management.invocation;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class NoSuchMethodInvoker implements MethodInvoker
+{
+   public Object invoke(Object o, Map<String, List<String>> argMap) throws IllegalAccessException, InvocationTargetException
+   {
+      throw new UnsupportedOperationException();
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SetterInvoker.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SetterInvoker.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SetterInvoker.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management.invocation;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class SetterInvoker extends SimpleMethodInvoker
+{
+
+   public SetterInvoker(Method method)
+   {
+      super(method);
+   }
+
+   @Override
+   protected String getArgumentName(int index)
+   {
+      if (index == 0)
+      {
+         return "value";
+      }
+      else
+      {
+         throw new IndexOutOfBoundsException();
+      }
+   }
+}

Added: ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SimpleMethodInvoker.java
===================================================================
--- ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SimpleMethodInvoker.java	                        (rev 0)
+++ ws/trunk/exo.ws.rest.ext/src/main/java/org/exoplatform/services/rest/ext/management/invocation/SimpleMethodInvoker.java	2011-05-05 12:14:47 UTC (rev 4345)
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.rest.ext.management.invocation;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public abstract class SimpleMethodInvoker implements MethodInvoker
+{
+
+   /** The method we invoke. */
+   private final Method method;
+
+   public SimpleMethodInvoker(Method method)
+   {
+      if (method == null)
+      {
+         throw new NullPointerException();
+      }
+
+      //
+      this.method = method;
+   }
+
+   public Object invoke(Object o, Map<String, List<String>> argMap) throws IllegalAccessException, InvocationTargetException
+   {
+      Class[] paramTypes = method.getParameterTypes();
+      Object[] args = new Object[paramTypes.length];
+      for (int i = 0;i < paramTypes.length;i++)
+      {
+         String argName = getArgumentName(i);
+         List<String> argValues = argMap.get(argName);
+         Class paramType = paramTypes[i];
+         Object arg;
+         if (paramType.isPrimitive())
+         {
+            throw new UnsupportedOperationException("Todo " + paramType);
+         }
+         else if (paramType.isArray())
+         {
+            throw new UnsupportedOperationException("Todo " + paramType);
+         }
+         else if (paramType == String.class)
+         {
+            arg = (argValues != null && argValues.size() > 0) ? argValues.get(0) : null;
+         }
+         else
+         {
+            throw new UnsupportedOperationException("Todo " + paramType);
+         }
+         args[i] = arg;
+      }
+
+      //
+      return method.invoke(o, args);
+   }
+
+   protected abstract String getArgumentName(int index);
+}
\ No newline at end of file



More information about the exo-jcr-commits mailing list