//package pt.ist.struts.dispatch.annotation; // //import java.io.BufferedWriter; //import java.io.IOException; //import java.util.Set; // //import javax.annotation.processing.AbstractProcessor; //import javax.annotation.processing.RoundEnvironment; //import javax.annotation.processing.SupportedAnnotationTypes; //import javax.annotation.processing.SupportedSourceVersion; //import javax.lang.model.SourceVersion; //import javax.lang.model.element.TypeElement; //import javax.tools.Diagnostic; //import javax.tools.FileObject; // //import com.sun.tools.javac.code.Symbol.ClassSymbol; // //@SupportedSourceVersion(SourceVersion.RELEASE_6) //@SupportedAnnotationTypes({ "pt.ist.struts.dispatch.annotation.Mapping" }) //public class ActionAnnotationProcessor extends AbstractProcessor { // // static final String LOG_FILENAME = ".actionAnnotationLog"; // public static final String ENTRY_SEPERATOR = "\n"; // // @Override // public boolean process(Set annotations, RoundEnvironment roundEnv) { // // // final Set actions = new HashSet(); // // final Set elements = (Set) roundEnv.getElementsAnnotatedWith(Mapping.class); // if (!elements.isEmpty()) { // try { // // FileObject mappings = null; // // mappings = // // processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, // // "", "mappings"); // // if (mappings.getLastModified() != 0) { // // InputStream reader = null; // // try { // // reader = mappings.openInputStream(); // // String[] current = // // IOUtils.toString(reader).split(ENTRY_SEPERATOR); // // actions.addAll(Arrays.asList(current)); // // } finally { // // if (reader != null) { // // reader.close(); // // } // // } // // } // // for (final ClassSymbol classSymbol : elements) { // FileObject loader = processingEnv.getFiler().createSourceFile( // classSymbol.getQualifiedName() + "MappingLoader"); // BufferedWriter writer = null; // try { // writer = new BufferedWriter(loader.openWriter()); // writer.append("package " + classSymbol.getEnclosingElement().getQualifiedName() + ";\n"); // writer.newLine(); // writer.append("import javax.servlet.ServletContextEvent;\n"); // writer.append("import javax.servlet.ServletContextListener;\n"); // writer.append("import javax.servlet.annotation.WebListener;\n"); // writer.newLine(); // // writer.append("import pt.ist.struts.dispatch.annotation.Mapping;\n"); // writer.append("import pt.ist.struts.dispatch.servlets.StrutsFrameworkInitializer;\n"); // writer.newLine(); // writer.append("@WebListener\n"); // writer.append("public class " + classSymbol.getSimpleName() // + "MappingLoader implements ServletContextListener {\n"); // writer.newLine(); // writer.append(" @Override\n"); // writer.append(" public void contextInitialized(ServletContextEvent event) {\n"); // writer.append(" StrutsFrameworkInitializer.registerMapping(" + classSymbol.getSimpleName() // + ".class);\n"); // writer.append(" }\n"); // writer.newLine(); // writer.append(" @Override\n"); // writer.append(" public void contextDestroyed(ServletContextEvent event) {\n"); // writer.append(" }\n"); // writer.newLine(); // writer.append("}\n"); // writer.flush(); // } finally { // if (writer != null) { // writer.close(); // } // } // // actions.add(classSymbol.getQualifiedName().toString()); // // processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, // // "Mapping: " + classSymbol.getQualifiedName().toString()); // } // // // mappings = // // processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, // // "", "mappings"); // // OutputStream writer = null; // // try { // // writer = mappings.openOutputStream(); // // IOUtils.write(StringUtils.join(actions, ENTRY_SEPERATOR), // // writer); // // } finally { // // if (writer != null) { // // writer.close(); // // } // // } // } catch (IOException e) { // processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, // "Could not access mapping file while processing @Mapping annotation"); // return false; // } // } // return true; // } // }