1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.boretti.drools.integration.drools5; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.FileInputStream; |
23 | |
import java.io.FileNotFoundException; |
24 | |
import java.io.FileOutputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.ObjectOutputStream; |
28 | |
import java.net.MalformedURLException; |
29 | |
import java.net.URL; |
30 | |
import java.net.URLClassLoader; |
31 | |
import java.util.List; |
32 | |
|
33 | |
import org.apache.maven.plugin.MojoExecutionException; |
34 | |
import org.apache.maven.plugin.logging.Log; |
35 | |
import org.apache.maven.project.MavenProject; |
36 | |
import org.drools.compiler.DroolsError; |
37 | |
import org.drools.compiler.DroolsParserException; |
38 | |
import org.drools.compiler.PackageBuilder; |
39 | |
import org.drools.compiler.PackageBuilderConfiguration; |
40 | |
import org.drools.compiler.PackageBuilderErrors; |
41 | |
import org.drools.rule.Package; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
class DroolsHelper { |
48 | 0 | private DroolsHelper() {} |
49 | |
|
50 | |
public static PackageBuilder getPackageBuilder(Log logger,MavenProject project) |
51 | |
throws MojoExecutionException{ |
52 | 0 | if (logger.isDebugEnabled()) logger.debug("starting creation of package builder"); |
53 | 0 | ClassLoader loader = DroolsHelper.class.getClassLoader(); |
54 | 0 | List<?> classpathFiles = null; |
55 | |
try { |
56 | 0 | classpathFiles = project.getRuntimeClasspathElements(); |
57 | 0 | } catch (Exception e) { |
58 | 0 | throw new MojoExecutionException("Error during build "+e.getMessage(), e); |
59 | 0 | } |
60 | 0 | URL[] urls = new URL[classpathFiles.size()]; |
61 | |
|
62 | 0 | for (int i = 0; i < classpathFiles.size(); ++i) { |
63 | |
try { |
64 | 0 | urls[i] = new File((String)classpathFiles.get(i)).toURI().toURL(); |
65 | 0 | } catch (MalformedURLException e) { |
66 | 0 | throw new MojoExecutionException("Error during build "+e.getMessage(), e); |
67 | 0 | } |
68 | |
} |
69 | |
|
70 | 0 | URLClassLoader ucl = new URLClassLoader(urls, loader); |
71 | |
|
72 | 0 | PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); |
73 | 0 | conf.setClassLoader(ucl); |
74 | 0 | return new PackageBuilder(conf); |
75 | |
} |
76 | |
|
77 | |
public static boolean compileSourceFile(Log logger,MavenProject project,File src,File dest,boolean xml) |
78 | |
throws MojoExecutionException{ |
79 | 0 | if (logger.isDebugEnabled()) logger.debug("starting compilation of source file "+src); |
80 | 0 | PackageBuilder builder = getPackageBuilder(logger,project); |
81 | |
try { |
82 | 0 | InputStreamReader instream = new InputStreamReader( new FileInputStream(src)); |
83 | 0 | if (xml) builder.addPackageFromXml(instream); |
84 | 0 | else builder.addPackageFromDrl(instream); |
85 | 0 | if(!validationError(logger,builder.getErrors(),src)) return false; |
86 | 0 | dest.getParentFile().mkdirs(); |
87 | 0 | Package dpackage = builder.getPackage(); |
88 | 0 | ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream(dest)); |
89 | 0 | outstream.writeObject(dpackage); |
90 | 0 | if (outstream != null) |
91 | 0 | outstream.close(); |
92 | 0 | return true; |
93 | 0 | } catch (FileNotFoundException e) { |
94 | 0 | throw new MojoExecutionException("Error because of file not found "+e.getMessage(),e); |
95 | 0 | } catch (DroolsParserException e) { |
96 | 0 | if (!validationError(logger,builder.getErrors(),src)) return false; |
97 | 0 | throw new MojoExecutionException("Error because of unexpected drools error "+e.getMessage(),e); |
98 | 0 | } catch (IOException e) { |
99 | 0 | throw new MojoExecutionException("Error because of IO Error "+e.getMessage(),e); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
public static boolean validateSourceFile(Log logger,MavenProject project,File src,boolean xml) |
104 | |
throws MojoExecutionException{ |
105 | 0 | if (logger.isDebugEnabled()) logger.debug("starting validation of source file "+src); |
106 | 0 | PackageBuilder builder = getPackageBuilder(logger,project); |
107 | |
try { |
108 | 0 | InputStreamReader instream = new InputStreamReader( new FileInputStream(src)); |
109 | 0 | if (xml) builder.addPackageFromXml(instream); |
110 | 0 | else builder.addPackageFromDrl(instream); |
111 | 0 | return validationError(logger,builder.getErrors(),src); |
112 | 0 | } catch (FileNotFoundException e) { |
113 | 0 | throw new MojoExecutionException("Error because of file not found "+e.getMessage(),e); |
114 | 0 | } catch (DroolsParserException e) { |
115 | 0 | if (!validationError(logger,builder.getErrors(),src)) return false; |
116 | 0 | throw new MojoExecutionException("Error because of unexpected drools error "+e.getMessage(),e); |
117 | 0 | } catch (IOException e) { |
118 | 0 | throw new MojoExecutionException("Error because of IO Error "+e.getMessage(),e); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
private static boolean validationError(Log logger,PackageBuilderErrors errors,File src) { |
123 | 0 | if (errors!=null) { |
124 | 0 | DroolsError err[] = errors.getErrors(); |
125 | 0 | if (err.length>0) { |
126 | 0 | for(DroolsError e:err) { |
127 | 0 | StringBuilder sp = new StringBuilder(); |
128 | 0 | sp.append(""+src.getAbsolutePath()).append(":"); |
129 | 0 | if (e.getErrorLines()!=null) for(int l:e.getErrorLines()) sp.append(l).append(":"); |
130 | 0 | sp.append(e.getMessage()); |
131 | 0 | logger.error("Drools Error : "+sp); |
132 | |
} |
133 | 0 | return false; |
134 | |
} |
135 | |
} |
136 | 0 | return true; |
137 | |
} |
138 | |
|
139 | |
} |