use of org.eclipse.equinox.internal.p2.updatesite.metadata.UpdateSiteMetadataRepositoryFactory in project webtools.servertools by eclipse.
the class ExtensionUpdateSite method getExtensions.
public List<IServerExtension> getExtensions(IProgressMonitor monitor) throws CoreException, ProvisionException {
URI url2 = null;
IMetadataRepositoryManager manager = null;
IProvisioningAgent agent = null;
List<IServerExtension> list = new ArrayList<IServerExtension>();
URI[] existingSites = null;
try {
/*
* To discovery the server adapter, there are three methods:
* 1. Looking at the site.xml (if it exists). This is the legacy method
* 2. Looking for the org.eclipse.wst.server.core.serverAdapter property in a p2
* update site (that may not have a site.xml). The property is necessary to identify
* the InstallableUnit as a server type. Otherwise, all the InstallableUnits will show
* up regardless of whether it is a server or not
* 3. If the user created the p2 update site using a category.xml file (migrating old site.xml
* to use category.xml)
*/
BundleContext bd = org.eclipse.wst.server.discovery.internal.Activator.getDefault().getBundle().getBundleContext();
agent = ExtensionUtility.getAgent(bd);
url2 = new URI(url);
// Method 1: Looking at the site.xml
UpdateSiteMetadataRepositoryFactory mrf = new UpdateSiteMetadataRepositoryFactory();
mrf.setAgent(ExtensionUtility.getAgent(bd));
manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
// Sites already existing for both enabled and disabled
URI[] existingSitesAll = manager.getKnownRepositories(IMetadataRepositoryManager.REPOSITORIES_ALL);
URI[] existingSitesDisabled = manager.getKnownRepositories(IMetadataRepositoryManager.REPOSITORIES_DISABLED);
int existingSitesAllLen = existingSitesAll == null ? 0 : existingSitesAll.length;
int existingSitesDisabledLen = existingSitesDisabled == null ? 0 : existingSitesDisabled.length;
existingSites = new URI[existingSitesAllLen + existingSitesDisabledLen];
if (existingSitesAllLen > 0) {
System.arraycopy(existingSitesAll, 0, existingSites, 0, existingSitesAllLen);
}
if (existingSitesDisabledLen > 0) {
System.arraycopy(existingSitesDisabled, 0, existingSites, existingSitesAllLen, existingSitesDisabledLen);
}
// If the site.xml does not exist, the load will throw a org.eclipse.equinox.p2.core.ProvisionException
try {
IMetadataRepository repo = mrf.load(url2, IRepositoryManager.REPOSITORIES_ALL, monitor);
// $NON-NLS-1$
IQuery<IInstallableUnit> query = QueryUtil.createMatchQuery("id ~=/*org.eclipse.wst.server.core.serverAdapter/");
list = getInstallableUnits(repo, query, url2, monitor);
} catch (ProvisionException pe) {
// $NON-NLS-1$
Trace.trace(Trace.WARNING, "Error getting update site information", pe);
}
// specifying any server adapters there or no site.xml exists)
if (list.isEmpty()) {
manager.addRepository(url2);
// Need to query for all IUs
IQuery<IInstallableUnit> query = QueryUtil.createIUAnyQuery();
IMetadataRepository repo = manager.loadRepository(url2, monitor);
List<IServerExtension> list2 = getInstallableUnits(repo, query, url2, monitor);
int size = list2.size();
for (int i = 0; i < size; i++) {
Extension e = (Extension) list2.get(i);
IInstallableUnit[] iuArr = e.getIUs();
if (iuArr != null && iuArr.length > 0) {
if (iuArr[0] != null) {
if (iuArr[0].getProperty(SERVER_ADAPTER_ID) != null) {
list.add(e);
}
}
}
}
}
// a provider property with org.eclipse.wst.server.core.serverAdapter
if (list.isEmpty()) {
manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
manager.addRepository(url2);
// $NON-NLS-1$
IQuery<IInstallableUnit> query = QueryUtil.createMatchQuery("id ~=/*org.eclipse.wst.server.core.serverAdapter/");
IMetadataRepository repo = manager.loadRepository(url2, monitor);
list = getInstallableUnits(repo, query, url2, monitor);
}
return list;
} catch (ProvisionException e) {
// $NON-NLS-1$
Trace.trace(Trace.WARNING, "Error getting update info", e);
throw e;
} catch (Exception e) {
// $NON-NLS-1$
Trace.trace(Trace.WARNING, "Error getting update info", e);
return new ArrayList<IServerExtension>(0);
} finally {
if (url2 != null && url2.getPath().length() != 0 && manager != null) {
if (existingSites != null && existingSites.length > 0) {
boolean urlExists = false;
for (URI uri : existingSites) {
if (uri.getPath().equals(url2.getPath())) {
urlExists = true;
break;
}
}
// If site did not exist before, remove it as it was added with load
if (!urlExists) {
manager.removeRepository(url2);
}
}
}
}
}
Aggregations