diff --git a/framework/org.tinygroup.ini/src/test/resources/test2.ini b/framework/org.tinygroup.ini/src/test/resources/test2.ini index a9cc06bbdeaeb231506ad2b001c238f155b95d2b..3431061a91a3c3f6ef9303e67d882f70c8bc1344 100644 --- a/framework/org.tinygroup.ini/src/test/resources/test2.ini +++ b/framework/org.tinygroup.ini/src/test/resources/test2.ini @@ -1,8 +1,8 @@ -[Section1] -name=b -age=11 -grade=3 -type=a -[Section2] -left=1 -right=1 +[Section1] +name=b +age=11 +grade=3 +type=a +[Section2] +left=1 +right=1 diff --git a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/AbstractJobCenter.java b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/AbstractJobCenter.java index 3675179c4b7e9999c5da162df5182b3c76490725..08539a75df83b0f1820a974895ec22a1820f2386 100644 --- a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/AbstractJobCenter.java +++ b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/AbstractJobCenter.java @@ -31,7 +31,7 @@ import java.util.List; */ public class AbstractJobCenter implements JobCenter { private static Logger logger = LoggerFactory.getLogger(AbstractJobCenter.class); - private RmiServer rmiServer; + private RmiServer rmiServer;//RMI服务器 private WorkQueue workQueue; public WorkQueue getWorkQueue() { @@ -56,11 +56,13 @@ public class AbstractJobCenter implements JobCenter { } private void registerParallelObject(String objectType, ParallelObject parallelObject) throws RemoteException { + //parallelObject实例(Worker/Foreman)注册到rmiServer的RMI服务器 rmiServer.registerLocalObject(parallelObject, objectType + "|" + parallelObject.getType(), parallelObject.getId()); } private void unregisterParallelObject(String objectType, ParallelObject parallelObject) throws RemoteException { + //卸载rmiServer上的RMI服务器上的parallelObject实例(Worker/Foreman) rmiServer.unregisterObject(objectType + "|" + parallelObject.getType(), parallelObject.getId()); } @@ -130,13 +132,13 @@ public class AbstractJobCenter implements JobCenter { public Warehouse doWork(Work work) throws IOException { String foremanType = work.getForemanType(); List foremanList = null; - foremanList = getForemans(work, foremanType); + foremanList = getForemans(work, foremanType);//根据工头类型从RMI服务器中获取工头列表(本地/远程) // 如果存在空闲的工头 - Foreman foreman = getForeman(foremanType, foremanList); + Foreman foreman = getForeman(foremanType, foremanList);//随机选择一个空闲工头 //获取所有能执行该任务的工人 - List workers = this.getWorkerList(work); + List workers = this.getWorkerList(work);//获取所有能执行该任务的工人(工人类型与工作类型关联) //存放接受该work的工人列表 - List acceptWorkers = getAcceptWorkers(work, workers); + List acceptWorkers = getAcceptWorkers(work, workers);//判断工人是否愿意接受该工作 if (acceptWorkers.size() > 0) { Warehouse outputWarehouse = foreman.work(work, cloneWorkers(acceptWorkers)); // 检查是否有子任务 diff --git a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/ForemanSelectOneWorker.java b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/ForemanSelectOneWorker.java index 0678134cc1ebae77a950b135c0dcccba51903173..118e72f8242ea05cec5a94fb9bf366b20e5e7efe 100644 --- a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/ForemanSelectOneWorker.java +++ b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/ForemanSelectOneWorker.java @@ -44,10 +44,10 @@ public class ForemanSelectOneWorker extends AbstractForeman { public Warehouse work(Work work, List workerList) throws RemoteException { - Worker worker = workerList.get(Util.randomIndex(workerList.size())); + Worker worker = workerList.get(Util.randomIndex(workerList.size()));//随机选取一个工人 List workersActived = new ArrayList(); - workersActived.add(worker); - workerList.remove(worker); + workersActived.add(worker);//将该工人添加到活跃工人列表 + workerList.remove(worker);//从空闲工人列表中去除该工人 return dealWork(work, worker, workerList, workersActived); } @@ -57,7 +57,7 @@ public class ForemanSelectOneWorker extends AbstractForeman { throws RemoteException { try { logger.logMessage(LogLevel.DEBUG,"worker:{0}开始执行",worker.getId()); - Warehouse w = worker.work(work); + Warehouse w = worker.work(work);//工人工作 logger.logMessage(LogLevel.DEBUG,"worker:{0}执行完成",worker.getId()); return w; } catch (Exception e) { diff --git a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterLocal.java b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterLocal.java index 0ad6a13c9702ef434e99a981c1338074335170cd..fa5c8f96e793482467a18b0de6e8662dd6d3e7ac 100644 --- a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterLocal.java +++ b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterLocal.java @@ -33,7 +33,7 @@ public class JobCenterLocal extends AbstractJobCenter { RmiServer rmiServer = new RmiServerImpl(port); WorkQueue workQueue = new WorkQueueImpl(); setWorkQueue(workQueue); - rmiServer.registerLocalObject(workQueue, "WorkQueue"); + rmiServer.registerLocalObject(workQueue, "WorkQueue");//RMI上注册本地工作队列 setRmiServer(rmiServer); } diff --git a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterRemote.java b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterRemote.java index 9da8f30ed55fa330edaadf8da96b7ce6ebc9a764..c1c685c6a9bf3848b0e12258fb07244967116d1e 100644 --- a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterRemote.java +++ b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/JobCenterRemote.java @@ -26,7 +26,7 @@ import org.tinygroup.tinypc.WorkQueue; public class JobCenterRemote extends AbstractJobCenter { public JobCenterRemote(String hostName, int port,String remoteHostName,int remotePort) throws IOException { - setRmiServer(new RmiServerImpl(hostName, port,remoteHostName,remotePort)); - setWorkQueue((WorkQueue) getRmiServer().getObject("WorkQueue")); + setRmiServer(new RmiServerImpl(hostName, port,remoteHostName,remotePort));//创建本地RMI服务并连接远程RMI服务器 + setWorkQueue((WorkQueue) getRmiServer().getObject("WorkQueue"));//获取远程RMI服务器上注册的工作队列 } } diff --git a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/WorkDefault.java b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/WorkDefault.java index c6a1305f50808ad0bfc7d28587ae0a33c66e6993..cda1732aa601dc613b636446ea5c9649afd05263 100644 --- a/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/WorkDefault.java +++ b/framework/org.tinygroup.pc/src/main/java/org/tinygroup/tinypc/impl/WorkDefault.java @@ -32,9 +32,9 @@ public class WorkDefault implements Work { private static final long serialVersionUID = -7558871247472425574L; private String id; private String type; - private Warehouse inputWarehouse; + private Warehouse inputWarehouse;//仓库 private boolean needSerialize = false; - private Work nextStepWork = null; + private Work nextStepWork = null;//下一工作 private WorkStatus workStatus = WorkStatus.WAITING; private String foremanType; @@ -127,6 +127,6 @@ public class WorkDefault implements Work { public String getForemanType() { if (foremanType != null) return foremanType; - return getType(); + return getType();//默认工头类型与工作类型关联 } } diff --git a/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/serialwork/TestSerialWork.java b/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/serialwork/TestSerialWork.java index bb734f4c8b550f2927bb7670f7b59fff4fdbb49d..1a1facf22a1f45622b8ae959329f2c22b4917b29 100644 --- a/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/serialwork/TestSerialWork.java +++ b/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/serialwork/TestSerialWork.java @@ -31,18 +31,27 @@ import java.io.IOException; */ public class TestSerialWork { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { - JobCenter jobCenter = new JobCenterLocal(); + JobCenter jobCenter = new JobCenterLocal();//本地RMI for (int i = 0; i < 5; i++) { - jobCenter.registerWorker(new WorkerHello()); + jobCenter.registerWorker(new WorkerHello());//RMI注册Worker + } + for(int i = 0; i < 3; i++) { + jobCenter.registerWorker(new WorkerBye());//RMI注册Worker } Foreman helloForeman = new ForemanSelectOneWorker("hello"); - jobCenter.registerForeman(helloForeman); + jobCenter.registerForeman(helloForeman);//RMI注册Foreman Warehouse inputWarehouse = new WarehouseDefault(); inputWarehouse.put("name", "world"); - Work work = new WorkDefault("hello", inputWarehouse); + Work work = new WorkDefault("hello", inputWarehouse);//工作序列 work.setNextWork(new WorkDefault("hello")).setNextWork(new WorkDefault("hello")); - Warehouse warehouse = jobCenter.doWork(work); + Warehouse warehouse = null; + try { + warehouse = jobCenter.doWork(work); + } catch (Exception e) { + jobCenter.stop(); + return; + } System.out.println(warehouse.get("name")); jobCenter.stop(); } diff --git a/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/sum/Test.java b/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/sum/Test.java index 65f3d06ebfa95cc7852ef468dc8f06242b0696d9..347c9fec28317fb3bca7b620063ee88a2fa0f3e8 100644 --- a/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/sum/Test.java +++ b/framework/org.tinygroup.pc/src/test/java/org/tinygroup/tinypc/sum/Test.java @@ -27,8 +27,8 @@ import java.io.IOException; * Created by luoguo on 14-1-8. */ public class Test { - static String SIP = "192.168.84.23"; - static String CIP = "192.168.84.23"; + static String SIP = "192.168.84.76"; + static String CIP = "192.168.84.76"; static int SP = 8888; static int CP = 7777; public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { diff --git a/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunClient.java b/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunClient.java index f11ed45457aaaea5f89301f03d1e8f89bbc17e6a..988e3432b7b091a063f6a8614caf0b4cc8b66a05 100644 --- a/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunClient.java +++ b/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunClient.java @@ -24,7 +24,7 @@ public class NewRmiRunClient { private static String SERVERIP = "127.0.0.1"; private static String LOCALIP = "127.0.0.1"; - public static void main(String[] args) { + public static void main(String[] args) throws RemoteException { RmiServer remoteServer = null; try { @@ -34,15 +34,17 @@ public class NewRmiRunClient { // TODO Auto-generated catch block e1.printStackTrace(); } - try { - remoteServer.registerLocalObject(new HelloImpl(), "hello1"); - - } catch (RemoteException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - NewRmiRunClient c = new NewRmiRunClient(remoteServer); - c.run(); + Hello hello = remoteServer.getObject("hello"); + System.out.println(hello.sayHello("ljf")); +// try { +// remoteServer.registerLocalObject(new HelloImpl(), "hello1"); +// +// } catch (RemoteException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// NewRmiRunClient c = new NewRmiRunClient(remoteServer); +// c.run(); } diff --git a/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunServer.java b/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunServer.java index 0b644365474e080f4ce1c1cb9b090aab748eeaae..a6e5b5774e6d6c8e1f8c2dcf9ffd61275afd1923 100644 --- a/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunServer.java +++ b/framework/org.tinygroup.rmi/src/test/java/org/tinygroup/rmi/test/NewRmiRunServer.java @@ -38,8 +38,8 @@ public class NewRmiRunServer { } catch (RemoteException e) { e.printStackTrace(); } - NewRmiRunServer r = new NewRmiRunServer(); - r.runThread(localServer); +// NewRmiRunServer r = new NewRmiRunServer(); +// r.runThread(localServer); } diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/HandlerExecutionChain.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/HandlerExecutionChain.java index e3042b52dd8bcccdb4a789eb5b673e654fc43ec5..5381a99b327c2a156a42586b826855e4e6cd7e31 100644 --- a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/HandlerExecutionChain.java +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/HandlerExecutionChain.java @@ -15,11 +15,14 @@ */ package org.tinygroup.weblayer.mvc; +import java.io.IOException; +import java.io.PrintWriter; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.tinygroup.beancontainer.BeanContainerFactory; @@ -31,7 +34,17 @@ import org.tinygroup.context2object.impl.ClassNameObjectGenerator; import org.tinygroup.loader.LoaderManager; import org.tinygroup.springutil.SpringBeanContainer; import org.tinygroup.weblayer.WebContext; +import org.tinygroup.weblayer.mvc.MappingMethodModel; +import org.tinygroup.weblayer.mvc.annotation.Json2Object; +import org.tinygroup.weblayer.mvc.annotation.ResultJson; +import org.tinygroup.weblayer.mvc.annotation.ResultXml; import org.tinygroup.weblayer.mvc.annotation.View; +import org.tinygroup.weblayer.util.AsmUtil; +import org.tinygroup.convert.objectjson.fastjson.ObjectToJson; +import org.tinygroup.convert.objectxml.xstream.ObjectToXml; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; /** * @@ -43,6 +56,9 @@ import org.tinygroup.weblayer.mvc.annotation.View; *
*/ public class HandlerExecutionChain { + + ObjectToXml objectToXml = new ObjectToXml(); + ObjectToJson objectToJson = new ObjectToJson(SerializerFeature.DisableCircularReferenceDetect); private MappingClassModel model; @@ -81,49 +97,65 @@ public class HandlerExecutionChain { this.context = context; } - public void execute() throws ClassNotFoundException { + public void execute() throws ClassNotFoundException, ServletException, IOException { Method method = methodModel.getMapMethod(); - Class[] paramTypes = method.getParameterTypes(); + Class[] paramTypes = method.getParameterTypes(); Object[] args = new Object[paramTypes.length]; - String[] parameterNames = BeanUtil.getMethodParameterName( - method.getDeclaringClass(), method); - for (int i = 0; i < paramTypes.length; i++) { - args[i] = context.get(parameterNames[i]); - Class type = paramTypes[i]; - if (args[i] == null) { - if (type.equals(WebContext.class)) { - args[i] = context; - } else { - ClassNameObjectGenerator generator = BeanContainerFactory - .getBeanContainer(this.getClass().getClassLoader()) - .getBean( - GeneratorFileProcessor.CLASSNAME_OBJECT_GENERATOR_BEAN); - //TODO:此处应该还要考虑数据的情况 - if (Collection.class.isAssignableFrom(type)) { - ParameterizedType pt = (ParameterizedType) (method - .getGenericParameterTypes()[i]); - Type[] actualTypeArguments = pt - .getActualTypeArguments(); - ClassLoader loader = LoaderManager - .getLoader(getClassName(actualTypeArguments[0] - .toString())); - args[i] = generator - .getObjectCollection(null, type.getName(), - getClassName(actualTypeArguments[0] - .toString()), loader, context); + String[] parameterNames = AsmUtil.getParameterNames(method);//替换字节码库,支持泛型 + + Json2Object json2Object = methodModel.getJson2Object(); + if (json2Object != null) { + for (int i = 0; i < paramTypes.length; i++) { + String text = context.getRequest().getParameter(parameterNames[i]); + if(text != null && text.trim().length() > 0) { + Object object = JSON.parseObject(text, paramTypes[i]); + args[i] = object; + context.put(parameterNames[i], object); + } + } + } else {//form表单提交 + for (int i = 0; i < paramTypes.length; i++) { + args[i] = context.get(parameterNames[i]); + Class type = paramTypes[i]; + if (args[i] == null) { + if (type.equals(WebContext.class)) { + args[i] = context; + }else if(isSimpleType(type)) {//基本类型参数 + throw new RuntimeException("基本数据参数值不允许为空"); + }else if(isWapperType(type)) {//包装类型参数 + args[i] = null; } else { - ClassLoader loader = LoaderManager.getLoader(type - .getName()); - args[i] = generator.getObject(null, null, - type.getName(), loader, context); + ClassNameObjectGenerator generator = BeanContainerFactory + .getBeanContainer(this.getClass().getClassLoader()) + .getBean( + GeneratorFileProcessor.CLASSNAME_OBJECT_GENERATOR_BEAN); + //TODO:此处应该还要考虑数据的情况 + if (Collection.class.isAssignableFrom(type)) { + ParameterizedType pt = (ParameterizedType) (method + .getGenericParameterTypes()[i]); + Type[] actualTypeArguments = pt + .getActualTypeArguments(); + ClassLoader loader = LoaderManager + .getLoader(getClassName(actualTypeArguments[0] + .toString())); + args[i] = generator + .getObjectCollection(null, type.getName(), + getClassName(actualTypeArguments[0] + .toString()), loader, context); + } else { + ClassLoader loader = LoaderManager.getLoader(type + .getName()); + args[i] = generator.getObject(null, null, + type.getName(), loader, context); + } } + } else { + args[i] = ValueUtil + .getValue(args[i].toString(), type.getName()); } - } else { - args[i] = ValueUtil - .getValue(args[i].toString(), type.getName()); + context.put(parameterNames[i], args[i]); + } - context.put(parameterNames[i], args[i]); - } try { Object object = getInstance(model.getMapClass()); @@ -139,6 +171,8 @@ public class HandlerExecutionChain { throw new RuntimeException(e); } View view = methodModel.getView(); + ResultJson resultJson = methodModel.getResultJson(); + ResultXml resultXml = methodModel.getResultXml(); if (view != null) { String pathInfo = view.value(); HttpServletRequest request = context.getRequest(); @@ -148,6 +182,42 @@ public class HandlerExecutionChain { } catch (Exception e) { throw new RuntimeException(e); } + } else if (resultJson != null) { + PrintWriter writer = null; + String[] resultKeys = resultJson.value(); + context.getResponse().setContentType("text/html; charset=utf-8"); + try { + writer = context.getResponse().getWriter(); + for(String resultKey : resultKeys) { + Object result = context.get(resultKey); + if(result != null) { + writer.write(objectToJson.convert(result)); + } + } + writer.flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + writer.close(); + } + } else if (resultXml != null) { + PrintWriter writer = null; + String[] resultKeys = resultXml.value(); + context.getResponse().setContentType("application/xml; charset=utf-8"); + try { + writer = context.getResponse().getWriter(); + for(String resultKey : resultKeys) { + Object result = context.get(resultKey); + if(result != null) { + writer.write(objectToXml.convert(result)); + } + } + writer.flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + writer.close(); + } } } @@ -177,5 +247,27 @@ public class HandlerExecutionChain { } } } + + private boolean isSimpleType(Class clazz) { + if (clazz.equals(int.class) || clazz.equals(char.class) + || clazz.equals(byte.class) || clazz.equals(boolean.class) + || clazz.equals(short.class) || clazz.equals(long.class) + || clazz.equals(double.class) || clazz.equals(float.class)) { + return true; + } + return false; + } + + private boolean isWapperType(Class clazz) { + if (clazz.equals(Integer.class) || clazz.equals(Character.class) + || clazz.equals(Byte.class) || clazz.equals(Boolean.class) + || clazz.equals(Short.class) || clazz.equals(Long.class) + || clazz.equals(Double.class) || clazz.equals(Float.class) + || clazz.equals(String.class) + ) { + return true; + } + return false; + } } diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/MappingMethodModel.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/MappingMethodModel.java index a6b505cce23fab3a50b09e1dde8db0487e67161e..5da2cc6f226e7e7cd2bc3aab38fb982ba450166e 100644 --- a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/MappingMethodModel.java +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/MappingMethodModel.java @@ -18,8 +18,11 @@ package org.tinygroup.weblayer.mvc; import java.lang.reflect.Method; import java.util.Set; +import org.tinygroup.weblayer.mvc.annotation.Json2Object; import org.tinygroup.weblayer.mvc.annotation.RequestMapping; +import org.tinygroup.weblayer.mvc.annotation.ResultJson; import org.tinygroup.weblayer.mvc.annotation.ResultKey; +import org.tinygroup.weblayer.mvc.annotation.ResultXml; import org.tinygroup.weblayer.mvc.annotation.View; public class MappingMethodModel { @@ -33,6 +36,13 @@ public class MappingMethodModel { private View view; private ResultKey resultKey; + + private ResultJson resultJson; + + private ResultXml resultXml; + + private Json2Object json2Object; + public ResultKey getResultKey() { return resultKey; @@ -87,9 +97,36 @@ public class MappingMethodModel { public void setView(View view) { this.view = view; } - - - + + public ResultJson getResultJson() { + return resultJson; + } + + + public void setResultJson(ResultJson resultJson) { + this.resultJson = resultJson; + } + + + public ResultXml getResultXml() { + return resultXml; + } + + + public void setResultXml(ResultXml resultXml) { + this.resultXml = resultXml; + } + + + public Json2Object getJson2Object() { + return json2Object; + } + + + public void setJson2Object(Json2Object json2Object) { + this.json2Object = json2Object; + } + } diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/Json2Object.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/Json2Object.java new file mode 100644 index 0000000000000000000000000000000000000000..9a6bf6923e0b6680c338d97718fd0067a2628886 --- /dev/null +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/Json2Object.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). + * + * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html + * + * 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 org.tinygroup.weblayer.mvc.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 功能说明: JSON字符串转换对象
+ * @author ljf + * @date 2015-2-15 下午5:56:26 + * @version V1.0 + */ + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Json2Object { +} diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultJson.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultJson.java new file mode 100644 index 0000000000000000000000000000000000000000..63b312edb4fcf4a7a4dbce4cbe38ca2b7b6e5df3 --- /dev/null +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultJson.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). + * + * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html + * + * 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 org.tinygroup.weblayer.mvc.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 功能说明: 返回JSON串
+ * @author ljf + * @date 2015-2-7 下午4:12:15 + * @version V1.0 + */ + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ResultJson { + String[] value() default {}; +} diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultXml.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultXml.java new file mode 100644 index 0000000000000000000000000000000000000000..7f3263d0cc57c70ae858de0629e0968941242572 --- /dev/null +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/annotation/ResultXml.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). + * + * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html + * + * 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 org.tinygroup.weblayer.mvc.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 功能说明: 返回XML
+ * @author ljf + * @date 2015-2-7 下午4:20:54 + * @version V1.0 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ResultXml { + String[] value() default {}; +} diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/handlermapping/AnnotationHandlerMapping.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/handlermapping/AnnotationHandlerMapping.java index 83b7d81b3592003bb7b3897e74b757e768732ad5..da5c1e251dfa3c92467214f6ec9f4fb13c10bc0d 100644 --- a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/handlermapping/AnnotationHandlerMapping.java +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/handlermapping/AnnotationHandlerMapping.java @@ -28,8 +28,11 @@ import org.tinygroup.logger.LoggerFactory; import org.tinygroup.weblayer.mvc.HandlerExecutionChain; import org.tinygroup.weblayer.mvc.MappingClassModel; import org.tinygroup.weblayer.mvc.MappingMethodModel; +import org.tinygroup.weblayer.mvc.annotation.Json2Object; import org.tinygroup.weblayer.mvc.annotation.RequestMapping; +import org.tinygroup.weblayer.mvc.annotation.ResultJson; import org.tinygroup.weblayer.mvc.annotation.ResultKey; +import org.tinygroup.weblayer.mvc.annotation.ResultXml; import org.tinygroup.weblayer.mvc.annotation.View; import org.tinygroup.weblayer.util.TinyPathMatcher; @@ -126,7 +129,19 @@ public class AnnotationHandlerMapping extends AbstractHandlerMapping { if(resultKey!=null){ methodModel.setResultKey(resultKey); } - + ResultJson resultJson = AnnotationUtils.findAnnotation(method, ResultJson.class); + if(resultJson!=null){ + methodModel.setResultJson(resultJson); + } + ResultXml resultXml = AnnotationUtils.findAnnotation(method, ResultXml.class); + if(resultXml!=null){ + methodModel.setResultXml(resultXml); + } + + Json2Object json2Object = AnnotationUtils.findAnnotation(method, Json2Object.class); + if(json2Object!=null){ + methodModel.setJson2Object(json2Object); + } } return StringUtil.toStringArray(urls); diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/impl/MappingModelExecuteImpl.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/impl/MappingModelExecuteImpl.java index b05c7f383f4bd15cdb424c14ac7d177f81b3e56a..b9f35fb9ebb513e4e744e84cc32fafafed0c9217 100644 --- a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/impl/MappingModelExecuteImpl.java +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/mvc/impl/MappingModelExecuteImpl.java @@ -15,6 +15,8 @@ */ package org.tinygroup.weblayer.mvc.impl; +import java.io.IOException; +import javax.servlet.ServletException; import org.tinygroup.weblayer.WebContext; import org.tinygroup.weblayer.mvc.HandlerExecutionChain; import org.tinygroup.weblayer.mvc.MappingModelExecute; @@ -36,6 +38,10 @@ public class MappingModelExecuteImpl implements MappingModelExecute { chain.execute(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); + } catch (ServletException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/util/AsmUtil.java b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/util/AsmUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..eba590df914ecd7f056e8a43c3d65c013994efd6 --- /dev/null +++ b/web/org.tinygroup.weblayer/src/main/java/org/tinygroup/weblayer/util/AsmUtil.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). + * + * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html + * + * 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 org.tinygroup.weblayer.util; + +import java.lang.reflect.Method; + +import org.springframework.core.LocalVariableTableParameterNameDiscoverer; +import org.springframework.core.ParameterNameDiscoverer; + + +/** + * 功能说明: ASM获取方法参数名称 + * @author ljf + * @date 2015-2-25 下午5:23:10 + * @version V1.0 + */ +public class AsmUtil { + + private static ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); + + public static String[] getParameterNames(Method method) { + return parameterNameDiscoverer.getParameterNames(method); + } +}