|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SemaphoreFactory.java | 83.3% | 85.7% | 50% | 82.8% |
|
1 | package org.drools.semantics.base; | |
2 | ||
3 | /* | |
4 | * $Id: SemaphoreFactory.java,v 1.2.2.3 2005/05/01 03:20:27 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2004 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.util.Set; | |
45 | ||
46 | import org.drools.rule.Rule; | |
47 | import org.drools.smf.Configuration; | |
48 | import org.drools.smf.FactoryException; | |
49 | import org.drools.smf.ObjectTypeFactory; | |
50 | import org.drools.spi.ObjectType; | |
51 | import org.drools.spi.RuleBaseContext; | |
52 | ||
53 | public class SemaphoreFactory | |
54 | implements | |
55 | ObjectTypeFactory | |
56 | { | |
57 | private static final SemaphoreFactory INSTANCE = new SemaphoreFactory( ); | |
58 | ||
59 | 0 | public static SemaphoreFactory getInstance() |
60 | { | |
61 | 0 | return INSTANCE; |
62 | } | |
63 | ||
64 | 11 | public ObjectType newObjectType(Rule rule, |
65 | RuleBaseContext context, | |
66 | Configuration config) throws FactoryException | |
67 | { | |
68 | 11 | String className = config.getAttribute( "type" ); |
69 | 11 | String fieldName = "identifier"; |
70 | 11 | String fieldValue = config.getAttribute( "identifier" ); |
71 | ||
72 | 11 | if ( className == null || className.trim( ).equals( "" ) ) |
73 | { | |
74 | 1 | throw new FactoryException( "no Semaphore type specified" ); |
75 | } | |
76 | 10 | className = "org.drools.semantics.base." + className + "Semaphore"; |
77 | ||
78 | 10 | if ( fieldValue == null || fieldValue.trim( ).equals( "" ) ) |
79 | { | |
80 | 1 | throw new FactoryException( "no Semaphore identifier specified" ); |
81 | } | |
82 | ||
83 | 9 | try |
84 | { | |
85 | 9 | ClassLoader cl = Thread.currentThread( ).getContextClassLoader( ); |
86 | 9 | if ( cl == null ) |
87 | { | |
88 | 0 | cl = getClass().getClassLoader(); |
89 | } | |
90 | ||
91 | 9 | Class clazz = null; |
92 | /* All semaphore types should be in system classloader*/ | |
93 | 9 | try |
94 | { | |
95 | 9 | clazz = cl.loadClass( className ); |
96 | } | |
97 | catch ( ClassNotFoundException e ) | |
98 | { | |
99 | // Semaphore type does not exist | |
100 | 1 | throw new FactoryException( "Unable create Semaphore for type '" + config.getAttribute( "type" ) + "'" ); |
101 | } | |
102 | ||
103 | // make sure field getter exists | |
104 | 8 | clazz.getMethod( "get" + fieldName.toUpperCase( ).charAt( 0 ) + fieldName.substring( 1 ), |
105 | ( Class[] ) null ); | |
106 | ||
107 | 7 | return new ClassFieldObjectType( clazz, |
108 | fieldName, | |
109 | fieldValue ); | |
110 | } | |
111 | catch ( SecurityException e ) | |
112 | { | |
113 | 0 | throw new FactoryException( "Field '" + fieldName + "' is not accessible for Class '" + className + "'" ); |
114 | } | |
115 | catch ( NoSuchMethodException e ) | |
116 | { | |
117 | 1 | throw new FactoryException( "Field '" + fieldName + "' does not exist for Class '" + className + "'" ); |
118 | } | |
119 | } | |
120 | ||
121 | } |
|