<commit_before>// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2013 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.pathvisio.pluginmanager.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import org.apache.felix.bundlerepository.Reason;
import org.apache.felix.bundlerepository.Repository;
import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.apache.felix.bundlerepository.Requirement;
import org.apache.felix.bundlerepository.Resolver;
import org.apache.felix.bundlerepository.Resource;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.pathvisio.core.debug.Logger;
import org.pathvisio.core.util.ProgressKeeper;
import org.pathvisio.desktop.PvDesktop;
import org.pathvisio.desktop.plugin.IPluginManager;
import org.pathvisio.desktop.plugin.Plugin;
import org.pathvisio.gui.ProgressDialog;
import org.pathvisio.pluginmanager.impl.data.BundleVersion;
import org.pathvisio.pluginmanager.impl.data.PVBundle;
import org.pathvisio.pluginmanager.impl.data.PVRepository;
import org.pathvisio.pluginmanager.impl.dialogs.PluginManagerDialog;
import org.pathvisio.pluginmanager.impl.io.RepoXmlReader;

/**
 * implementation of the IPluginManager interface
 * functionality to install plugins, connecting to the repository,
 * uninstalling plugins
 * 
 * @author martina
 *
 */
public class PluginManager implements IPluginManager {

	private List<PVRepository> onlineRepos;
	private List<BundleVersion> problems;
	private Map<String, BundleVersion> tmpBundles;
	
	private BundleContext context;
	private RepositoryAdmin repoAdmin;

	private List<Plugin> runningPlugins;
	
	private PvDesktop desktop;
	
	private LocalRepositoryHandler localHandler;
	private PluginManagerDialog dlg;
	
	public PluginManager (BundleContext context) {
		this.context = context;
		onlineRepos = new ArrayList<PVRepository>();
		runningPlugins = new ArrayList<Plugin>();
		problems = new ArrayList<BundleVersion>();
		localHandler = new LocalRepositoryHandler(context, this);
		tmpBundles = new HashMap<String, BundleVersion>();
	}
	
	public void init(File localRepo, Set<URL> onlineRepo, PvDesktop desktop) {
		this.desktop = desktop;
	
		// initialize local repository
		localHandler.init(localRepo);
		
		// do not save bundles that could not get started
		localHandler.clean(problems);
		
		// initializes running plugins
		initPlugins();
		
		// initialize online repositories
		ServiceReference ref = context.getServiceReference(RepositoryAdmin.class.getName());
		if(ref != null) {
			repoAdmin = (RepositoryAdmin) context.getService(ref);
			for(URL url : onlineRepo) {
				try {
					Repository repo = repoAdmin.addRepository(url);
					setUpOnlineRepo(repo, url);
				} catch (Exception e) {
					Logger.log.error("Could not initialize repository " + url + "\t" + e.getMessage());
				} 
			}
		} else {
			Logger.log.error("Could not initialize online repositories.");
		}
	}
	
	/**<commit_msg>Download repository data in background thread<commit_after>// PathVisio,
// a tool for data visualization and analysis using Biological Pathways
// Copyright 2006-2013 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.pathvisio.pluginmanager.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import org.apache.felix.bundlerepository.Reason;
import org.apache.felix.bundlerepository.Repository;
import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.apache.felix.bundlerepository.Requirement;
import org.apache.felix.bundlerepository.Resolver;
import org.apache.felix.bundlerepository.Resource;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.pathvisio.core.debug.Logger;
import org.pathvisio.core.util.ProgressKeeper;
import org.pathvisio.desktop.PvDesktop;
import org.pathvisio.desktop.plugin.IPluginManager;
import org.pathvisio.desktop.plugin.Plugin;
import org.pathvisio.gui.ProgressDialog;
import org.pathvisio.pluginmanager.impl.data.BundleVersion;
import org.pathvisio.pluginmanager.impl.data.PVBundle;
import org.pathvisio.pluginmanager.impl.data.PVRepository;
import org.pathvisio.pluginmanager.impl.dialogs.PluginManagerDialog;
import org.pathvisio.pluginmanager.impl.io.RepoXmlReader;

/**
 * implementation of the IPluginManager interface
 * functionality to install plugins, connecting to the repository,
 * uninstalling plugins
 * 
 * @author martina
 *
 */
public class PluginManager implements IPluginManager {

	private List<PVRepository> onlineRepos;
	private List<BundleVersion> problems;
	private Map<String, BundleVersion> tmpBundles;
	
	private BundleContext context;
	private RepositoryAdmin repoAdmin;

	private List<Plugin> runningPlugins;
	
	private PvDesktop desktop;
	
	private LocalRepositoryHandler localHandler;
	private PluginManagerDialog dlg;
	
	public PluginManager (BundleContext context) {
		this.context = context;
		onlineRepos = new ArrayList<PVRepository>();
		runningPlugins = new ArrayList<Plugin>();
		problems = new ArrayList<BundleVersion>();
		localHandler = new LocalRepositoryHandler(context, this);
		tmpBundles = new HashMap<String, BundleVersion>();
	}
	
	public void init(File localRepo, final Set<URL> onlineRepo, PvDesktop desktop) {
		this.desktop = desktop;
	
		// initialize local repository
		localHandler.init(localRepo);
		
		// do not save bundles that could not get started
		localHandler.clean(problems);
		
		// initializes running plugins
		initPlugins();
		
		// initialize online repositories
		final ServiceReference ref = context.getServiceReference(RepositoryAdmin.class.getName());
		repoAdmin = (RepositoryAdmin) context.getService(ref);

		if(ref != null) 
		{
			SwingWorker<Void, Map.Entry<Repository, URL>> worker = new SwingWorker<Void, Map.Entry<Repository, URL>>()
			{

				@Override
				protected Void doInBackground() throws Exception
				{
					for(URL url : onlineRepo) 
					{
						try 
						{
							Repository repo = repoAdmin.addRepository(url);
							publish (new AbstractMap.SimpleEntry<Repository, URL>(repo, url));
						}
						catch (Exception e) 
						{
							Logger.log.error("Could not initialize repository " + url + "\t" + e.getMessage());
						} 
					}
					return null;
				}
				
				@Override
				protected void process(List<Map.Entry<Repository, URL>> result) 
				{
					for (Map.Entry<Repository, URL> repo : result)
					{
						setUpOnlineRepo(repo.getKey(), repo.getValue());
					}
				}
				
			};
			worker.execute();
		} 
		else 
		{
			Logger.log.error("Could not initialize online repositories.");
		}
	}
	
	/**
{'fraction_non_alphanumeric': 0.09762979683972912, 'fraction_numerical': 0.002708803611738149, 'mean_word_length': 4.594065656565657, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456375', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2471, 'n_words': 664}