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
8 changes: 5 additions & 3 deletions src/main/java/com/hunantv/fw/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Dispatcher extends AbstractHandler {

public final static FwLogger logger = new FwLogger(Dispatcher.class);
Application app = Application.getInstance();
protected Routes routes = app.getRoutes();;
protected Routes routes = app.getRoutes();
protected boolean debug = app.isDebug();

protected static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
Expand All @@ -32,7 +32,9 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
Long btime = System.currentTimeMillis();
logger.initSeqid();
logger.debug(target);
response.setContentType("text/html;charset=UTF-8");
if (WebUtil.isTextHtml(request)) {
response.setContentType("text/html;charset=UTF-8");
}
if (WebUtil.isMultipart(request)) {
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
}
Expand All @@ -47,7 +49,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
this.err500(response, ex);
} finally {
Long etime = System.currentTimeMillis();
logger.delayInfo("cost", new Long(etime - btime).toString());
logger.delayInfo("cost", Long.toString(etime - btime));
logger.clearSeqid();
}
}
Expand Down
173 changes: 85 additions & 88 deletions src/main/java/com/hunantv/fw/db/C3P0.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,92 +15,89 @@
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0 {
public static final FwLogger logger = new FwLogger(C3P0.class);
private static C3P0 c3p0 = null;
private Map<String, DataSource> dataSources = new HashMap<String, DataSource>();
public static final String DEFUAT_DB_NAME = "write";

private C3P0() {
try {
Map<String, Map<String, Object>> specialPros = initPros();
for (Iterator<String> iter = specialPros.keySet().iterator(); iter.hasNext();) {
String name = iter.next();
printPros(name, specialPros.get(name));
DataSource ds = new ComboPooledDataSource();
BeanUtils.populate(ds, specialPros.get(name));
this.dataSources.put(name, ds);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

private void printPros(String name, Map<String, Object> pros) {
logger.debug("datasource name:" + name);
for (Iterator<String> iter = pros.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
logger.debug(key + "=" + (String) pros.get(key));
}
}

private Map<String, Map<String, Object>> initPros() {
try {
Properties pros = new SysConf().read("c3p0.properties");

Map<String, Object> sharePros = new HashMap<String, Object>();
String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ",");

Map<String, Map<String, Object>> specialPros = new HashMap<String, Map<String, Object>>();
for (String name : names) {
specialPros.put(name, new HashMap<String, Object>());
}

for (Iterator iter = pros.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) {
for (String specialName : specialPros.keySet()) {
if (!key.startsWith("c3p0." + specialName)) {
sharePros.put(key.substring(5), pros.get(key));
} else {
// c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度
int len = 5 + specialName.length() + 1;
specialPros.get(specialName).put(key.substring(len), pros.get(key));
}
}
}
}

/**
* 把常规属性放到特殊属性里面去
*/
for (Map<String, Object> ps : specialPros.values()) {
for (Iterator<String> iter = sharePros.keySet().iterator(); iter.hasNext();) {
String key = iter.next();
ps.put(key, sharePros.get(key));
}
}
return specialPros;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public static C3P0 instance() {
if (c3p0 == null)
c3p0 = new C3P0();
return c3p0;
}

public DataSource getDataSource() {
return this.getDataSource(this.DEFUAT_DB_NAME);
}

public DataSource getDataSource(String name) {
return this.dataSources.get(name);
}

public static void main(String[] args) throws Exception {
ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource();
System.out.println(ds.getMaxPoolSize());
}
public static final FwLogger logger = new FwLogger(C3P0.class);
private static C3P0 c3p0 = null;
private Map<String, DataSource> dataSources = new HashMap<>();
public static final String DEFUAT_DB_NAME = "write";

private C3P0() {
try {
Map<String, Map<String, Object>> specialPros = initPros();
for (Iterator<String> iter = specialPros.keySet().iterator(); iter.hasNext(); ) {
String name = iter.next();
printPros(name, specialPros.get(name));
DataSource ds = new ComboPooledDataSource();
BeanUtils.populate(ds, specialPros.get(name));
this.dataSources.put(name, ds);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

private void printPros(String name, Map<String, Object> pros) {
logger.debug("datasource name:" + name);
pros.forEach((k, v) -> logger.debug(k + "=" + v));
}

private Map<String, Map<String, Object>> initPros() {
try {
Properties pros = new SysConf().read("c3p0.properties");

Map<String, Object> sharePros = new HashMap<>();
String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ",");

Map<String, Map<String, Object>> specialPros = new HashMap<>();
for (String name : names) {
specialPros.put(name, new HashMap<>());
}

for (Iterator iter = pros.keySet().iterator(); iter.hasNext(); ) {
String key = (String) iter.next();
if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) {
for (String specialName : specialPros.keySet()) {
if (!key.startsWith("c3p0." + specialName)) {
sharePros.put(key.substring(5), pros.get(key));
} else {
// c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度
int len = 5 + specialName.length() + 1;
specialPros.get(specialName).put(key.substring(len), pros.get(key));
}
}
}
}

/**
* 把常规属性放到特殊属性里面去
*/
for (Map<String, Object> ps : specialPros.values()) {
for (Iterator<String> iter = sharePros.keySet().iterator(); iter.hasNext(); ) {
String key = iter.next();
ps.put(key, sharePros.get(key));
}
}
return specialPros;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public static C3P0 instance() {
if (c3p0 == null)
c3p0 = new C3P0();
return c3p0;
}

public DataSource getDataSource() {
return this.getDataSource(this.DEFUAT_DB_NAME);
}

public DataSource getDataSource(String name) {
return this.dataSources.get(name);
}

public static void main(String[] args) throws Exception {
ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource();
System.out.println(ds.getMaxPoolSize());
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/hunantv/fw/route/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.hunantv.fw.utils.StringUtil;

public class Route {
public static enum HttpMethod {
GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE;
public enum HttpMethod {
GET, POST, PUT, DELETE, OPTIONS, HEAD, TRACE
}

Map<String, Object[]> classAndRegMapping = new HashMap<String, Object[]>() {
Expand Down Expand Up @@ -260,9 +260,8 @@ public ControllerAndAction buildControllerAndAction() throws HttpException {

public ControllerAndAction buildControllerAndAction(Object[] args) throws HttpException {
Class<? extends Controller> controllerClass = this.getController();
Controller controller = null;
try {
controller = controllerClass.newInstance();
Controller controller = controllerClass.newInstance();
if (args == null)
return new ControllerAndAction(controller, this.action);
return new ControllerAndAction(controller, this.action, args);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/hunantv/fw/route/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class Routes {
/**
* { uri: { httpMethod: Route } }
*/
private Map<String, Map<String, Route>> staticRoutes = new HashMap<String, Map<String, Route>>();
private Map<String, Map<String, Route>> staticRoutes = new HashMap<>();

/**
* { Route: { httpMethod } }
*/
private Map<Route, Set<String>> dyRoutes = new HashMap<Route, Set<String>>();
private Map<Route, Set<String>> dyRoutes = new HashMap<>();

public Routes() {
}
Expand All @@ -38,7 +38,7 @@ public Routes addStatic(Route route) {
String routeUri = route.getUriReg();
Map<String, Route> tmpMap = staticRoutes.get(routeUri);
if (null == tmpMap) {
tmpMap = new HashMap<String, Route>();
tmpMap = new HashMap<>();
staticRoutes.put(routeUri, tmpMap);
}
tmpMap.put(route.getHttpMethod().toString(), route);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hunantv/fw/utils/SysConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ private String sysPath() throws Exception {
String path = url.getFile();
int fileStrPosition = path.indexOf("file:/");
int begin = 0;
int end = path.length();
if (fileStrPosition >= 0)
begin = fileStrPosition + 5;
end = path.indexOf("WEB-INF/");
int end = path.indexOf("WEB-INF/");
if (end > 0) {
String rf = path.substring(begin, end);
webPath = rf;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/hunantv/fw/utils/WebUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public static boolean isMultipart(HttpServletRequest request) {
String contentType = request.getContentType();
return contentType != null && contentType.startsWith("multipart/form-data");
}

public static boolean isTextHtml(HttpServletRequest request) {
String HeaderValue = request.getHeader("Accept");
return HeaderValue != null && HeaderValue.startsWith("text/html");
}
private static String[] IP_HEADS = new String[] {
"X-Forwarded-For",
"Proxy-Client-IP",
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/hunantv/fw/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
public interface View {
FwLogger logger = FwLogger.getLogger(View.class);

public abstract String render();
String render();

public abstract void renderTo(Writer out) throws IOException;
void renderTo(Writer out) throws IOException;

public abstract Object getV();
Object getV();

/**
* Determine if this view should rendered as byte's stream, <br/>
* default is false,
*
* @return
*/
public default boolean isStreamView() {
default boolean isStreamView() {
return false;
}

Expand All @@ -35,7 +35,7 @@ public default boolean isStreamView() {
* @param response: HttpServletResponse
* @throws IOException
*/
public default void renderTo(HttpServletResponse response) throws IOException {
default void renderTo(HttpServletResponse response) throws IOException {
if (isStreamView()) {
renderTo(response.getOutputStream());
} else {
Expand All @@ -48,7 +48,7 @@ public default void renderTo(HttpServletResponse response) throws IOException {
*
* @return View's bytes
*/
public default byte[] getBytes() {
default byte[] getBytes() {
return null;
}

Expand All @@ -61,7 +61,7 @@ public default byte[] getBytes() {
* @param out: OutputStream
* @throws IOException
*/
public default void renderTo(OutputStream out) throws IOException {
default void renderTo(OutputStream out) throws IOException {
byte[] bytes = getBytes();

if (bytes == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fw/test/unit/DummyServletOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void write(byte[] bytes) throws IOException {
}

@Override
public void write(byte[] b, int len, int off) throws IOException {
outputStream.write(b, len, off);
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
}

@Override
Expand Down