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
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7817 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java b/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
index 71ff490..ce98000 100644
--- a/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
+++ b/bikeshed/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
@@ -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) {
diff --git a/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java b/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java
index b582bd8..fad5e99 100644
--- a/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java
+++ b/bikeshed/src/com/google/gwt/requestfactory/shared/RequestFactory.java
@@ -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();
+ }
}
diff --git a/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java b/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java
index 0447be6..61fe729 100644
--- a/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java
+++ b/bikeshed/src/com/google/gwt/requestfactory/shared/impl/RequestDataManager.java
@@ -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];
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java b/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java
index 3012914..7b02f32 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/gen/EmployeeRequestImpl.java
@@ -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));
}
};
}
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java b/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java
index 07642e3..8ceebc8 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/gen/ReportRequestImpl.java
@@ -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));
}
};
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java b/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java
index 1c5c728..717cf00 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/server/ExpensesDataServlet.java
@@ -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;
}
diff --git a/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java b/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java
index 1f36972..93f2397 100644
--- a/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java
+++ b/bikeshed/src/com/google/gwt/sample/expenses/shared/ExpenseRequestFactory.java
@@ -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.
diff --git a/bikeshed/war/WEB-INF/classes/servlet.properties b/bikeshed/war/WEB-INF/classes/servlet.properties
new file mode 100644
index 0000000..ae714e2
--- /dev/null
+++ b/bikeshed/war/WEB-INF/classes/servlet.properties
@@ -0,0 +1 @@
+servlet.serveroperation=com.google.gwt.sample.expenses.shared.ExpenseRequestFactory$ServerSideOperation