public void initialize()

in BSFAnnotator/src/main/java/org/apache/uima/annotator/bsf/BSFAnnotator.java [69:162]


    public void initialize(UimaContext aContext)
        throws ResourceInitializationException {
        super.initialize(aContext);
		// Initialize a BSF manager and do some 'cooking' to adapt the class loader
		manager = new BSFManager();
		ClassLoader classLoader = this.getClass().getClassLoader();
		Thread.currentThread().setContextClassLoader(classLoader);
		manager.setClassLoader(classLoader);
		// Is a UIMAClassLoader
		if (classLoader instanceof URLClassLoader) {
			manager.setClassPath(classpathFromUrls(((URLClassLoader) classLoader).getURLs()));
		}
		Reader reader = null;
		scriptFileName = null;
		try {
			logger = aContext.getLogger();
			// get UIMA datapath and tokenize it into its elements
			StringTokenizer tokenizer = new StringTokenizer(aContext
					.getDataPath(), PATH_SEPARATOR);
			List<File> datapathElements = new ArrayList<File>();
			while (tokenizer.hasMoreTokens()) {
				// add datapath elements to the 'datapathElements' array list
				datapathElements.add(new File(tokenizer.nextToken()));
			}

			// Get config. parameter values
			scriptFileName = (String) aContext.getConfigParameterValue(SCRIPT_SOURCE_FILE);
			File scriptFile = new File(scriptFileName);
			if (!scriptFile.isAbsolute()) {
				// try to resolve the relative file name with classpath or
				// datapath
				scriptFile = resolveRelativeFilePath(scriptFileName, datapathElements);

				// if the current script file wasn't found, throw an exception
				if (scriptFile == null) {
					throw new BSFAnnotatorConfigurationException(
							"bsf_annotator_resource_not_found",
							new Object[] { scriptFileName });
				}
			}
			reader = new FileReader(scriptFile);
		} catch (FileNotFoundException fnfe) {
			throw new BSFAnnotatorConfigurationException(
					"bsf_annotator_resource_not_found",
					new Object[] { scriptFileName }, fnfe);
		}

		try {
			scriptLanguage = BSFManager.getLangFromFilename(scriptFileName);
			engine = manager.loadScriptingEngine(scriptLanguage);
		} catch (BSFException bsfe) {
			Throwable cause = bsfe.getTargetException();
			if (cause == null) cause = bsfe;
			throw new BSFAnnotatorConfigurationException(
					"bsf_annotator_language_not_supported",
					new Object[] { scriptLanguage }, cause);
		}

		// read and execute the script
		try {
			String script = IOUtils.getStringFromReader(reader);
			engine.exec(scriptFileName, 0, 0, script);
		} catch (IOException ioe) {
			throw new BSFAnnotatorInitializationException(
					"bsf_annotator_error_reading_script",
					new Object[] { scriptFileName }, ioe);
		} catch (BSFException bsfe) {
			Throwable cause = bsfe.getTargetException();
			if (cause == null) cause = bsfe;
			throw new BSFAnnotatorInitializationException(
					"bsf_annotator_error_executing_script",
					new Object[] { scriptFileName }, cause);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
		// Call the initialize function implemented in the script
		String methodName = null;
		try {
			methodName = "initialize";
			engine.call(null, methodName, new Object[] { aContext });
		} catch (BSFException bsfe) {
			Throwable cause = bsfe.getTargetException();
			if (cause == null) cause = bsfe;
			throw new BSFAnnotatorInitializationException(
					"bsf_annotator_error_calling_method",
					new Object[] { methodName }, cause);
		}

	}