1
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 package com.reuters.msgtest.config;
33
34 import com.reuters.msgtest.Creator;
35 import com.reuters.msgtest.SubjectSubstitution;
36 import com.reuters.msgtest.URI;
37 import org.nanocontainer.script.xml.XMLContainerBuilder;
38 import org.picocontainer.MutablePicoContainer;
39 import org.picocontainer.PicoContainer;
40 import org.picocontainer.defaults.DefaultPicoContainer;
41 import org.picocontainer.defaults.ObjectReference;
42 import org.picocontainer.defaults.SimpleReference;
43 import java.io.IOException;
44 import java.io.InputStreamReader;
45 import java.io.Reader;
46
47
48 /***
49 * @author Michael Ward
50 */
51 public class RvTestConfigurationImpl implements RvTestConfiguration {
52 private static ThreadLocal threadLocal = new ThreadLocal();
53
54 public RvTestConfigurationImpl() {
55 if (threadLocal.get() == null) {
56 PicoContainer picoContainer = buildPicoContainer();
57 threadLocal.set(picoContainer);
58 }
59 }
60
61 protected PicoContainer buildPicoContainer() {
62 try {
63 MutablePicoContainer parent = new DefaultPicoContainer();
64 parent.registerComponentInstance(this);
65
66 Reader reader = new InputStreamReader(this.getClass()
67 .getResource("/rvtestconfig.xml")
68 .openStream());
69
70 XMLContainerBuilder builder = new XMLContainerBuilder(reader,
71 this.getClass().getClassLoader());
72 ObjectReference parentContainerRef = new SimpleReference();
73 parentContainerRef.set(parent);
74
75 ObjectReference containerRef = new SimpleReference();
76 builder.buildContainer(containerRef, parentContainerRef, "rvtest",
77 true);
78
79 MutablePicoContainer pico = (MutablePicoContainer) containerRef.get();
80
81
82 if (pico.getComponentInstance(SubjectSubstitution.class) == null) {
83 pico.registerComponentInstance(SubjectSubstitution.class,
84 new DummySubjectSubstitution());
85 }
86
87 return pico;
88 } catch (IOException e) {
89 throw new RuntimeException("Unable to locate rvtestconfig.xml", e);
90 }
91 }
92
93 protected PicoContainer getPicoContainer() {
94 return (PicoContainer) threadLocal.get();
95 }
96
97 public Creator getCreator(String type) {
98 return (Creator) getPicoContainer().getComponentInstance(type);
99 }
100
101 public Object getInstance(Object key) {
102 return getPicoContainer().getComponentInstance(key);
103 }
104
105 public Transport getTransport() {
106 return (Transport) getPicoContainer().getComponentInstance(Transport.class);
107 }
108
109 public SubjectSubstitution getSubjectSubstitution() {
110 return (SubjectSubstitution) getPicoContainer().getComponentInstance(SubjectSubstitution.class);
111 }
112
113 public Object create(String uri) {
114 return create(new URI(uri));
115 }
116
117 public Object create(URI uri) {
118 String scheme = uri.getScheme();
119 Creator creator = getCreator(scheme);
120
121 if (creator == null) {
122 throw new RuntimeException("Could not find a factory for scheme " +
123 scheme + ".");
124 }
125
126 return creator.create(uri.getSchemeSpecificPart());
127 }
128
129 public int getLoglevel() {
130 String level = (String) getPicoContainer().getComponentInstance("loglevel");
131
132 return LogLevelLookup.getLogLevel(level);
133 }
134
135 /***
136 * This dummy is used when no subject substitution was defined by the user
137 */
138 public class DummySubjectSubstitution implements SubjectSubstitution {
139 public String process(String subject) {
140 return subject;
141 }
142 }
143 }