svnno****@sourc*****
svnno****@sourc*****
2009年 4月 4日 (土) 23:17:27 JST
Revision: 3105 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3105 Author: j5ik2o Date: 2009-04-04 23:17:27 +0900 (Sat, 04 Apr 2009) Log Message: ----------- リファクタリング Modified Paths: -------------- 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/impl/EntityMetaReaderImpl.java -------------- next part -------------- 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:17:06 UTC (rev 3104) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMeta.java 2009-04-04 14:17:27 UTC (rev 3105) @@ -23,7 +23,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; -import org.jiemamy.composer.importer.exception.PropertyNotFoundException; +import org.jiemamy.composer.importer.utils.PropertyNotFoundException; import org.jiemamy.utils.CollectionsUtil; /** @@ -33,6 +33,9 @@ */ public class EntityMeta { + /** エンティティクラス */ + protected Class<?> entityClass; + /** エンティティ名 */ protected String name; @@ -49,6 +52,10 @@ protected PropertyMeta versionPropertyMeta; + public Class<?> getEntityClass() { + return entityClass; + } + /** * IDプロパティメタのリストを取得する。 * @@ -110,6 +117,10 @@ return false; } + public void setEntityClass(Class<?> entityClass) { + this.entityClass = entityClass; + } + /** * IDプロパティメタのリストを設定する。 * 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:17:06 UTC (rev 3104) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/EntityMetaFactory.java 2009-04-04 14:17:27 UTC (rev 3105) @@ -18,6 +18,8 @@ */ package org.jiemamy.composer.importer.meta; +import org.jiemamy.composer.importer.meta.impl.NonEntityException; + /** * {@link EntityMeta}のファクトリインターフェイス * @@ -30,6 +32,7 @@ * * @param entityClass エンティティクラス * @return {@link EntityMeta} + * @throws NonEntityException entityClassがエンティティでない場合 */ - EntityMeta getEntityMeta(Class<?> entityClass); + EntityMeta getEntityMeta(Class<?> entityClass) throws NonEntityException; } 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:17:06 UTC (rev 3104) +++ charon/jiemamy-jpa-importer/trunk/src/main/java/org/jiemamy/composer/importer/meta/impl/EntityMetaReaderImpl.java 2009-04-04 14:17:27 UTC (rev 3105) @@ -20,12 +20,15 @@ import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.jiemamy.composer.importer.meta.EntityMeta; import org.jiemamy.composer.importer.meta.EntityMetaFactory; @@ -35,7 +38,9 @@ import org.jiemamy.composer.importer.utils.ClassTraversal; import org.jiemamy.composer.importer.utils.CollectionsUtil; import org.jiemamy.composer.importer.utils.FileUtil; +import org.jiemamy.composer.importer.utils.TraversalHandlerException; import org.jiemamy.composer.importer.utils.ClassTraversal.ClassHandler; +import org.jiemamy.utils.LogMarker; /** * {@link EntityMetaReader}の実装クラス。 @@ -44,6 +49,8 @@ */ public class EntityMetaReaderImpl implements EntityMetaReader { + private static final Logger LOG = LoggerFactory.getLogger(EntityMetaReaderImpl.class); + private final EntityMetaReaderContext entityMetaReaderContext; @@ -56,42 +63,67 @@ Validate.notNull(entityMetaReaderContext); Validate.notNull(entityMetaReaderContext.getClassPathDirs()); Validate.notEmpty(entityMetaReaderContext.getClassPathDirs()); - Validate.notNull(entityMetaReaderContext.getJavaFileSrcDirs()); - Validate.notEmpty(entityMetaReaderContext.getJavaFileSrcDirs()); +// Validate.notNull(entityMetaReaderContext.getJavaFileSrcDirs()); +// Validate.notEmpty(entityMetaReaderContext.getJavaFileSrcDirs()); Validate.notNull(entityMetaReaderContext.getJavaFileEncoding()); Validate.notNull(entityMetaReaderContext.getPackageName()); - Validate.notNull(entityMetaReaderContext.getShortClassNamePatterns()); - Validate.notNull(entityMetaReaderContext.getIgnoreShortClassNamePatterns()); +// Validate.notNull(entityMetaReaderContext.getShortClassNamePatterns()); +// Validate.notNull(entityMetaReaderContext.getIgnoreShortClassNamePatterns()); this.entityMetaReaderContext = entityMetaReaderContext; } + /** + * クラスローダを生成します。 + * + * @return {@link ClassLoader} + * @throws IOException 入出力が失敗した場合 + */ + private ClassLoader createClassLoader() throws IOException { + List<URL> urlList = toURLs(entityMetaReaderContext.getClassPathDirs()); + final URLClassLoader urlClassLoader = new URLClassLoader(urlList.toArray(new URL[urlList.size()])); + return urlClassLoader; + } + public List<EntityMeta> read() throws IOException { final List<EntityMeta> entityMetas = CollectionsUtil.newArrayList(); final String entityFullPackageName = entityMetaReaderContext.getPackageName(); - List<URL> urlList = new ArrayList<URL>(); + final ClassLoader classLoader = createClassLoader(); for (File classPathDir : entityMetaReaderContext.getClassPathDirs()) { - urlList.add(FileUtil.toURL(classPathDir)); - final URLClassLoader urlClassLoader = new URLClassLoader(urlList.toArray(new URL[1])); - ClassTraversal.forEach(classPathDir, new ClassHandler() { - - public void processClass(String packageName, String shortClassName) { - if (!packageName.equals(entityFullPackageName)) { - return; + + try { + ClassTraversal.forEach(classPathDir, new ClassHandler() { + + public void processClass(String packageName, String shortClassName) + throws TraversalHandlerException { + if (packageName.equals(entityFullPackageName) == false) { + return; + } + try { + Class<?> entityClass = + ClassLoaderUtil.loadClass(classLoader, packageName + "." + shortClassName); + EntityMetaFactory factory = entityMetaReaderContext.getEntityMetaFactory(); + EntityMeta entityMeta = factory.getEntityMeta(entityClass); + LOG.debug(LogMarker.DETAIL, packageName + "." + shortClassName); + entityMetas.add(entityMeta); + } catch (ClassNotFoundException e) { + throw new TraversalHandlerException(e); + } catch (NonEntityException e) { + throw new TraversalHandlerException(e); + } } - try { - Class<?> entityClass = - ClassLoaderUtil.loadClass(urlClassLoader, packageName + "." + shortClassName); - EntityMetaFactory factory = entityMetaReaderContext.getEntityMetaFactory(); - EntityMeta entityMeta = factory.getEntityMeta(entityClass); - System.out.println(packageName + "." + shortClassName); - entityMetas.add(entityMeta); - } catch (ClassNotFoundException ignore) { - // ignore - } - } - }); + }); + } catch (TraversalHandlerException e) { + e.printStackTrace(); + } } - return entityMetas; } + + private List<URL> toURLs(List<File> files) throws MalformedURLException { + List<URL> urlList = new ArrayList<URL>(); + for (File classPathDir : entityMetaReaderContext.getClassPathDirs()) { + urlList.add(FileUtil.toURL(classPathDir)); + } + return urlList; + } }