Fix NPE when connecting to local cdm server with dev settings
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / update / P2Util.java
1 package eu.etaxonomy.taxeditor.handler.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.model.MessagingUtils;
17 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
18 import eu.etaxonomy.taxeditor.util.ApplicationUtil;
19
20 /**
21 * This class is a utility class for updating the editor from a p2 update site,
22 * greatly inspired by the links given below.
23 *
24 *
25 * To Do :
26 * - Allow configurable update sites
27 *
28 * Notes :
29 *
30 *
31 * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
32 * @see http://bugs.eclipse.org/281226
33 * @see http://www.vogella.com/tutorials/EclipseP2Update/article.html
34 */
35 public class P2Util {
36
37 private static String EDIT_NIGHTLY_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/nightly/";
38 private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
39
40 private static String EDIT_STABLE_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/stable/";
41 private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
42
43 /**
44 * Retrieve and load the saved list of repositories from the preference store,
45 * making sure that at least the default repository is always loaded.
46 */
47 @SuppressWarnings("restriction")
48 public static void setP2UpdateRepositories() {
49 List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
50 List<MetadataRepositoryElement> savedRepoElements = new ArrayList<>();
51
52 MetadataRepositoryElement mre = new MetadataRepositoryElement(null, getP2UpdateRepository(), true);
53 mre.setNickname(getP2UpdateRepositoryName());
54 savedRepoElements.add(mre);
55
56 repoElements.addAll(savedRepoElements);
57
58 ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
59 .toArray(new MetadataRepositoryElement[]{} ));
60
61 }
62
63 public static String getP2UpdateRepositoryName(){
64 return ApplicationUtil.isStable()?EDIT_STABLE_UPDATE_SITE_NAME:EDIT_NIGHTLY_UPDATE_SITE_NAME;
65 }
66
67 public static URI getP2UpdateRepository(){
68 try {
69 return ApplicationUtil.isStable()?new URI(EDIT_STABLE_UPDATE_SITE):new URI(EDIT_NIGHTLY_UPDATE_SITE);
70 } catch (URISyntaxException e) {
71 MessagingUtils.error(P2Util.class, e);
72 }
73 return null;
74 }
75
76 /**
77 * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
78 * in preferences does not create a preference store and hence the changes are not saved. This means
79 * that we need to save it ourselves.
80 *
81 * This method saves the list of current repositories in the preference store as a string with
82 * specific delimiters.
83 */
84
85 public static void saveP2RepositoryPreferences() {
86
87 IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
88
89 URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
90 URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
91
92 List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
93
94 for(URI repo : currentlyEnabled) {
95 boolean enabled = true;
96 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
97 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
98 element.setNickname(nickname);
99 element.setEnabled(enabled);
100 repoElements.add(element);
101 }
102
103 for(URI repo : currentlyDisabled) {
104 boolean enabled = false;
105 String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
106 MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
107 element.setNickname(nickname);
108 element.setEnabled(enabled);
109 repoElements.add(element);
110 }
111 PreferencesUtil.setP2Repositories(repoElements);
112 }
113 }