This commit is contained in:
地平线
2015-04-14 09:23:35 +08:00
parent 506e4ef05b
commit 151273cce3
8 changed files with 129 additions and 42 deletions

View File

@@ -5,7 +5,6 @@
*/ */
package com.wentch.redkale.convert; package com.wentch.redkale.convert;
import static com.wentch.redkale.convert.ObjectEncoder.TYPEZERO; import static com.wentch.redkale.convert.ObjectEncoder.TYPEZERO;
import com.wentch.redkale.util.Creator; import com.wentch.redkale.util.Creator;
import java.lang.reflect.*; import java.lang.reflect.*;
@@ -34,7 +33,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
protected Factory factory; protected Factory factory;
protected ObjectDecoder(Type type) { protected ObjectDecoder(Type type) {
this.type = type; this.type = ((type instanceof Class) && ((Class) type).isInterface()) ? Object.class : type;
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) type; final ParameterizedType pt = (ParameterizedType) type;
this.typeClass = (Class) pt.getRawType(); this.typeClass = (Class) pt.getRawType();

View File

@@ -15,39 +15,51 @@ import javax.net.ssl.*;
*/ */
public class SSLBuilder { public class SSLBuilder {
private static char[] keypasswd; private static SSLContext sslContext;
public static void main(String[] args) throws Exception { static {
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); try {
keyStore.load(null, keypasswd); char[] keypasswd = new char[32];
final String algorithm = System.getProperty("ssl.algorithm", KeyManagerFactory.getDefaultAlgorithm()); final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); keyStore.load(null, keypasswd);
kmf.init(keyStore, keypasswd); final String algorithm = System.getProperty("ssl.algorithm", KeyManagerFactory.getDefaultAlgorithm());
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(keyStore, keypasswd);
SSLContext sslContext0 = SSLContext.getInstance("TLS");
sslContext0.init(kmf.getKeyManagers(), null, new SecureRandom());
sslContext = sslContext0;
} catch (Exception e) {
throw new Error(e);
}
}
final SSLContext sslContext = SSLContext.getInstance("TLS"); private final SSLEngine sslEngine;
sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());
//------------------------- private int appBufferSize;
final SSLEngine sslEngine = sslContext.createSSLEngine();
private int netBufferSize;
public SSLBuilder() {
sslEngine = sslContext.createSSLEngine();
//sslEngine.setEnabledCipherSuites(null); //sslEngine.setEnabledCipherSuites(null);
//sslEngine.setEnabledProtocols(null); //sslEngine.setEnabledProtocols(null);
sslEngine.setUseClientMode(false); sslEngine.setUseClientMode(false);
sslEngine.setWantClientAuth(false); sslEngine.setWantClientAuth(false);
sslEngine.setNeedClientAuth(false); sslEngine.setNeedClientAuth(false);
//---------------------------
updateBufferSizes();
} }
private static final byte CHANGE_CIPHER_SPECT_CONTENT_TYPE = 20; private void updateBufferSizes() {
final SSLSession session = sslEngine.getSession();
appBufferSize = session.getApplicationBufferSize();
netBufferSize = session.getPacketBufferSize();
}
private static final byte APPLICATION_DATA_CONTENT_TYPE = 23; public static void main(String[] args) throws Exception {
private static final int SSLV3_RECORD_HEADER_SIZE = 5; // SSLv3 record header }
private static final int SSL20_HELLO_VERSION = 0x0002;
private static final int MIN_VERSION = 0x0300;
private static final int MAX_MAJOR_VERSION = 0x03;
private static int getSSLPacketSize(final ByteBuffer buf) throws SSLException { private static int getSSLPacketSize(final ByteBuffer buf) throws SSLException {
@@ -88,7 +100,7 @@ public class SSLBuilder {
* determination. Otherwise, try one last hueristic to * determination. Otherwise, try one last hueristic to
* see if it's SSL/TLS. * see if it's SSL/TLS.
*/ */
if (byte0 >= CHANGE_CIPHER_SPECT_CONTENT_TYPE && byte0 <= APPLICATION_DATA_CONTENT_TYPE) { if (byte0 >= 20 && byte0 <= 23) {
/* /*
* Last sanity check that it's not a wild record * Last sanity check that it's not a wild record
*/ */
@@ -99,14 +111,14 @@ public class SSLBuilder {
// Check if too old (currently not possible) // Check if too old (currently not possible)
// or if the major version does not match. // or if the major version does not match.
// The actual version negotiation is in the handshaker classes // The actual version negotiation is in the handshaker classes
if ((v < MIN_VERSION) || (major > MAX_MAJOR_VERSION)) { if ((v < 0x0300) || (major > 0x03)) {
throw new SSLException("Unsupported record version major=" + major + " minor=" + minor); throw new SSLException("Unsupported record version major=" + major + " minor=" + minor);
} }
/* /*
* One of the SSLv3/TLS message types. * One of the SSLv3/TLS message types.
*/ */
len = ((byte3 & 0xff) << 8) + (byte4 & 0xff) + SSLV3_RECORD_HEADER_SIZE; len = ((byte3 & 0xff) << 8) + (byte4 & 0xff) + 5; // SSLv3 record header
} else { } else {
/* /*
@@ -127,10 +139,9 @@ public class SSLBuilder {
// Check if too old (currently not possible) // Check if too old (currently not possible)
// or if the major version does not match. // or if the major version does not match.
// The actual version negotiation is in the handshaker classes // The actual version negotiation is in the handshaker classes
if ((v < MIN_VERSION) || (major > MAX_MAJOR_VERSION)) { if ((v < 0x0300) || (major > 0x03)) {
// if it's not SSLv2, we're out of here. // if it's not SSLv2, we're out of here.
if (v != SSL20_HELLO_VERSION) throw new SSLException("Unsupported record version major=" + major + " minor=" + minor); if (v != 0x0002) throw new SSLException("Unsupported record version major=" + major + " minor=" + minor);
} }
/* /*

View File

@@ -229,7 +229,10 @@ public final class HttpRequest extends Request {
String val = getHeader(remoteAddrHeader); String val = getHeader(remoteAddrHeader);
if (val != null) return val; if (val != null) return val;
} }
return String.valueOf(getRemoteAddress()); SocketAddress addr = getRemoteAddress();
if (addr == null) return "";
if (addr instanceof InetSocketAddress) return ((InetSocketAddress) addr).getAddress().getHostAddress();
return String.valueOf(addr);
} }
public SocketAddress getRemoteAddress() { public SocketAddress getRemoteAddress() {
@@ -376,6 +379,10 @@ public final class HttpRequest extends Request {
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
public String[] getHeaderNames() {
return header.getNames();
}
public String getHeader(String name) { public String getHeader(String name) {
return header.getValue(name); return header.getValue(name);
} }
@@ -414,6 +421,11 @@ public final class HttpRequest extends Request {
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
public String[] getParameterNames() {
parseBody();
return params.getNames();
}
public String getParameter(String name) { public String getParameter(String name) {
parseBody(); parseBody();
return params.getValue(name); return params.getValue(name);

View File

@@ -1048,6 +1048,16 @@ public final class DataJDBCSource implements DataSource {
return getSingleResult(ReckonType.COUNT, entityClass, null, bean); return getSingleResult(ReckonType.COUNT, entityClass, null, bean);
} }
@Override
public Number getCountDistinctSingleResult(final Class entityClass, String column) {
return getCountDistinctSingleResult(entityClass, column, null);
}
@Override
public Number getCountDistinctSingleResult(final Class entityClass, String column, FilterBean bean) {
return getSingleResult(ReckonType.COUNT, entityClass, true, column, bean);
}
//-----------------------------AVG----------------------------- //-----------------------------AVG-----------------------------
@Override @Override
public Number getAvgSingleResult(final Class entityClass, final String column) { public Number getAvgSingleResult(final Class entityClass, final String column) {
@@ -1060,11 +1070,15 @@ public final class DataJDBCSource implements DataSource {
} }
private <T> Number getSingleResult(final ReckonType type, final Class<T> entityClass, final String column, FilterBean bean) { private <T> Number getSingleResult(final ReckonType type, final Class<T> entityClass, final String column, FilterBean bean) {
return getSingleResult(type, entityClass, false, column, bean);
}
private <T> Number getSingleResult(final ReckonType type, final Class<T> entityClass, final boolean distinct, final String column, FilterBean bean) {
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final EntityXInfo<T> info = EntityXInfo.load(this, entityClass); final EntityXInfo<T> info = EntityXInfo.load(this, entityClass);
final String sql = "SELECT " + type.getReckonColumn("a." + column) + " FROM " + info.getTable() + " a" + createWhereExpression(info, null, bean); final String sql = "SELECT " + type.getReckonColumn((distinct ? "DISTINCT " : "") + "a." + column) + " FROM " + info.getTable() + " a" + createWhereExpression(info, null, bean);
if (debug.get()) logger.finest(entityClass.getSimpleName() + " single sql=" + sql); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(entityClass.getSimpleName() + " single sql=" + sql);
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
Number rs = null; Number rs = null;
ResultSet set = prestmt.executeQuery(); ResultSet set = prestmt.executeQuery();
@@ -1104,7 +1118,7 @@ public final class DataJDBCSource implements DataSource {
} }
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
if (debug.get()) logger.finest(clazz.getSimpleName() + " find sql=" + info.query.sql.replace("?", String.valueOf(pk))); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + info.query.sql.replace("?", String.valueOf(pk)));
final PreparedStatement prestmt = conn.prepareStatement(info.query.sql); final PreparedStatement prestmt = conn.prepareStatement(info.query.sql);
prestmt.setObject(1, pk); prestmt.setObject(1, pk);
T rs = null; T rs = null;
@@ -1178,7 +1192,7 @@ public final class DataJDBCSource implements DataSource {
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = ? AND " + info.getSQLColumn(column2) + " = ?"; final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column1) + " = ? AND " + info.getSQLColumn(column2) + " = ?";
if (debug.get()) logger.finest(clazz.getSimpleName() + " find sql=" + sql.replaceFirst("\\?", String.valueOf(key1)).replaceFirst("\\?", String.valueOf(key2))); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " find sql=" + sql.replaceFirst("\\?", String.valueOf(key1)).replaceFirst("\\?", String.valueOf(key2)));
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
prestmt.setObject(1, key1); prestmt.setObject(1, key1);
prestmt.setObject(2, key2); prestmt.setObject(2, key2);
@@ -1269,7 +1283,7 @@ public final class DataJDBCSource implements DataSource {
final Connection conn = createReadSQLConnection(); final Connection conn = createReadSQLConnection();
try { try {
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + sqlcolumn + " = ?"; final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + sqlcolumn + " = ?";
if (debug.get()) logger.finest(clazz.getSimpleName() + " query sql=" + sql); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " query sql=" + sql);
final PreparedStatement prestmt = conn.prepareStatement(sql); final PreparedStatement prestmt = conn.prepareStatement(sql);
T[] rs = (T[]) Array.newInstance(clazz, keys.length); T[] rs = (T[]) Array.newInstance(clazz, keys.length);
for (int i = 0; i < keys.length; i++) { for (int i = 0; i < keys.length; i++) {
@@ -1327,7 +1341,7 @@ public final class DataJDBCSource implements DataSource {
try { try {
final List<V> list = new ArrayList(); final List<V> list = new ArrayList();
final String sql = "SELECT " + info.getSQLColumn(selectedColumn) + " FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column) + " = ?"; final String sql = "SELECT " + info.getSQLColumn(selectedColumn) + " FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column) + " = ?";
if (debug.get()) logger.finest(clazz.getSimpleName() + " query sql=" + sql.replaceFirst("\\?", String.valueOf(key))); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " query sql=" + sql.replaceFirst("\\?", String.valueOf(key)));
final PreparedStatement ps = conn.prepareStatement(sql); final PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, key); ps.setObject(1, key);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1451,7 +1465,7 @@ public final class DataJDBCSource implements DataSource {
final SelectColumn sels = selects; final SelectColumn sels = selects;
final List<T> list = new ArrayList(); final List<T> list = new ArrayList();
final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column) + " " + express.value() + " ?"; final String sql = "SELECT * FROM " + info.getTable() + " WHERE " + info.getSQLColumn(column) + " " + express.value() + " ?";
if (debug.get()) logger.finest(clazz.getSimpleName() + " query sql=" + sql); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " query sql=" + sql);
final PreparedStatement ps = conn.prepareStatement(sql); final PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, key); ps.setObject(1, key);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
@@ -1550,7 +1564,7 @@ public final class DataJDBCSource implements DataSource {
final SelectColumn sels = selects; final SelectColumn sels = selects;
final List<T> list = new ArrayList(); final List<T> list = new ArrayList();
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + createWhereExpression(info, flipper, bean); final String sql = "SELECT a.* FROM " + info.getTable() + " a" + createWhereExpression(info, flipper, bean);
if (debug.get()) logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize()))); if (debug.get() && info.isLoggable(Level.FINEST)) logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.index() + "," + flipper.getSize())));
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
if (flipper != null && flipper.index() > 0) set.absolute(flipper.index()); if (flipper != null && flipper.index() > 0) set.absolute(flipper.index());
@@ -1914,6 +1928,8 @@ public final class DataJDBCSource implements DataSource {
//表字段与字段名是否全部一致 //表字段与字段名是否全部一致
private final boolean same; private final boolean same;
private final int logLevel;
private class ActionInfo { private class ActionInfo {
final String sql; final String sql;
@@ -1937,6 +1953,9 @@ public final class DataJDBCSource implements DataSource {
DistributeTables dt = type.getAnnotation(DistributeTables.class); DistributeTables dt = type.getAnnotation(DistributeTables.class);
this.distributeTables = dt == null ? null : dt.value(); this.distributeTables = dt == null ? null : dt.value();
LogLevel ll = type.getAnnotation(LogLevel.class);
this.logLevel = ll == null ? Integer.MIN_VALUE : Level.parse(ll.value()).intValue();
Class cltmp = type; Class cltmp = type;
Set<String> fields = new HashSet<>(); Set<String> fields = new HashSet<>();
boolean auto = false; boolean auto = false;
@@ -2037,6 +2056,10 @@ public final class DataJDBCSource implements DataSource {
} }
} }
public boolean isLoggable(Level l) {
return l.intValue() >= this.logLevel;
}
public T createInstance() { public T createInstance() {
return inner.getCreator().create(); return inner.getCreator().create();
} }
@@ -2086,7 +2109,7 @@ public final class DataJDBCSource implements DataSource {
MAX, MIN, SUM, COUNT, AVG; MAX, MIN, SUM, COUNT, AVG;
public String getReckonColumn(String col) { public String getReckonColumn(String col) {
if (this == COUNT) return this.name() + "(*)"; if (this == COUNT && !col.contains("DISTINCT")) return this.name() + "(*)";
return this.name() + "(" + col + ")"; return this.name() + "(" + col + ")";
} }
} }

View File

@@ -49,6 +49,16 @@ final class DataJPASource implements DataSource {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override
public Number getCountDistinctSingleResult(Class entityClass, String column) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Number getCountDistinctSingleResult(Class entityClass, String column, FilterBean bean) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class DataJPAConnection extends DataConnection { private static class DataJPAConnection extends DataConnection {
private final EntityManager manager; private final EntityManager manager;

View File

@@ -243,6 +243,10 @@ public interface DataSource {
public Number getCountSingleResult(final Class entityClass, FilterBean bean); public Number getCountSingleResult(final Class entityClass, FilterBean bean);
public Number getCountDistinctSingleResult(final Class entityClass, String column);
public Number getCountDistinctSingleResult(final Class entityClass, String column, FilterBean bean);
//-----------------------------AVG----------------------------- //-----------------------------AVG-----------------------------
public Number getAvgSingleResult(final Class entityClass, final String column); public Number getAvgSingleResult(final Class entityClass, final String column);

View File

@@ -73,8 +73,10 @@ final class FilterInfo<T extends FilterBean> {
fields.add(field.getName()); fields.add(field.getName());
FilterItem item = new FilterItem(field, "a", null); FilterItem item = new FilterItem(field, "a", null);
FilterJoinColumn joinCol = field.getAnnotation(FilterJoinColumn.class); FilterJoinColumn joinCol = field.getAnnotation(FilterJoinColumn.class);
boolean again = true;
if (joinCol != null) { if (joinCol != null) {
if (!joinTables.containsKey(joinCol.table())) { if (!joinTables.containsKey(joinCol.table())) {
again = false;
joinTables.put(joinCol.table(), String.valueOf((char) ('a' + (++index)))); joinTables.put(joinCol.table(), String.valueOf((char) ('a' + (++index))));
} }
String alias = joinTables.get(joinCol.table()); String alias = joinTables.get(joinCol.table());
@@ -87,9 +89,11 @@ final class FilterInfo<T extends FilterBean> {
} }
item = new FilterItem(field, alias, cache); item = new FilterItem(field, alias, cache);
EntityInfo secinfo = EntityInfo.load(joinCol.table(), null); EntityInfo secinfo = EntityInfo.load(joinCol.table(), null);
joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable()).append(" ").append(alias) if (!again) {
.append(" ON a.# = ").append(alias).append(".") joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable())
.append(joinCol.column().isEmpty() ? secinfo.getPrimary().field() : joinCol.column()); .append(" ").append(alias).append(" ON a.# = ").append(alias).append(".")
.append(joinCol.column().isEmpty() ? secinfo.getPrimary().field() : joinCol.column());
}
} }
getters.put(field.getName(), item); getters.put(field.getName(), item);
FilterGroup[] refs = field.getAnnotationsByType(FilterGroup.class); FilterGroup[] refs = field.getAnnotationsByType(FilterGroup.class);

View File

@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.wentch.redkale.util;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* 被标记的日志级别以上的才会被记录
*
* @author zhangjx
*/
@Inherited
@Documented
@Target({TYPE})
@Retention(RUNTIME)
public @interface LogLevel {
String value();
}