1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.registry;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import com.eviware.soapui.SoapUI;
22 import com.eviware.soapui.config.RegistryEntryConfig;
23
24 public abstract class AbstractRegistry<T1 extends RegistryEntry<T2,T3>, T2 extends RegistryEntryConfig, T3 extends Object>
25 {
26 private Map<String,Class<? extends T1>> registry = new HashMap<String,Class<? extends T1>>();
27
28 public void mapType( String type, Class<? extends T1> clazz )
29 {
30 registry.put( type, clazz );
31 }
32
33 public T1 create( String type, T3 parent )
34 {
35 if( registry.containsKey( type ))
36 {
37 T2 config = addNewConfig( parent );
38 config.setType( type );
39 return build( config, parent );
40 }
41 else throw new RuntimeException( "Invalid type [" + type + "]" );
42 }
43
44 protected abstract T2 addNewConfig(T3 parent);
45
46 public T1 build(T2 config, T3 parent )
47 {
48 try
49 {
50 Class<? extends T1> clazz = registry.get(config.getType());
51 if( clazz == null )
52 {
53 return null;
54 }
55
56 T1 entry = clazz.newInstance();
57 entry.init( config, parent );
58 return entry;
59 }
60 catch (Exception e)
61 {
62 SoapUI.logError( e );
63 }
64
65 return null;
66 }
67
68 public String [] getTypes()
69 {
70 return registry.keySet().toArray( new String[registry.size()] );
71 }
72
73 public String [] getTypesWithInterface( Class<?> clazz )
74 {
75 List<String> result = new ArrayList<String>();
76
77 for( String type : registry.keySet())
78 {
79 if( Arrays.asList( registry.get( type ).getInterfaces() ).contains( clazz ))
80 result.add( type );
81 }
82
83 return result.toArray( new String[result.size()] );
84 }
85 }