Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DroolsHelper |
|
| 8.0;8 |
1 | /* | |
2 | Drools4 Integration Helper | |
3 | Copyright (C) 2009 Mathieu Boretti mathieu.boretti@gmail.com | |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation, either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | */ | |
19 | package org.boretti.drools.integration.drools4; | |
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 | * @author mbo | |
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 | /* | |
78 | * | |
79 | for (File current : getSrcFiles()) { | |
80 | getLog().info("Building " + current); | |
81 | try { | |
82 | InputStreamReader instream = new InputStreamReader( | |
83 | new FileInputStream(current)); | |
84 | builder.addPackageFromDrl(instream); | |
85 | if (builder.getErrors()!=null) { | |
86 | if (builder.getErrors().getErrors()!=null) { | |
87 | if (builder.getErrors().getErrors().length>0) { | |
88 | for(DroolsError er :builder.getErrors().getErrors()) { | |
89 | getLog().error(""+toLine(er.getErrorLines())+":"+er.getMessage()); | |
90 | } | |
91 | throw new MojoExecutionException(builder.getErrors().getErrors()[0].toString()); | |
92 | } | |
93 | } | |
94 | } | |
95 | if (instream != null) | |
96 | instream.close(); | |
97 | } catch (Exception e) { | |
98 | throw new MojoExecutionException("Error during build of " | |
99 | + current, e); | |
100 | } | |
101 | } | |
102 | try { | |
103 | outputDirectory.mkdir(); | |
104 | getLog().info( | |
105 | "Generating into " + outputDirectory + " with name " | |
106 | + destFile); | |
107 | org.drools.rule.Package pkg = builder.getPackage(); | |
108 | ObjectOutputStream outstream = new ObjectOutputStream( | |
109 | new FileOutputStream(new File(outputDirectory, destFile))); | |
110 | outstream.writeObject(pkg); | |
111 | if (outstream != null) | |
112 | outstream.close(); | |
113 | } catch (Exception e) { | |
114 | throw new MojoExecutionException("Error during generation of " | |
115 | + destFile + " in " + outputDirectory, e); | |
116 | } | |
117 | ||
118 | */ | |
119 | ||
120 | public static boolean compileSourceFile(Log logger,MavenProject project,File src,File dest) | |
121 | throws MojoExecutionException{ | |
122 | 0 | if (logger.isDebugEnabled()) logger.debug("starting compilation of source file "+src); |
123 | 0 | PackageBuilder builder = getPackageBuilder(logger,project); |
124 | try { | |
125 | 0 | InputStreamReader instream = new InputStreamReader( new FileInputStream(src)); |
126 | 0 | builder.addPackageFromDrl(instream); |
127 | 0 | if(!validationError(logger,builder.getErrors(),src)) return false; |
128 | 0 | dest.getParentFile().mkdirs(); |
129 | 0 | Package dpackage = builder.getPackage(); |
130 | 0 | ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream(dest)); |
131 | 0 | outstream.writeObject(dpackage); |
132 | 0 | if (outstream != null) |
133 | 0 | outstream.close(); |
134 | 0 | return true; |
135 | 0 | } catch (FileNotFoundException e) { |
136 | 0 | throw new MojoExecutionException("Error because of file not found "+e.getMessage(),e); |
137 | 0 | } catch (DroolsParserException e) { |
138 | 0 | if (!validationError(logger,builder.getErrors(),src)) return false; |
139 | 0 | throw new MojoExecutionException("Error because of unexpected drools error "+e.getMessage(),e); |
140 | 0 | } catch (IOException e) { |
141 | 0 | throw new MojoExecutionException("Error because of IO Error "+e.getMessage(),e); |
142 | } | |
143 | } | |
144 | ||
145 | public static boolean validateSourceFile(Log logger,MavenProject project,File src) | |
146 | throws MojoExecutionException{ | |
147 | 0 | if (logger.isDebugEnabled()) logger.debug("starting validation of source file "+src); |
148 | 0 | PackageBuilder builder = getPackageBuilder(logger,project); |
149 | try { | |
150 | 0 | InputStreamReader instream = new InputStreamReader( new FileInputStream(src)); |
151 | 0 | builder.addPackageFromDrl(instream); |
152 | 0 | return validationError(logger,builder.getErrors(),src); |
153 | 0 | } catch (FileNotFoundException e) { |
154 | 0 | throw new MojoExecutionException("Error because of file not found "+e.getMessage(),e); |
155 | 0 | } catch (DroolsParserException e) { |
156 | 0 | if (!validationError(logger,builder.getErrors(),src)) return false; |
157 | 0 | throw new MojoExecutionException("Error because of unexpected drools error "+e.getMessage(),e); |
158 | 0 | } catch (IOException e) { |
159 | 0 | throw new MojoExecutionException("Error because of IO Error "+e.getMessage(),e); |
160 | } | |
161 | } | |
162 | ||
163 | private static boolean validationError(Log logger,PackageBuilderErrors errors,File src) { | |
164 | 0 | if (errors!=null) { |
165 | 0 | DroolsError err[] = errors.getErrors(); |
166 | 0 | if (err.length>0) { |
167 | 0 | for(DroolsError e:err) { |
168 | 0 | StringBuilder sp = new StringBuilder(); |
169 | 0 | sp.append(""+src.getAbsolutePath()).append(":"); |
170 | 0 | if (e.getErrorLines()!=null) for(int l:e.getErrorLines()) sp.append(l).append(":"); |
171 | 0 | sp.append(e.getMessage()); |
172 | 0 | logger.error("Drools Error : "+sp); |
173 | } | |
174 | 0 | return false; |
175 | } | |
176 | } | |
177 | 0 | return true; |
178 | } | |
179 | ||
180 | } |