| 1 |
4 |
package org.trails.hibernate; |
| 2 |
|
|
| 3 |
|
import com.sun.mirror.apt.AnnotationProcessor; |
| 4 |
|
import com.sun.mirror.apt.AnnotationProcessorEnvironment; |
| 5 |
|
import com.sun.mirror.apt.AnnotationProcessorFactory; |
| 6 |
|
import com.sun.mirror.declaration.AnnotationTypeDeclaration; |
| 7 |
|
import com.sun.mirror.declaration.ClassDeclaration; |
| 8 |
|
import com.sun.mirror.declaration.Declaration; |
| 9 |
|
import com.sun.mirror.util.SimpleDeclarationVisitor; |
| 10 |
|
import org.apache.commons.logging.Log; |
| 11 |
|
import org.apache.commons.logging.LogFactory; |
| 12 |
|
import org.dom4j.Document; |
| 13 |
|
import org.dom4j.Element; |
| 14 |
|
import org.dom4j.io.OutputFormat; |
| 15 |
|
import org.dom4j.io.SAXReader; |
| 16 |
|
import org.dom4j.io.XMLWriter; |
| 17 |
|
import org.hibernate.util.DTDEntityResolver; |
| 18 |
|
|
| 19 |
|
import java.io.File; |
| 20 |
|
import java.io.PrintWriter; |
| 21 |
|
import java.util.*; |
| 22 |
|
|
| 23 |
4 |
public class HibernateAnnotationProcessorFactory implements AnnotationProcessorFactory |
| 24 |
|
{ |
| 25 |
20 |
private static final Log LOG = LogFactory.getLog(HibernateAnnotationProcessorFactory.class); |
| 26 |
|
|
| 27 |
|
public static final String TRAILS_PACKAGE = "org.trails"; |
| 28 |
|
|
| 29 |
|
public static final String configFileOption = "configFile"; |
| 30 |
|
|
| 31 |
|
public static final String destFileOption = "destFile"; |
| 32 |
|
|
| 33 |
|
public HibernateAnnotationProcessorFactory() |
| 34 |
|
{ |
| 35 |
4 |
super(); |
| 36 |
|
|
| 37 |
4 |
} |
| 38 |
|
|
| 39 |
|
public Collection<String> supportedOptions() |
| 40 |
|
{ |
| 41 |
0 |
return null; |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
public Collection<String> supportedAnnotationTypes() |
| 45 |
|
{ |
| 46 |
0 |
return Arrays.asList("javax.persistence.Entity"); |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
public AnnotationProcessor getProcessorFor( |
| 50 |
|
Set<AnnotationTypeDeclaration> decls, |
| 51 |
|
AnnotationProcessorEnvironment env) |
| 52 |
|
{ |
| 53 |
4 |
return new HibernateAnnotationProcessor(env, decls); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
public class HibernateAnnotationProcessor implements AnnotationProcessor |
| 57 |
|
{ |
| 58 |
|
private AnnotationProcessorEnvironment env; |
| 59 |
|
|
| 60 |
|
private Set<AnnotationTypeDeclaration> annotationTypeDeclarations; |
| 61 |
|
|
| 62 |
4 |
public HibernateAnnotationProcessor(AnnotationProcessorEnvironment env, |
| 63 |
|
Set<AnnotationTypeDeclaration> annTypeDecls) |
| 64 |
|
{ |
| 65 |
4 |
this.env = env; |
| 66 |
4 |
this.annotationTypeDeclarations = annTypeDecls; |
| 67 |
4 |
} |
| 68 |
|
|
| 69 |
|
Element sessionFactoryElement; |
| 70 |
|
|
| 71 |
|
public void process() |
| 72 |
|
{ |
| 73 |
4 |
String configTemplateFilePath = getOptionValue(configFileOption); |
| 74 |
|
|
| 75 |
4 |
LOG.info(configTemplateFilePath); |
| 76 |
|
try |
| 77 |
|
{ |
| 78 |
4 |
SAXReader reader = new SAXReader(); |
| 79 |
4 |
reader.setValidation(false); |
| 80 |
4 |
reader.setEntityResolver(new DTDEntityResolver()); |
| 81 |
4 |
reader.setIncludeExternalDTDDeclarations(false); |
| 82 |
4 |
reader.setIncludeInternalDTDDeclarations(false); |
| 83 |
|
|
| 84 |
|
|
| 85 |
4 |
File configTemplateFile = new File(configTemplateFilePath); |
| 86 |
4 |
Document doc = reader.read(configTemplateFile); |
| 87 |
|
|
| 88 |
8 |
sessionFactoryElement = (Element) doc.getRootElement() |
| 89 |
4 |
.selectSingleNode("//session-factory"); |
| 90 |
|
|
| 91 |
4 |
String trailsXPath = "mapping[not(starts-with(@class, '" |
| 92 |
|
+ TRAILS_PACKAGE + "'))]"; |
| 93 |
20 |
for (Iterator iter = sessionFactoryElement.selectNodes( |
| 94 |
20 |
trailsXPath).iterator(); iter.hasNext();) |
| 95 |
|
{ |
| 96 |
4 |
Element element = (Element) iter.next(); |
| 97 |
|
|
| 98 |
4 |
sessionFactoryElement.remove(element); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
8 |
List listenerElements = sessionFactoryElement |
| 102 |
4 |
.elements("listener"); |
| 103 |
4 |
sessionFactoryElement.elements().removeAll(listenerElements); |
| 104 |
|
|
| 105 |
12 |
for (AnnotationTypeDeclaration annotationTypeDecl : annotationTypeDeclarations) |
| 106 |
|
{ |
| 107 |
4 |
LOG.info(annotationTypeDecl); |
| 108 |
12 |
for (Declaration declaration : env |
| 109 |
4 |
.getDeclarationsAnnotatedWith(annotationTypeDecl)) |
| 110 |
|
{ |
| 111 |
4 |
declaration.accept(new SimpleDeclarationVisitor() |
| 112 |
|
{ |
| 113 |
|
public void visitClassDeclaration( |
| 114 |
|
ClassDeclaration classDeclaration) |
| 115 |
|
{ |
| 116 |
8 |
LOG.info(classDeclaration |
| 117 |
4 |
.getQualifiedName()); |
| 118 |
4 |
sessionFactoryElement.addElement("mapping") |
| 119 |
4 |
.setAttributeValue( |
| 120 |
4 |
"class", |
| 121 |
4 |
classDeclaration |
| 122 |
4 |
.getQualifiedName()); |
| 123 |
4 |
} |
| 124 |
|
}); |
| 125 |
|
} |
| 126 |
4 |
sessionFactoryElement.elements().addAll(listenerElements); |
| 127 |
|
} |
| 128 |
4 |
String filename = getOptionValue(destFileOption); |
| 129 |
4 |
LOG.info("Creating destFile: " + filename); |
| 130 |
4 |
File f = new File(filename); |
| 131 |
4 |
f.getParentFile().mkdirs(); |
| 132 |
|
|
| 133 |
4 |
OutputFormat format = OutputFormat.createPrettyPrint(); |
| 134 |
4 |
XMLWriter writer = new XMLWriter(new PrintWriter(f), format); |
| 135 |
4 |
writer.write(doc); |
| 136 |
4 |
writer.close(); |
| 137 |
|
|
| 138 |
0 |
} catch (Exception ex) |
| 139 |
|
{ |
| 140 |
0 |
ex.printStackTrace(); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
4 |
} |
| 144 |
|
|
| 145 |
|
private String getOptionValue(String option) |
| 146 |
|
{ |
| 147 |
|
|
| 148 |
20 |
for (String key : env.getOptions().keySet()) |
| 149 |
|
{ |
| 150 |
12 |
if (key.startsWith("-A" + option)) |
| 151 |
|
{ |
| 152 |
8 |
return key.split("=")[1]; |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
0 |
return null; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
} |
| 159 |
|
} |