一,主要注解
1,Async:表示一个异步处理
1 /** 2 * 3 */ 4 package com.jjh.jee.hawk; 5 6 import java.lang.annotation.ElementType; 7 import java.lang.annotation.Retention; 8 import java.lang.annotation.RetentionPolicy; 9 import java.lang.annotation.Target; 10 11 /** 12 * @author Administrator 13 * 14 */ 15 @Target(ElementType.METHOD) 16 @Retention(RetentionPolicy.RUNTIME) 17 public @interface Async { 18 int value() default 3000; 19 }
2,Cache:类上面,表示该实例会被缓存
1 /** 2 * 3 */ 4 package com.jjh.jee.hawk; 5 6 import java.lang.annotation.ElementType; 7 import java.lang.annotation.Retention; 8 import java.lang.annotation.RetentionPolicy; 9 import java.lang.annotation.Target; 10 11 /** 12 * @author Administrator 13 * 14 */ 15 @Target(ElementType.TYPE) 16 @Retention(RetentionPolicy.RUNTIME) 17 public @interface Cache { 18 19 }
3,Service:表示一个业务类
1 /** 2 * 3 */ 4 package com.jjh.jee.hawk; 5 6 import java.lang.annotation.ElementType; 7 import java.lang.annotation.Retention; 8 import java.lang.annotation.RetentionPolicy; 9 import java.lang.annotation.Target; 10 11 /** 12 * @author Administrator 13 * 14 */ 15 @Target(ElementType.TYPE) 16 @Retention(RetentionPolicy.RUNTIME) 17 public @interface Service { 18 String value() default ""; 19 }
4,Work:一个处理节点
1 /** 2 * 3 */ 4 package com.jjh.jee.hawk; 5 6 import java.lang.annotation.ElementType; 7 import java.lang.annotation.Retention; 8 import java.lang.annotation.RetentionPolicy; 9 import java.lang.annotation.Target; 10 11 /** 12 * @author Administrator 13 * 14 */ 15 @Target(ElementType.METHOD) 16 @Retention(RetentionPolicy.RUNTIME) 17 public @interface Work { 18 String mapping() default ""; 19 String value() default ""; 20 boolean main() default false; 21 boolean UI() default true; 22 }
二,Bundle类,所有work的唯一参数。
1 /** 2 * 3 */ 4 package com.jjh.jee.hawk; 5 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.lang.reflect.Array; 9 import java.lang.reflect.Method; 10 import java.math.BigDecimal; 11 import java.net.URL; 12 import java.nio.charset.StandardCharsets; 13 import java.util.Collection; 14 import java.util.Collections; 15 import java.util.HashMap; 16 import java.util.Map; 17 import java.util.Objects; 18 import java.util.function.Function; 19 20 import javax.servlet.ServletException; 21 import javax.servlet.http.HttpServletRequest; 22 import javax.servlet.http.HttpServletResponse; 23 import javax.servlet.http.Part; 24 25 import org.json.JSONArray; 26 import org.json.JSONException; 27 import org.json.JSONObject; 28 29 import com.jjh.common.Files; 30 import com.jjh.common.StringX; 31 import com.jjh.common.Utils; 32 33 /** 34 * @author Administrator 35 * 36 */ 37 public final class Bundle { 38 /** 39 * @author Administrator 40 * 41 */ 42 public static enum Scope { 43 REQUEST, SESSION, APPLICATION; 44 } 45 46 private final Map<String, Object> map = Collections.synchronizedMap(new HashMap<>()); 47 private static final String JSON = "json"; 48 private static final String XML = "xml"; 49 private static final String TEXT = "text"; 50 private static final String BINARY = "binary"; 51 private static final String REQUEST = "request"; 52 private static final String RESPONSE = "response"; 53 54 private final Function<String, Void> func = key -> { 55 switch (key) { 56 case JSON: 57 throw new IllegalArgumentException("key=='json'already exist."); 58 case XML: 59 throw new IllegalArgumentException("key=='xml'already exist."); 60 case TEXT: 61 throw new IllegalArgumentException("key=='text'already exist."); 62 case BINARY: 63 throw new IllegalArgumentException("key=='binary'already exist."); 64 case REQUEST: 65 throw new IllegalArgumentException("key=='request'already exist."); 66 case RESPONSE: 67 throw new IllegalArgumentException("key=='response'already exist."); 68 } 69 return null; 70 }; 71 72 /** 73 * 74 */ 75 Bundle() { 76 super(); 77 // TODO Auto-generated constructor stub 78 } 79 80 /** 81 * @return the hawkContext 82 */ 83 public HawkContext getContext() { 84 return HawkContext.getContext(); 85 } 86 87 void setRequest(HttpServletRequest request) { 88 map.put(REQUEST, request); 89 } 90 91 public HttpServletRequest getRequest() { 92 return (HttpServletRequest) map.get(REQUEST); 93 } 94 95 void setResponse(HttpServletResponse request) { 96 map.put(RESPONSE, request); 97 } 98 99 public HttpServletResponse getResponse() { 100 return (HttpServletResponse) map.get(RESPONSE); 101 } 102 103 public Bundle setJson(Object json) { 104 map.put(JSON, json); 105 return this; 106 } 107 108 @SuppressWarnings("unchecked") 109 public <T> T getJson() { 110 T json = (T) map.get(JSON); 111 if (Objects.isNull(json)) { 112 try { 113 String str = new String(Files.read(getInputStream()), StandardCharsets.UTF_8); 114 if (str.trim().startsWith("{")) 115 json = (T) new JSONObject(str); 116 else if (str.trim().startsWith("[")) 117 json = (T) new JSONArray(str); 118 } catch (Exception e) { 119 throw new RuntimeException(e); 120 } 121 } 122 return json; 123 } 124 125 public <T> T getXml(Class<T> claze) { 126 try { 127 return javax.xml.bind.JAXB.unmarshal(getInputStream(), claze); 128 } catch (IOException e) { 129 // TODO Auto-generated catch block 130 throw new RuntimeException(e); 131 } 132 } 133 134 @SuppressWarnings("unchecked") 135 public <T> T getXml() { 136 return (T) map.get(XML); 137 } 138 139 public Bundle setXml(Object object) { 140 map.put(XML, object); 141 return this; 142 } 143 144 public String getText() { 145 String text = (String) map.get(TEXT); 146 if (StringX.isEmpty(text)) { 147 try { 148 text = new String(Files.read(getInputStream()), StandardCharsets.UTF_8); 149 } catch (IOException e) { 150 // TODO Auto-generated catch block 151 throw new RuntimeException(e); 152 } 153 } 154 return text; 155 } 156 157 public Bundle setText(String text) { 158 map.put(TEXT, text); 159 return this; 160 } 161 162 public byte[] getBytes() { 163 byte[] bytes = (byte[]) map.get(BINARY); 164 if (Objects.isNull(bytes)) { 165 try { 166 bytes = Files.read(getInputStream()); 167 } catch (IOException e) { 168 // TODO Auto-generated catch block 169 throw new RuntimeException(e); 170 } 171 } 172 return bytes; 173 } 174 175 public Bundle setBytes(byte[] bytes) { 176 map.put(BINARY, bytes); 177 return this; 178 } 179 180 public Part getPart(String name) throws IOException, ServletException { 181 return getRequest().getPart(name); 182 } 183 184 public Collection<Part> getParts() throws IOException, ServletException { 185 return getRequest().getParts(); 186 } 187 188 public Bundle set(String key, Object object) { 189 func.apply(key); 190 map.put(key, object); 191 return this; 192 } 193 194 @SuppressWarnings("unchecked") 195 public <T> T get(String key) { 196 func.apply(key); 197 return (T) map.get(key); 198 } 199 200 public String getString(String name) { 201 return getRequest().getParameter(name); 202 } 203 204 public long getLong(String name) { 205 return Utils.convert(Long.class, getString(name)); 206 } 207 208 public int getInt(String name) { 209 return Utils.convert(Integer.class, getString(name)); 210 } 211 212 public short getShort(String name) { 213 return Utils.convert(Short.class, getString(name)); 214 } 215 216 public byte getByte(String name) { 217 return Utils.convert(Byte.class, getString(name)); 218 } 219 220 public float getFloat(String name) { 221 return Utils.convert(Float.class, getString(name)); 222 } 223 224 public double getDouble(String name) { 225 return Utils.convert(Double.class, getString(name)); 226 } 227 228 public boolean getBoolean(String name) { 229 return Utils.convert(Boolean.class, getString(name)); 230 } 231 232 public java.util.Date getDate(String name) { 233 return Utils.convert(java.util.Date.class, getString(name)); 234 } 235 236 public BigDecimal getBigDecimal(String name) { 237 return Utils.convert(BigDecimal.class, getString(name)); 238 } 239 240 public String getParameter(String name) { 241 return getString(name); 242 } 243 244 public <T> T getParameter(Class<T> classType) throws Exception{ 245 T obj = classType.newInstance(); 246 for (Map.Entry<String, String[]> entry : getRequest().getParameterMap().entrySet()) { 247 String key = entry.getKey(); 248 String[] value = entry.getValue(); 249 if (Objects.nonNull(value)) { 250 Method m = Utils.getPublicMethod(classType, Utils.getSetMethodName(key)); 251 if (Objects.nonNull(m)) { 252 Class<?> paramClass = m.getParameterTypes()[0]; 253 if (paramClass.isArray()) { 254 Object arr=Array.newInstance(paramClass, value.length); 255 for(int i=0;i<value.length;i++) 256 Array.set(arr, i, Utils.convert(paramClass, value[i])); 257 m.invoke(obj,arr); 258 } else if (paramClass.isPrimitive()) { 259 m.invoke(obj, Utils.convert(paramClass, value[0])); 260 } 261 } 262 } 263 } 264 return obj; 265 } 266 267 public String[] getParameters(String name) { 268 return getRequest().getParameterValues(name); 269 } 270 271 public JSONObject getParameters() throws JSONException { 272 JSONObject json=new JSONObject(); 273 for(Map.Entry<String, String[]> entry:getRequest().getParameterMap().entrySet()) 274 { 275 String key=entry.getKey(); 276 String[] values=entry.getValue(); 277 if(values.length==0) 278 json.put(key, null); 279 else if(values.length==1) 280 json.put(key, values[0]); 281 else if(values.length>1) 282 json.put(key, new JSONArray(values)); 283 } 284 return json; 285 } 286 287 public InputStream getInputStream() throws IOException { 288 return getRequest().getInputStream(); 289 } 290 291 public URL getResource(String resource) throws IOException { 292 return HawkContext.getContext().getResource(resource); 293 } 294 295 public InputStream getResourceAsStream(String resource) { 296 return HawkContext.getContext().getResourceAsStream(resource); 297 } 298 299 public Bundle set(String key, Object value, Scope scope) { 300 switch (scope) { 301 case REQUEST: 302 getRequest().setAttribute(key, value); 303 break; 304 case SESSION: 305 getRequest().getSession().setAttribute(key, value); 306 break; 307 case APPLICATION: 308 getContext().cache(key, value); 309 break; 310 } 311 return this; 312 } 313 314 @SuppressWarnings("unchecked") 315 public <T> T get(String key, Scope scope) { 316 T ret = null; 317 switch (scope) { 318 case REQUEST: 319 ret = (T) getRequest().getAttribute(key); 320 break; 321 case SESSION: 322 ret = (T) getRequest().getSession().getAttribute(key); 323 break; 324 case APPLICATION: 325 ret = (T) getContext().get(key); 326 break; 327 } 328 return ret; 329 } 330 331 public void clear() { 332 map.clear(); 333 } 334 }