package net.sourceforge.fenixedu.presentationTier.Action.commons; import java.io.Serializable; import java.io.StringWriter; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import pt.ist.fenixWebFramework.services.Service; import com.qubit.fenixframework.plugins.job.JavaCodeJob; @SuppressWarnings("serial") public class JavaCodeJobBean implements Serializable { private JavaCodeJob job; private String className; private String source; public JavaCodeJobBean() { setSource(getDefaultSource()); } public JavaCodeJobBean(JavaCodeJob job) { this.job = job; this.className = job.getClassName(); this.source = job.getSource(); } public JavaCodeJobBean(String className, String source) { this.className = className; this.source = source; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } @Service public JavaCodeJob saveJob() throws DomainException { StringWriter out = new StringWriter(); try { JavaCodeJob.compileAndLoad(className, source, out); } catch (Throwable e) { throw new DomainException("error.jobs.compile", e, out.toString()); } if (job != null) { job.setClassName(className); job.setSource(source); return job; } return new JavaCodeJob(className, source); } private String getDefaultSource() { final StringBuilder result = new StringBuilder(); result.append("import java.io.PrintStream;\n"); result.append("\n"); result.append("import net.sourceforge.fenixedu.domain.jobs.JobExecutionRequestor;\n"); result.append("\n"); result.append("import com.qubit.fenixframework.plugins.job.JobExecutionResult;\n"); result.append("import com.qubit.fenixframework.plugins.job.JobExecutor;\n"); result.append("\n"); result.append("public class Example implements JobExecutor {\n"); result.append(" @Override\n"); result.append(" public JobExecutionResult execute(JobExecutionRequestor arguments, PrintStream printer) {\n"); result.append(" }\n"); result.append("\n"); result.append(" @Override\n"); result.append(" public String getExecutionDescription() {\n"); result.append(" return \"Example\";\n"); result.append(" }\n"); result.append("\n"); result.append(" @Override\n"); result.append(" public boolean isToRepeatOnFail() {\n"); result.append(" return false;\n"); result.append(" }\n"); result.append("\n"); result.append(" @Override\n"); result.append(" public Class getContextArgumentsType() {\n"); result.append(" return JobExecutionRequestor.class;\n"); result.append(" }\n"); result.append("\n"); result.append("}\n"); return result.toString(); } }