Revision: 7817
Author: amitman...@google.com
Date: Mon Mar 29 14:11:08 2010
Log: The servlet now reads the enum name from a properties file. Thus, the servlet
portion responsible for all READ operations is a stock servlet.

Patch by: amitmanjhi
Review by: rjrjr (desk review)

Review at http://gwt-code-reviews.appspot.com/286802

http://code.google.com/p/google-web-toolkit/source/detail?r=7817

Added:
 /trunk/bikeshed/war/WEB-INF/classes
 /trunk/bikeshed/war/WEB-INF/classes/servlet.properties
Modified:
/trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java /trunk/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java

=======================================
--- /dev/null
+++ /trunk/bikeshed/war/WEB-INF/classes/servlet.properties Mon Mar 29 14:11:08 2010
@@ -0,0 +1,1 @@
+servlet.serveroperation=com.google.gwt.sample.expenses.shared.ExpenseRequestFactory$ServerSideOperation
=======================================
--- /trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java Mon Mar 29 14:11:08 2010
@@ -21,7 +21,6 @@
 import com.google.gwt.http.client.RequestException;
 import com.google.gwt.http.client.Response;
 import com.google.gwt.requestfactory.client.gen.ClientRequestObject;
-import com.google.gwt.sample.expenses.shared.ExpenseRequestFactory.ServerSideOperation;
 import com.google.gwt.requestfactory.shared.RequestFactory;
 import com.google.gwt.requestfactory.shared.SyncRequest;
 import com.google.gwt.requestfactory.shared.impl.RequestDataManager;
@@ -125,7 +124,7 @@

         // TODO: Fix the className for this request
builder.setRequestData(ClientRequestObject.getRequestString(RequestDataManager.getRequestMap(
-            ServerSideOperation.SYNC, null, requestData.toString())));
+            RequestFactory.UPDATE_STRING, null, requestData.toString())));
         builder.setCallback(new RequestCallback() {

           public void onError(Request request, Throwable exception) {
=======================================
--- /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java Mon Mar 22 16:05:18 2010 +++ /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java Mon Mar 29 14:11:08 2010
@@ -27,6 +27,11 @@

   String URL = "/expenses/data";

+  /*
+   * eventually, this will become an enum of update operations.
+   */
+  String UPDATE_STRING = "SYNC";
+
   /**
    * Implemented by the request objects created by this factory.
    */
@@ -50,4 +55,34 @@
   // TODO actually a DeltaValueStore, List is an interim hack
   SyncRequest syncRequest(final List<Values<?>> deltaValueStore);

-}
+  /**
+   * Implemented by Enums sent in the request.
+   */
+  interface RequestDefinition {
+    /**
+     * Returns the name.
+     */
+    String name();
+
+    /**
+ * Returns the name of the (domain) class that contains the method to be
+     * invoked on the server.
+     */
+    String getDomainClassName();
+
+    /**
+     * Returns the name of the method to be invoked on the server.
+     */
+    String getDomainMethodName();
+
+    /**
+ * Returns the parameter types of the method to be invoked on the server.
+     */
+    Class<?>[] getParameterTypes();
+
+    /**
+     * Returns the return type of the method to be invoked on the server.
+     */
+    Class<? extends EntityKey<?>> getReturnType();
+  }
+}
=======================================
--- /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java Mon Mar 29 14:11:08 2010
@@ -15,8 +15,6 @@
  */
 package com.google.gwt.requestfactory.shared.impl;

-import com.google.gwt.sample.expenses.shared.ExpenseRequestFactory.ServerSideOperation;
-
 import java.util.HashMap;
 import java.util.Map;

@@ -49,9 +47,9 @@
    *
    */
   public static Map<String, String> getRequestMap(
-      ServerSideOperation operation, Object values[], String content) {
+      String operation, Object values[], String content) {
     Map<String, String> requestMap = new HashMap<String, String>();
-    requestMap.put(OPERATION_TOKEN, operation.name());
+    requestMap.put(OPERATION_TOKEN, operation);
     if (values != null) {
       for (int i = 0; i < values.length; i++) {
         Object value = values[i];
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java Mon Mar 29 14:11:08 2010
@@ -59,7 +59,7 @@
     return new Request() {
       public String getRequestData() {
return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap( - ExpenseRequestFactory.ServerSideOperation.FIND_ALL_EMPLOYEES, null, null)); + ExpenseRequestFactory.ServerSideOperation.FIND_ALL_EMPLOYEES.name(), null, null));
       }
     };
   }
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java Mon Mar 29 14:11:08 2010
@@ -60,7 +60,7 @@
     return new Request() {
       public String getRequestData() {
return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap( - ExpenseRequestFactory.ServerSideOperation.FIND_ALL_REPORTS, null, + ExpenseRequestFactory.ServerSideOperation.FIND_ALL_REPORTS.name(), null,
             null));
       }

@@ -72,7 +72,7 @@
     return new Request() {
       public String getRequestData() {
return ClientRequestObject.getRequestString(RequestDataManager.getRequestMap( - ExpenseRequestFactory.ServerSideOperation.FIND_REPORTS_BY_EMPLOYEE, + ExpenseRequestFactory.ServerSideOperation.FIND_REPORTS_BY_EMPLOYEE.name(),
             new Object[] {id.get()}, null));
       }
     };
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java Mon Mar 29 14:11:08 2010
@@ -16,10 +16,11 @@
 package com.google.gwt.sample.expenses.server;

 import com.google.gwt.requestfactory.shared.EntityKey;
+import com.google.gwt.requestfactory.shared.RequestFactory;
+import com.google.gwt.requestfactory.shared.RequestFactory.RequestDefinition;
 import com.google.gwt.requestfactory.shared.impl.RequestDataManager;
 import com.google.gwt.sample.expenses.server.domain.Report;
 import com.google.gwt.sample.expenses.server.domain.Storage;
-import com.google.gwt.sample.expenses.shared.ExpenseRequestFactory.ServerSideOperation;
 import com.google.gwt.sample.expenses.shared.ReportKey;
 import com.google.gwt.valuestore.shared.Property;

@@ -29,6 +30,7 @@

 import java.io.BufferedInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -38,6 +40,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;

 import javax.servlet.http.HttpServlet;
@@ -49,6 +52,7 @@
  */
 public class ExpensesDataServlet extends HttpServlet {

+  private static final String PROPERTY_FILENAME = "servlet.properties";
   // TODO: Remove this hack
   private static final Set<String> PROPERTY_SET = new HashSet<String>();
   static {
@@ -62,48 +66,48 @@
protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws IOException {

-    ServerSideOperation operation = null;
+    RequestDefinition operation = null;
     try {
       response.setStatus(HttpServletResponse.SC_OK);
-      JSONObject topLevelJsonObject = new JSONObject(getContent(request));
- operation = getOperationFromName(topLevelJsonObject.getString(RequestDataManager.OPERATION_TOKEN));
       PrintWriter writer = response.getWriter();
-      switch (operation) {
-        case SYNC:
- sync(topLevelJsonObject.getString(RequestDataManager.CONTENT_TOKEN),
-              writer);
-          break;
-        case FIND_ALL_EMPLOYEES:
-        case FIND_ALL_REPORTS:
-        case FIND_EMPLOYEE:
-        case FIND_REPORTS_BY_EMPLOYEE:
- Class<?> domainClass = Class.forName(operation.getDomainClassName());
-          Method domainMethod = domainClass.getMethod(
- operation.getDomainMethodName(), operation.getParameterTypes());
-          if (!Modifier.isStatic(domainMethod.getModifiers())) {
- throw new IllegalArgumentException("the " + domainMethod.getName()
-                + " is not static");
-          }
-          Object args[] = RequestDataManager.getObjectsFromParameterMap(
-              getParameterMap(topLevelJsonObject),
-              domainMethod.getParameterTypes());
-          Object resultList = domainMethod.invoke(null, args);
-          if (!(resultList instanceof List)) {
-            throw new IllegalArgumentException("return value not a list "
-                + resultList);
-          }
-          JSONArray jsonArray = getJsonArray((List<?>) resultList,
-              operation.getReturnType());
-          writer.print(jsonArray.toString());
-          break;
-        default:
- throw new IllegalArgumentException("Unknow operation " + operation);
+      JSONObject topLevelJsonObject = new JSONObject(getContent(request));
+ String operationName = topLevelJsonObject.getString(RequestDataManager.OPERATION_TOKEN);
+      if (operationName.equals(RequestFactory.UPDATE_STRING)) {
+ sync(topLevelJsonObject.getString(RequestDataManager.CONTENT_TOKEN),
+            writer);
+      } else {
+ InputStream is = this.getClass().getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
+        if (is == null) {
+ throw new IllegalArgumentException("unable to find servlet.properties");
+        }
+        Properties properties = new Properties();
+        properties.load(is);
+        operation = getOperationFromName(
+            operationName,
+ (Class<RequestDefinition>) Class.forName(properties.getProperty("servlet.serveroperation"))); + Class<?> domainClass = Class.forName(operation.getDomainClassName());
+        Method domainMethod = domainClass.getMethod(
+ operation.getDomainMethodName(), operation.getParameterTypes());
+        if (!Modifier.isStatic(domainMethod.getModifiers())) {
+ throw new IllegalArgumentException("the " + domainMethod.getName()
+              + " is not static");
+        }
+        Object args[] = RequestDataManager.getObjectsFromParameterMap(
+            getParameterMap(topLevelJsonObject),
+            domainMethod.getParameterTypes());
+        Object resultList = domainMethod.invoke(null, args);
+        if (!(resultList instanceof List)) {
+          throw new IllegalArgumentException("return value not a list "
+              + resultList);
+        }
+        JSONArray jsonArray = getJsonArray((List<?>) resultList,
+            operation.getReturnType());
+        writer.print(jsonArray.toString());
       }
       writer.flush();
       // TODO: clean exception handling code below.
     } catch (ClassNotFoundException e) {
-      throw new IllegalArgumentException("unable to load the class: "
-          + operation.getDomainClassName());
+      throw new IllegalArgumentException(e);
     } catch (IllegalAccessException e) {
       throw new IllegalArgumentException(e);
     } catch (InvocationTargetException e) {
@@ -177,8 +181,11 @@
     return methodName.toString();
   }

-  private ServerSideOperation getOperationFromName(String operationName) {
-    for (ServerSideOperation operation : ServerSideOperation.values()) {
+  private RequestDefinition getOperationFromName(String operationName,
+      Class<RequestDefinition> enumClass) throws SecurityException,
+ IllegalAccessException, InvocationTargetException, NoSuchMethodException { + for (RequestDefinition operation : ((RequestDefinition[]) enumClass.getMethod(
+        "values").invoke(null))) {
       if (operation.name().equals(operationName)) {
         return operation;
       }
=======================================
--- /trunk/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java Tue Mar 23 12:24:31 2010 +++ /trunk/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java Mon Mar 29 14:11:08 2010
@@ -64,21 +64,20 @@
    * generated by the JPA-aware tool.
    *
    */
-  public enum ServerSideOperation {
+  public enum ServerSideOperation implements RequestDefinition {
FIND_ALL_EMPLOYEES("com.google.gwt.sample.expenses.server.domain.Employee",
         "findAllEmployees", null,
-        com.google.gwt.sample.expenses.shared.EmployeeKey.class), /* */
+        com.google.gwt.sample.expenses.shared.EmployeeKey.class), //
     FIND_ALL_REPORTS("com.google.gwt.sample.expenses.server.domain.Report",
         "findAllReports", null,
-        com.google.gwt.sample.expenses.shared.ReportKey.class), /* */
+        com.google.gwt.sample.expenses.shared.ReportKey.class), //
     FIND_EMPLOYEE("com.google.gwt.sample.expenses.server.domain.Employee",
         "findEmployee", new Class[] {java.lang.Long.class},
-        com.google.gwt.sample.expenses.shared.EmployeeKey.class), /* */
+        com.google.gwt.sample.expenses.shared.EmployeeKey.class), //
     FIND_REPORTS_BY_EMPLOYEE(
         "com.google.gwt.sample.expenses.server.domain.Report",
         "findReportsByEmployee", new Class[] {java.lang.Long.class},
-        com.google.gwt.sample.expenses.shared.ReportKey.class), /* */
-    SYNC("", "", null, null);
+        com.google.gwt.sample.expenses.shared.ReportKey.class); //

     /**
      * the server side domain class.

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

To unsubscribe, reply using "remove me" as the subject.

Reply via email to