Skip to content

Commit

Permalink
分解service注解为controller requestMapping ,并实现了服务注册器相应的更新
Browse files Browse the repository at this point in the history
  • Loading branch information
blue-troy committed Apr 9, 2018
1 parent e7b2d27 commit ca0563d
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 17 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions src/main/java/com/bluetroy/httpservice/mvc/ServiceRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@


import com.bluetroy.httpservice.StartUp;
import com.bluetroy.httpservice.mvc.annotation.Service;
import com.bluetroy.httpservice.mvc.annotation.Controller;
import com.bluetroy.httpservice.mvc.annotation.RequestMapping;
import com.bluetroy.httpservice.mvc.service.ServiceInterface;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -27,7 +30,7 @@ private static void register(String urlPattern, ServiceInterface service) {

public static ServiceInterface findService(String url) {
for (String pattern : services.keySet()) {
System.out.println("url = " +url + " pattern = " + pattern + url.matches(pattern));
System.out.println("url = " + url + " pattern = " + pattern + url.matches(pattern));
if (url.matches(pattern)) return services.get(pattern);
}
// 弃用 无法利用正则表达 return services.get(urlPattern);
Expand Down Expand Up @@ -58,25 +61,33 @@ private static void registerFromPackage(String packageName, String packageURL, F
File dir = new File(packageURL);
if (!dir.exists() || !dir.isDirectory()) return;
File[] dirFiles = dir.listFiles(fileFilter);
for (File file : dirFiles) {
for (File file : dirFiles)
if (file.isDirectory()) {
registerFromPackage(packageName + "." + file.getName(), file.getAbsolutePath(), fileFilter);
} else {
String className = file.getName().substring(0, file.getName().indexOf("."));
try {
Class<?> aClass = Class.forName(packageName + "." + className);
Service annotation = aClass.getAnnotation(Service.class);
// 实现了注解,并且实现了接口
if (annotation != null && ServiceInterface.class.isAssignableFrom(aClass)) {
register(annotation.urlPattern(),aClass.asSubclass(ServiceInterface.class).getDeclaredConstructor().newInstance());
System.out.println("成功注册服务: " + annotation.urlPattern() + " " + className);
Annotation[] annotations = aClass.getAnnotations();
if (annotations.length > 1 && annotations[0].annotationType().equals(Controller.class) && annotations[1].annotationType().equals(RequestMapping.class)) {
RequestMapping requestMapping = aClass.getAnnotation(RequestMapping.class);
for (int i = 0; i < requestMapping.value().length; i++) {
register(requestMapping.value()[i],aClass.asSubclass(ServiceInterface.class).getConstructor().newInstance());
System.out.println("成功注册服务: " + requestMapping.value()[i] +" " + aClass.getName());
}
}

//放弃以下早期的扫描服务代码
// 实现了注解,并且实现了接口
// if (annotation != null && ServiceInterface.class.isAssignableFrom(aClass)) {
//
//// register(annotation.urlPattern(),aClass.asSubclass(ServiceInterface.class).getDeclaredConstructor().newInstance());
//// System.out.println("成功注册服务: " + annotation.urlPattern() + " " + className);
// }
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
@Retention(RetentionPolicy.RUNTIME) //指明了该Annotation被保留的时间长短。RetentionPolicy取值为SOURCE,CLASS,RUNTIME
@Documented
//指明拥有这个注解的元素可以被javadoc此类的工具文档化。这种类型应该用于注解那些影响客户使用带注释的元素声明的类型。如果一种声明使用Documented进行注解,这种类型的注解被作为被标注的程序成员的公共API
public @interface Service {
String urlPattern();
public @interface Controller {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bluetroy.httpservice.mvc.annotation;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
//指明该类型的注解可以注解的程序元素的范围。该元注解的取值可以为TYPE,METHOD,CONSTRUCTOR,FIELD等。如果Target元注解没有出现,那么定义的注解可以应用于程序的任何元素
@Retention(RetentionPolicy.RUNTIME) //指明了该Annotation被保留的时间长短。RetentionPolicy取值为SOURCE,CLASS,RUNTIME
@Documented
public @interface RequestMapping {
String[] value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.bluetroy.httpservice.http.Status;
import com.bluetroy.httpservice.http.request.Request;
import com.bluetroy.httpservice.http.response.Response;
import com.bluetroy.httpservice.mvc.annotation.Service;
import com.bluetroy.httpservice.mvc.annotation.Controller;
import com.bluetroy.httpservice.mvc.annotation.RequestMapping;
import com.bluetroy.httpservice.mvc.service.ServiceInterface;

@Service(urlPattern = "^/$")
@Controller()
@RequestMapping(value = "^/$")
public class IndexService implements ServiceInterface {
@Override
public Response service(Request request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
import com.bluetroy.httpservice.http.request.Request;
import com.bluetroy.httpservice.http.response.Response;
import com.bluetroy.httpservice.http.response.impl.FileResponse;
import com.bluetroy.httpservice.mvc.annotation.Service;
import com.bluetroy.httpservice.mvc.annotation.Controller;
import com.bluetroy.httpservice.mvc.annotation.RequestMapping;
import com.bluetroy.httpservice.mvc.service.ServiceInterface;

import java.io.File;

@Service(urlPattern = "\\S*\\.\\S*")
@Controller()
@RequestMapping(value = "\\S*\\.\\S*")
public class StaticFileService implements ServiceInterface {
private static String staticPath;
private static String root;

static {
root = StaticFileService.class.getResource("/").getPath();
}

@Override
public Response service(Request request) {
String filePath = root + request.getHeader().getURI().substring(1);
return new FileResponse(Status.SUCCESS_200,new File(filePath));
return new FileResponse(Status.SUCCESS_200, new File(filePath));
}


Expand Down

0 comments on commit ca0563d

Please sign in to comment.