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
42 throw new RuntimeException( "Invalid type [" + type + "]" );
43 }
44
45 protected abstract T2 addNewConfig( T3 parent );
46
47 public T1 build( T2 config, T3 parent )
48 {
49 try
50 {
51 Class<? extends T1> clazz = registry.get( config.getType() );
52 if( clazz == null )
53 {
54 return null;
55 }
56
57 T1 entry = clazz.newInstance();
58 entry.init( config, parent );
59 return entry;
60 }
61 catch( Exception e )
62 {
63 SoapUI.logError( e );
64 }
65
66 return null;
67 }
68
69 public String[] getTypes()
70 {
71 return registry.keySet().toArray( new String[registry.size()] );
72 }
73
74 public String[] getTypesWithInterface( Class<?> clazz )
75 {
76 List<String> result = new ArrayList<String>();
77
78 for( String type : registry.keySet() )
79 {
80 if( Arrays.asList( registry.get( type ).getInterfaces() ).contains( clazz ) )
81 result.add( type );
82 }
83
84 return result.toArray( new String[result.size()] );
85 }
86 }