1 |
| package org.drools.smf; |
2 |
| |
3 |
| import java.util.Collections; |
4 |
| import java.util.HashMap; |
5 |
| import java.util.HashSet; |
6 |
| import java.util.Iterator; |
7 |
| import java.util.Map; |
8 |
| import java.util.Set; |
9 |
| |
10 |
| import org.drools.spi.ImportEntry; |
11 |
| import org.drools.spi.Importer; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| public class DefaultImporter |
54 |
| implements |
55 |
| Importer |
56 |
| { |
57 |
| private Set importEntrySet = Collections.EMPTY_SET; |
58 |
| |
59 |
| private Set importSet = Collections.EMPTY_SET; |
60 |
| |
61 |
| private Map cachedImports = Collections.EMPTY_MAP; |
62 |
| |
63 |
106
| public DefaultImporter()
|
64 |
| { |
65 |
106
| super( );
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
0
| public Set getImportEntries()
|
74 |
| { |
75 |
0
| return this.importEntrySet;
|
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
265
| public Set getImports()
|
84 |
| { |
85 |
265
| if (! importEntrySet.isEmpty( ) )
|
86 |
| { |
87 |
97
| if ( importSet == Collections.EMPTY_SET )
|
88 |
| { |
89 |
22
| importSet = new HashSet( );
|
90 |
| } |
91 |
| |
92 |
97
| Iterator i = this.importEntrySet.iterator( );
|
93 |
97
| ImportEntry importEntry;
|
94 |
97
| while ( i.hasNext( ) )
|
95 |
| { |
96 |
216
| importSet.add( ( (ImportEntry) i.next( ) ).getImportEntry() );
|
97 |
| } |
98 |
| } |
99 |
| |
100 |
265
| return importSet;
|
101 |
| } |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
55
| public void addImport(ImportEntry importEntry)
|
109 |
| { |
110 |
55
| if ( this.importEntrySet == Collections.EMPTY_SET )
|
111 |
| { |
112 |
30
| this.importEntrySet = new HashSet( );
|
113 |
| } |
114 |
55
| this.importEntrySet.add( importEntry );
|
115 |
| } |
116 |
| |
117 |
42
| public Class lookupFromCache(String className)
|
118 |
| { |
119 |
42
| if ( cachedImports == Collections.EMPTY_MAP )
|
120 |
| { |
121 |
23
| return null;
|
122 |
| } |
123 |
| |
124 |
19
| return (Class) cachedImports.get( className );
|
125 |
| |
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
188
| public Class importClass(ClassLoader cl,
|
135 |
| String className) throws ClassNotFoundException |
136 |
| { |
137 |
188
| Class clazz = null;
|
138 |
| |
139 |
| |
140 |
188
| try
|
141 |
| { |
142 |
188
| clazz = cl.loadClass( className );
|
143 |
| } |
144 |
| catch ( ClassNotFoundException e ) |
145 |
| { |
146 |
42
| clazz = null;
|
147 |
| } |
148 |
| |
149 |
| |
150 |
188
| if ( clazz == null )
|
151 |
| { |
152 |
42
| clazz = lookupFromCache( className );
|
153 |
| } |
154 |
| |
155 |
| |
156 |
188
| if ( clazz == null )
|
157 |
| { |
158 |
41
| Set validClazzCandidates = new HashSet( );
|
159 |
| |
160 |
41
| Iterator it = importEntrySet.iterator( );
|
161 |
41
| while ( it.hasNext( ) )
|
162 |
| { |
163 |
72
| clazz = importClass( cl,
|
164 |
| ((ImportEntry) it.next( )).getImportEntry( ), |
165 |
| className.trim( ) ); |
166 |
72
| if ( clazz != null )
|
167 |
| { |
168 |
40
| validClazzCandidates.add( clazz );
|
169 |
| } |
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
41
| if ( validClazzCandidates.size( ) > 1 )
|
177 |
| { |
178 |
1
| StringBuffer sb = new StringBuffer( );
|
179 |
1
| Iterator clazzCandIter = validClazzCandidates.iterator( );
|
180 |
1
| while ( clazzCandIter.hasNext( ) )
|
181 |
| { |
182 |
2
| if ( 0 != sb.length( ) )
|
183 |
| { |
184 |
1
| sb.append( ", " );
|
185 |
| } |
186 |
2
| sb.append( ((Class) clazzCandIter.next( )).getName( ) );
|
187 |
| } |
188 |
1
| throw new Error( "Unable to find unambiguously defined class '" + className + "', candidates are: [" + sb.toString( ) + "]" );
|
189 |
| } |
190 |
40
| else if ( validClazzCandidates.size( ) == 1 )
|
191 |
| { |
192 |
38
| clazz = (Class) validClazzCandidates.toArray( )[0];
|
193 |
| } |
194 |
| else |
195 |
| { |
196 |
2
| clazz = null;
|
197 |
| } |
198 |
| |
199 |
| } |
200 |
| |
201 |
| |
202 |
187
| if ( clazz == null )
|
203 |
| { |
204 |
2
| throw new ClassNotFoundException( "Unable to find class '" + className + "'" );
|
205 |
| } |
206 |
| |
207 |
185
| return clazz;
|
208 |
| } |
209 |
| |
210 |
72
| private Class importClass(ClassLoader cl,
|
211 |
| String importText, |
212 |
| String className) |
213 |
| { |
214 |
72
| String qualifiedClass = null;
|
215 |
72
| Class clazz = null;
|
216 |
| |
217 |
72
| String convertedImportText;
|
218 |
72
| if ( importText.startsWith( "from " ) )
|
219 |
| { |
220 |
0
| convertedImportText = convertFromPythonImport( importText );
|
221 |
| } |
222 |
| else |
223 |
| { |
224 |
72
| convertedImportText = importText;
|
225 |
| } |
226 |
| |
227 |
| |
228 |
72
| if ( convertedImportText.endsWith( "*" ) )
|
229 |
| { |
230 |
7
| qualifiedClass = convertedImportText.substring( 0,
|
231 |
| convertedImportText.indexOf( '*' ) ) + className; |
232 |
| } |
233 |
65
| else if ( convertedImportText.endsWith( "." + className ) )
|
234 |
| { |
235 |
34
| qualifiedClass = convertedImportText;
|
236 |
| } |
237 |
31
| else if ( convertedImportText.equals( className ) )
|
238 |
| { |
239 |
0
| qualifiedClass = convertedImportText;
|
240 |
| } |
241 |
| |
242 |
72
| if ( qualifiedClass != null )
|
243 |
| { |
244 |
41
| try
|
245 |
| { |
246 |
41
| clazz = cl.loadClass( qualifiedClass );
|
247 |
| } |
248 |
| catch ( ClassNotFoundException e ) |
249 |
| { |
250 |
1
| clazz = null;
|
251 |
| } |
252 |
| } |
253 |
| |
254 |
72
| if ( clazz != null )
|
255 |
| { |
256 |
40
| if ( this.cachedImports == Collections.EMPTY_MAP )
|
257 |
| { |
258 |
23
| this.cachedImports = new HashMap( );
|
259 |
| } |
260 |
| |
261 |
40
| this.cachedImports.put( className,
|
262 |
| clazz ); |
263 |
| } |
264 |
| |
265 |
72
| return clazz;
|
266 |
| } |
267 |
| |
268 |
0
| private String convertFromPythonImport(String packageText)
|
269 |
| { |
270 |
0
| String fromString = "from ";
|
271 |
0
| String importString = "import ";
|
272 |
0
| int fromIndex = packageText.indexOf( fromString );
|
273 |
0
| int importIndex = packageText.indexOf( importString );
|
274 |
0
| return packageText.substring( fromIndex + fromString.length( ),
|
275 |
| importIndex ).trim( ) + "." + packageText.substring( importIndex + importString.length( ) ).trim( ); |
276 |
| } |
277 |
| |
278 |
0
| public boolean isEmpty()
|
279 |
| { |
280 |
0
| return this.importEntrySet.isEmpty( );
|
281 |
| } |
282 |
| } |