Skip to content

Commit

Permalink
=> upgrade jdk version to 8
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 17, 2016
1 parent c6880e7 commit 556d869
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 130 deletions.
22 changes: 12 additions & 10 deletions blade-core/src/main/java/com/blade/Blade.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.blade.kit.CollectionKit;
import com.blade.kit.StringKit;
import com.blade.kit.base.Config;
import com.blade.mvc.handler.RouteHandler;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.interceptor.Interceptor;
import com.blade.mvc.route.*;
Expand All @@ -34,7 +35,9 @@
import javax.servlet.http.HttpServlet;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Blade Core Class
Expand Down Expand Up @@ -74,11 +77,6 @@ public final class Blade {
*/
private int port = Const.DEFAULT_PORT;

/**
* Web context path
*/
private String contextPath = Const.DEFAULT_CONTEXTPATH;

/**
* Is enabled server
*/
Expand Down Expand Up @@ -530,13 +528,17 @@ public Blade listen(int port) {
*
* @param applicationClass your app root package starter
*/
public EmbedServer start(Class<?> applicationClass) {
startNoJoin(applicationClass);
public EmbedServer start(Class<?> applicationClass, String contextPath) {
startNoJoin(applicationClass, contextPath);
embedServer.join();
return embedServer;
}

public EmbedServer startNoJoin(Class<?> applicationClass) {

public EmbedServer start(Class<?> applicationClass) {
return start(applicationClass, "/");
}

public EmbedServer startNoJoin(Class<?> applicationClass, String contextPath) {

this.loadAppConf(Const.APP_PROPERTIES);

Expand Down
5 changes: 0 additions & 5 deletions blade-core/src/main/java/com/blade/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public interface Const {
*/
String VERSION = "1.7.0-alpha";

/**
* default embedd server context path
*/
String DEFAULT_CONTEXTPATH = "/";

/**
* server 500 error HTML
*/
Expand Down
23 changes: 23 additions & 0 deletions blade-core/src/main/java/com/blade/kit/AbortPolicyWithReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.blade.kit;

import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {

private final String threadName;

public AbortPolicyWithReport(String threadName) {
this.threadName = threadName;
}

public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
String msg = String.format("Blade["
+ " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d),"
+ " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s)]",
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating());
System.out.println(msg);
throw new RejectedExecutionException(msg);
}
}
20 changes: 20 additions & 0 deletions blade-core/src/main/java/com/blade/kit/BladeThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.blade.kit;

import java.util.concurrent.*;

public class BladeThreadPool {

public static Executor getExecutor(int threads, int queues) {
return getThreadPoolExecutor(threads, queues);
}

public static ThreadPoolExecutor getThreadPoolExecutor(int threads, int queues) {
String name = "blade-pool";
return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
queues == 0 ? new SynchronousQueue<Runnable>()
: (queues < 0 ? new LinkedBlockingQueue<Runnable>()
: new LinkedBlockingQueue<Runnable>(queues)),
new NamedThreadFactory(name, true), new AbortPolicyWithReport(name));
}

}
43 changes: 43 additions & 0 deletions blade-core/src/main/java/com/blade/kit/NamedThreadFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.blade.kit;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory implements ThreadFactory {

private static final AtomicInteger threadNumber = new AtomicInteger(1);

private final AtomicInteger mThreadNum = new AtomicInteger(1);

private final String prefix;

private final boolean daemoThread;

private final ThreadGroup threadGroup;

public NamedThreadFactory() {
this("blade-threadpool-" + threadNumber.getAndIncrement(), false);
}

public NamedThreadFactory(String prefix) {
this(prefix, false);
}

public NamedThreadFactory(String prefix, boolean daemo) {
this.prefix = prefix + "-thread-";
daemoThread = daemo;
SecurityManager s = System.getSecurityManager();
threadGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}

public Thread newThread(Runnable runnable) {
String name = prefix + mThreadNum.getAndIncrement();
Thread ret = new Thread(threadGroup, runnable, name, 0);
ret.setDaemon(daemoThread);
return ret;
}

public ThreadGroup getThreadGroup() {
return threadGroup;
}
}
56 changes: 26 additions & 30 deletions blade-core/src/main/java/com/blade/mvc/DispatcherHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@
*/
package com.blade.mvc;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blade.Blade;
import com.blade.Const;
import com.blade.context.WebContextHolder;
import com.blade.exception.BladeException;
import com.blade.ioc.Ioc;
import com.blade.kit.DispatchKit;
import com.blade.kit.StringKit;
import com.blade.mvc.handler.RouteHandler;
import com.blade.mvc.http.HttpStatus;
import com.blade.mvc.http.Path;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.http.wrapper.ServletRequest;
import com.blade.mvc.http.wrapper.ServletResponse;
import com.blade.mvc.route.Route;
import com.blade.mvc.route.RouteHandler;
import com.blade.mvc.route.RouteMatcher;
import com.blade.mvc.route.Routers;
import com.blade.mvc.view.ModelAndView;
import com.blade.mvc.view.ViewSettings;
import com.blade.mvc.view.resolve.RouteViewResolve;
import com.blade.mvc.view.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* Synchronous request processor
Expand Down Expand Up @@ -79,25 +76,24 @@ public DispatcherHandler(ServletContext servletContext, Routers routers) {
}

public void handle(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {

// http method, GET/POST ...
String method = httpRequest.getMethod();

// reuqest uri
String uri = Path.getRelativePath(httpRequest.getRequestURI(), servletContext.getContextPath());

// Create Response
Response response = new ServletResponse(httpResponse);;

// If it is static, the resource is handed over to the filter
if(staticFileFilter.isStatic(uri)){
LOGGER.debug("Request : {}\t{}", method, uri);
DispatchKit.printStatic(uri, httpRequest, response);
return;
}

// Create Response
Response response = new ServletResponse(httpResponse);

try {


// http method, GET/POST ...
String method = httpRequest.getMethod();

// reuqest uri
String uri = Path.getRelativePath(httpRequest.getRequestURI(), servletContext.getContextPath());

// If it is static, the resource is handed over to the filter
if(staticFileFilter.isStatic(uri)){
LOGGER.debug("Request : {}\t{}", method, uri);
DispatchKit.printStatic(uri, httpRequest, response);
return;
}

LOGGER.info("Request : {}\t{}", method, uri);

Request request = new ServletRequest(httpRequest);
Expand Down
9 changes: 4 additions & 5 deletions blade-core/src/main/java/com/blade/mvc/StaticFileFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.blade.mvc;

import java.util.HashMap;
import com.blade.kit.CollectionKit;

import java.util.Map;
import java.util.Set;

Expand All @@ -26,7 +27,7 @@ public class StaticFileFilter {

public StaticFileFilter(Set<String> prefixList) {
this.prefixList = prefixList;
this.cache = new HashMap<String, Boolean>(128);
this.cache = CollectionKit.newHashMap(128);
}

public boolean isStatic(String path){
Expand All @@ -36,7 +37,7 @@ public boolean isStatic(String path){
return found == Boolean.TRUE;
}
}

for(String prefix : prefixList){
if(path.startsWith(prefix)){
if (cache != null) {
Expand All @@ -45,11 +46,9 @@ public boolean isStatic(String path){
return true;
}
}

if (cache != null) {
cache.put(path, Boolean.FALSE);
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blade.mvc.multipart;
package com.blade.mvc.handler;

import com.blade.mvc.multipart.FileItem;

/**
* MultipartHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blade.mvc.route;
package com.blade.mvc.handler;

import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
Expand All @@ -24,8 +24,9 @@
* @author <a href="mailto:[email protected]" target="_blank">biezhi</a>
* @since 1.5
*/
@FunctionalInterface
public interface RouteHandler {

public void handle(Request request, Response response);
void handle(Request request, Response response);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.blade.mvc.multipart.FileItem;
import com.blade.mvc.multipart.Multipart;
import com.blade.mvc.multipart.MultipartException;
import com.blade.mvc.multipart.MultipartHandler;
import com.blade.mvc.handler.MultipartHandler;
import com.blade.mvc.route.Route;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ public PrintWriter writer() throws IOException {

@Override
public Response render(String view) {
Assert.notBlank(view, "view not is null");
String viewPath = Path.cleanPath(view);
ModelAndView modelAndView = new ModelAndView(viewPath);
try {
Expand All @@ -286,7 +285,6 @@ public Response render(String view) {

@Override
public Response render(ModelAndView modelAndView) {
Assert.notNull(modelAndView, "ModelAndView not is null!");
Assert.notBlank(modelAndView.getView(), "view not is null");

String viewPath = Path.cleanPath(modelAndView.getView());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.servlet.http.HttpServletRequest;

import com.blade.kit.IOKit;
import com.blade.mvc.handler.MultipartHandler;

/**
* Multipart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.blade.mvc.route;

import com.blade.Blade;
import com.blade.mvc.handler.RouteHandler;

/**
* Route Group.
Expand Down
Loading

0 comments on commit 556d869

Please sign in to comment.