View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
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              // if NO SubjectSubstitution is registered lets default to a dummy implementation
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 }