View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.integration.guice;
14  
15  import net.sf.oval.Check;
16  import net.sf.oval.configuration.CheckInitializationListener;
17  import net.sf.oval.constraint.CheckWithCheck;
18  
19  import com.google.inject.Injector;
20  
21  /**
22   * Injects Guice bean dependencies into {@link Check} instances.
23   *
24   * Required dependencies must be annotated with <code>@Autowired</code> within {@link Check} the class.
25   *
26   * @author Sebastian Thomschke
27   */
28  public class GuiceCheckInitializationListener implements CheckInitializationListener
29  {
30  	private final Injector injector;
31  
32  	public GuiceCheckInitializationListener(final Injector injector)
33  	{
34  		this.injector = injector;
35  	}
36  
37  	public void onCheckInitialized(final Check check)
38  	{
39  		injector.injectMembers(check);
40  
41  		if (check instanceof CheckWithCheck)
42  		{
43  			final CheckWithCheck checkWith = (CheckWithCheck) check;
44  			injector.injectMembers(checkWith.getSimpleCheck());
45  		}
46  	}
47  }