Merge branch 'release/4.7.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / P2Util.java
1 package eu.etaxonomy.taxeditor.update;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.eclipse.equinox.internal.p2.ui.ProvUI;
9 import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
10 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
11 import org.eclipse.equinox.p2.repository.IRepository;
12 import org.eclipse.equinox.p2.repository.IRepositoryManager;
13 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
14 import org.eclipse.equinox.p2.ui.ProvisioningUI;
15
16 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
17 import eu.etaxonomy.taxeditor.model.MessagingUtils;
18 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
19 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
20
21 /**
22 * This class is a utility class for updating the editor from a p2 update site,
23 * greatly inspired by the links given below.
24 *
25 *
26 * To Do :
27 * - Allow configurable update sites
28 *
29 * Notes :
30 *
31 *
32 * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
33 * @see http://bugs.eclipse.org/281226
34 * @see http://www.vogella.com/tutorials/EclipseP2Update/article.html
35 */
36 public class P2Util {
37
38 //private static String LOCAL_UPDATE_SITE = "file:///path/.../to/Development/EDIT/taxeditor/eu.etaxonomy.taxeditor/target/repository/";
39
40 private static String EDIT_NIGHTLY_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/nightly/";
41 private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
42
43 private static String EDIT_SNAPSHOT_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/snapshot/";
44 private static String EDIT_SNAPSHOT_UPDATE_SITE_NAME = "Taxonomic Editor Snapshot";
45
46 private static String EDIT_STABLE_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/stable/";
47 private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
48
49 /**
50 * Retrieve and load the saved list of repositories from the preference store,
51 * making sure that at least the default repository is always loaded.
52 */
53 @SuppressWarnings("restriction")
54 public static void setP2UpdateRepositories() {
55 String updateSite = EDIT_NIGHTLY_UPDATE_SITE;
56 String updateSiteName = EDIT_NIGHTLY_UPDATE_SITE_NAME;
57
58 if(ApplicationUtil.isStable()) {
59 updateSite = EDIT_STABLE_UPDATE_SITE;
60 updateSiteName = EDIT_STABLE_UPDATE_SITE_NAME;
61 }
62 List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
63 List<MetadataRepositoryElement> savedRepoElements = PreferencesUtil.getP2Repositories();
64 if(savedRepoElements.isEmpty()) {
65 // we always need an update site, so we add the default one
66 try {
67 MetadataRepositoryElement element = new MetadataRepositoryElement(null, new URI(updateSite), true);
68 element.setNickname(updateSiteName);
69 repoElements.add(element);
70 } catch (URISyntaxException e) {
71 MessagingUtils.errorDialog("Invalid update site URI",
72 P2Util.class,
73 "The update site URI has an invalid syntax",
74 TaxonomicEditorPlugin.PLUGIN_ID,
75 e,
76 false);
77 }
78 }
79 repoElements.addAll(savedRepoElements);
80
81 ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
82 .toArray(new MetadataRepositoryElement[]{} ));
83
84 }
85
86 /**
87 * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
88 * in preferences does not create a preference store and hence the changes are not saved. This means
89 * that we need to save it ourselves.
90 *
91 * This method saves the list of current repositories in the preference store as a string with
92 * specific delimiters.
93 */
94
95 public static void saveP2RepositoryPreferences() {
96
97 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
98
99 URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
100 URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
101
102 List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
103
104 for(URI repo : currentlyEnabled) {
105 boolean enabled = true;
106 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
107 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
108 element.setNickname(nickname);
109 element.setEnabled(enabled);
110 repoElements.add(element);
111 }
112
113 for(URI repo : currentlyDisabled) {
114 boolean enabled = false;
115 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
116 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
117 element.setNickname(nickname);
118 element.setEnabled(enabled);
119 repoElements.add(element);
120 }
121 PreferencesUtil.setP2Repositories(repoElements);
122 }
123 }