Debian Codegraph @

bundles/org.eclipse.equinox.p2.updatechecker/src/org/eclipse/equinox/internal/p2/updatechecker/UpdateChecker.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Sonatype, Inc. - ongoing development
14 * Mikael Barbero (Eclipse Foundation) - Bug 498116
15 *******************************************************************************/
16 package org.eclipse.equinox.internal.p2.updatechecker;
17
18 import java.net.URI;
19 import java.text.SimpleDateFormat;
20 import java.util.*;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
24 import org.eclipse.equinox.internal.p2.core.helpers.Tracing;
25 import org.eclipse.equinox.internal.provisional.p2.updatechecker.*;
26 import org.eclipse.equinox.p2.core.IProvisioningAgent;
27 import org.eclipse.equinox.p2.core.ProvisionException;
28 import org.eclipse.equinox.p2.engine.*;
29 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
30 import org.eclipse.equinox.p2.planner.IPlanner;
31 import org.eclipse.equinox.p2.query.*;
32 import org.eclipse.equinox.p2.repository.IRepositoryManager;
33 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
34
35 /**
36 * Default implementation of {@link IUpdateChecker}.
37 * <p>
38 * This implementation is not optimized. It doesn't optimize for multiple
39 * polls on the same profile, nor does it cache any info about a profile from
40 * poll to poll.
41 */
42 public class UpdateChecker implements IUpdateChecker {
43 public static boolean DEBUG = false;
44 public static boolean TRACE = false;
45 /**
46 * Map of IUpdateListener->UpdateCheckThread.
47 */
48 private HashMap<IUpdateListener, UpdateCheckThread> checkers = new HashMap<>();
49
50 private final IProvisioningAgent agent;
51 IProfileRegistry profileRegistry;
52 IPlanner planner;
53
54 private class UpdateCheckThread extends Thread {
55 boolean done = false;
56 long poll, delay;
57 IUpdateListener listener;
58 String profileId;
59 IQuery<IInstallableUnit> query;
60
61 UpdateCheckThread(String profileId, IQuery<IInstallableUnit> query, long delay, long poll, IUpdateListener listener) {
62 this.poll = poll;
63 this.delay = delay;
64 this.profileId = profileId;
65 this.query = query;
66 this.listener = listener;
67 }
68
69 @Override
70 public void run() {
71 try {
72 if (delay != ONE_TIME_CHECK && delay > 0) {
73 Thread.sleep(delay);
74 }
75 while (!done) {
76 listener.checkingForUpdates();
77 trace("Checking for updates for " + profileId + " at " + getTimeStamp()); //$NON-NLS-1$ //$NON-NLS-2$
78 Collection<IInstallableUnit> iusWithUpdates = checkForUpdates(profileId, query);
79 if (iusWithUpdates.size() > 0) {
80 trace("Notifying listener of available updates"); //$NON-NLS-1$
81 UpdateEvent event = new UpdateEvent(profileId, iusWithUpdates);
82 if (!done)
83 listener.updatesAvailable(event);
84 } else {
85 trace("No updates were available"); //$NON-NLS-1$
86 }
87 if (delay == ONE_TIME_CHECK || delay <= 0 || poll <= 0) {
88 done = true;
89 } else {
90 Thread.sleep(poll);
91 }
92 }
93 } catch (InterruptedException e) {
94 // nothing
95 } catch (Exception e) {
96 LogHelper.log(new Status(IStatus.ERROR, UpdateCheckerComponent.BUNDLE_ID, "Exception in update check thread", e)); //$NON-NLS-1$
97 }
98 }
99 }
100
101 public UpdateChecker(IProvisioningAgent agent) {
102 this.agent = agent;
103 }
104
105 @Override
106 public void addUpdateCheck(String profileId, IQuery<IInstallableUnit> query, long delay, long poll, IUpdateListener listener) {
107 if (checkers.containsKey(listener))
108 return;
109 trace("Adding update checker for " + profileId + " at " + getTimeStamp()); //$NON-NLS-1$ //$NON-NLS-2$
110 UpdateCheckThread thread = new UpdateCheckThread(profileId, query, delay, poll, listener);
111 checkers.put(listener, thread);
112 thread.start();
113 }
114
115 @Override
116 public void removeUpdateCheck(IUpdateListener listener) {
117 checkers.remove(listener);
118 }
119
120 /*
121 * Return the array of ius in the profile that have updates
122 * available.
123 */
124 Collection<IInstallableUnit> checkForUpdates(String profileId, IQuery<IInstallableUnit> query) {
125 IProfile profile = getProfileRegistry().getProfile(profileId);
126 ArrayList<IInstallableUnit> iusWithUpdates = new ArrayList<>();
127 if (profile == null)
128 return Collections.emptyList();
129 ProvisioningContext context = new ProvisioningContext(agent);
130 context.setMetadataRepositories(getAvailableRepositories());
131 if (query == null)
132 query = QueryUtil.createIUAnyQuery();
133 Iterator<IInstallableUnit> iter = profile.query(query, null).iterator();
134 while (iter.hasNext()) {
135 IInstallableUnit iu = iter.next();
136 IQueryResult<IInstallableUnit> replacements = getPlanner().updatesFor(iu, context, null);
137 if (!replacements.isEmpty())
138 iusWithUpdates.add(iu);
139 }
140 return iusWithUpdates;
141 }
142
143 /**
144 * Returns the list of metadata repositories that are currently available.
145 */
146 private URI[] getAvailableRepositories() {
147 IMetadataRepositoryManager repoMgr = agent.getService(IMetadataRepositoryManager.class);
148 URI[] repositories = repoMgr.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
149 ArrayList<URI> available = new ArrayList<>();
150 for (URI repositorie : repositories) {
151 try {
152 repoMgr.loadRepository(repositorie, null);
153 available.add(repositorie);
154 } catch (ProvisionException e) {
155 LogHelper.log(e.getStatus());
156 }
157 }
158 return available.toArray(new URI[available.size()]);
159 }
160
161 void trace(String message) {
162 if (Tracing.DEBUG_UPDATE_CHECK)
163 Tracing.debug(message);
164 }
165
166 String getTimeStamp() {
167 Date d = new Date();
168 SimpleDateFormat df = new SimpleDateFormat("[MM/dd/yy;HH:mm:ss:SSS]"); //$NON-NLS-1$
169 return df.format(d);
170 }
171
172 IPlanner getPlanner() {
173 if (planner == null) {
174 planner = agent.getService(IPlanner.class);
175 if (planner == null) {
176 throw new IllegalStateException("Provisioning system has not been initialized"); //$NON-NLS-1$
177 }
178 }
179 return planner;
180 }
181
182 IProfileRegistry getProfileRegistry() {
183 if (profileRegistry == null) {
184 profileRegistry = agent.getService(IProfileRegistry.class);
185 if (profileRegistry == null) {
186 throw new IllegalStateException("Provisioning system has not been initialized"); //$NON-NLS-1$
187 }
188 }
189 return profileRegistry;
190 }
191
192 }