01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.injection.web;
18:
19: import org.apache.wicket.injection.ConfigurableInjector;
20:
21: /**
22: * Holds a reference to the injector that will be used to automatically
23: * initialize components that are used. Usually the application class should set
24: * the injector in the holder when it initializes.
25: * <p>
26: * This class can be used for unit-testing to switch the standard injector with
27: * an injector that will lookup dependencies from a mock application context.
28: * <p>
29: *
30: * <pre>
31: * class MockSpringInjector extends SpringInjector
32: * {
33: * protected ISpringContextLocator getContextLocator()
34: * {
35: * return new MockContextLocator();
36: * }
37: * }
38: *
39: * InjectorHolder.setInjector(new MockSpringInjector());
40: *
41: * //from this point on InjectableWebPage and InjectablePanel
42: * //will be injected using the MockSpringInjector
43: * </pre>
44: *
45: * @author Igor Vaynberg (ivaynberg)
46: *
47: * TODO shouldn't we move this class to org.apache.wicket.injection ?
48: */
49: public class InjectorHolder {
50: private static ConfigurableInjector injector = null;
51:
52: /**
53: * Gets an injector
54: *
55: * NOTICE this method is not thread safe if setInjector() is used
56: *
57: * @return injector
58: */
59: public static ConfigurableInjector getInjector() {
60: if (injector == null) {
61: throw new IllegalStateException(
62: "InjectorHolder has not been assigned an injector. "
63: + "Use InjectorHolder.setInjector() to assign an injector. "
64: + "In most cases this should be done once inside "
65: + "SpringWebApplication subclass's init() method.");
66: }
67: return injector;
68: }
69:
70: /**
71: * Sets an injector
72: *
73: * NOTICE this method is not thread safe.
74: *
75: * @param newInjector
76: * new injector
77: */
78: public static void setInjector(ConfigurableInjector newInjector) {
79: injector = newInjector;
80: }
81:
82: }
|