1994
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
*/
|
|
13 |
|
|
14 |
package com.nokia.carbide.internal.discovery.ui.p2;
|
|
15 |
|
|
16 |
import java.io.File;
|
|
17 |
import java.io.FileNotFoundException;
|
|
18 |
import java.net.URI;
|
|
19 |
|
|
20 |
import org.eclipse.core.filesystem.URIUtil;
|
|
21 |
import org.eclipse.core.runtime.CoreException;
|
|
22 |
import org.eclipse.core.runtime.IPath;
|
|
23 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
24 |
import org.eclipse.core.runtime.IStatus;
|
|
25 |
import org.eclipse.core.runtime.Path;
|
|
26 |
import org.eclipse.core.runtime.Status;
|
|
27 |
import org.eclipse.equinox.internal.provisional.p2.director.IDirector;
|
|
28 |
import org.eclipse.equinox.internal.provisional.p2.director.ProfileChangeRequest;
|
|
29 |
import org.eclipse.equinox.p2.core.IProvisioningAgent;
|
|
30 |
import org.eclipse.equinox.p2.engine.IProfile;
|
|
31 |
import org.eclipse.equinox.p2.engine.IProfileRegistry;
|
|
32 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
|
|
33 |
import org.eclipse.equinox.p2.query.IQueryResult;
|
|
34 |
import org.eclipse.equinox.p2.query.QueryUtil;
|
|
35 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
|
|
36 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
|
|
37 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
|
|
38 |
import org.eclipse.equinox.p2.ui.ProvisioningUI;
|
|
39 |
|
|
40 |
import com.nokia.carbide.discovery.ui.Activator;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Installer to install features from a p2 repository at a supplied URL or directory.
|
|
44 |
*
|
|
45 |
* The supplied directory should have as sub-directories features and plugins. These
|
|
46 |
* sub-directories in turn can contain any number of features and their corresponding
|
|
47 |
* plug-ins.
|
|
48 |
*
|
|
49 |
* <p/>
|
|
50 |
* e.g.
|
|
51 |
* <p/>
|
|
52 |
* <pre>
|
|
53 |
* <Feature Directory>
|
|
54 |
* |
|
|
55 |
* | --- artifacts.jar
|
|
56 |
* | --- content.jar
|
|
57 |
* |
|
|
58 |
* ----<features>
|
|
59 |
* |
|
|
60 |
* |----<feature folder name>
|
|
61 |
* |
|
|
62 |
* |---features.xml file (contains list of feature jars)
|
|
63 |
* ----<plugins>
|
|
64 |
* |
|
|
65 |
* ---jar files;
|
|
66 |
* </pre>
|
|
67 |
*/
|
|
68 |
|
|
69 |
@SuppressWarnings("restriction")
|
|
70 |
public class DynamicP2Installer {
|
|
71 |
|
|
72 |
private IPath repositoryLocation;
|
|
73 |
private ProvisioningUI provisioningUI;
|
|
74 |
private IProvisioningAgent agent;
|
|
75 |
private IProfile profile;
|
|
76 |
|
|
77 |
/**
|
|
78 |
* Performs the install.
|
|
79 |
* @param monitor An implementation of IProgressMonitor, usually showing the
|
|
80 |
* installation progress as a Progress Bar to the user.
|
|
81 |
*/
|
|
82 |
public static IStatus install(File repositoryDirectory, IProgressMonitor monitor) {
|
|
83 |
try {
|
|
84 |
DynamicP2Installer installer = new DynamicP2Installer(repositoryDirectory);
|
|
85 |
installer.doInstall(monitor);
|
|
86 |
return Status.OK_STATUS;
|
|
87 |
} catch (CoreException e) {
|
|
88 |
return e.getStatus();
|
|
89 |
} catch (FileNotFoundException e) {
|
|
90 |
return Activator.makeErrorStatus(null, e);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
private DynamicP2Installer(File repositoryDirectory) throws FileNotFoundException {
|
|
95 |
checkRepository(repositoryDirectory);
|
|
96 |
repositoryLocation = new Path(repositoryDirectory.getAbsolutePath());
|
|
97 |
provisioningUI = ProvisioningUI.getDefaultUI();
|
|
98 |
agent = provisioningUI.getSession().getProvisioningAgent();
|
|
99 |
profile = getProfile();
|
|
100 |
}
|
|
101 |
|
|
102 |
private void checkRepository(File repositoryDirectory) throws FileNotFoundException {
|
|
103 |
if (!repositoryDirectory.isDirectory()) {
|
|
104 |
throw new FileNotFoundException(repositoryDirectory + " is not a directory.");
|
|
105 |
}
|
|
106 |
if (!(new File(repositoryDirectory, "features").isDirectory())) {
|
|
107 |
throw new FileNotFoundException(repositoryDirectory + " missing \"features\" directory.");
|
|
108 |
}
|
|
109 |
if (!(new File(repositoryDirectory, "plugins").isDirectory())) {
|
|
110 |
throw new FileNotFoundException(repositoryDirectory + " missing \"plugins\" directory.");
|
|
111 |
}
|
|
112 |
if (!(new File(repositoryDirectory, "artifacts.jar").exists()) && !(new File(repositoryDirectory, "artifacts.xml").exists())) {
|
|
113 |
throw new FileNotFoundException(repositoryDirectory + " missing artifacts.jar.");
|
|
114 |
}
|
|
115 |
if (!(new File(repositoryDirectory, "content.jar").exists()) && !(new File(repositoryDirectory, "content.xml").exists())) {
|
|
116 |
throw new FileNotFoundException(repositoryDirectory + " missing content.jar.");
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
private void doInstall(IProgressMonitor monitor) throws CoreException {
|
|
121 |
URI uri = URIUtil.toURI(repositoryLocation);
|
|
122 |
IMetadataRepositoryManager metadataRepoManager =
|
|
123 |
(IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
|
|
124 |
IArtifactRepositoryManager artifactRepoManager =
|
|
125 |
(IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
|
|
126 |
try {
|
|
127 |
// add and load repository
|
|
128 |
metadataRepoManager.addRepository(uri);
|
|
129 |
IMetadataRepository metadataRepository = metadataRepoManager.loadRepository(uri, null);
|
|
130 |
artifactRepoManager.addRepository(uri);
|
|
131 |
artifactRepoManager.loadRepository(uri, null);
|
|
132 |
|
|
133 |
// get IU from repository
|
|
134 |
IQueryResult<IInstallableUnit> units = metadataRepository.query(QueryUtil.createIUGroupQuery(), monitor);
|
|
135 |
if (units.isEmpty())
|
|
136 |
throw new CoreException(Activator.makeErrorStatus("Could not find installable unit", null));
|
|
137 |
|
|
138 |
// check if installed
|
|
139 |
IQueryResult<IInstallableUnit> result = profile.query(QueryUtil.createIUQuery(units.iterator().next()), monitor);
|
|
140 |
if (!result.isEmpty())
|
|
141 |
throw new CoreException(Activator.makeStatus(IStatus.CANCEL, null, null)); // already installed
|
|
142 |
|
|
143 |
// do provisioning operation
|
|
144 |
ProfileChangeRequest request = new ProfileChangeRequest(profile);
|
|
145 |
request.addAll(units.toUnmodifiableSet());
|
|
146 |
IDirector director = (IDirector) agent.getService(IDirector.SERVICE_NAME);
|
|
147 |
IStatus status = director.provision(request, null, monitor);
|
|
148 |
|
|
149 |
if (!status.isOK())
|
|
150 |
throw new CoreException(status);
|
|
151 |
}
|
|
152 |
finally {
|
|
153 |
metadataRepoManager.removeRepository(uri);
|
|
154 |
artifactRepoManager.removeRepository(uri);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
private IProfile getProfile() {
|
|
159 |
IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
|
|
160 |
return profileRegistry.getProfile(provisioningUI.getProfileId());
|
|
161 |
}
|
|
162 |
|
|
163 |
}
|