public void execute()

in taverna-maven-plugin/src/main/java/org/apache/taverna/mavenplugin/TavernaPluginDeployFileMojo.java [74:186]


	public void execute() throws MojoExecutionException {
		if (!file.exists()) {
			throw new MojoExecutionException("The Taverna Plugin file " + file
					+ " does not exist");
		}

		JarFile pluginJarFile;
		try {
			pluginJarFile = new JarFile(file);
		} catch (ZipException e) {
			throw new MojoExecutionException(file + " is not a valid Taverna Plugin file", e);
		} catch (IOException e) {
			throw new MojoExecutionException("Error opening Taverna Plugin file: " + file, e);
		}

		ZipEntry pluginFileEntry = pluginJarFile.getJarEntry(PLUGIN_FILE_ENTRY);
		if (pluginFileEntry == null) {
			throw new MojoExecutionException(file
					+ " is not a valid Taverna Plugin file, missing " + PLUGIN_FILE_ENTRY);
		}

		JAXBContext jaxbContext;
		try {
			jaxbContext = JAXBContext.newInstance(PluginInfo.class, Plugins.class);
		} catch (JAXBException e) {
			throw new MojoExecutionException("Error setting up JAXB context ", e);
		}

		PluginInfo plugin;
		try {
			InputStream inputStream = pluginJarFile.getInputStream(pluginFileEntry);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			plugin = (PluginInfo) unmarshaller.unmarshal(inputStream);
			inputStream.close();
		} catch (IOException e) {
			throw new MojoExecutionException("Error reading " + file, e);
		} catch (JAXBException e) {
			throw new MojoExecutionException("Error reading " + file, e);
		}

		getLog().debug("The Taverna plugin will be deployed to '" + url + "'");

		Repository repository = new Repository(serverId, url);

		// create the wagon
		Wagon wagon;
		try {
			wagon = wagonManager.getWagon(repository.getProtocol());
		} catch (UnsupportedProtocolException e) {
			throw new MojoExecutionException("Unsupported protocol: '" + repository.getProtocol()
					+ "'", e);
		}

		Debug debug = new Debug();
		if (getLog().isDebugEnabled()) {
			wagon.addSessionListener(debug);
			wagon.addTransferListener(debug);
		}

		// connect to the plugin site
		try {
			wagon.connect(repository, wagonManager.getAuthenticationInfo(serverId),
					wagonManager.getProxy(repository.getProtocol()));
		} catch (ConnectionException e) {
			throw new MojoExecutionException("Error connecting to plugin site at " + url, e);
		} catch (AuthenticationException e) {
			throw new MojoExecutionException("Authentication error connecting to plugin site at "
					+ url, e);
		}

		try {
			File pluginsFile = File.createTempFile("taverna", null);

			// fetch the plugins file
			Plugins plugins;
			try {
				Utils.downloadFile(PLUGINS_FILE, pluginsFile, wagon, getLog());
				try {
					Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
					plugins = (Plugins) unmarshaller.unmarshal(pluginsFile);
				} catch (JAXBException e) {
					throw new MojoExecutionException("Error reading " + pluginsFile, e);
				}
			} catch (ResourceDoesNotExistException e) {
				getLog().info("Creating new plugins file");
				plugins = new Plugins();
			}

			String deployedPluginFile = plugin.getId() + "-" + plugin.getVersion() + ".jar";

			if (addPlugin(plugins, plugin, deployedPluginFile)) {
				// write the new plugin site file
				try {
					Marshaller marshaller = jaxbContext.createMarshaller();
					marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
					marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
							TavernaPluginGenerateMojo.SCHEMA_LOCATION);
					marshaller.marshal(plugins, pluginsFile);
				} catch (JAXBException e) {
					throw new MojoExecutionException("Error writing " + PLUGINS_FILE, e);
				}

				// upload the plugin to the update site
				Utils.uploadFile(file, deployedPluginFile, wagon, getLog());
				// upload the plugin site file
				Utils.uploadFile(pluginsFile, PLUGINS_FILE, wagon, getLog());
			}
		} catch (IOException e) {
			throw new MojoExecutionException("Error writing " + PLUGINS_FILE, e);
		} finally {
			disconnectWagon(wagon, debug);
		}
	}