Search in sources :

Example 1 with Callback

use of org.nutz.lang.util.Callback in project nutz by nutzam.

the class JsonEntityFieldMakerImpl method make.

@Override
public JsonEntityField make(Mirror<?> mirror, final Method method) {
    final JsonField jf = method.getAnnotation(JsonField.class);
    // 忽略方法
    if (null == jf || jf.ignore())
        return null;
    final JsonEntityField[] result = new JsonEntityField[1];
    // 如果有,尝试作新的 Entity
    Callback<Method> whenError = new Callback<Method>() {

        // 给定方法即不是 getter 也不是 setter,靠!玩我!
        public void invoke(Method m) {
            throw Lang.makeThrow(JsonException.class, "JsonField '%s' should be getter/setter pair!", m);
        }
    };
    Callback3<String, Method, Method> whenOk = new Callback3<String, Method, Method>() {

        public void invoke(String name, Method getter, Method setter) {
            // 防止错误
            if (null == getter || null == setter || Strings.isBlank(name)) {
                throw Lang.makeThrow(JsonException.class, "JsonField '%s' should be getter/setter pair!", method);
            }
            // 加入字段表
            JsonEntityField ef = JsonEntityField.eval(Strings.sBlank(jf.value(), name), getter, setter);
            result[0] = ef;
        }
    };
    Mirror.evalGetterSetter(method, whenOk, whenError);
    return result[0];
}
Also used : JsonField(org.nutz.json.JsonField) JsonEntityField(org.nutz.json.entity.JsonEntityField) Callback(org.nutz.lang.util.Callback) Callback3(org.nutz.lang.util.Callback3) Method(java.lang.reflect.Method)

Example 2 with Callback

use of org.nutz.lang.util.Callback in project nutz by nutzam.

the class DaoSupport method setDataSource.

public void setDataSource(DataSource ds, boolean isLazy) {
    if (null != dataSource)
        if (log.isWarnEnabled())
            log.warn("Replaced a running dataSource!");
    dataSource = ds;
    if (expert == null)
        expert = Jdbcs.getExpert(ds);
    log.debug("select expert : " + expert.getClass().getName());
    pojoMaker = new NutPojoMaker(expert);
    meta = new DatabaseMeta();
    final Set<String> keywords = new HashSet<String>(Daos.sql2003Keywords());
    run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            try {
                DatabaseMetaData dmd = conn.getMetaData();
                meta.setProductName(dmd.getDatabaseProductName());
                meta.setVersion(dmd.getDatabaseProductVersion());
                log.debug("JDBC Driver --> " + dmd.getDriverVersion());
                log.debug("JDBC Name   --> " + dmd.getDriverName());
                if (!Strings.isBlank(dmd.getURL()))
                    log.debug("JDBC URL    --> " + dmd.getURL());
                if (dmd.getDriverName().contains("mariadb") || dmd.getDriverName().contains("sqlite")) {
                    log.warn("Auto-select fetch size to Integer.MIN_VALUE, enable for ResultSet Streaming");
                    SqlContext.DEFAULT_FETCH_SIZE = Integer.MIN_VALUE;
                }
                String tmp = dmd.getSQLKeywords();
                if (tmp != null) {
                    for (String keyword : tmp.split(",")) {
                        keywords.add(keyword.toUpperCase());
                    }
                }
                expert.checkDataSource(conn);
            } catch (Exception e) {
                log.info("something wrong when checking DataSource", e);
            }
        }
    });
    if (log.isDebugEnabled())
        log.debug("Database info --> " + meta);
    expert.setKeywords(keywords);
    if (!isLazy) {
        holder = new EntityHolder(this.expert, new Callback<ConnCallback>() {

            public void invoke(ConnCallback obj) {
                run(obj);
            }
        });
        holder.maker = createEntityMaker();
    }
    setRunner(runner);
    setExecutor(executor);
}
Also used : ConnCallback(org.nutz.dao.ConnCallback) Callback(org.nutz.lang.util.Callback) ConnCallback(org.nutz.dao.ConnCallback) NutPojoMaker(org.nutz.dao.impl.sql.NutPojoMaker) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseMeta(org.nutz.dao.DatabaseMeta) HashSet(java.util.HashSet)

Aggregations

Callback (org.nutz.lang.util.Callback)2 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 HashSet (java.util.HashSet)1 ConnCallback (org.nutz.dao.ConnCallback)1 DatabaseMeta (org.nutz.dao.DatabaseMeta)1 NutPojoMaker (org.nutz.dao.impl.sql.NutPojoMaker)1 JsonField (org.nutz.json.JsonField)1 JsonEntityField (org.nutz.json.entity.JsonEntityField)1 Callback3 (org.nutz.lang.util.Callback3)1