001 /*
002 * GateResourceFactoryBean.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Ian Roberts, 22/Jan/2008
013 *
014 * $Id: GateResourceFactoryBean.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.util.spring;
018
019 import java.util.List;
020
021 import gate.Factory;
022 import gate.FeatureMap;
023
024 import org.springframework.beans.factory.DisposableBean;
025 import org.springframework.beans.factory.FactoryBean;
026
027 /**
028 * Spring factory bean to create a GATE resource (LR, PR, controller).
029 * Generally used via the <code>gate:</code> extension namespace,
030 * e.g.:
031 *
032 * <pre>
033 * <gate:resource id="doc" scope="prototype"
034 * resource-class="gate.corpora.DocumentImpl"
035 * resource-name="News document">
036 * <gate:parameters>
037 * <entry key="sourceUrl">
038 * <value type="org.springframework.core.io.Resource">resources/doc.xm</value>
039 * </entry>
040 * <entry key="preserveOriginalContent" value="true" />
041 * </gate:parameters>
042 * <gate:features>
043 * <entry key="genre" value="news" />
044 * </gate:features>
045 * <gate:customisers>
046 * <!-- optional list of {@link ResourceCustomiser}s applied to the resource after creation -->
047 * </gate:customisers>
048 * </gate:resource>
049 * </pre>
050 *
051 * The <code>gate:parameters</code> and <code>gate:features</code>
052 * elements are FeatureMaps giving the init-time parameters and features
053 * for the resource respectively. Any Spring Resource values in these
054 * maps are converted to URLs, so the rest of the GATE code does not
055 * need to know about Spring. For details of how to declare the
056 * <code>gate</code> namespace, see {@link Init}.
057 */
058 public class GateResourceFactoryBean extends GateAwareObject implements
059 FactoryBean,
060 DisposableBean {
061
062 private String resourceClass;
063
064 private String resourceName;
065
066 private FeatureMap parameters;
067
068 private FeatureMap features;
069
070 private List<ResourceCustomiser> customisers;
071
072 private gate.Resource object;
073
074 /**
075 * Create the resource specified by this bean.
076 */
077 public Object getObject() throws Exception {
078 if(object == null) {
079 ensureGateInit();
080
081 if(parameters == null) {
082 parameters = Factory.newFeatureMap();
083 }
084
085 if(features == null) {
086 features = Factory.newFeatureMap();
087 }
088
089 if(resourceName == null) {
090 object = Factory.createResource(resourceClass, parameters, features);
091 }
092 else {
093 object = Factory.createResource(resourceClass, parameters, features,
094 resourceName);
095 }
096
097 if(customisers != null) {
098 for(ResourceCustomiser c : customisers) {
099 c.customiseResource(object);
100 }
101 }
102 }
103
104 return object;
105 }
106
107 public Class getObjectType() {
108 if(object != null) {
109 return object.getClass();
110 }
111 return null;
112 }
113
114 public boolean isSingleton() {
115 return true;
116 }
117
118 /**
119 * Destroy the resource created by this bean, by passing it to
120 * {@link Factory#deleteResource}.
121 */
122 public void destroy() throws Exception {
123 if(object != null) {
124 Factory.deleteResource(object);
125 }
126 }
127
128 public void setResourceClass(String resourceClass) {
129 this.resourceClass = resourceClass;
130 }
131
132 public void setResourceName(String resourceName) {
133 this.resourceName = resourceName;
134 }
135
136 public void setParameters(FeatureMap parameters) {
137 this.parameters = parameters;
138 }
139
140 public void setFeatures(FeatureMap features) {
141 this.features = features;
142 }
143
144 public void setCustomisers(List<ResourceCustomiser> customisers) {
145 this.customisers = customisers;
146 }
147 }
|