|
Lines 13-20
Link Here
|
| 13 |
import java.net.URI; |
13 |
import java.net.URI; |
| 14 |
import java.net.URISyntaxException; |
14 |
import java.net.URISyntaxException; |
| 15 |
import java.util.Map; |
15 |
import java.util.Map; |
| 16 |
import java.util.StringTokenizer; |
|
|
| 17 |
import org.eclipse.core.runtime.CoreException; |
16 |
import org.eclipse.core.runtime.CoreException; |
|
|
17 |
import org.eclipse.core.runtime.preferences.IPreferencesService; |
| 18 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
18 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 19 |
import org.eclipse.equinox.internal.p2.engine.Profile; |
19 |
import org.eclipse.equinox.internal.p2.engine.Profile; |
| 20 |
import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Activator; |
20 |
import org.eclipse.equinox.internal.p2.touchpoint.eclipse.Activator; |
|
Lines 24-29
Link Here
|
| 24 |
import org.eclipse.equinox.internal.provisional.p2.engine.*; |
24 |
import org.eclipse.equinox.internal.provisional.p2.engine.*; |
| 25 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
25 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
| 26 |
import org.eclipse.osgi.util.NLS; |
26 |
import org.eclipse.osgi.util.NLS; |
|
|
27 |
import org.osgi.service.prefs.BackingStoreException; |
| 28 |
import org.osgi.service.prefs.Preferences; |
| 27 |
|
29 |
|
| 28 |
/** |
30 |
/** |
| 29 |
* Helper base class for dealing with repositories associated with profiles. Repositories |
31 |
* Helper base class for dealing with repositories associated with profiles. Repositories |
|
Lines 34-39
Link Here
|
| 34 |
*/ |
36 |
*/ |
| 35 |
abstract class RepositoryAction extends ProvisioningAction { |
37 |
abstract class RepositoryAction extends ProvisioningAction { |
| 36 |
|
38 |
|
|
|
39 |
private static final String METADATA_REPOSITORY = "org.eclipse.equinox.p2.metadata.repository"; //$NON-NLS-1$ |
| 40 |
private static final String ARTIFACT_REPOSITORY = "org.eclipse.equinox.p2.artifact.repository"; //$NON-NLS-1$ |
| 41 |
|
| 42 |
private static final String NODE_REPOSITORIES = "repositories"; //$NON-NLS-1$ |
| 43 |
private static final String REPOSITORY_COUNT = "count"; //$NON-NLS-1$ |
| 44 |
private static final String KEY_URI = "uri"; //$NON-NLS-1$ |
| 45 |
private static final String KEY_ENABLED = "enabled"; //$NON-NLS-1$ |
| 46 |
|
| 37 |
/** |
47 |
/** |
| 38 |
* Returns the repository manager of the given type, or <code>null</code> |
48 |
* Returns the repository manager of the given type, or <code>null</code> |
| 39 |
* if not available. |
49 |
* if not available. |
|
Lines 51-72
Link Here
|
| 51 |
* Associates the repository described by the given event with the given profile. |
61 |
* Associates the repository described by the given event with the given profile. |
| 52 |
* Has no effect if the repository is already associated with this profile. |
62 |
* Has no effect if the repository is already associated with this profile. |
| 53 |
*/ |
63 |
*/ |
| 54 |
protected void addRepositoryToProfile(Profile profile, URI location, int type) { |
64 |
protected void addRepositoryToProfile(Profile profile, URI location, int type, boolean enabled) { |
| 55 |
String key = type == IRepository.TYPE_METADATA ? IProfile.PROP_METADATA_REPOSITORIES : IProfile.PROP_ARTIFACT_REPOSITORIES; |
65 |
Preferences node = getRepositoryPreferenceNode(profile, location, type); |
| 56 |
String encodedURI = encodeURI(location); |
66 |
int count = 0; |
| 57 |
String currentRepos = profile.getProperty(key); |
67 |
|
| 58 |
if (currentRepos == null) { |
68 |
if (repositoryExists(node)) { |
| 59 |
currentRepos = encodedURI; |
69 |
count = getRepositoryCount(node);; |
| 60 |
} else { |
70 |
// If a user as added a repository we need to set the initial count manually |
| 61 |
//if we already have the repository location, we are done |
71 |
if (count == 0) |
| 62 |
StringTokenizer tokens = new StringTokenizer(currentRepos, ","); //$NON-NLS-1$ |
72 |
count = 1; |
| 63 |
while (tokens.hasMoreTokens()) |
73 |
} |
| 64 |
if (tokens.nextToken().equals(encodedURI)) |
74 |
node.put(KEY_URI, location.toString()); |
| 65 |
return; |
75 |
node.put(KEY_ENABLED, Boolean.toString(enabled)); |
| 66 |
//add to comma-separated list |
76 |
count++; |
| 67 |
currentRepos = currentRepos + ',' + encodedURI; |
77 |
setRepositoryCount(node, count); |
|
|
78 |
try { |
| 79 |
node.flush(); |
| 80 |
} catch (BackingStoreException e) { |
| 81 |
// TODO: perhaps an Exception should be passed backwards and associated with State |
| 68 |
} |
82 |
} |
| 69 |
profile.setProperty(key, currentRepos); |
|
|
| 70 |
} |
83 |
} |
| 71 |
|
84 |
|
| 72 |
/** |
85 |
/** |
|
Lines 74-81
Link Here
|
| 74 |
*/ |
87 |
*/ |
| 75 |
protected void addToSelf(RepositoryEvent event) { |
88 |
protected void addToSelf(RepositoryEvent event) { |
| 76 |
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType()); |
89 |
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType()); |
| 77 |
if (manager != null) |
90 |
Preferences node = getRepositoryPreferenceNode(null, event.getRepositoryLocation(), event.getRepositoryType()); |
| 78 |
manager.addRepository(event.getRepositoryLocation()); |
91 |
|
|
|
92 |
int count = getRepositoryCount(node); |
| 93 |
if (manager.contains(event.getRepositoryLocation())) { |
| 94 |
// If a user as added a repository we need to set the initial count manually |
| 95 |
if (count == 0) |
| 96 |
count = 1; |
| 97 |
} else { |
| 98 |
if (manager != null) |
| 99 |
manager.addRepository(event.getRepositoryLocation()); |
| 100 |
} |
| 101 |
// increment the counter & send to preferences |
| 102 |
count++; |
| 103 |
setRepositoryCount(node, count); |
| 104 |
|
| 79 |
if (!event.isRepositoryEnabled()) |
105 |
if (!event.isRepositoryEnabled()) |
| 80 |
manager.setEnabled(event.getRepositoryLocation(), false); |
106 |
manager.setEnabled(event.getRepositoryLocation(), false); |
| 81 |
} |
107 |
} |
|
Lines 106-127
Link Here
|
| 106 |
} |
132 |
} |
| 107 |
|
133 |
|
| 108 |
/** |
134 |
/** |
| 109 |
* Encodes a URI as a string, in a form suitable for storing in a comma-separated |
|
|
| 110 |
* list of location strings. Any comma character in the local string is encoded. |
| 111 |
*/ |
| 112 |
private String encodeURI(URI repositoryLocation) { |
| 113 |
char[] chars = repositoryLocation.toString().toCharArray(); |
| 114 |
StringBuffer result = new StringBuffer(chars.length); |
| 115 |
for (int i = 0; i < chars.length; i++) { |
| 116 |
if (chars[i] == ',') |
| 117 |
result.append("${#").append(Integer.toString(chars[i])).append('}'); //$NON-NLS-1$ |
| 118 |
else |
| 119 |
result.append(chars[i]); |
| 120 |
} |
| 121 |
return result.toString(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the id of this action. |
135 |
* Returns the id of this action. |
| 126 |
*/ |
136 |
*/ |
| 127 |
protected abstract String getId(); |
137 |
protected abstract String getId(); |
|
Lines 149-156
Link Here
|
| 149 |
*/ |
159 |
*/ |
| 150 |
protected void removeFromSelf(RepositoryEvent event) { |
160 |
protected void removeFromSelf(RepositoryEvent event) { |
| 151 |
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType()); |
161 |
IRepositoryManager manager = getRepositoryManager(event.getRepositoryType()); |
| 152 |
if (manager != null) |
162 |
Preferences node = getRepositoryPreferenceNode(null, event.getRepositoryLocation(), event.getRepositoryType()); |
|
|
163 |
int count = getRepositoryCount(node); |
| 164 |
if ((--count < 1) && (manager != null)) |
| 153 |
manager.removeRepository(event.getRepositoryLocation()); |
165 |
manager.removeRepository(event.getRepositoryLocation()); |
|
|
166 |
setRepositoryCount(node, count); |
| 154 |
} |
167 |
} |
| 155 |
|
168 |
|
| 156 |
/** |
169 |
/** |
|
Lines 159-187
Link Here
|
| 159 |
* this profile. |
172 |
* this profile. |
| 160 |
*/ |
173 |
*/ |
| 161 |
protected void removeRepositoryFromProfile(Profile profile, URI location, int type) { |
174 |
protected void removeRepositoryFromProfile(Profile profile, URI location, int type) { |
| 162 |
String key = type == IRepository.TYPE_METADATA ? IProfile.PROP_METADATA_REPOSITORIES : IProfile.PROP_ARTIFACT_REPOSITORIES; |
175 |
Preferences node = getRepositoryPreferenceNode(profile, location, type); |
| 163 |
String encodedURI = encodeURI(location); |
176 |
|
| 164 |
String currentRepos = profile.getProperty(key); |
177 |
int count = getRepositoryCount(node); |
| 165 |
//if this profile has no associated repositories, we are done |
178 |
if (--count < 1) { |
| 166 |
if (currentRepos == null) |
179 |
// TODO: Remove all associated values |
| 167 |
return; |
180 |
try { |
| 168 |
//find the matching location, if any |
181 |
String[] keys = node.keys(); |
| 169 |
StringTokenizer tokens = new StringTokenizer(currentRepos, ","); //$NON-NLS-1$ |
182 |
|
| 170 |
StringBuffer result = new StringBuffer(currentRepos.length()); |
183 |
for (int i = 0; i < keys.length; i++) |
| 171 |
boolean found = false; |
184 |
node.remove(keys[i]); |
| 172 |
while (tokens.hasMoreTokens()) { |
185 |
} catch (BackingStoreException e) { |
| 173 |
final String nextLocation = tokens.nextToken(); |
186 |
// TODO: Should this be passed back to be associated with State? |
| 174 |
if (nextLocation.equals(encodedURI)) { |
|
|
| 175 |
found = true; |
| 176 |
} else { |
| 177 |
//add back any location not being removed |
| 178 |
result.append(nextLocation); |
| 179 |
if (tokens.hasMoreTokens()) |
| 180 |
result.append(','); |
| 181 |
} |
187 |
} |
|
|
188 |
|
| 189 |
} else |
| 190 |
setRepositoryCount(node, count); |
| 191 |
|
| 192 |
try { |
| 193 |
node.flush(); |
| 194 |
} catch (BackingStoreException e) { |
| 195 |
// TODO: perhaps an Exception should be passed backwards and associated with State |
| 182 |
} |
196 |
} |
| 183 |
if (!found) |
197 |
} |
| 184 |
return; |
198 |
|
| 185 |
profile.setProperty(key, result.toString()); |
199 |
/* |
|
|
200 |
* Get the counter associated with a repository |
| 201 |
*/ |
| 202 |
protected int getRepositoryCount(Preferences node) { |
| 203 |
return node.getInt(REPOSITORY_COUNT, 0); |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* Sets the counter associated with this repository to a specific value |
| 208 |
*/ |
| 209 |
protected void setRepositoryCount(Preferences node, int count) { |
| 210 |
if (count < 1) |
| 211 |
node.remove(REPOSITORY_COUNT); |
| 212 |
else |
| 213 |
node.putInt(REPOSITORY_COUNT, count); |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* Determine if a repository is already known |
| 218 |
*/ |
| 219 |
protected boolean repositoryExists(Preferences node) { |
| 220 |
if (node.get(KEY_URI, null) == null) |
| 221 |
return false; |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Get the preference node associated with profile & location |
| 227 |
*/ |
| 228 |
protected Preferences getRepositoryPreferenceNode(Profile profile, URI location, int type) { |
| 229 |
String key = type == IRepository.TYPE_METADATA ? METADATA_REPOSITORY : ARTIFACT_REPOSITORY; |
| 230 |
IPreferencesService prefService = (IPreferencesService) ServiceHelper.getService(Activator.getContext(), IPreferencesService.class.getName()); |
| 231 |
|
| 232 |
if (profile != null) |
| 233 |
return prefService.getRootNode().node("/profile/" + profile.getProfileId() + "/" + key + "/" + NODE_REPOSITORIES + "/" + getKey(location)); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ |
| 234 |
return prefService.getRootNode().node("/profile/_SELF_/" + key + "/" + NODE_REPOSITORIES + "/" + getKey(location)); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* Copied from AbstractRepositoryManager |
| 239 |
*/ |
| 240 |
private String getKey(URI location) { |
| 241 |
String key = location.toString().replace('/', '_'); |
| 242 |
//remove trailing slash |
| 243 |
if (key.endsWith("_")) //$NON-NLS-1$ |
| 244 |
key = key.substring(0, key.length() - 1); |
| 245 |
return key; |
| 186 |
} |
246 |
} |
| 187 |
} |
247 |
} |