001 /*
002 * SpringFactory.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, 07/Oct/2006
013 *
014 * $Id: SpringFactory.java 12940 2010-08-08 17:41:22Z ian_roberts $
015 */
016
017 package gate.util.spring;
018
019 import gate.FeatureMap;
020 import gate.util.GateException;
021 import gate.util.persistence.PersistenceManager;
022
023 import java.io.File;
024 import java.io.IOException;
025 import java.net.URL;
026 import java.util.Map;
027
028 import org.springframework.core.io.Resource;
029
030 /**
031 * This class contains spring-aware factory methods for useful GATE
032 * components. Most of these methods have now been superseded by (and
033 * delegate their processing to) the specific factory beans in this
034 * package, but are retained for compatibility with existing
035 * configurations.
036 */
037 public class SpringFactory {
038
039 /**
040 * Creates a feature map from a source map. Any values in the source
041 * map that are of type
042 * <code>org.springframework.core.io.Resource</code> are converted to
043 * their corresponding {@link java.net.URL}. For example:
044 *
045 * <pre>
046 * <bean id="feature-map" class="gate.util.spring.SpringFactory"
047 * factory-method="createFeatureMap">
048 * <constructor-arg>
049 * <map>
050 * <entry key="inputASName" value="Extra" />
051 * <entry key="config">
052 * <value type="org.springframework.core.io.Resource">path/to/config.xml</value>
053 * </entry>
054 * </map>
055 * </constructor-arg>
056 * </bean>
057 * </pre>
058 *
059 * For an easier way to achieve this see {@link FeatureMapFactoryBean},
060 * to which this method delegates.
061 */
062 public static FeatureMap createFeatureMap(Map sourceMap) throws IOException {
063 FeatureMapFactoryBean factory = new FeatureMapFactoryBean();
064 factory.setSourceMap(sourceMap);
065
066 return (FeatureMap)factory.getObject();
067 }
068
069 /**
070 * Loads a saved application state (gapp file) from the given Spring
071 * resource. The resource is first looked up as a {@link File}, and if
072 * found the application is loaded using
073 * {@link PersistenceManager#loadObjectFromFile(File)
074 * loadObjectFromFile}. If the resource cannot be accessed as a file
075 * it is accessed as a {@link URL} and the application loaded with
076 * {@link PersistenceManager#loadObjectFromUrl(URL) loadObjectFromUrl}
077 * . This is useful as many PRs either require or function better with
078 * file: URLs than with other kinds of URL. For an easier way to
079 * achieve this, see {@link SavedApplicationFactoryBean}, to which
080 * this method delegates.
081 */
082 public static Object loadObjectFromResource(Resource res)
083 throws GateException, IOException {
084 SavedApplicationFactoryBean factory = new SavedApplicationFactoryBean();
085 factory.setLocation(res);
086
087 return factory.getObject();
088 }
089
090 /**
091 * Convert a Spring resource to a URL. The resource is first looked up
092 * as a {@link File}, and if this succeeds then the file is converted
093 * to a URL. If the resource cannot be accessed as a file then it is
094 * converted directly to a URL. This is useful as many PRs either
095 * require or function better with file: URLs than with other kinds of
096 * URL. In a Spring configuration this method may be accessed using
097 * the shorthand
098 * <code><gate:url>WEB-INF/file.txt</gate:url></code>
099 */
100 public static URL resourceToUrl(Resource res) throws IOException {
101 File resourceFile = null;
102 try {
103 resourceFile = res.getFile();
104 }
105 catch(IOException e) {
106 // ignore, leaving resourceFile == null
107 }
108
109 if(resourceFile == null) {
110 // couldn't get a file, so resolve the resource as a URL
111 return res.getURL();
112 }
113 else {
114 // get the URL to the file
115 return resourceFile.toURI().toURL();
116 }
117 }
118 }
|