Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/java/kis/di/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import javassist.NotFoundException;
import kis.di.annotation.InvokeLog;
import kis.di.annotation.RequestScoped;
import kis.di.annotation.SessionScoped;
import kis.di.mvc.BeanSession;

/**
* @author naoki
Expand All @@ -38,6 +40,12 @@ public class Context {
static Map<String, Object> beans = new HashMap<>();
static ThreadLocal<Map<String, Object>> requestBeans = new InheritableThreadLocal<>();

static BeanSession beanSession;

public static void setBeanSession(BeanSession beanSession) {
Context.beanSession = beanSession;
}

public static void autoRegister() {
try {
URL res = Context.class.getResource(
Expand Down Expand Up @@ -87,6 +95,8 @@ public static Object getBean(String name) {
scope = new HashMap<>();
requestBeans.set(scope);
}
} else if (type.isAnnotationPresent(SessionScoped.class)) {
scope = beanSession.getBeans();
} else {
scope = beans;
}
Expand Down Expand Up @@ -139,14 +149,25 @@ private static <T> Class<? extends T> wrap(Class<T> type) {
}
}

private static int scopeRank(Class type) {
if (type.isAnnotationPresent(RequestScoped.class)) {
return 0;
}
if (type.isAnnotationPresent(SessionScoped.class)) {
return 5;
}
return 10;
}

private static <T> void inject(Class<T> type, T object) throws IllegalArgumentException, IllegalAccessException {
for (Field field : type.getDeclaredFields()) {
if (!field.isAnnotationPresent(Inject.class)) {
continue;
}
field.setAccessible(true);
Object bean;
if (!type.isAnnotationPresent(RequestScoped.class) && field.getType().isAnnotationPresent(RequestScoped.class)) {

if (scopeRank(type) > scopeRank(field.getType())) {
bean = scopeWrapper(field.getType(), field.getName());
} else {
bean = getBean(field.getName());
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/kis/di/annotation/SessionScoped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kis.di.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* @author naoki
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SessionScoped {

}
29 changes: 29 additions & 0 deletions src/main/java/kis/di/mvc/BeanSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kis.di.mvc;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author naoki
*/
public class BeanSession {
private final ThreadLocal<String> sessionId = new InheritableThreadLocal<>();
private final Map<String, Map<String, Object>> beans = new HashMap<>();

public void setSessionId(String id) {
sessionId.set(id);
if (!beans.containsKey(id)) {
beans.put(id, new HashMap<>());
}
}

public Map<String, Object> getBeans() {
return beans.get(sessionId.get());
}

public boolean isSessionRegistered(String id) {
return beans.containsKey(id);
}

}
27 changes: 18 additions & 9 deletions src/main/java/kis/di/mvc/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -41,6 +39,8 @@ static String trimSlash(String str) {

public static void main(String[] args) throws IOException {
Context.autoRegister();
BeanSession beanSession = new BeanSession();
Context.setBeanSession(beanSession);
Map<String, ProcessorMethod> methods = new HashMap<>();
Context.registeredClasses().forEach(entry -> {
Class cls = entry.getValue();
Expand Down Expand Up @@ -73,12 +73,11 @@ public static void main(String[] args) throws IOException {

Pattern pattern = Pattern.compile("([A-Z]+) ([^ ]+) (.+)");
Pattern patternHeader = Pattern.compile("([A-Za-z-]+): (.+)");
AtomicLong lastSessionId = new AtomicLong();
AtomicLong lastSessionId = new AtomicLong(10);
ServerSocket serverSoc = new ServerSocket(8989);
ExecutorService executors = Executors.newFixedThreadPool(10);
for (;;) {
Socket s = serverSoc.accept();
executors.execute(() -> {
new Thread(() -> {
try (InputStream is = s.getInputStream();
BufferedReader bur = new BufferedReader(new InputStreamReader(is)))
{
Expand Down Expand Up @@ -113,8 +112,17 @@ public static void main(String[] args) throws IOException {
}
}
}
String sessionId = cookies.getOrDefault("jsessionid",
Long.toString(lastSessionId.incrementAndGet()));

String sessionId = cookies.get("jsessionid");
if (sessionId != null) {
if (!beanSession.isSessionRegistered(sessionId)) {
sessionId = null;
}
}
if (sessionId == null) {
sessionId = Long.toString(lastSessionId.incrementAndGet());
}
beanSession.setSessionId(sessionId);
info.setSessionId(sessionId);
try (OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os))
Expand All @@ -137,17 +145,18 @@ public static void main(String[] args) throws IOException {
pw.println();
pw.println(output);
} catch (Exception ex) {
pw.println("HTTP/1.0 200 OK");
pw.println("HTTP/1.0 500 Internal Server Error");
pw.println("Content-Type: text/html");
pw.println();
pw.println("<h1>500 Internal Server Error</h1>");
pw.println(ex);
ex.printStackTrace();
}
}
} catch (IOException ex) {
System.out.println(ex);
}
});
}).start();
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/kis/di/sample/mvc/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package kis.di.sample.mvc;

import java.time.LocalDateTime;

import javax.inject.Inject;
import javax.inject.Named;

import kis.di.annotation.Path;

/**
*
* @author naoki
*/
@Named
@Path("login")
public class LoginController {

@Inject
LoginSession loginSession;

@Path("index")
public String index() {
String title = "<h1>Login</h1>";
if (loginSession.isLogined()) {
return title + "Login at " + loginSession.getLoginTime();
} else {
return title + "Not Login";
}
}

@Path("login")
public String login() {
loginSession.setLogined(true);
loginSession.setLoginTime(LocalDateTime.now());
return "<h1>Login</h1>login";
}
@Path("logout")
public String logout() {
loginSession.setLogined(false);
return "<h1>Login</h1>logout";
}
}
20 changes: 20 additions & 0 deletions src/main/java/kis/di/sample/mvc/LoginSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kis.di.sample.mvc;

import java.time.LocalDateTime;

import javax.inject.Named;

import kis.di.annotation.SessionScoped;
import lombok.Data;

/**
*
* @author naoki
*/
@Named
@SessionScoped
@Data
public class LoginSession {
boolean logined;
LocalDateTime loginTime;
}