All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gate.util.spring.Init Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
/*
 *  Init.java
 *
 *  Copyright (c) 1995-2012, The University of Sheffield. See the file
 *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
 *
 *  This file is part of GATE (see http://gate.ac.uk/), and is free
 *  software, licenced under the GNU Library General Public License,
 *  Version 2, June 1991 (in the distribution as file licence.html,
 *  and also available at http://gate.ac.uk/gate/licence.html).
 *
 *  Ian Roberts, 07/Oct/2006
 *
 *  $Id: Init.java 17530 2014-03-04 15:57:43Z markagreenwood $
 */

package gate.util.spring;

import gate.Gate;
import gate.creole.Plugin;
import gate.creole.ResourceInstantiationException;
import gate.util.GateException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.io.Resource;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 

* Helper class to support GATE initialisation via Spring. The following is a * typical XML fragment to initialise GATE. *

* *
 * <beans xmlns="http://www.springframework.org/schema/beans"
 *        xmlns:gate="http://gate.ac.uk/ns/spring"
 *        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 *        xsi:schemaLocation="
 *          http://www.springframework.org/schema/beans
 *          http://www.springframework.org/schema/beans/spring-beans.xsd
 *          http://gate.ac.uk/ns/spring
 *          http://gate.ac.uk/ns/spring.xsd">
 * 
 *   <gate:init gate-home="path/to/GATE"
 *              site-config-file="site/gate.xml"
 *              user-config-file="user/gate.xml">
 *     <gate:preload-plugins>
 *       <value>plugins/ANNIE</value>
 *       <value>http://plugins.org/another/plugin</value>
 *     </gate:preload-plugins>
 *   </gate:init>
 * 
* *

* Valid attributes are gate-home, * plugins-home, site-config-file, * user-config-file and builtin-creole-dir - * Spring Resources corresponding to the equivalent static * set methods of {@link gate.Gate}. Also, preload-plugins * is a list of Resources that will be loaded as GATE * plugins after GATE is initialised. *

* *

* Alternatively, instead of specifying gate-home, * plugins-home and the configuration files, specifying * run-in-sandbox="true" will tell GATE to initialize without * reading any configuration files. See {@link gate.Gate#runInSandbox} for * details. *

* *

* As well as any plugins specified using preload-plugins, * we also scan the defining bean factory for any beans of type * {@link ExtraGatePlugin}, and load the plugins they refer to. This is * useful if bean definitions are provided in several separate files, or * if you are providing additional bean definitions to a context that * already defines an Init bean definition that you cannot edit. *

* *

* The equivalent definition in "normal" Spring form (without the * gate: namespace) would be: *

* *
 * <bean class="gate.util.spring.Init"
 *      init-method="init">
 *   <property name="gateHome" value="path/to/GATE" />
 *   <property name="siteConfigFile" value="site/gate.xml" />
 *   <property name="userConfigFile" value="user/gate.xml" />
 *   <property name="preloadPlugins">
 *     <list>
 *       <value>plugins/ANNIE</value>
 *       <value>http://plugins.org/another/plugin</value>
 *     </list>
 *   </property>
 * </bean>
 * 
* * Note that when using this form the init-method="init" in the above * definition is vital. GATE will not work if it is omitted. */ public class Init implements BeanFactoryAware { private static final Logger log = Logger.getLogger(Init.class); /** * An optional list of Maven cache directories to add to GATE's * resolution logic. Note that these must be * resolvable as java.io.File, otherwise they will be ignored. */ private List mavenCaches; /** * An optional list of plugins to load after GATE initialisation. */ private List plugins; private BeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; } private File gateHome = null; public void setGateHome(Resource gateHome) throws IOException { this.gateHome = gateHome.getFile(); } private File pluginsHome = null; public void setPluginsHome(Resource pluginsHome) throws IOException { this.pluginsHome = pluginsHome.getFile(); } private File siteConfigFile = null; public void setSiteConfigFile(Resource siteConfigFile) throws IOException { this.siteConfigFile = siteConfigFile.getFile(); } private File userConfigFile = null; public void setUserConfigFile(Resource userConfigFile) throws IOException { this.userConfigFile = userConfigFile.getFile(); } private URL builtinCreoleDir = null; public void setBuiltinCreoleDir(Resource builtinCreoleDir) throws IOException { this.builtinCreoleDir = builtinCreoleDir.getURL(); } // use Boolean rather than boolean so we can distinguish "set to false" from // "not set" private Boolean runInSandbox = null; public void setRunInSandbox(boolean runInSandbox) { this.runInSandbox = Boolean.valueOf(runInSandbox); } public void setMavenCaches(List caches) { this.mavenCaches = caches; } public void setPreloadPlugins(List plugins) { this.plugins = plugins; } /** * Initialises GATE and loads any preloadPlugins that have been * specified, as well as any defined by {@link ExtraGatePlugin} beans * in the containing factory. */ public void init() throws Exception { if(mavenCaches != null) { for(Resource cache : mavenCaches) { try { gate.util.maven.Utils.addCacheDirectory(cache.getFile()); } catch(Exception e) { log.warn("Could not add " + cache + " as a Maven cache - is it resolvable as a file?", e); } } } if(!Gate.isInitialised()) { log.info("Initialising GATE"); if(gateHome != null) Gate.setGateHome(gateHome); if(pluginsHome != null) Gate.setPluginsHome(pluginsHome); if(siteConfigFile != null) Gate.setSiteConfigFile(siteConfigFile); if(userConfigFile != null) Gate.setUserConfigFile(userConfigFile); if(builtinCreoleDir != null) Gate.setBuiltinCreoleDir(builtinCreoleDir); if(runInSandbox != null) Gate.runInSandbox(runInSandbox.booleanValue()); Gate.init(); } else { log.info("GATE already initialised"); } if(plugins != null && !plugins.isEmpty()) { for(Resource plugin : plugins) { loadPlugin(plugin); } } // look for any ExtraGatePlugin beans if(beanFactory instanceof ListableBeanFactory) { String[] extraPluginBeanNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors( (ListableBeanFactory)beanFactory, ExtraGatePlugin.class); for(String name : extraPluginBeanNames) { ExtraGatePlugin xp = beanFactory.getBean(name, ExtraGatePlugin.class); loadPlugin(xp); } } } // init() private void loadPlugin(ExtraGatePlugin plugin) throws GateException, IOException, MalformedURLException { if(plugin.getLocation() != null) { loadPlugin(plugin.getLocation()); } else { // maven plugin if(plugin.getGroupId() == null) { throw new ResourceInstantiationException( "Extra plugin definition requires either a location or a set of Maven co-ordinates"); } log.debug("Loading Maven plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion()); Gate.getCreoleRegister().registerPlugin(new Plugin.Maven( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion())); } } private void loadPlugin(Resource plugin) throws GateException, IOException { log.debug("Loading plugin from " + plugin); // directory plugin File pluginFile = null; try { pluginFile = plugin.getFile(); } catch(IOException e) { // no problem, try just as URL } if(pluginFile == null) { Gate.getCreoleRegister().registerPlugin(new Plugin.Directory(plugin.getURL())); } else { Gate.getCreoleRegister().registerPlugin(new Plugin.Directory(pluginFile.toURI().toURL())); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy