svnno****@sourc*****
svnno****@sourc*****
2009年 4月 5日 (日) 00:34:17 JST
Revision: 3111 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3111 Author: j5ik2o Date: 2009-04-05 00:34:17 +0900 (Sun, 05 Apr 2009) Log Message: ----------- リファクタリング Modified Paths: -------------- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/JpaImporter.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMeta.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaFactory.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaReader.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/PropertyMeta.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMeta.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaFactoryImpl.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaReaderImpl.java charon/jiemamy-jpa-importer/trunk/src/test/java/org/jiemamy/composer/importer/JpaImporterTest.java Added Paths: ----------- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMetaFactory.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityClassNotFoundException.java charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/TableMetaFactoryImpl.java -------------- next part -------------- Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/JpaImporter.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/JpaImporter.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/JpaImporter.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -27,8 +27,10 @@ import org.jiemamy.composer.importer.meta.EntityMeta; import org.jiemamy.composer.importer.meta.EntityMetaReader; import org.jiemamy.composer.importer.meta.EntityMetaReaderContext; +import org.jiemamy.composer.importer.meta.impl.EntityClassNotFoundException; import org.jiemamy.composer.importer.meta.impl.EntityMetaFactoryImpl; import org.jiemamy.composer.importer.meta.impl.EntityMetaReaderImpl; +import org.jiemamy.composer.importer.meta.impl.TableMetaFactoryImpl; import org.jiemamy.composer.importer.utils.ClassUtil; import org.jiemamy.composer.importer.utils.CollectionsUtil; import org.jiemamy.model.RootModel; @@ -67,7 +69,7 @@ context.setPackageName(ClassUtil.concatName(config.getRootPackageName(), config.getEntityPackageName())); context.setJavaFileEncoding(config.getJavaFileEncoding()); - context.setEntityMetaFactory(new EntityMetaFactoryImpl()); + context.setEntityMetaFactory(new EntityMetaFactoryImpl(new TableMetaFactoryImpl())); EntityMetaReader entityMetaReader = new EntityMetaReaderImpl(context); @@ -78,6 +80,8 @@ } } catch (IOException e) { e.printStackTrace(); + } catch (EntityClassNotFoundException e) { + e.printStackTrace(); } return false; Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMeta.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMeta.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMeta.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -52,6 +52,11 @@ protected PropertyMeta versionPropertyMeta; + /** + * エンンティクラスを取得する。 + * + * @return エンティティクラス + */ public Class<?> getEntityClass() { return entityClass; } @@ -117,6 +122,11 @@ return false; } + /** + * エンティティクラスを設定する。 + * + * @param entityClass エンティティクラス + */ public void setEntityClass(Class<?> entityClass) { this.entityClass = entityClass; } Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaFactory.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaFactory.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaFactory.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -19,13 +19,14 @@ package org.jiemamy.composer.importer.meta; import org.jiemamy.composer.importer.meta.impl.NonEntityException; +import org.jiemamy.composer.importer.utils.Disposable; /** * {@link EntityMeta}のファクトリインターフェイス * * @author j5ik2o */ -public interface EntityMetaFactory { +public interface EntityMetaFactory extends Disposable { /** * エンティティメタデータを取得する。 Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaReader.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaReader.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaReader.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.List; +import org.jiemamy.composer.importer.meta.impl.EntityClassNotFoundException; + /** * {@link EntityMeta エンティティメタデータ}を読み取るインタフェース。 * @@ -33,6 +35,7 @@ * * @return {@link EntityMeta}のリスト * @throws IOException 入出力が失敗した場合 + * @throws EntityClassNotFoundException エンティティクラスがみつからなかった場合 */ - List<EntityMeta> read() throws IOException; + List<EntityMeta> read() throws IOException, EntityClassNotFoundException; } Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/PropertyMeta.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/PropertyMeta.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/PropertyMeta.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -20,6 +20,8 @@ import java.lang.reflect.Field; +import org.apache.commons.lang.builder.ToStringBuilder; + import org.jiemamy.utils.metadata.ColumnMeta; /** @@ -135,4 +137,9 @@ this.propertyClass = propertyClass; } + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + } Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMeta.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMeta.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMeta.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -1,5 +1,7 @@ package org.jiemamy.composer.importer.meta; +import org.apache.commons.lang.builder.ToStringBuilder; + /** * JPA用のテーブルメタデータクラス。 * @@ -87,4 +89,9 @@ public void setSchema(String schema) { this.schema = schema; } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } } Added: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMetaFactory.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMetaFactory.java (rev 0) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMetaFactory.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -0,0 +1,35 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on Apr 5, 2009 + * + * This file is part of Jiemamy. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.jiemamy.composer.importer.meta; + +/** + * {@link TableMeta}のファクトリクラス。 + * + * @author j5ik2o + */ +public interface TableMetaFactory { + + /** + * テーブルメタデータを作成する。 + * + * @param entityMeta エンティティメタデータ + * @return テーブルメタデータ + */ + TableMeta createTableMeta(EntityMeta entityMeta); +} Property changes on: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/TableMetaFactory.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityClassNotFoundException.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityClassNotFoundException.java (rev 0) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityClassNotFoundException.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -0,0 +1,64 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on Apr 4, 2009 + * + * This file is part of Jiemamy. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.jiemamy.composer.importer.meta.impl; + +/** + * エンティティクラスが見つからなかった場合の例外クラス。 + * + * @author j5ik2o + */ + @ SuppressWarnings("serial") +public class EntityClassNotFoundException extends Exception { + + /** + * インスタンスを生成する。 + * + */ + public EntityClassNotFoundException() { + } + + /** + * インスタンスを生成する。 + * + * @param message メッセージ + */ + public EntityClassNotFoundException(String message) { + super(message); + } + + /** + * インスタンスを生成する。 + * + * @param message メッセージ + * @param cause 原因 + */ + public EntityClassNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + /** + * インスタンスを生成する。 + * + * @param cause 原因 + */ + public EntityClassNotFoundException(Throwable cause) { + super(cause); + } + +} Property changes on: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityClassNotFoundException.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaFactoryImpl.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaFactoryImpl.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaFactoryImpl.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -18,6 +18,8 @@ */ package org.jiemamy.composer.importer.meta.impl; +import java.util.concurrent.ConcurrentHashMap; + import javax.persistence.Entity; import org.apache.commons.lang.StringUtils; @@ -25,6 +27,9 @@ import org.jiemamy.composer.importer.meta.EntityMeta; import org.jiemamy.composer.importer.meta.EntityMetaFactory; +import org.jiemamy.composer.importer.meta.TableMeta; +import org.jiemamy.composer.importer.meta.TableMetaFactory; +import org.jiemamy.composer.importer.utils.CollectionsUtil; /** * {@link EntityMetaFactory}の実装クラス。 @@ -33,6 +38,21 @@ */ public class EntityMetaFactoryImpl implements EntityMetaFactory { + private ConcurrentHashMap<String, EntityMeta> entityMetaCache = CollectionsUtil.newConcurrentHashMap(); + + private TableMetaFactory tableMetaFactory; + + + /** + * インスタンスを生成する。 + * + * @param tableMetaFactory テーブルメタファクトリ + */ + public EntityMetaFactoryImpl(TableMetaFactory tableMetaFactory) { + Validate.notNull(tableMetaFactory); + this.tableMetaFactory = tableMetaFactory; + } + private EntityMeta createEntityMeta(Class<?> entityClass) throws NonEntityException { Validate.notNull(entityClass); Entity entityAnnotation = entityClass.getAnnotation(Entity.class); @@ -42,10 +62,17 @@ EntityMeta entityMeta = new EntityMeta(); doEntityClass(entityMeta, entityClass); doName(entityMeta, entityAnnotation); + doTableMeta(entityMeta); + doPropertyMeta(entityMeta); + //doCustomize(entityMeta); // TODO エンティティクラスからEntityMetaを作成する(残項目あり) return entityMeta; } + public void dispose() { + entityMetaCache.clear(); + } + private void doEntityClass(EntityMeta entityMeta, Class<?> entityClass) { entityMeta.setEntityClass(entityClass); } @@ -58,11 +85,23 @@ entityMeta.setName(entityName); } + private void doPropertyMeta(EntityMeta entityMeta) { + + } + + private void doTableMeta(EntityMeta entityMeta) { + TableMeta tableMeta = tableMetaFactory.createTableMeta(entityMeta); + entityMeta.setTableMeta(tableMeta); + } + public EntityMeta getEntityMeta(Class<?> entityClass) throws NonEntityException { Validate.notNull(entityClass); - // TODO まずキャッシュから探し、なかったら作成して返す - EntityMeta entityMeta = createEntityMeta(entityClass); - return entityMeta; + EntityMeta entityMeta = entityMetaCache.get(entityClass.getName()); + if (entityMeta != null) { + return entityMeta; + } + entityMeta = createEntityMeta(entityClass); + EntityMeta entityMeta2 = entityMetaCache.putIfAbsent(entityClass.getName(), entityMeta); + return entityMeta2 != null ? entityMeta2 : entityMeta; } - } Modified: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaReaderImpl.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaReaderImpl.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaReaderImpl.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -26,6 +26,8 @@ import java.util.ArrayList; import java.util.List; +import javax.persistence.Entity; + import org.apache.commons.lang.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -85,39 +87,48 @@ return urlClassLoader; } - public List<EntityMeta> read() throws IOException { - final List<EntityMeta> entityMetas = CollectionsUtil.newArrayList(); - final String entityFullPackageName = entityMetaReaderContext.getPackageName(); - final ClassLoader classLoader = createClassLoader(); - for (File classPathDir : entityMetaReaderContext.getClassPathDirs()) { - - try { - ClassTraversal.forEach(classPathDir, new ClassHandler() { - - public void processClass(String packageName, String shortClassName) - throws TraversalHandlerException { - if (packageName.equals(entityFullPackageName) == false) { - return; + public List<EntityMeta> read() throws IOException, EntityClassNotFoundException { + final EntityMetaFactory factory = entityMetaReaderContext.getEntityMetaFactory(); + try { + final List<EntityMeta> entityMetas = CollectionsUtil.newArrayList(); + final String entityFullPackageName = entityMetaReaderContext.getPackageName(); + final ClassLoader classLoader = createClassLoader(); + for (File classPathDir : entityMetaReaderContext.getClassPathDirs()) { + + try { + ClassTraversal.forEach(classPathDir, new ClassHandler() { + + public void processClass(String packageName, String shortClassName) + throws TraversalHandlerException { + if (packageName.equals(entityFullPackageName) == false) { + return; + } + try { + String entityClassName = ClassUtil.concatName(packageName, shortClassName); + Class<?> entityClass = ClassLoaderUtil.loadClass(classLoader, entityClassName); + if (entityClass.isAnnotationPresent(Entity.class)) { + EntityMeta entityMeta = factory.getEntityMeta(entityClass); + LOG.debug(LogMarker.DETAIL, entityClassName); + entityMetas.add(entityMeta); + } + } catch (ClassNotFoundException e) { + throw new TraversalHandlerException(e); + } catch (NonEntityException e) { + throw new TraversalHandlerException(e); + } } - try { - String entityClassName = ClassUtil.concatName(packageName, shortClassName); - Class<?> entityClass = ClassLoaderUtil.loadClass(classLoader, entityClassName); - EntityMetaFactory factory = entityMetaReaderContext.getEntityMetaFactory(); - EntityMeta entityMeta = factory.getEntityMeta(entityClass); - LOG.debug(LogMarker.DETAIL, entityClassName); - entityMetas.add(entityMeta); - } catch (ClassNotFoundException e) { - throw new TraversalHandlerException(e); - } catch (NonEntityException e) { - throw new TraversalHandlerException(e); - } - } - }); - } catch (TraversalHandlerException e) { - e.printStackTrace(); + }); + } catch (TraversalHandlerException e) { + throw new IOException(); + } } + if (entityMetas.isEmpty()) { + throw new EntityClassNotFoundException(); + } + return entityMetas; + } finally { + factory.dispose(); } - return entityMetas; } private List<URL> toURLs(List<File> files) throws MalformedURLException { Added: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/TableMetaFactoryImpl.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/TableMetaFactoryImpl.java (rev 0) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/TableMetaFactoryImpl.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -0,0 +1,64 @@ +/* + * Copyright 2007-2009 Jiemamy Project and the Others. + * Created on Apr 5, 2009 + * + * This file is part of Jiemamy. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.jiemamy.composer.importer.meta.impl; + +import javax.persistence.Table; + +import org.apache.commons.lang.StringUtils; + +import org.jiemamy.composer.importer.meta.EntityMeta; +import org.jiemamy.composer.importer.meta.TableMeta; +import org.jiemamy.composer.importer.meta.TableMetaFactory; + +/** + * TODO for junichi + * + * @author junichi + */ +public class TableMetaFactoryImpl implements TableMetaFactory { + + public TableMeta createTableMeta(EntityMeta entityMeta) { + TableMeta tableMeta = new TableMeta(); + String defaultName = fromEntityNameToTableName(entityMeta.getName()); + Table table = entityMeta.getEntityClass().getAnnotation(Table.class); + if (table != null) { + String name = table.name(); + if (StringUtils.isEmpty(name)) { + name = defaultName; + } + tableMeta.setName(name); + String catalog = table.catalog(); + if (!StringUtils.isEmpty(catalog)) { + tableMeta.setCatalog(catalog); + } + String schema = table.schema(); + if (!StringUtils.isEmpty(schema)) { + tableMeta.setSchema(schema); + } + } else { + tableMeta.setName(defaultName); + } + return tableMeta; + } + + public String fromEntityNameToTableName(String entityName) { + String tableName = StringUtils.uncapitalize(entityName); + return tableName; + } +} Property changes on: charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/TableMetaFactoryImpl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: charon/jiemamy-jpa-importer/trunk/src/test/java/org/jiemamy/composer/importer/JpaImporterTest.java =================================================================== --- charon/jiemamy-jpa-importer/trunk/src/test/java/org/jiemamy/composer/importer/JpaImporterTest.java 2009-04-04 14:36:08 UTC (rev 3110) +++ charon/jiemamy-jpa-importer/trunk/src/test/java/org/jiemamy/composer/importer/JpaImporterTest.java 2009-04-04 15:34:17 UTC (rev 3111) @@ -74,7 +74,6 @@ try { buildDir = ResourceUtil.getBuildDir(example.entity.Employee.class).getCanonicalPath(); } catch (ResourceNotFoundException e1) { - // TODO Auto-generated catch block e1.printStackTrace(); }