Skip to content
Browse files

扩展Module接口,使之可以返回值

  • Loading branch information...
1 parent 0d9b2df commit 397e979d3e5163aefea242803f6b8a19837fd4dd Michael Zhou committed Aug 16, 2012
View
27 ...uleloader/src/main/java/com/alibaba/citrus/service/moduleloader/ModuleReturningValue.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.citrus.service.moduleloader;
+
+/**
+ * 代表一个模块,支持返回值。
+ *
+ * @author Michael Zhou
+ */
+public interface ModuleReturningValue extends Module {
+ Object executeAndReturn() throws Exception;
+}
View
4 ...java/com/alibaba/citrus/service/moduleloader/impl/adapter/AbstractDataBindingAdapter.java
@@ -19,11 +19,11 @@
import static com.alibaba.citrus.util.Assert.*;
-import com.alibaba.citrus.service.moduleloader.Module;
+import com.alibaba.citrus.service.moduleloader.ModuleReturningValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public abstract class AbstractDataBindingAdapter implements Module {
+public abstract class AbstractDataBindingAdapter implements ModuleReturningValue {
protected final Logger log;
protected final Object moduleObject;
View
10 ...java/com/alibaba/citrus/service/moduleloader/impl/adapter/AbstractModuleEventAdapter.java
@@ -55,6 +55,12 @@ public void afterPropertiesSet() {
/** 执行一个module。 */
public void execute() throws ModuleEventException, ModuleEventNotFoundException {
+ executeAndReturn();
+ }
+
+ /** 执行一个module,并返回值。 */
+ public Object executeAndReturn() throws ModuleEventException, ModuleEventNotFoundException {
+ Object result = null;
String event = getEventName(request);
MethodInvoker handler = null;
@@ -91,7 +97,7 @@ public void execute() throws ModuleEventException, ModuleEventNotFoundException
log.debug("Invoking handler for event {}: {}", event, handler);
try {
- handler.invoke(moduleObject, log);
+ result = handler.invoke(moduleObject, log);
} catch (Exception e) {
exception = new ModuleEventException("Failed to execute handler: " + handler, e);
}
@@ -113,6 +119,8 @@ public void execute() throws ModuleEventException, ModuleEventNotFoundException
if (exception != null) {
throw exception;
}
+
+ return result;
}
/** 取得event名称。 */
View
4 ...rc/main/java/com/alibaba/citrus/service/moduleloader/impl/adapter/DataBindingAdapter.java
@@ -37,6 +37,10 @@ public void execute() throws Exception {
executeMethod.invoke(moduleObject, log);
}
+ public Object executeAndReturn() throws Exception {
+ return executeMethod.invoke(moduleObject, log);
+ }
+
@Override
public String toString() {
MapBuilder mb = new MapBuilder();
View
8 ...der/src/main/java/com/alibaba/citrus/service/moduleloader/impl/adapter/MethodInvoker.java
@@ -39,7 +39,7 @@ public MethodInvoker(FastMethod fastMethod, DataResolver[] resolvers, boolean sk
this.skippable = skippable;
}
- public void invoke(Object moduleObject, Logger log) throws Exception {
+ public Object invoke(Object moduleObject, Logger log) throws Exception {
Object[] args = new Object[resolvers.length];
for (int i = 0; i < args.length; i++) {
@@ -50,7 +50,7 @@ public void invoke(Object moduleObject, Logger log) throws Exception {
} catch (SkipModuleExecutionException e) {
if (skippable) {
log.debug("Module execution has been skipped. Method: {}, {}", fastMethod, e.getMessage());
- return;
+ return null;
}
value = e.getValueForNonSkippable();
@@ -67,10 +67,10 @@ public void invoke(Object moduleObject, Logger log) throws Exception {
}
try {
- fastMethod.invoke(moduleObject, args);
+ return fastMethod.invoke(moduleObject, args);
} catch (InvocationTargetException e) {
throwExceptionOrError(e.getCause());
- return;
+ return null;
}
}
View
18 .../src/test/java/com/alibaba/citrus/service/moduleloader/impl/adapter/ActionEventTests.java
@@ -188,6 +188,24 @@ public void doSomethingInteresting() throws Exception {
assertEquals("doSomethingInteresting", rundata.getAttribute("handler"));
assertEquals(null, rundata.getAttribute("before"));
assertEquals(null, rundata.getAttribute("after"));
+
+ assertNull(actionEvent.executeAndReturn());
+ }
+
+ @Test
+ public void doReturnValue() throws Exception {
+ getInvocationContext("/app1?eventSubmitDoReturnValue=true");
+ initRequestContext();
+
+ ActionEventAdapter actionEvent = (ActionEventAdapter) moduleLoader.getModule("action", "class.myprod.MyAction");
+
+ actionEvent.execute();
+
+ assertEquals("doReturnValue", rundata.getAttribute("handler"));
+ assertEquals(null, rundata.getAttribute("before"));
+ assertEquals(null, rundata.getAttribute("after"));
+
+ assertEquals("myresult", actionEvent.executeAndReturn());
}
@Test
View
12 .../src/test/java/com/alibaba/citrus/service/moduleloader/impl/adapter/DataBindingTests.java
@@ -59,7 +59,19 @@ public void execute() throws Exception {
DataBindingAdapter adapter = (DataBindingAdapter) moduleLoader.getModule("control", "class.myprod.MyControl");
adapter.execute();
+ assertEquals("execute", rundata.getAttribute("handler"));
+
+ assertNull(adapter.executeAndReturn());
+ }
+
+ @Test
+ public void executeAndReturn() throws Exception {
+ getInvocationContext("/app1");
+ initRequestContext();
+
+ DataBindingAdapter adapter = (DataBindingAdapter) moduleLoader.getModule("control", "class.myprod.MyControl2");
+ assertEquals("myresult", adapter.executeAndReturn());
assertEquals("execute", rundata.getAttribute("handler"));
}
View
20 .../src/test/java/com/alibaba/citrus/service/moduleloader/impl/adapter/ScreenEventTests.java
@@ -131,6 +131,26 @@ public void doSomethingInteresting() throws Exception {
assertEquals("doSomethingInteresting", rundata.getAttribute("handler"));
assertEquals(null, rundata.getAttribute("before"));
assertEquals(null, rundata.getAttribute("after"));
+
+ assertNull(screenEvent.executeAndReturn());
+ }
+
+ @Test
+ public void doReturnValue() throws Exception {
+ getInvocationContext("/app1");
+ initRequestContext();
+
+ ScreenEventUtil.setEventName(newRequest, "returnValue");
+
+ ScreenEventAdapter screenEvent = (ScreenEventAdapter) moduleLoader.getModule("screen", "MyEventScreen");
+
+ screenEvent.execute();
+
+ assertEquals("doReturnValue", rundata.getAttribute("handler"));
+ assertEquals(null, rundata.getAttribute("before"));
+ assertEquals(null, rundata.getAttribute("after"));
+
+ assertEquals("myresult", screenEvent.executeAndReturn());
}
@Test
View
6 ...st/java/com/alibaba/citrus/service/moduleloader/impl/factory/ClassModuleFactoryTests.java
@@ -71,7 +71,7 @@ public void search_packages() {
names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("control");
- assertArrayEquals(new String[] { "InvalidControl", "myprod.MyControl" },
+ assertArrayEquals(new String[] { "InvalidControl", "myprod.MyControl", "myprod.MyControl2" },
names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("screens");
@@ -118,7 +118,7 @@ private void search_classes(String name) {
"app1.myprod.MyActionNoRunData" }, names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("control");
- assertArrayEquals(new String[] { "app1.InvalidControl", "app1.myprod.MyControl" },
+ assertArrayEquals(new String[] { "app1.InvalidControl", "app1.myprod.MyControl", "app1.myprod.MyControl2" },
names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("screens");
@@ -156,7 +156,7 @@ public void search_multi() {
names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("control");
- assertArrayEquals(new String[] { "app1.InvalidControl", "app1.myprod.MyControl" },
+ assertArrayEquals(new String[] { "app1.InvalidControl", "app1.myprod.MyControl", "app1.myprod.MyControl2" },
names.toArray(new String[names.size()]));
names = moduleLoaderService.getModuleNames("screen");
View
5 service/moduleloader/src/test/java/com/alibaba/test/app1/module/action/myprod/MyAction.java
@@ -34,6 +34,11 @@ public void doSomethingInteresting() throws Exception {
rundata.setAttribute("handler", "doSomethingInteresting");
}
+ public Object doReturnValue() throws Exception {
+ rundata.setAttribute("handler", "doReturnValue");
+ return "myresult";
+ }
+
// inner class不被包含
public static class InnerClass {
}
View
31 ...ce/moduleloader/src/test/java/com/alibaba/test/app1/module/control/myprod/MyControl2.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.test.app1.module.control.myprod;
+
+import com.alibaba.citrus.service.requestcontext.rundata.RunData;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class MyControl2 {
+ @Autowired
+ private RunData rundata;
+
+ public String execute() {
+ rundata.setAttribute("handler", "execute");
+ return "myresult";
+ }
+}
View
5 service/moduleloader/src/test/java/com/alibaba/test/app1/module2/screen/MyEventScreen.java
@@ -34,6 +34,11 @@ public void doSomethingInteresting() throws Exception {
rundata.setAttribute("handler", "doSomethingInteresting");
}
+ public Object doReturnValue() throws Exception {
+ rundata.setAttribute("handler", "doReturnValue");
+ return "myresult";
+ }
+
// inner class不被包含
public static class InnerClass {
}

0 comments on commit 397e979

Please sign in to comment.
Something went wrong with that request. Please try again.