1 |
| package org.drools.semantics.base; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
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 |
| import java.util.HashSet; |
45 |
| import java.util.Iterator; |
46 |
| import java.util.Set; |
47 |
| |
48 |
| import org.drools.rule.Rule; |
49 |
| import org.drools.smf.Configuration; |
50 |
| import org.drools.smf.FactoryException; |
51 |
| import org.drools.smf.ObjectTypeFactory; |
52 |
| import org.drools.spi.ImportEntry; |
53 |
| import org.drools.spi.Importer; |
54 |
| import org.drools.spi.ObjectType; |
55 |
| import org.drools.spi.RuleBaseContext; |
56 |
| |
57 |
| public class ClassFieldObjectTypeFactory |
58 |
| implements |
59 |
| ObjectTypeFactory |
60 |
| { |
61 |
| private static final ClassFieldObjectTypeFactory INSTANCE = new ClassFieldObjectTypeFactory( ); |
62 |
| |
63 |
0
| public static ClassFieldObjectTypeFactory getInstance()
|
64 |
| { |
65 |
0
| return INSTANCE;
|
66 |
| } |
67 |
| |
68 |
21
| public ObjectType newObjectType(Rule rule,
|
69 |
| RuleBaseContext context, |
70 |
| Configuration config) throws FactoryException |
71 |
| { |
72 |
21
| String className = config.getText( );
|
73 |
21
| String fieldName = config.getAttribute( "field" );
|
74 |
21
| String fieldValue = config.getAttribute( "value" );
|
75 |
| |
76 |
21
| if ( className == null || className.trim( ).equals( "" ) )
|
77 |
| { |
78 |
1
| throw new FactoryException( "no class name specified" );
|
79 |
| } |
80 |
| |
81 |
20
| if ( fieldName == null || fieldName.trim( ).equals( "" ) )
|
82 |
| { |
83 |
1
| throw new FactoryException( "no field name specified" );
|
84 |
| } |
85 |
| |
86 |
19
| if ( fieldValue == null || fieldValue.trim( ).equals( "" ) )
|
87 |
| { |
88 |
1
| throw new FactoryException( "no field value specified" );
|
89 |
| } |
90 |
18
| Class clazz = null;
|
91 |
18
| try
|
92 |
| { |
93 |
18
| ClassLoader cl = (ClassLoader) context.get( "smf-classLoader" );
|
94 |
18
| if ( cl == null )
|
95 |
| { |
96 |
3
| cl = Thread.currentThread( ).getContextClassLoader( );
|
97 |
3
| context.put( "smf-classLoader",
|
98 |
| cl ); |
99 |
| } |
100 |
| |
101 |
18
| if ( cl == null )
|
102 |
| { |
103 |
0
| cl = getClass( ).getClassLoader( );
|
104 |
0
| context.put( "smf-classLoader",
|
105 |
| cl ); |
106 |
| } |
107 |
| |
108 |
18
| Importer importer = rule.getImporter( );
|
109 |
18
| clazz = importer.importClass( cl,
|
110 |
| className ); |
111 |
| |
112 |
| |
113 |
18
| clazz.getMethod( "get" + fieldName.toUpperCase( ).charAt( 0 ) + fieldName.substring( 1 ),
|
114 |
| (Class[]) null ); |
115 |
| |
116 |
| } |
117 |
| catch ( SecurityException e ) |
118 |
| { |
119 |
0
| throw new FactoryException( "Field '" + fieldName + "' is not accessible for Class '" + className + "'" );
|
120 |
| } |
121 |
| catch ( NoSuchMethodException e ) |
122 |
| { |
123 |
0
| throw new FactoryException( "Field '" + fieldName + "' does not exist for Class '" + className + "'" );
|
124 |
| } |
125 |
| catch ( ClassNotFoundException e ) |
126 |
| { |
127 |
0
| throw new FactoryException( e.getMessage( ) );
|
128 |
| } |
129 |
| catch ( Error e ) |
130 |
| { |
131 |
0
| throw new FactoryException( e.getMessage( ) );
|
132 |
| } |
133 |
| |
134 |
18
| return new ClassFieldObjectType( clazz,
|
135 |
| fieldName, |
136 |
| fieldValue ); |
137 |
| } |
138 |
| } |