• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision22 (tree)
Time2015-08-17 15:37:24
Authort_nakayama1971

Log Message

(empty log message)

Change Summary

Incremental Difference

--- branches/framework/20111016/src/online/jndi/lookup/tomcat/S2TransactionManagerJNDILookup.java (nonexistent)
+++ branches/framework/20111016/src/online/jndi/lookup/tomcat/S2TransactionManagerJNDILookup.java (revision 22)
@@ -0,0 +1,29 @@
1+package online.jndi.lookup.tomcat;
2+
3+import org.hibernate.transaction.JNDITransactionManagerLookup;
4+
5+/**
6+ * TransactionManagerJNDILookupクラス
7+ *
8+ * @author Tadashi Nakayama
9+ * @version 1.0.0
10+ */
11+public final class S2TransactionManagerJNDILookup extends JNDITransactionManagerLookup {
12+
13+ /**
14+ * @see org.hibernate.transaction.JNDITransactionManagerLookup#getName()
15+ */
16+ @Override
17+ protected String getName() {
18+ return "java:comp/env/TransactionManager";
19+ }
20+
21+ /**
22+ * @see org.hibernate.transaction.TransactionManagerLookup#getUserTransactionName()
23+ */
24+ @Override
25+ public String getUserTransactionName() {
26+ return "java:comp/UserTransaction";
27+ }
28+
29+}
--- branches/framework/20111016/src/common/db/dao/dialect/PostgreSQLNoWaitDialect.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/dialect/PostgreSQLNoWaitDialect.java (revision 22)
@@ -0,0 +1,40 @@
1+package common.db.dao.dialect;
2+
3+import java.sql.Types;
4+
5+import org.hibernate.dialect.PostgreSQLDialect;
6+import org.hibernate.exception.SQLExceptionConverter;
7+import org.hibernate.type.StringType;
8+
9+/**
10+ * Postgres用Dialect
11+ *
12+ * @author Tadashi Nakayama
13+ * @version 1.0.0
14+ */
15+public final class PostgreSQLNoWaitDialect extends PostgreSQLDialect {
16+
17+ /**
18+ * コンストラクタ
19+ */
20+ public PostgreSQLNoWaitDialect() {
21+ registerHibernateType(Types.CHAR, new StringType().getName());
22+ registerHibernateType(Types.OTHER, new StringType().getName());
23+ }
24+
25+ /**
26+ * @see org.hibernate.dialect.Dialect#getForUpdateNowaitString()
27+ */
28+ @Override
29+ public String getForUpdateNowaitString() {
30+ return "for update nowait";
31+ }
32+
33+ /**
34+ * @see org.hibernate.dialect.Dialect#buildSQLExceptionConverter()
35+ */
36+ @Override
37+ public SQLExceptionConverter buildSQLExceptionConverter() {
38+ return new PostgreSQLStateConverter(getViolatedConstraintNameExtracter());
39+ }
40+}
--- branches/framework/20111016/src/common/db/dao/dialect/PostgreSQLStateConverter.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/dialect/PostgreSQLStateConverter.java (revision 22)
@@ -0,0 +1,48 @@
1+package common.db.dao.dialect;
2+
3+import java.sql.SQLException;
4+
5+import org.hibernate.JDBCException;
6+import org.hibernate.exception.JDBCExceptionHelper;
7+import org.hibernate.exception.LockAcquisitionException;
8+import org.hibernate.exception.SQLStateConverter;
9+import org.hibernate.exception.ViolatedConstraintNameExtracter;
10+
11+/**
12+ * Postgres用SQLStateConverter
13+ *
14+ * @author Tadashi Nakayama
15+ * @version 1.0.0
16+ */
17+public final class PostgreSQLStateConverter extends SQLStateConverter {
18+
19+ /**
20+ * コンストラクタ
21+ *
22+ * @param extracter ViolatedConstraintNameExtracter
23+ */
24+ public PostgreSQLStateConverter(final ViolatedConstraintNameExtracter extracter) {
25+ super(extracter);
26+ }
27+
28+ /**
29+ * @see org.hibernate.exception.SQLStateConverter
30+ * #handledNonSpecificException(java.sql.SQLException, java.lang.String, java.lang.String)
31+ */
32+ @Override
33+ protected JDBCException handledNonSpecificException(
34+ final SQLException ex, final String msg, final String sql) {
35+ String state = JDBCExceptionHelper.extractSqlState(ex);
36+ if (state != null) {
37+ // NOWAIT
38+ if ("55P03".equals(state)) {
39+ return new LockAcquisitionException(msg, ex, sql);
40+ }
41+ // DEADLOCK
42+ if ("40P01".equals(state)) {
43+ return new LockAcquisitionException(msg, ex, sql);
44+ }
45+ }
46+ return super.handledNonSpecificException(ex, msg, sql);
47+ }
48+}
--- branches/framework/20111016/src/common/db/dao/DaoSession.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/DaoSession.java (revision 22)
@@ -0,0 +1,168 @@
1+package common.db.dao;
2+
3+import java.util.Properties;
4+import java.util.concurrent.ConcurrentHashMap;
5+import java.util.concurrent.ConcurrentMap;
6+
7+import org.apache.commons.logging.LogFactory;
8+import org.hibernate.HibernateException;
9+import org.hibernate.SessionFactory;
10+import org.hibernate.cfg.Configuration;
11+import org.hibernate.cfg.Environment;
12+import org.hibernate.dialect.Dialect;
13+
14+import common.transaction.TransactionUtil;
15+
16+import core.exception.PhysicalException;
17+import core.exception.ThrowableUtil;
18+
19+/**
20+ * DAOセションクラス
21+ *
22+ * @author Tadashi Nakayama
23+ * @version 1.0.0
24+ */
25+final class DaoSession {
26+
27+ /** 設定保存マップ */
28+ private static final ConcurrentMap<String, Config> CONFIG =
29+ new ConcurrentHashMap<String, Config>();
30+ /** インスタンス */
31+ private static final DaoSession INSTANCE = new DaoSession();
32+
33+ /**
34+ * コンストラクタ
35+ *
36+ */
37+ private DaoSession() {
38+ if (INSTANCE != null) {
39+ throw new AssertionError();
40+ }
41+ }
42+
43+ /**
44+ * ファクトリメソッド
45+ *
46+ * @return インスタンス
47+ */
48+ public static DaoSession getInstance() {
49+ return INSTANCE;
50+ }
51+
52+ /**
53+ * DAO作成
54+ *
55+ * @param cname 設定ファイル名
56+ * @return DAOオブジェクト
57+ */
58+ public Dao getDao(final String cname) {
59+ try {
60+ BaseDao bd = null;
61+ Config config = getConfig(cname);
62+ Properties prop = config.getConfiguration().getProperties();
63+ if (prop != null) {
64+ String factory = prop.getProperty(Environment.TRANSACTION_STRATEGY);
65+ if ("org.hibernate.transaction.CMTTransactionFactory".equals(factory)) {
66+ bd = new JtaDao(config);
67+ bd.setNoWait(isNoWaitAccess(prop));
68+ } else if ("org.hibernate.transaction.JTATransactionFactory".equals(factory)) {
69+ if (TransactionUtil.isInTransaction()) {
70+ bd = new JtaDao(config);
71+ } else {
72+ bd = new MainDao(config);
73+ }
74+ bd.setNoWait(isNoWaitAccess(prop));
75+ } else {
76+ bd = new MainDao(config);
77+ }
78+ }
79+ return bd;
80+ } catch (HibernateException ex) {
81+ ThrowableUtil.error(LogFactory.getLog(DaoSession.class), ex);
82+ throw new PhysicalException(ex);
83+ }
84+ }
85+
86+ /**
87+ * 設定取得
88+ *
89+ * @param cname 設定名
90+ * @return 設定
91+ */
92+ private Config getConfig(final String cname) {
93+ String key = cname;
94+ if (key == null) {
95+ key = "/hibernate.cfg.xml";
96+ }
97+
98+ Config config = CONFIG.get(key);
99+ if (config == null) {
100+ Configuration cfg = new Configuration();
101+ cfg.configure(key);
102+
103+ config = new Config(cfg, cfg.buildSessionFactory());
104+ if (CONFIG.putIfAbsent(key, config) != null) {
105+ config = CONFIG.get(key);
106+ }
107+ }
108+ return config;
109+ }
110+
111+ /**
112+ * NO WAIT アクセス判断
113+ * @param prop Properties
114+ * @return NO WAIT アクセスの場合 true を返す。
115+ */
116+ private boolean isNoWaitAccess(final Properties prop) {
117+ return Boolean.valueOf(prop.getProperty("no_wait_access")).booleanValue();
118+ }
119+
120+ /**
121+ * 環境保持
122+ * @author Tadashi Nakayama
123+ * @version 1.0.0
124+ */
125+ static final class Config {
126+ /** Configuration */
127+ private final Configuration config;
128+ /** SessionFactory */
129+ private final SessionFactory factory;
130+ /** Dialect */
131+ private final Dialect dialect;
132+
133+ /**
134+ * コンストラクタ
135+ * @param cfg Configuration
136+ * @param sf SessionFactory
137+ */
138+ public Config(final Configuration cfg, final SessionFactory sf) {
139+ this.config = cfg;
140+ this.factory = sf;
141+ this.dialect = Dialect.getDialect(this.config.getProperties());
142+ }
143+
144+ /**
145+ * Configuration取得
146+ * @return Configuration
147+ */
148+ public Configuration getConfiguration() {
149+ return this.config;
150+ }
151+
152+ /**
153+ * SessionFactory取得
154+ * @return SessionFactory
155+ */
156+ public SessionFactory getSessionFactory() {
157+ return this.factory;
158+ }
159+
160+ /**
161+ * Dialect取得
162+ * @return Dialect
163+ */
164+ public Dialect getDialect() {
165+ return this.dialect;
166+ }
167+ }
168+}
--- branches/framework/20111016/src/common/db/dao/MainDao.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/MainDao.java (revision 22)
@@ -0,0 +1,249 @@
1+package common.db.dao;
2+
3+import java.io.Serializable;
4+import java.sql.Connection;
5+import java.sql.SQLException;
6+import java.util.Map;
7+import java.util.concurrent.ConcurrentHashMap;
8+import java.util.concurrent.ConcurrentMap;
9+
10+import org.apache.commons.logging.Log;
11+import org.apache.commons.logging.LogFactory;
12+import org.hibernate.HibernateException;
13+import org.hibernate.JDBCException;
14+import org.hibernate.PropertyValueException;
15+import org.hibernate.Session;
16+import org.hibernate.Transaction;
17+import org.hibernate.exception.ConstraintViolationException;
18+import org.hibernate.exception.LockAcquisitionException;
19+import org.hibernate.jdbc.Work;
20+
21+import common.db.dao.DaoSession.Config;
22+
23+import core.exception.PhysicalException;
24+import core.exception.ThrowableUtil;
25+
26+/**
27+ * バッチ用DAO
28+ *
29+ * @author Tadashi Nakayama
30+ * @version 1.0.0
31+ */
32+public class MainDao extends BaseDao {
33+ /** ログ出力用クラス */
34+ static final Log LOGGER = LogFactory.getLog(MainDao.class);
35+
36+ /** シーケンスクエリキャッシュ */
37+ private final ConcurrentMap<String, SequenceWork> sequence =
38+ new ConcurrentHashMap<String, SequenceWork>();
39+
40+ /** セションオブジェクト */
41+ private Session sess = null;
42+ /** トランザクション */
43+ private boolean tran = false;
44+
45+ /**
46+ * コンストラクタ
47+ *
48+ * @param config コンフィグ
49+ */
50+ protected MainDao(final Config config) {
51+ super(config);
52+ }
53+
54+ /**
55+ * コミット処理
56+ *
57+ */
58+ @Override
59+ public final void commit() {
60+ if (this.sess != null && this.tran) {
61+ flush();
62+ try {
63+ this.sess.getTransaction().commit();
64+ this.sess.clear();
65+ } catch (HibernateException ex) {
66+ ThrowableUtil.error(LOGGER, ex);
67+ throw new PhysicalException(ex);
68+ } finally {
69+ this.tran = false;
70+ clearSequence();
71+ }
72+ }
73+ }
74+
75+ /**
76+ * ロールバック処理
77+ *
78+ */
79+ @Override
80+ public final void rollback() {
81+ if (this.sess != null && this.tran) {
82+ try {
83+ this.sess.getTransaction().rollback();
84+ this.sess.clear();
85+ } catch (HibernateException ex) {
86+ ThrowableUtil.error(LOGGER, ex);
87+ throw new PhysicalException(ex);
88+ } finally {
89+ this.tran = false;
90+ clearSequence();
91+ }
92+ }
93+ }
94+
95+ /**
96+ * フラッシュ処理
97+ *
98+ * @return フラッシュされた場合 true を返す。対象なしの場合 false を返す。
99+ */
100+ @Override
101+ public final boolean flush() {
102+ try {
103+ if (this.sess != null) {
104+ this.sess.flush();
105+ this.sess.clear();
106+ return true;
107+ }
108+ return false;
109+ } catch (LockAcquisitionException ex) {
110+ LOGGER.info(ex.getMessage());
111+ throw new DaoLockException(ex, super.isNoWait());
112+ } catch (ConstraintViolationException ex) {
113+ LOGGER.info(ex.getMessage());
114+ throw new DaoConstraintException(ex, super.isNoWait());
115+ } catch (PropertyValueException ex) {
116+ // not null例外
117+ LOGGER.info(ex.getMessage());
118+ throw new DaoPropertyException(ex);
119+ } catch (JDBCException ex) {
120+ ThrowableUtil.error(LOGGER, ex);
121+ throw new PhysicalException(ex);
122+ } catch (HibernateException ex) {
123+ if (super.isUpdateFailed(ex)) {
124+ LOGGER.info(ex.getMessage());
125+ return false;
126+ }
127+ ThrowableUtil.error(LOGGER, ex);
128+ throw new PhysicalException(ex);
129+ }
130+ }
131+
132+ /**
133+ * セションクローズ
134+ *
135+ */
136+ @Override
137+ public final void close() {
138+ // コミットまたはロールバック済みなら処理されない。
139+ rollback();
140+
141+ if (this.sess != null) {
142+ try {
143+ this.sess.close();
144+ } catch (HibernateException e) {
145+ LOGGER.warn(e.getMessage(), e);
146+ } finally {
147+ this.sess = null;
148+ clearSequence();
149+ }
150+ }
151+ }
152+
153+ /**
154+ * @see common.db.dao.Dao#sequence(java.lang.String)
155+ */
156+ @Override
157+ public final long sequence(final String name) {
158+ SequenceWork sw = this.sequence.get(name);
159+ if (sw == null) {
160+ sw = new SequenceWork(getSequenceNextValString(name));
161+ if (this.sequence.putIfAbsent(name, sw) != null) {
162+ sw = this.sequence.get(name);
163+ }
164+ }
165+
166+ Session session = getSession();
167+ beginTransaction();
168+ session.doWork(sw);
169+ return sw.getSequence();
170+ }
171+
172+ /**
173+ * シーケンスマップクリア
174+ */
175+ private void clearSequence() {
176+ for (final Map.Entry<String, SequenceWork> me : this.sequence.entrySet()) {
177+ me.getValue().close();
178+ }
179+ this.sequence.clear();
180+ }
181+
182+ /**
183+ * セッション取得
184+ *
185+ * @return セションオブジェクト
186+ */
187+ @Override
188+ protected final Session getSession() {
189+ if (this.sess == null) {
190+ this.sess = super.getSessionFactory().openSession();
191+ }
192+
193+ return this.sess;
194+ }
195+
196+ /**
197+ * トランザクション開始
198+ *
199+ * @return トランザクション
200+ */
201+ @Override
202+ protected final Transaction beginTransaction() {
203+ this.tran = true;
204+ return getSession().beginTransaction();
205+ }
206+
207+ /**
208+ * フラッシュ処理
209+ *
210+ * @param session セションオブジェクト
211+ */
212+ @Override
213+ protected final void flushSession(final Session session, final Serializable item) {
214+ // 処理無し。
215+ }
216+
217+ /**
218+ * 実行クラスラップ
219+ *
220+ * @author Tadashi Nakayama
221+ * @version 1.0.0
222+ */
223+ static final class WorkWrap implements Work {
224+ /** 実行クラス */
225+ private final JdbcWork work;
226+
227+ /**
228+ * コンストラクタ
229+ *
230+ * @param val 実行クラス
231+ */
232+ WorkWrap(final JdbcWork val) {
233+ this.work = val;
234+ }
235+
236+ /**
237+ * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
238+ */
239+ @Override
240+ public void execute(final Connection conn) throws SQLException {
241+ try {
242+ this.work.execute(conn);
243+ } catch (SQLException ex) {
244+ ThrowableUtil.error(LOGGER, ex);
245+ throw new PhysicalException(ex);
246+ }
247+ }
248+ }
249+}
--- branches/framework/20111016/src/common/db/dao/provider/DaoConnectionProvider.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/provider/DaoConnectionProvider.java (revision 22)
@@ -0,0 +1,59 @@
1+package common.db.dao.provider;
2+
3+import java.sql.Connection;
4+import java.sql.SQLException;
5+import java.util.Properties;
6+
7+import org.hibernate.connection.ConnectionProvider;
8+
9+import common.db.jdbc.JdbcFactory;
10+
11+/**
12+ * コネクション提供クラス
13+ * @author Tadashi Nakayama
14+ */
15+public final class DaoConnectionProvider implements ConnectionProvider {
16+
17+ /** 接続名 */
18+ private String name;
19+
20+ /**
21+ * @see org.hibernate.connection.ConnectionProvider#close()
22+ */
23+ @Override
24+ public void close() {
25+ return;
26+ }
27+
28+ /**
29+ * @see org.hibernate.connection.ConnectionProvider#closeConnection(java.sql.Connection)
30+ */
31+ @Override
32+ public void closeConnection(final Connection arg0) throws SQLException {
33+ arg0.close();
34+ }
35+
36+ /**
37+ * @see org.hibernate.connection.ConnectionProvider#configure(java.util.Properties)
38+ */
39+ @Override
40+ public void configure(final Properties arg0) {
41+ this.name = arg0.getProperty("resource_name");
42+ }
43+
44+ /**
45+ * @see org.hibernate.connection.ConnectionProvider#getConnection()
46+ */
47+ @Override
48+ public Connection getConnection() throws SQLException {
49+ return JdbcFactory.newConnection(this.name);
50+ }
51+
52+ /**
53+ * @see org.hibernate.connection.ConnectionProvider#supportsAggressiveRelease()
54+ */
55+ @Override
56+ public boolean supportsAggressiveRelease() {
57+ return false;
58+ }
59+}
--- branches/framework/20111016/src/common/db/dao/DaoUtil.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/DaoUtil.java (revision 22)
@@ -0,0 +1,408 @@
1+package common.db.dao;
2+
3+import java.io.Serializable;
4+import java.lang.reflect.Method;
5+import java.util.ArrayList;
6+import java.util.List;
7+
8+import javassist.CannotCompileException;
9+import javassist.ClassClassPath;
10+import javassist.ClassPool;
11+import javassist.CtClass;
12+import javassist.NotFoundException;
13+import javassist.bytecode.AnnotationsAttribute;
14+import javassist.bytecode.ClassFile;
15+import javassist.bytecode.annotation.Annotation;
16+import javassist.bytecode.annotation.BooleanMemberValue;
17+import javassist.bytecode.annotation.StringMemberValue;
18+
19+import javax.persistence.Column;
20+import javax.persistence.EmbeddedId;
21+import javax.persistence.GeneratedValue;
22+import javax.persistence.Id;
23+import javax.persistence.SequenceGenerator;
24+import javax.persistence.Table;
25+
26+import org.apache.commons.logging.Log;
27+import org.apache.commons.logging.LogFactory;
28+import org.hibernate.annotations.Entity;
29+
30+import core.config.Factory;
31+import core.exception.PhysicalException;
32+import core.util.CamelCase;
33+
34+/**
35+ * DAO共通クラス
36+ *
37+ * @author Tadashi Nakayama
38+ * @version 1.0.0
39+ */
40+public final class DaoUtil {
41+ /** ログ出力用クラス */
42+ private static final Log LOGGER = LogFactory.getLog(DaoUtil.class);
43+
44+ /** 主キー取得メソッド */
45+ private static final String PKEY_GET = "getId";
46+
47+ /**
48+ * コンストラクタ
49+ *
50+ */
51+ private DaoUtil() {
52+ throw new AssertionError();
53+ }
54+
55+ /**
56+ * シーケンス名取得
57+ * @param <T> ジェネリクス
58+ * @param cls エンティティクラス
59+ * @return シーケンス名
60+ */
61+ public static <T> String getSequenceName(final Class<T> cls) {
62+ Method m = Factory.getMethod(cls, PKEY_GET);
63+ if (m != null) {
64+ GeneratedValue gv = m.getAnnotation(GeneratedValue.class);
65+ if (gv != null && gv.generator() != null) {
66+ SequenceGenerator sg = m.getAnnotation(SequenceGenerator.class);
67+ if (sg != null && gv.generator().equals(sg.name())) {
68+ return sg.sequenceName();
69+ }
70+ }
71+ }
72+ return "";
73+ }
74+
75+ /**
76+ * 複合キー確認
77+ * @param cls エンティティクラス
78+ * @return 複合キークラスの場合 true を返す。
79+ */
80+ public static boolean isEmbeddedId(final Class<?> cls) {
81+ Method m = Factory.getMethod(cls, PKEY_GET);
82+ return (m != null && m.getAnnotation(EmbeddedId.class) != null);
83+ }
84+
85+ /**
86+ * 動的作成確認
87+ * @param cls テーブルクラス
88+ * @return 動的作成の場合 true を返す。
89+ */
90+ public static boolean isDynamicInsert(final Class<?> cls) {
91+ Entity ent = cls.getAnnotation(Entity.class);
92+ return (ent != null && ent.dynamicInsert());
93+ }
94+
95+ /**
96+ * 動的更新確認
97+ * @param cls テーブルクラス
98+ * @return 動的更新の場合 true を返す。
99+ */
100+ public static boolean isDynamicUpdate(final Class<?> cls) {
101+ Entity ent = cls.getAnnotation(Entity.class);
102+ return (ent != null && ent.dynamicUpdate());
103+ }
104+
105+ /**
106+ * テーブル名取得
107+ * @param cls モデルクラス
108+ * @return テーブル名
109+ */
110+ public static String getTableName(final Class<?> cls) {
111+ Table tbl = cls.getAnnotation(Table.class);
112+ if (tbl != null) {
113+ return tbl.name();
114+ }
115+ return "";
116+ }
117+
118+ /**
119+ * カラム名取得
120+ * @param m メソッド
121+ * @return カラム名
122+ */
123+ public static String getColumnName(final Method m) {
124+ Column col = m.getAnnotation(Column.class);
125+ if (col != null) {
126+ return col.name();
127+ }
128+ return "";
129+ }
130+
131+ /**
132+ * Null可確認
133+ * @param m メソッド
134+ * @return Null可の場合 true を返す。
135+ */
136+ public static boolean isNullable(final Method m) {
137+ Column col = m.getAnnotation(Column.class);
138+ return (col != null && col.nullable());
139+ }
140+
141+ /**
142+ * Id確認
143+ * @param m メソッド
144+ * @return Idの場合 true を返す。
145+ */
146+ public static boolean isId(final Method m) {
147+ return (m.getAnnotation(Id.class) != null);
148+ }
149+
150+ /**
151+ * バイナリ拡張クラスパス追加
152+ * @param cls 追加用クラスパス保持クラス
153+ */
154+ public static void addClassPath(final Class<?> cls) {
155+ ClassPool pool = ClassPool.getDefault();
156+ pool.appendClassPath(new ClassClassPath(cls));
157+ }
158+
159+ /**
160+ * エンティティクラス拡張
161+ * @param <T> ジェネリクス
162+ * @param cls 基底クラス
163+ * @param table テーブル名
164+ * @return 拡張クラス
165+ */
166+ public static <T extends Serializable> Class<T> extend(
167+ final Class<T> cls, final String table) {
168+ String name = cls.getPackage().getName() + "." + CamelCase.convert(table);
169+
170+ Class<?> c = Factory.loadClass(name);
171+ if (c != null) {
172+ return Factory.cast(c);
173+ }
174+
175+ try {
176+ ClassPool pool = ClassPool.getDefault();
177+ CtClass ct = pool.makeClass(name, pool.get(cls.getName()));
178+
179+ ClassFile cf = ct.getClassFile2();
180+ AnnotationsAttribute attr = new AnnotationsAttribute(
181+ cf.getConstPool(), AnnotationsAttribute.visibleTag);
182+ attr.addAnnotation(getJpaTable(cf, table));
183+ Entity anno = cls.getAnnotation(Entity.class);
184+ if (anno != null) {
185+ attr.addAnnotation(getHbmEntry(cf, anno));
186+ }
187+ ct.setAttribute(AnnotationsAttribute.visibleTag, attr.get());
188+
189+ return Factory.cast(ct.toClass(cls.getClassLoader(), null));
190+
191+ } catch (NotFoundException e) {
192+ LOGGER.error(e.getMessage(), e);
193+ throw new PhysicalException(e);
194+ } catch (CannotCompileException e) {
195+ LOGGER.error(e.getMessage(), e);
196+ throw new PhysicalException(e);
197+ }
198+ }
199+
200+ /**
201+ * JPA Tableアノテーション取得
202+ * @param cf ClassFile
203+ * @param table テーブル名
204+ * @return アノテーション
205+ */
206+ private static Annotation getJpaTable(final ClassFile cf, final String table) {
207+ Annotation anno = new Annotation(Table.class.getName(), cf.getConstPool());
208+ anno.addMemberValue("name", new StringMemberValue(table, cf.getConstPool()));
209+ return anno;
210+ }
211+
212+ /**
213+ * Entityアノテーション取得
214+ * @param cf ClassFile
215+ * @param entity Entityアノテーション
216+ * @return アノテーション
217+ */
218+ private static Annotation getHbmEntry(final ClassFile cf, final Entity entity) {
219+ Annotation anno = new Annotation(Entity.class.getName(), cf.getConstPool());
220+ anno.addMemberValue("dynamicInsert", new BooleanMemberValue(
221+ entity.dynamicInsert(), cf.getConstPool()));
222+ anno.addMemberValue("dynamicUpdate", new BooleanMemberValue(
223+ entity.dynamicUpdate(), cf.getConstPool()));
224+ return anno;
225+ }
226+
227+ /**
228+ * 数字文字列バインド変数化
229+ * @param val クエリ
230+ * @return 数字文字列バインド変数クエリ
231+ */
232+ public static String toStringBind(final String val) {
233+ int start = 0;
234+ StringBuilder sb = new StringBuilder();
235+ for (int loc = val.indexOf("?"), bind = 1;
236+ 0 <= loc; loc = val.indexOf("?", loc + 1), bind++) {
237+ sb.append(val.substring(start, loc));
238+ sb.append(":" + bind);
239+ start = loc + 1;
240+ }
241+ sb.append(val.substring(start));
242+ return sb.toString();
243+ }
244+
245+ /**
246+ * 値取得
247+ *
248+ * @param obj DAOモデル
249+ * @param name 設定項目名
250+ * @return 取得値
251+ */
252+ public static Object getValue(final Object obj, final String name) {
253+ if (obj != null) {
254+ return Factory.invoke(obj, Factory.getMethod(obj.getClass(), "get" + name));
255+ }
256+ return null;
257+ }
258+
259+ /**
260+ * オブジェクトが指定アイテム名のセッター、ゲッターを持っているか
261+ *
262+ * @param cls DAOモデルクラス
263+ * @param name 指定アイテム
264+ * @return 持っている場合 true を返す。
265+ */
266+ public static boolean hasItem(final Class<?> cls, final String name) {
267+ for (final String item : getModelNames(cls)) {
268+ if (item.equals(name)) {
269+ return true;
270+ }
271+ }
272+ return false;
273+ }
274+
275+ /**
276+ * DAOモデルの主キー以外の同じ項目名値をコピーします。
277+ * @param <T> ジェネリクス
278+ * @param orig コピー元DAOモデル
279+ * @param dest コピー先DAOモデル
280+ */
281+ public static <T extends Serializable> void duplicate(final T dest, final T orig) {
282+ if (orig != null && dest != null) {
283+ for (final Method m : orig.getClass().getMethods()) {
284+ if (m.getName().startsWith("get") && !isId(m)
285+ && !Object.class.equals(m.getDeclaringClass())
286+ && !m.isBridge() && !m.isSynthetic()) {
287+ Method mt = Factory.getMethod(dest.getClass(),
288+ "set" + m.getName().substring(3), m.getReturnType());
289+ Factory.invoke(dest, mt, Factory.invoke(orig, m));
290+ }
291+ }
292+ }
293+ }
294+
295+ /**
296+ * 同一内容の値を持ったオブジェクトかを判断する。
297+ * @param <T> ジェネリクス
298+ * @param o1 DAOモデル
299+ * @param o2 DAOモデル
300+ * @return 同一内容の値を持ったオブジェクトの場合 true を返す
301+ */
302+ public static <T extends Serializable> boolean isSame(final T o1, final T o2) {
303+ if (o1 != o2) {
304+ if (o1 == null || o2 == null || o1.getClass() != o2.getClass()) {
305+ return false;
306+ }
307+
308+ for (final Method mt : o1.getClass().getMethods()) {
309+ if (mt.getName().startsWith("get")
310+ && !Object.class.equals(mt.getDeclaringClass())
311+ && !mt.isBridge() && !mt.isSynthetic()) {
312+ String name = mt.getName().substring(3);
313+ Object v1 = getValue(o1, name);
314+ Object v2 = getValue(o2, name);
315+ if (v1 != v2 && (v1 == null || !v1.equals(v2))) {
316+ return false;
317+ }
318+ }
319+ }
320+ }
321+ return true;
322+ }
323+
324+ /**
325+ * DAOモデルクラスからDB項目名を配列で返却する。
326+ *
327+ * @param cls DAOモデルクラス
328+ * @return DB項目名配列
329+ */
330+ public static String[] getDBNames(final Class<?> cls) {
331+ List<String> ret = new ArrayList<String>();
332+ for (final Method mt : cls.getMethods()) {
333+ String mname = mt.getName();
334+ if (mname.startsWith("get") && !isId(mt)
335+ && !Object.class.equals(mt.getDeclaringClass())
336+ && !mt.isBridge() && !mt.isSynthetic()) {
337+ ret.add(toDBName(mname.substring(3)));
338+ }
339+ }
340+ return ret.toArray(new String[ret.size()]);
341+ }
342+
343+ /**
344+ * DAOモデルクラスから項目名を配列で返却する。
345+ *
346+ * @param cls DAOモデルクラス
347+ * @return 項目名配列
348+ */
349+ public static String[] getModelNames(final Class<?> cls) {
350+ List<String> ret = new ArrayList<String>();
351+ for (final Method mt : cls.getMethods()) {
352+ String mname = mt.getName();
353+ if (mname.startsWith("get") && !isId(mt)
354+ && !Object.class.equals(mt.getDeclaringClass())
355+ && !mt.isBridge() && !mt.isSynthetic()) {
356+ ret.add(mname.substring(3));
357+ }
358+ }
359+ return ret.toArray(new String[ret.size()]);
360+ }
361+
362+ /**
363+ * モデル項目名取得
364+ *
365+ * @param cols DBカラム名配列
366+ * @return モデル項目名配列
367+ */
368+ public static String[] toDBNames(final String... cols) {
369+ String[] ret = new String[0];
370+ if (cols != null) {
371+ ret = new String[cols.length];
372+ for (int i = 0; i < cols.length; i++) {
373+ ret[i] = toDBName(cols[i]);
374+ }
375+ }
376+ return ret;
377+ }
378+
379+ /**
380+ * DB名称取得
381+ *
382+ * @param name 項目名称
383+ * @return DB名称
384+ */
385+ public static String toDBName(final String name) {
386+ StringBuilder sb = new StringBuilder();
387+ for (int i = 0; name != null && i < name.length(); i = name.offsetByCodePoints(i, 1)) {
388+ int c = name.codePointAt(i);
389+ if ('A' <= c && c <= 'Z') {
390+ if (0 < sb.length()) {
391+ sb.append('_');
392+ }
393+ } else if ('a' <= c && c <= 'z') {
394+ sb.appendCodePoint((c - ('a' - 'A')));
395+ continue;
396+ } else if ('0' <= c && c <= '9') {
397+ if (0 < i) {
398+ int pre = name.codePointBefore(i);
399+ if (pre < '0' || '9' < pre) {
400+ sb.append('_');
401+ }
402+ }
403+ }
404+ sb.appendCodePoint(c);
405+ }
406+ return sb.toString();
407+ }
408+}
--- branches/framework/20111016/src/common/db/dao/JtaDao.java (nonexistent)
+++ branches/framework/20111016/src/common/db/dao/JtaDao.java (revision 22)
@@ -0,0 +1,149 @@
1+package common.db.dao;
2+
3+import java.io.Serializable;
4+
5+import org.apache.commons.logging.Log;
6+import org.apache.commons.logging.LogFactory;
7+import org.hibernate.HibernateException;
8+import org.hibernate.JDBCException;
9+import org.hibernate.PropertyValueException;
10+import org.hibernate.Session;
11+import org.hibernate.Transaction;
12+import org.hibernate.exception.ConstraintViolationException;
13+import org.hibernate.exception.LockAcquisitionException;
14+
15+import common.db.dao.DaoSession.Config;
16+
17+import core.exception.PhysicalException;
18+import core.exception.ThrowableUtil;
19+
20+
21+/**
22+ * 汎用DAO
23+ *
24+ * @author Tadashi Nakayama
25+ * @version 1.0.0
26+ */
27+public class JtaDao extends BaseDao {
28+ /** ログ出力用クラス */
29+ private static final Log LOGGER = LogFactory.getLog(JtaDao.class);
30+
31+ /**
32+ * コンストラクタ
33+ *
34+ * @param config コンフィグ
35+ */
36+ protected JtaDao(final Config config) {
37+ super(config);
38+ setNoWait(true);
39+ }
40+
41+ /**
42+ * コミット処理
43+ *
44+ */
45+ @Override
46+ public final void commit() {
47+ // 処理無し。
48+ }
49+
50+ /**
51+ * ロールバック処理
52+ *
53+ */
54+ @Override
55+ public final void rollback() {
56+ // 処理無し。
57+ }
58+
59+ /**
60+ * フラッシュ処理
61+ *
62+ * @return フラッシュされた場合 true を返す。対象なしの場合 false を返す。
63+ */
64+ @Override
65+ public final boolean flush() {
66+ try {
67+ getSession().flush();
68+ return true;
69+ } catch (LockAcquisitionException ex) {
70+ LOGGER.info(ex.getMessage());
71+ throw new DaoLockException(ex, super.isNoWait());
72+ } catch (ConstraintViolationException ex) {
73+ LOGGER.info(ex.getMessage());
74+ throw new DaoConstraintException(ex, super.isNoWait());
75+ } catch (PropertyValueException ex) {
76+ // not null例外
77+ LOGGER.info(ex.getMessage());
78+ throw new DaoPropertyException(ex);
79+ } catch (JDBCException ex) {
80+ ThrowableUtil.error(LOGGER, ex);
81+ throw new PhysicalException(ex);
82+ } catch (HibernateException ex) {
83+ if (super.isUpdateFailed(ex)) {
84+ LOGGER.info(ex.getMessage());
85+ return false;
86+ }
87+ ThrowableUtil.error(LOGGER, ex);
88+ throw new PhysicalException(ex);
89+ }
90+ }
91+
92+ /**
93+ * セションクローズ
94+ *
95+ */
96+ @Override
97+ public final void close() {
98+ if (0 < getSession().getStatistics().getEntityCount()) {
99+ getSession().clear();
100+ }
101+ }
102+
103+ /**
104+ * @see common.db.dao.Dao#sequence(java.lang.String)
105+ */
106+ @Override
107+ public final long sequence(final String name) {
108+ Session session = getSession();
109+ beginTransaction();
110+ SequenceWork sw = new SequenceWork(super.getSequenceNextValString(name));
111+ try {
112+ session.doWork(sw);
113+ return sw.getSequence();
114+ } finally {
115+ sw.close();
116+ }
117+ }
118+
119+ /**
120+ * セッション取得
121+ *
122+ * @return セションオブジェクト
123+ */
124+ @Override
125+ protected final Session getSession() {
126+ return super.getSessionFactory().getCurrentSession();
127+ }
128+
129+ /**
130+ * トランザクション開始
131+ *
132+ * @return トランザクション
133+ */
134+ @Override
135+ protected final Transaction beginTransaction() {
136+ return getSession().beginTransaction();
137+ }
138+
139+ /**
140+ * フラッシュ処理
141+ *
142+ * @param session セションオブジェクト
143+ */
144+ @Override
145+ protected final void flushSession(final Session session, final Serializable item) {
146+ session.flush();
147+ session.evict(item);
148+ }
149+}
--- branches/framework/test/branches/20111016/src/common/db/dao/DaoUtil.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/DaoUtil.java (nonexistent)
@@ -1,408 +0,0 @@
1-package common.db.dao;
2-
3-import java.io.Serializable;
4-import java.lang.reflect.Method;
5-import java.util.ArrayList;
6-import java.util.List;
7-
8-import javassist.CannotCompileException;
9-import javassist.ClassClassPath;
10-import javassist.ClassPool;
11-import javassist.CtClass;
12-import javassist.NotFoundException;
13-import javassist.bytecode.AnnotationsAttribute;
14-import javassist.bytecode.ClassFile;
15-import javassist.bytecode.annotation.Annotation;
16-import javassist.bytecode.annotation.BooleanMemberValue;
17-import javassist.bytecode.annotation.StringMemberValue;
18-
19-import javax.persistence.Column;
20-import javax.persistence.EmbeddedId;
21-import javax.persistence.GeneratedValue;
22-import javax.persistence.Id;
23-import javax.persistence.SequenceGenerator;
24-import javax.persistence.Table;
25-
26-import org.apache.commons.logging.Log;
27-import org.apache.commons.logging.LogFactory;
28-import org.hibernate.annotations.Entity;
29-
30-import core.config.Factory;
31-import core.exception.PhysicalException;
32-import core.util.CamelCase;
33-
34-/**
35- * DAO共通クラス
36- *
37- * @author Tadashi Nakayama
38- * @version 1.0.0
39- */
40-public final class DaoUtil {
41- /** ログ出力用クラス */
42- private static final Log LOGGER = LogFactory.getLog(DaoUtil.class);
43-
44- /** 主キー取得メソッド */
45- private static final String PKEY_GET = "getId";
46-
47- /**
48- * コンストラクタ
49- *
50- */
51- private DaoUtil() {
52- throw new AssertionError();
53- }
54-
55- /**
56- * シーケンス名取得
57- * @param <T> ジェネリクス
58- * @param cls エンティティクラス
59- * @return シーケンス名
60- */
61- public static <T> String getSequenceName(final Class<T> cls) {
62- Method m = Factory.getMethod(cls, PKEY_GET);
63- if (m != null) {
64- GeneratedValue gv = m.getAnnotation(GeneratedValue.class);
65- if (gv != null && gv.generator() != null) {
66- SequenceGenerator sg = m.getAnnotation(SequenceGenerator.class);
67- if (sg != null && gv.generator().equals(sg.name())) {
68- return sg.sequenceName();
69- }
70- }
71- }
72- return "";
73- }
74-
75- /**
76- * 複合キー確認
77- * @param cls エンティティクラス
78- * @return 複合キークラスの場合 true を返す。
79- */
80- public static boolean isEmbeddedId(final Class<?> cls) {
81- Method m = Factory.getMethod(cls, PKEY_GET);
82- return (m != null && m.getAnnotation(EmbeddedId.class) != null);
83- }
84-
85- /**
86- * 動的作成確認
87- * @param cls テーブルクラス
88- * @return 動的作成の場合 true を返す。
89- */
90- public static boolean isDynamicInsert(final Class<?> cls) {
91- Entity ent = cls.getAnnotation(Entity.class);
92- return (ent != null && ent.dynamicInsert());
93- }
94-
95- /**
96- * 動的更新確認
97- * @param cls テーブルクラス
98- * @return 動的更新の場合 true を返す。
99- */
100- public static boolean isDynamicUpdate(final Class<?> cls) {
101- Entity ent = cls.getAnnotation(Entity.class);
102- return (ent != null && ent.dynamicUpdate());
103- }
104-
105- /**
106- * テーブル名取得
107- * @param cls モデルクラス
108- * @return テーブル名
109- */
110- public static String getTableName(final Class<?> cls) {
111- Table tbl = cls.getAnnotation(Table.class);
112- if (tbl != null) {
113- return tbl.name();
114- }
115- return "";
116- }
117-
118- /**
119- * カラム名取得
120- * @param m メソッド
121- * @return カラム名
122- */
123- public static String getColumnName(final Method m) {
124- Column col = m.getAnnotation(Column.class);
125- if (col != null) {
126- return col.name();
127- }
128- return "";
129- }
130-
131- /**
132- * Null可確認
133- * @param m メソッド
134- * @return Null可の場合 true を返す。
135- */
136- public static boolean isNullable(final Method m) {
137- Column col = m.getAnnotation(Column.class);
138- return (col != null && col.nullable());
139- }
140-
141- /**
142- * Id確認
143- * @param m メソッド
144- * @return Idの場合 true を返す。
145- */
146- public static boolean isId(final Method m) {
147- return (m.getAnnotation(Id.class) != null);
148- }
149-
150- /**
151- * バイナリ拡張クラスパス追加
152- * @param cls 追加用クラスパス保持クラス
153- */
154- public static void addClassPath(final Class<?> cls) {
155- ClassPool pool = ClassPool.getDefault();
156- pool.appendClassPath(new ClassClassPath(cls));
157- }
158-
159- /**
160- * エンティティクラス拡張
161- * @param <T> ジェネリクス
162- * @param cls 基底クラス
163- * @param table テーブル名
164- * @return 拡張クラス
165- */
166- public static <T extends Serializable> Class<T> extend(
167- final Class<T> cls, final String table) {
168- String name = cls.getPackage().getName() + "." + CamelCase.convert(table);
169-
170- Class<?> c = Factory.loadClass(name);
171- if (c != null) {
172- return Factory.cast(c);
173- }
174-
175- try {
176- ClassPool pool = ClassPool.getDefault();
177- CtClass ct = pool.makeClass(name, pool.get(cls.getName()));
178-
179- ClassFile cf = ct.getClassFile2();
180- AnnotationsAttribute attr = new AnnotationsAttribute(
181- cf.getConstPool(), AnnotationsAttribute.visibleTag);
182- attr.addAnnotation(getJpaTable(cf, table));
183- Entity anno = cls.getAnnotation(Entity.class);
184- if (anno != null) {
185- attr.addAnnotation(getHbmEntry(cf, anno));
186- }
187- ct.setAttribute(AnnotationsAttribute.visibleTag, attr.get());
188-
189- return Factory.cast(ct.toClass(cls.getClassLoader(), null));
190-
191- } catch (NotFoundException e) {
192- LOGGER.error(e.getMessage(), e);
193- throw new PhysicalException(e);
194- } catch (CannotCompileException e) {
195- LOGGER.error(e.getMessage(), e);
196- throw new PhysicalException(e);
197- }
198- }
199-
200- /**
201- * JPA Tableアノテーション取得
202- * @param cf ClassFile
203- * @param table テーブル名
204- * @return アノテーション
205- */
206- private static Annotation getJpaTable(final ClassFile cf, final String table) {
207- Annotation anno = new Annotation(Table.class.getName(), cf.getConstPool());
208- anno.addMemberValue("name", new StringMemberValue(table, cf.getConstPool()));
209- return anno;
210- }
211-
212- /**
213- * Entityアノテーション取得
214- * @param cf ClassFile
215- * @param entity Entityアノテーション
216- * @return アノテーション
217- */
218- private static Annotation getHbmEntry(final ClassFile cf, final Entity entity) {
219- Annotation anno = new Annotation(Entity.class.getName(), cf.getConstPool());
220- anno.addMemberValue("dynamicInsert", new BooleanMemberValue(
221- entity.dynamicInsert(), cf.getConstPool()));
222- anno.addMemberValue("dynamicUpdate", new BooleanMemberValue(
223- entity.dynamicUpdate(), cf.getConstPool()));
224- return anno;
225- }
226-
227- /**
228- * 数字文字列バインド変数化
229- * @param val クエリ
230- * @return 数字文字列バインド変数クエリ
231- */
232- public static String toStringBind(final String val) {
233- int start = 0;
234- StringBuilder sb = new StringBuilder();
235- for (int loc = val.indexOf("?"), bind = 1;
236- 0 <= loc; loc = val.indexOf("?", loc + 1), bind++) {
237- sb.append(val.substring(start, loc));
238- sb.append(":" + bind);
239- start = loc + 1;
240- }
241- sb.append(val.substring(start));
242- return sb.toString();
243- }
244-
245- /**
246- * 値取得
247- *
248- * @param obj DAOモデル
249- * @param name 設定項目名
250- * @return 取得値
251- */
252- public static Object getValue(final Object obj, final String name) {
253- if (obj != null) {
254- return Factory.invoke(obj, Factory.getMethod(obj.getClass(), "get" + name));
255- }
256- return null;
257- }
258-
259- /**
260- * オブジェクトが指定アイテム名のセッター、ゲッターを持っているか
261- *
262- * @param cls DAOモデルクラス
263- * @param name 指定アイテム
264- * @return 持っている場合 true を返す。
265- */
266- public static boolean hasItem(final Class<?> cls, final String name) {
267- for (final String item : getModelNames(cls)) {
268- if (item.equals(name)) {
269- return true;
270- }
271- }
272- return false;
273- }
274-
275- /**
276- * DAOモデルの主キー以外の同じ項目名値をコピーします。
277- * @param <T> ジェネリクス
278- * @param orig コピー元DAOモデル
279- * @param dest コピー先DAOモデル
280- */
281- public static <T extends Serializable> void duplicate(final T dest, final T orig) {
282- if (orig != null && dest != null) {
283- for (final Method m : orig.getClass().getMethods()) {
284- if (m.getName().startsWith("get") && !isId(m)
285- && !Object.class.equals(m.getDeclaringClass())
286- && !m.isBridge() && !m.isSynthetic()) {
287- Method mt = Factory.getMethod(dest.getClass(),
288- "set" + m.getName().substring(3), m.getReturnType());
289- Factory.invoke(dest, mt, Factory.invoke(orig, m));
290- }
291- }
292- }
293- }
294-
295- /**
296- * 同一内容の値を持ったオブジェクトかを判断する。
297- * @param <T> ジェネリクス
298- * @param o1 DAOモデル
299- * @param o2 DAOモデル
300- * @return 同一内容の値を持ったオブジェクトの場合 true を返す
301- */
302- public static <T extends Serializable> boolean isSame(final T o1, final T o2) {
303- if (o1 != o2) {
304- if (o1 == null || o2 == null || o1.getClass() != o2.getClass()) {
305- return false;
306- }
307-
308- for (final Method mt : o1.getClass().getMethods()) {
309- if (mt.getName().startsWith("get")
310- && !Object.class.equals(mt.getDeclaringClass())
311- && !mt.isBridge() && !mt.isSynthetic()) {
312- String name = mt.getName().substring(3);
313- Object v1 = getValue(o1, name);
314- Object v2 = getValue(o2, name);
315- if (v1 != v2 && (v1 == null || !v1.equals(v2))) {
316- return false;
317- }
318- }
319- }
320- }
321- return true;
322- }
323-
324- /**
325- * DAOモデルクラスからDB項目名を配列で返却する。
326- *
327- * @param cls DAOモデルクラス
328- * @return DB項目名配列
329- */
330- public static String[] getDBNames(final Class<?> cls) {
331- List<String> ret = new ArrayList<String>();
332- for (final Method mt : cls.getMethods()) {
333- String mname = mt.getName();
334- if (mname.startsWith("get") && !isId(mt)
335- && !Object.class.equals(mt.getDeclaringClass())
336- && !mt.isBridge() && !mt.isSynthetic()) {
337- ret.add(toDBName(mname.substring(3)));
338- }
339- }
340- return ret.toArray(new String[ret.size()]);
341- }
342-
343- /**
344- * DAOモデルクラスから項目名を配列で返却する。
345- *
346- * @param cls DAOモデルクラス
347- * @return 項目名配列
348- */
349- public static String[] getModelNames(final Class<?> cls) {
350- List<String> ret = new ArrayList<String>();
351- for (final Method mt : cls.getMethods()) {
352- String mname = mt.getName();
353- if (mname.startsWith("get") && !isId(mt)
354- && !Object.class.equals(mt.getDeclaringClass())
355- && !mt.isBridge() && !mt.isSynthetic()) {
356- ret.add(mname.substring(3));
357- }
358- }
359- return ret.toArray(new String[ret.size()]);
360- }
361-
362- /**
363- * モデル項目名取得
364- *
365- * @param cols DBカラム名配列
366- * @return モデル項目名配列
367- */
368- public static String[] toDBNames(final String... cols) {
369- String[] ret = new String[0];
370- if (cols != null) {
371- ret = new String[cols.length];
372- for (int i = 0; i < cols.length; i++) {
373- ret[i] = toDBName(cols[i]);
374- }
375- }
376- return ret;
377- }
378-
379- /**
380- * DB名称取得
381- *
382- * @param name 項目名称
383- * @return DB名称
384- */
385- public static String toDBName(final String name) {
386- StringBuilder sb = new StringBuilder();
387- for (int i = 0; name != null && i < name.length(); i = name.offsetByCodePoints(i, 1)) {
388- int c = name.codePointAt(i);
389- if ('A' <= c && c <= 'Z') {
390- if (0 < sb.length()) {
391- sb.append('_');
392- }
393- } else if ('a' <= c && c <= 'z') {
394- sb.appendCodePoint((c - ('a' - 'A')));
395- continue;
396- } else if ('0' <= c && c <= '9') {
397- if (0 < i) {
398- int pre = name.codePointBefore(i);
399- if (pre < '0' || '9' < pre) {
400- sb.append('_');
401- }
402- }
403- }
404- sb.appendCodePoint(c);
405- }
406- return sb.toString();
407- }
408-}
--- branches/framework/test/branches/20111016/src/common/db/dao/JtaDao.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/JtaDao.java (nonexistent)
@@ -1,149 +0,0 @@
1-package common.db.dao;
2-
3-import java.io.Serializable;
4-
5-import org.apache.commons.logging.Log;
6-import org.apache.commons.logging.LogFactory;
7-import org.hibernate.HibernateException;
8-import org.hibernate.JDBCException;
9-import org.hibernate.PropertyValueException;
10-import org.hibernate.Session;
11-import org.hibernate.Transaction;
12-import org.hibernate.exception.ConstraintViolationException;
13-import org.hibernate.exception.LockAcquisitionException;
14-
15-import common.db.dao.DaoSession.Config;
16-
17-import core.exception.PhysicalException;
18-import core.exception.ThrowableUtil;
19-
20-
21-/**
22- * 汎用DAO
23- *
24- * @author Tadashi Nakayama
25- * @version 1.0.0
26- */
27-public class JtaDao extends BaseDao {
28- /** ログ出力用クラス */
29- private static final Log LOGGER = LogFactory.getLog(JtaDao.class);
30-
31- /**
32- * コンストラクタ
33- *
34- * @param config コンフィグ
35- */
36- protected JtaDao(final Config config) {
37- super(config);
38- setNoWait(true);
39- }
40-
41- /**
42- * コミット処理
43- *
44- */
45- @Override
46- public final void commit() {
47- // 処理無し。
48- }
49-
50- /**
51- * ロールバック処理
52- *
53- */
54- @Override
55- public final void rollback() {
56- // 処理無し。
57- }
58-
59- /**
60- * フラッシュ処理
61- *
62- * @return フラッシュされた場合 true を返す。対象なしの場合 false を返す。
63- */
64- @Override
65- public final boolean flush() {
66- try {
67- getSession().flush();
68- return true;
69- } catch (LockAcquisitionException ex) {
70- LOGGER.info(ex.getMessage());
71- throw new DaoLockException(ex, super.isNoWait());
72- } catch (ConstraintViolationException ex) {
73- LOGGER.info(ex.getMessage());
74- throw new DaoConstraintException(ex, super.isNoWait());
75- } catch (PropertyValueException ex) {
76- // not null例外
77- LOGGER.info(ex.getMessage());
78- throw new DaoPropertyException(ex);
79- } catch (JDBCException ex) {
80- ThrowableUtil.error(LOGGER, ex);
81- throw new PhysicalException(ex);
82- } catch (HibernateException ex) {
83- if (super.isUpdateFailed(ex)) {
84- LOGGER.info(ex.getMessage());
85- return false;
86- }
87- ThrowableUtil.error(LOGGER, ex);
88- throw new PhysicalException(ex);
89- }
90- }
91-
92- /**
93- * セションクローズ
94- *
95- */
96- @Override
97- public final void close() {
98- if (0 < getSession().getStatistics().getEntityCount()) {
99- getSession().clear();
100- }
101- }
102-
103- /**
104- * @see common.db.dao.Dao#sequence(java.lang.String)
105- */
106- @Override
107- public final long sequence(final String name) {
108- Session session = getSession();
109- beginTransaction();
110- SequenceWork sw = new SequenceWork(super.getSequenceNextValString(name));
111- try {
112- session.doWork(sw);
113- return sw.getSequence();
114- } finally {
115- sw.close();
116- }
117- }
118-
119- /**
120- * セッション取得
121- *
122- * @return セションオブジェクト
123- */
124- @Override
125- protected final Session getSession() {
126- return super.getSessionFactory().getCurrentSession();
127- }
128-
129- /**
130- * トランザクション開始
131- *
132- * @return トランザクション
133- */
134- @Override
135- protected final Transaction beginTransaction() {
136- return getSession().beginTransaction();
137- }
138-
139- /**
140- * フラッシュ処理
141- *
142- * @param session セションオブジェクト
143- */
144- @Override
145- protected final void flushSession(final Session session, final Serializable item) {
146- session.flush();
147- session.evict(item);
148- }
149-}
--- branches/framework/test/branches/20111016/src/common/db/dao/dialect/PostgreSQLNoWaitDialect.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/dialect/PostgreSQLNoWaitDialect.java (nonexistent)
@@ -1,40 +0,0 @@
1-package common.db.dao.dialect;
2-
3-import java.sql.Types;
4-
5-import org.hibernate.dialect.PostgreSQLDialect;
6-import org.hibernate.exception.SQLExceptionConverter;
7-import org.hibernate.type.StringType;
8-
9-/**
10- * Postgres用Dialect
11- *
12- * @author Tadashi Nakayama
13- * @version 1.0.0
14- */
15-public final class PostgreSQLNoWaitDialect extends PostgreSQLDialect {
16-
17- /**
18- * コンストラクタ
19- */
20- public PostgreSQLNoWaitDialect() {
21- registerHibernateType(Types.CHAR, new StringType().getName());
22- registerHibernateType(Types.OTHER, new StringType().getName());
23- }
24-
25- /**
26- * @see org.hibernate.dialect.Dialect#getForUpdateNowaitString()
27- */
28- @Override
29- public String getForUpdateNowaitString() {
30- return "for update nowait";
31- }
32-
33- /**
34- * @see org.hibernate.dialect.Dialect#buildSQLExceptionConverter()
35- */
36- @Override
37- public SQLExceptionConverter buildSQLExceptionConverter() {
38- return new PostgreSQLStateConverter(getViolatedConstraintNameExtracter());
39- }
40-}
--- branches/framework/test/branches/20111016/src/common/db/dao/dialect/PostgreSQLStateConverter.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/dialect/PostgreSQLStateConverter.java (nonexistent)
@@ -1,48 +0,0 @@
1-package common.db.dao.dialect;
2-
3-import java.sql.SQLException;
4-
5-import org.hibernate.JDBCException;
6-import org.hibernate.exception.JDBCExceptionHelper;
7-import org.hibernate.exception.LockAcquisitionException;
8-import org.hibernate.exception.SQLStateConverter;
9-import org.hibernate.exception.ViolatedConstraintNameExtracter;
10-
11-/**
12- * Postgres用SQLStateConverter
13- *
14- * @author Tadashi Nakayama
15- * @version 1.0.0
16- */
17-public final class PostgreSQLStateConverter extends SQLStateConverter {
18-
19- /**
20- * コンストラクタ
21- *
22- * @param extracter ViolatedConstraintNameExtracter
23- */
24- public PostgreSQLStateConverter(final ViolatedConstraintNameExtracter extracter) {
25- super(extracter);
26- }
27-
28- /**
29- * @see org.hibernate.exception.SQLStateConverter
30- * #handledNonSpecificException(java.sql.SQLException, java.lang.String, java.lang.String)
31- */
32- @Override
33- protected JDBCException handledNonSpecificException(
34- final SQLException ex, final String msg, final String sql) {
35- String state = JDBCExceptionHelper.extractSqlState(ex);
36- if (state != null) {
37- // NOWAIT
38- if ("55P03".equals(state)) {
39- return new LockAcquisitionException(msg, ex, sql);
40- }
41- // DEADLOCK
42- if ("40P01".equals(state)) {
43- return new LockAcquisitionException(msg, ex, sql);
44- }
45- }
46- return super.handledNonSpecificException(ex, msg, sql);
47- }
48-}
--- branches/framework/test/branches/20111016/src/common/db/dao/DaoSession.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/DaoSession.java (nonexistent)
@@ -1,168 +0,0 @@
1-package common.db.dao;
2-
3-import java.util.Properties;
4-import java.util.concurrent.ConcurrentHashMap;
5-import java.util.concurrent.ConcurrentMap;
6-
7-import org.apache.commons.logging.LogFactory;
8-import org.hibernate.HibernateException;
9-import org.hibernate.SessionFactory;
10-import org.hibernate.cfg.Configuration;
11-import org.hibernate.cfg.Environment;
12-import org.hibernate.dialect.Dialect;
13-
14-import common.transaction.TransactionUtil;
15-
16-import core.exception.PhysicalException;
17-import core.exception.ThrowableUtil;
18-
19-/**
20- * DAOセションクラス
21- *
22- * @author Tadashi Nakayama
23- * @version 1.0.0
24- */
25-final class DaoSession {
26-
27- /** 設定保存マップ */
28- private static final ConcurrentMap<String, Config> CONFIG =
29- new ConcurrentHashMap<String, Config>();
30- /** インスタンス */
31- private static final DaoSession INSTANCE = new DaoSession();
32-
33- /**
34- * コンストラクタ
35- *
36- */
37- private DaoSession() {
38- if (INSTANCE != null) {
39- throw new AssertionError();
40- }
41- }
42-
43- /**
44- * ファクトリメソッド
45- *
46- * @return インスタンス
47- */
48- public static DaoSession getInstance() {
49- return INSTANCE;
50- }
51-
52- /**
53- * DAO作成
54- *
55- * @param cname 設定ファイル名
56- * @return DAOオブジェクト
57- */
58- public Dao getDao(final String cname) {
59- try {
60- BaseDao bd = null;
61- Config config = getConfig(cname);
62- Properties prop = config.getConfiguration().getProperties();
63- if (prop != null) {
64- String factory = prop.getProperty(Environment.TRANSACTION_STRATEGY);
65- if ("org.hibernate.transaction.CMTTransactionFactory".equals(factory)) {
66- bd = new JtaDao(config);
67- bd.setNoWait(isNoWaitAccess(prop));
68- } else if ("org.hibernate.transaction.JTATransactionFactory".equals(factory)) {
69- if (TransactionUtil.isInTransaction()) {
70- bd = new JtaDao(config);
71- } else {
72- bd = new MainDao(config);
73- }
74- bd.setNoWait(isNoWaitAccess(prop));
75- } else {
76- bd = new MainDao(config);
77- }
78- }
79- return bd;
80- } catch (HibernateException ex) {
81- ThrowableUtil.error(LogFactory.getLog(DaoSession.class), ex);
82- throw new PhysicalException(ex);
83- }
84- }
85-
86- /**
87- * 設定取得
88- *
89- * @param cname 設定名
90- * @return 設定
91- */
92- private Config getConfig(final String cname) {
93- String key = cname;
94- if (key == null) {
95- key = "/hibernate.cfg.xml";
96- }
97-
98- Config config = CONFIG.get(key);
99- if (config == null) {
100- Configuration cfg = new Configuration();
101- cfg.configure(key);
102-
103- config = new Config(cfg, cfg.buildSessionFactory());
104- if (CONFIG.putIfAbsent(key, config) != null) {
105- config = CONFIG.get(key);
106- }
107- }
108- return config;
109- }
110-
111- /**
112- * NO WAIT アクセス判断
113- * @param prop Properties
114- * @return NO WAIT アクセスの場合 true を返す。
115- */
116- private boolean isNoWaitAccess(final Properties prop) {
117- return Boolean.valueOf(prop.getProperty("no_wait_access")).booleanValue();
118- }
119-
120- /**
121- * 環境保持
122- * @author Tadashi Nakayama
123- * @version 1.0.0
124- */
125- static final class Config {
126- /** Configuration */
127- private final Configuration config;
128- /** SessionFactory */
129- private final SessionFactory factory;
130- /** Dialect */
131- private final Dialect dialect;
132-
133- /**
134- * コンストラクタ
135- * @param cfg Configuration
136- * @param sf SessionFactory
137- */
138- public Config(final Configuration cfg, final SessionFactory sf) {
139- this.config = cfg;
140- this.factory = sf;
141- this.dialect = Dialect.getDialect(this.config.getProperties());
142- }
143-
144- /**
145- * Configuration取得
146- * @return Configuration
147- */
148- public Configuration getConfiguration() {
149- return this.config;
150- }
151-
152- /**
153- * SessionFactory取得
154- * @return SessionFactory
155- */
156- public SessionFactory getSessionFactory() {
157- return this.factory;
158- }
159-
160- /**
161- * Dialect取得
162- * @return Dialect
163- */
164- public Dialect getDialect() {
165- return this.dialect;
166- }
167- }
168-}
--- branches/framework/test/branches/20111016/src/common/db/dao/MainDao.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/MainDao.java (nonexistent)
@@ -1,249 +0,0 @@
1-package common.db.dao;
2-
3-import java.io.Serializable;
4-import java.sql.Connection;
5-import java.sql.SQLException;
6-import java.util.Map;
7-import java.util.concurrent.ConcurrentHashMap;
8-import java.util.concurrent.ConcurrentMap;
9-
10-import org.apache.commons.logging.Log;
11-import org.apache.commons.logging.LogFactory;
12-import org.hibernate.HibernateException;
13-import org.hibernate.JDBCException;
14-import org.hibernate.PropertyValueException;
15-import org.hibernate.Session;
16-import org.hibernate.Transaction;
17-import org.hibernate.exception.ConstraintViolationException;
18-import org.hibernate.exception.LockAcquisitionException;
19-import org.hibernate.jdbc.Work;
20-
21-import common.db.dao.DaoSession.Config;
22-
23-import core.exception.PhysicalException;
24-import core.exception.ThrowableUtil;
25-
26-/**
27- * バッチ用DAO
28- *
29- * @author Tadashi Nakayama
30- * @version 1.0.0
31- */
32-public class MainDao extends BaseDao {
33- /** ログ出力用クラス */
34- static final Log LOGGER = LogFactory.getLog(MainDao.class);
35-
36- /** シーケンスクエリキャッシュ */
37- private final ConcurrentMap<String, SequenceWork> sequence =
38- new ConcurrentHashMap<String, SequenceWork>();
39-
40- /** セションオブジェクト */
41- private Session sess = null;
42- /** トランザクション */
43- private boolean tran = false;
44-
45- /**
46- * コンストラクタ
47- *
48- * @param config コンフィグ
49- */
50- protected MainDao(final Config config) {
51- super(config);
52- }
53-
54- /**
55- * コミット処理
56- *
57- */
58- @Override
59- public final void commit() {
60- if (this.sess != null && this.tran) {
61- flush();
62- try {
63- this.sess.getTransaction().commit();
64- this.sess.clear();
65- } catch (HibernateException ex) {
66- ThrowableUtil.error(LOGGER, ex);
67- throw new PhysicalException(ex);
68- } finally {
69- this.tran = false;
70- clearSequence();
71- }
72- }
73- }
74-
75- /**
76- * ロールバック処理
77- *
78- */
79- @Override
80- public final void rollback() {
81- if (this.sess != null && this.tran) {
82- try {
83- this.sess.getTransaction().rollback();
84- this.sess.clear();
85- } catch (HibernateException ex) {
86- ThrowableUtil.error(LOGGER, ex);
87- throw new PhysicalException(ex);
88- } finally {
89- this.tran = false;
90- clearSequence();
91- }
92- }
93- }
94-
95- /**
96- * フラッシュ処理
97- *
98- * @return フラッシュされた場合 true を返す。対象なしの場合 false を返す。
99- */
100- @Override
101- public final boolean flush() {
102- try {
103- if (this.sess != null) {
104- this.sess.flush();
105- this.sess.clear();
106- return true;
107- }
108- return false;
109- } catch (LockAcquisitionException ex) {
110- LOGGER.info(ex.getMessage());
111- throw new DaoLockException(ex, super.isNoWait());
112- } catch (ConstraintViolationException ex) {
113- LOGGER.info(ex.getMessage());
114- throw new DaoConstraintException(ex, super.isNoWait());
115- } catch (PropertyValueException ex) {
116- // not null例外
117- LOGGER.info(ex.getMessage());
118- throw new DaoPropertyException(ex);
119- } catch (JDBCException ex) {
120- ThrowableUtil.error(LOGGER, ex);
121- throw new PhysicalException(ex);
122- } catch (HibernateException ex) {
123- if (super.isUpdateFailed(ex)) {
124- LOGGER.info(ex.getMessage());
125- return false;
126- }
127- ThrowableUtil.error(LOGGER, ex);
128- throw new PhysicalException(ex);
129- }
130- }
131-
132- /**
133- * セションクローズ
134- *
135- */
136- @Override
137- public final void close() {
138- // コミットまたはロールバック済みなら処理されない。
139- rollback();
140-
141- if (this.sess != null) {
142- try {
143- this.sess.close();
144- } catch (HibernateException e) {
145- LOGGER.warn(e.getMessage(), e);
146- } finally {
147- this.sess = null;
148- clearSequence();
149- }
150- }
151- }
152-
153- /**
154- * @see common.db.dao.Dao#sequence(java.lang.String)
155- */
156- @Override
157- public final long sequence(final String name) {
158- SequenceWork sw = this.sequence.get(name);
159- if (sw == null) {
160- sw = new SequenceWork(getSequenceNextValString(name));
161- if (this.sequence.putIfAbsent(name, sw) != null) {
162- sw = this.sequence.get(name);
163- }
164- }
165-
166- Session session = getSession();
167- beginTransaction();
168- session.doWork(sw);
169- return sw.getSequence();
170- }
171-
172- /**
173- * シーケンスマップクリア
174- */
175- private void clearSequence() {
176- for (final Map.Entry<String, SequenceWork> me : this.sequence.entrySet()) {
177- me.getValue().close();
178- }
179- this.sequence.clear();
180- }
181-
182- /**
183- * セッション取得
184- *
185- * @return セションオブジェクト
186- */
187- @Override
188- protected final Session getSession() {
189- if (this.sess == null) {
190- this.sess = super.getSessionFactory().openSession();
191- }
192-
193- return this.sess;
194- }
195-
196- /**
197- * トランザクション開始
198- *
199- * @return トランザクション
200- */
201- @Override
202- protected final Transaction beginTransaction() {
203- this.tran = true;
204- return getSession().beginTransaction();
205- }
206-
207- /**
208- * フラッシュ処理
209- *
210- * @param session セションオブジェクト
211- */
212- @Override
213- protected final void flushSession(final Session session, final Serializable item) {
214- // 処理無し。
215- }
216-
217- /**
218- * 実行クラスラップ
219- *
220- * @author Tadashi Nakayama
221- * @version 1.0.0
222- */
223- static final class WorkWrap implements Work {
224- /** 実行クラス */
225- private final JdbcWork work;
226-
227- /**
228- * コンストラクタ
229- *
230- * @param val 実行クラス
231- */
232- WorkWrap(final JdbcWork val) {
233- this.work = val;
234- }
235-
236- /**
237- * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
238- */
239- @Override
240- public void execute(final Connection conn) throws SQLException {
241- try {
242- this.work.execute(conn);
243- } catch (SQLException ex) {
244- ThrowableUtil.error(LOGGER, ex);
245- throw new PhysicalException(ex);
246- }
247- }
248- }
249-}
--- branches/framework/test/branches/20111016/src/common/db/dao/provider/DaoConnectionProvider.java (revision 21)
+++ branches/framework/test/branches/20111016/src/common/db/dao/provider/DaoConnectionProvider.java (nonexistent)
@@ -1,59 +0,0 @@
1-package common.db.dao.provider;
2-
3-import java.sql.Connection;
4-import java.sql.SQLException;
5-import java.util.Properties;
6-
7-import org.hibernate.connection.ConnectionProvider;
8-
9-import common.db.jdbc.JdbcFactory;
10-
11-/**
12- * コネクション提供クラス
13- * @author Tadashi Nakayama
14- */
15-public final class DaoConnectionProvider implements ConnectionProvider {
16-
17- /** 接続名 */
18- private String name;
19-
20- /**
21- * @see org.hibernate.connection.ConnectionProvider#close()
22- */
23- @Override
24- public void close() {
25- return;
26- }
27-
28- /**
29- * @see org.hibernate.connection.ConnectionProvider#closeConnection(java.sql.Connection)
30- */
31- @Override
32- public void closeConnection(final Connection arg0) throws SQLException {
33- arg0.close();
34- }
35-
36- /**
37- * @see org.hibernate.connection.ConnectionProvider#configure(java.util.Properties)
38- */
39- @Override
40- public void configure(final Properties arg0) {
41- this.name = arg0.getProperty("resource_name");
42- }
43-
44- /**
45- * @see org.hibernate.connection.ConnectionProvider#getConnection()
46- */
47- @Override
48- public Connection getConnection() throws SQLException {
49- return JdbcFactory.newConnection(this.name);
50- }
51-
52- /**
53- * @see org.hibernate.connection.ConnectionProvider#supportsAggressiveRelease()
54- */
55- @Override
56- public boolean supportsAggressiveRelease() {
57- return false;
58- }
59-}
--- branches/framework/test/branches/20111016/src/online/jndi/lookup/tomcat/S2TransactionManagerJNDILookup.java (revision 21)
+++ branches/framework/test/branches/20111016/src/online/jndi/lookup/tomcat/S2TransactionManagerJNDILookup.java (nonexistent)
@@ -1,29 +0,0 @@
1-package online.jndi.lookup.tomcat;
2-
3-import org.hibernate.transaction.JNDITransactionManagerLookup;
4-
5-/**
6- * TransactionManagerJNDILookupクラス
7- *
8- * @author Tadashi Nakayama
9- * @version 1.0.0
10- */
11-public final class S2TransactionManagerJNDILookup extends JNDITransactionManagerLookup {
12-
13- /**
14- * @see org.hibernate.transaction.JNDITransactionManagerLookup#getName()
15- */
16- @Override
17- protected String getName() {
18- return "java:comp/env/TransactionManager";
19- }
20-
21- /**
22- * @see org.hibernate.transaction.TransactionManagerLookup#getUserTransactionName()
23- */
24- @Override
25- public String getUserTransactionName() {
26- return "java:comp/UserTransaction";
27- }
28-
29-}