01 /*
02 * GateAwareObject.java
03 *
04 * Copyright (c) 1995-2010, The University of Sheffield. See the file
05 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
06 *
07 * This file is part of GATE (see http://gate.ac.uk/), and is free
08 * software, licenced under the GNU Library General Public License,
09 * Version 2, June 1991 (in the distribution as file licence.html,
10 * and also available at http://gate.ac.uk/gate/licence.html).
11 *
12 * Ian Roberts, 22/Jan/2008
13 *
14 * $Id: GateAwareObject.java 12006 2009-12-01 17:24:28Z thomas_heitz $
15 */
16
17 package gate.util.spring;
18
19 import org.springframework.beans.factory.BeanFactory;
20 import org.springframework.beans.factory.BeanFactoryAware;
21 import org.springframework.beans.factory.BeanFactoryUtils;
22 import org.springframework.beans.factory.ListableBeanFactory;
23
24 /**
25 * Convenience superclass for objects that may be created by Spring and
26 * need to ensure that GATE is initialised before they do their work.
27 * Subclasses can call {@link #ensureGateInit} to force the
28 * initialisation of any {@link Init} beans in the creating
29 * {@link BeanFactory}, thus ensuring that GATE will be properly
30 * initialised. The method does nothing if the bean factory has not been
31 * set (e.g. if the object is being used outside Spring). In this case,
32 * we assume that GATE initialisation has already been handled
33 * elsewhere.
34 */
35 public class GateAwareObject implements BeanFactoryAware {
36
37 private BeanFactory factory;
38
39 public GateAwareObject() {
40 super();
41 }
42
43 /**
44 * To be called by subclasses to ensure that any {@link Init} beans in
45 * the containing bean factory (and its ancestor factories, if any)
46 * have been initialised.
47 */
48 protected void ensureGateInit() {
49 if(factory != null && factory instanceof ListableBeanFactory) {
50 String[] initNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
51 (ListableBeanFactory)factory, Init.class);
52 for(String name : initNames) {
53 factory.getBean(name);
54 }
55 }
56 }
57
58 public void setBeanFactory(BeanFactory factory) {
59 this.factory = factory;
60 }
61
62 }
|