Skip to content

Commit

Permalink
SONAR-6376 ws plugins/installed and plugins/pending add category field
Browse files Browse the repository at this point in the history
- use Optional when retrieving an UpdateCenter
  • Loading branch information
teryk committed Jul 21, 2015
1 parent 3d4701b commit 24d686a
Show file tree
Hide file tree
Showing 23 changed files with 426 additions and 209 deletions.
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.platform.ws; package org.sonar.server.platform.ws;


import com.google.common.base.Optional;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import java.util.List; import java.util.List;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
Expand Down Expand Up @@ -82,18 +83,22 @@ public void handle(Request request, Response response) throws Exception {
private void writeResponse(JsonWriter jsonWriter) { private void writeResponse(JsonWriter jsonWriter) {
jsonWriter.beginObject(); jsonWriter.beginObject();


UpdateCenter updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH); Optional<UpdateCenter> updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH);
writeUpgrades(jsonWriter, updateCenter); writeUpgrades(jsonWriter, updateCenter);
jsonWriter.propDateTime(PROPERTY_UPDATE_CENTER_REFRESH, updateCenter.getDate()); if (updateCenter.isPresent()) {
jsonWriter.propDateTime(PROPERTY_UPDATE_CENTER_REFRESH, updateCenter.get().getDate());
}


jsonWriter.endObject(); jsonWriter.endObject();
} }


private void writeUpgrades(JsonWriter jsonWriter, UpdateCenter updateCenter) { private void writeUpgrades(JsonWriter jsonWriter, Optional<UpdateCenter> updateCenter) {
jsonWriter.name(ARRAY_UPGRADES).beginArray(); jsonWriter.name(ARRAY_UPGRADES).beginArray();


for (SonarUpdate sonarUpdate : updateCenter.findSonarUpdates()) { if (updateCenter.isPresent()) {
writeUpgrade(jsonWriter, sonarUpdate); for (SonarUpdate sonarUpdate : updateCenter.get().findSonarUpdates()) {
writeUpgrade(jsonWriter, sonarUpdate);
}
} }


jsonWriter.endArray(); jsonWriter.endArray();
Expand Down Expand Up @@ -132,7 +137,7 @@ private void writePluginsToUpdate(JsonWriter jsonWriter, List<Release> pluginsTo
for (Release release : pluginsToUpgrade) { for (Release release : pluginsToUpgrade) {
jsonWriter.beginObject(); jsonWriter.beginObject();


pluginWSCommons.writeMetadata(jsonWriter, (Plugin) release.getArtifact()); pluginWSCommons.writePlugin(jsonWriter, (Plugin) release.getArtifact());
jsonWriter.prop(PROPERTY_VERSION, release.getVersion().toString()); jsonWriter.prop(PROPERTY_VERSION, release.getVersion().toString());


jsonWriter.endObject(); jsonWriter.endObject();
Expand All @@ -146,9 +151,7 @@ private void writeIncompatiblePlugins(JsonWriter jsonWriter, List<Plugin> incomp


for (Plugin incompatiblePlugin : incompatiblePlugins) { for (Plugin incompatiblePlugin : incompatiblePlugins) {
jsonWriter.beginObject(); jsonWriter.beginObject();

pluginWSCommons.writePlugin(jsonWriter, incompatiblePlugin);
pluginWSCommons.writeMetadata(jsonWriter, incompatiblePlugin);

jsonWriter.endObject(); jsonWriter.endObject();
} }


Expand Down
Expand Up @@ -19,6 +19,14 @@
*/ */
package org.sonar.server.plugins; package org.sonar.server.plugins;


import com.google.common.base.Optional;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.picocontainer.Startable; import org.picocontainer.Startable;
import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.HttpDownloader;
Expand All @@ -28,16 +36,9 @@
import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginInfo;
import org.sonar.server.platform.DefaultServerFileSystem; import org.sonar.server.platform.DefaultServerFileSystem;
import org.sonar.updatecenter.common.Release; import org.sonar.updatecenter.common.Release;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.Version; import org.sonar.updatecenter.common.Version;


import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayList;
import static org.apache.commons.io.FileUtils.cleanDirectory; import static org.apache.commons.io.FileUtils.cleanDirectory;
Expand Down Expand Up @@ -120,15 +121,17 @@ public Collection<PluginInfo> getDownloadedPlugins() {
} }


public void download(String pluginKey, Version version) { public void download(String pluginKey, Version version) {
for (Release release : updateCenterMatrixFactory.getUpdateCenter(true).findInstallablePlugins(pluginKey, version)) { Optional<UpdateCenter> updateCenter = updateCenterMatrixFactory.getUpdateCenter(true);
try { if (updateCenter.isPresent()) {
downloadRelease(release); for (Release release : updateCenter.get().findInstallablePlugins(pluginKey, version)) {

try {
} catch (Exception e) { downloadRelease(release);
String message = String.format("Fail to download the plugin (%s, version %s) from %s (error is : %s)", } catch (Exception e) {
release.getArtifact().getKey(), release.getVersion().getName(), release.getDownloadUrl(), e.getMessage()); String message = String.format("Fail to download the plugin (%s, version %s) from %s (error is : %s)",
LOG.debug(message, e); release.getArtifact().getKey(), release.getVersion().getName(), release.getDownloadUrl(), e.getMessage());
throw new SonarException(message, e); LOG.debug(message, e);
throw new SonarException(message, e);
}
} }
} }
} }
Expand Down
Expand Up @@ -19,6 +19,12 @@
*/ */
package org.sonar.server.plugins; package org.sonar.server.plugins;


import com.google.common.base.Optional;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.sonar.api.Properties; import org.sonar.api.Properties;
import org.sonar.api.Property; import org.sonar.api.Property;
Expand All @@ -30,12 +36,6 @@
import org.sonar.updatecenter.common.UpdateCenterDeserializer; import org.sonar.updatecenter.common.UpdateCenterDeserializer;
import org.sonar.updatecenter.common.UpdateCenterDeserializer.Mode; import org.sonar.updatecenter.common.UpdateCenterDeserializer.Mode;


import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/** /**
* HTTP client to load data from the remote update center hosted at http://update.sonarsource.org. * HTTP client to load data from the remote update center hosted at http://update.sonarsource.org.
* *
Expand Down Expand Up @@ -68,25 +68,31 @@ public class UpdateCenterClient {


private final URI uri; private final URI uri;
private final UriReader uriReader; private final UriReader uriReader;
private final boolean isActivated;
private UpdateCenter pluginCenter = null; private UpdateCenter pluginCenter = null;
private long lastRefreshDate = 0; private long lastRefreshDate = 0;


public UpdateCenterClient(UriReader uriReader, Settings settings) throws URISyntaxException { public UpdateCenterClient(UriReader uriReader, Settings settings) throws URISyntaxException {
this.uriReader = uriReader; this.uriReader = uriReader;
this.uri = new URI(settings.getString(URL_PROPERTY)); this.uri = new URI(settings.getString(URL_PROPERTY));
this.isActivated = settings.getBoolean(ACTIVATION_PROPERTY);
Loggers.get(getClass()).info("Update center: " + uriReader.description(uri)); Loggers.get(getClass()).info("Update center: " + uriReader.description(uri));
} }


public UpdateCenter getUpdateCenter() { public Optional<UpdateCenter> getUpdateCenter() {
return getUpdateCenter(false); return getUpdateCenter(false);
} }


public UpdateCenter getUpdateCenter(boolean forceRefresh) { public Optional<UpdateCenter> getUpdateCenter(boolean forceRefresh) {
if (!isActivated) {
return Optional.absent();
}

if (pluginCenter == null || forceRefresh || needsRefresh()) { if (pluginCenter == null || forceRefresh || needsRefresh()) {
pluginCenter = init(); pluginCenter = init();
lastRefreshDate = System.currentTimeMillis(); lastRefreshDate = System.currentTimeMillis();
} }
return pluginCenter; return Optional.fromNullable(pluginCenter);
} }


public Date getLastRefreshDate() { public Date getLastRefreshDate() {
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.plugins; package org.sonar.server.plugins;


import com.google.common.base.Optional;
import org.sonar.api.platform.Server; import org.sonar.api.platform.Server;
import org.sonar.updatecenter.common.UpdateCenter; import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.Version; import org.sonar.updatecenter.common.Version;
Expand All @@ -33,20 +34,19 @@ public class UpdateCenterMatrixFactory {
private final InstalledPluginReferentialFactory installedPluginReferentialFactory; private final InstalledPluginReferentialFactory installedPluginReferentialFactory;


public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, Server server, public UpdateCenterMatrixFactory(UpdateCenterClient centerClient, Server server,
InstalledPluginReferentialFactory installedPluginReferentialFactory) { InstalledPluginReferentialFactory installedPluginReferentialFactory) {
this.centerClient = centerClient; this.centerClient = centerClient;
this.installedPluginReferentialFactory = installedPluginReferentialFactory; this.installedPluginReferentialFactory = installedPluginReferentialFactory;
this.sonarVersion = Version.create(server.getVersion()); this.sonarVersion = Version.create(server.getVersion());
} }


public UpdateCenter getUpdateCenter(boolean refreshUpdateCenter) { public Optional<UpdateCenter> getUpdateCenter(boolean refreshUpdateCenter) {
UpdateCenter updatePluginCenter = centerClient.getUpdateCenter(refreshUpdateCenter); Optional<UpdateCenter> updatePluginCenter = centerClient.getUpdateCenter(refreshUpdateCenter);
if (updatePluginCenter != null) { if (updatePluginCenter.isPresent()) {
return updatePluginCenter.setInstalledSonarVersion(sonarVersion).registerInstalledPlugins( return Optional.of(updatePluginCenter.get().setInstalledSonarVersion(sonarVersion).registerInstalledPlugins(
installedPluginReferentialFactory.getInstalledPluginReferential()) installedPluginReferentialFactory.getInstalledPluginReferential())
.setDate(centerClient.getLastRefreshDate()); .setDate(centerClient.getLastRefreshDate()));
} }
return null; return Optional.absent();
} }
} }

Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.plugins.ws; package org.sonar.server.plugins.ws;


import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.ImmutableSortedSet;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import java.util.Collection; import java.util.Collection;
Expand Down Expand Up @@ -69,20 +70,21 @@ public void handle(Request request, Response response) throws Exception {
JsonWriter jsonWriter = response.newJsonWriter(); JsonWriter jsonWriter = response.newJsonWriter();
jsonWriter.beginObject(); jsonWriter.beginObject();


UpdateCenter updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH); Optional<UpdateCenter> updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH);


writePlugins(jsonWriter, updateCenter); writePlugins(jsonWriter, updateCenter);

pluginWSCommons.writeUpdateCenterProperties(jsonWriter, updateCenter); pluginWSCommons.writeUpdateCenterProperties(jsonWriter, updateCenter);


jsonWriter.endObject(); jsonWriter.endObject();
jsonWriter.close(); jsonWriter.close();
} }


private void writePlugins(JsonWriter jsonWriter, UpdateCenter updateCenter) { private void writePlugins(JsonWriter jsonWriter, Optional<UpdateCenter> updateCenter) {
jsonWriter.name(ARRAY_PLUGINS).beginArray(); jsonWriter.name(ARRAY_PLUGINS).beginArray();
for (PluginUpdate pluginUpdate : retrieveAvailablePlugins(updateCenter)) { if (updateCenter.isPresent()) {
pluginWSCommons.writePluginUpdate(jsonWriter, pluginUpdate); for (PluginUpdate pluginUpdate : retrieveAvailablePlugins(updateCenter.get())) {
pluginWSCommons.writePluginUpdate(jsonWriter, pluginUpdate);
}
} }
jsonWriter.endArray(); jsonWriter.endArray();
} }
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.plugins.ws; package org.sonar.server.plugins.ws;


import com.google.common.base.Optional;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import javax.annotation.Nullable; import javax.annotation.Nullable;
Expand All @@ -30,6 +31,7 @@
import org.sonar.server.plugins.UpdateCenterMatrixFactory; import org.sonar.server.plugins.UpdateCenterMatrixFactory;
import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSession;
import org.sonar.updatecenter.common.PluginUpdate; import org.sonar.updatecenter.common.PluginUpdate;
import org.sonar.updatecenter.common.UpdateCenter;


import static java.lang.String.format; import static java.lang.String.format;


Expand Down Expand Up @@ -77,15 +79,21 @@ public void handle(Request request, Response response) throws Exception {
} }


private PluginUpdate findAvailablePluginByKey(String key) { private PluginUpdate findAvailablePluginByKey(String key) {
PluginUpdate pluginUpdate = Iterables.find( PluginUpdate pluginUpdate = MISSING_PLUGIN;
updateCenterFactory.getUpdateCenter(false).findAvailablePlugins(),
hasKey(key), Optional<UpdateCenter> updateCenter = updateCenterFactory.getUpdateCenter(false);
MISSING_PLUGIN if (updateCenter.isPresent()) {
); pluginUpdate= Iterables.find(
updateCenter.get().findAvailablePlugins(),
hasKey(key),
MISSING_PLUGIN);
}

if (pluginUpdate == MISSING_PLUGIN) { if (pluginUpdate == MISSING_PLUGIN) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
format("No plugin with key '%s' or plugin '%s' is already installed in latest version", key, key)); format("No plugin with key '%s' or plugin '%s' is already installed in latest version", key, key));
} }

return pluginUpdate; return pluginUpdate;
} }


Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.plugins.ws; package org.sonar.server.plugins.ws;


import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import java.util.Collection; import java.util.Collection;
import java.util.SortedSet; import java.util.SortedSet;
Expand All @@ -28,9 +29,12 @@
import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.utils.text.JsonWriter;
import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginInfo;
import org.sonar.server.plugins.ServerPluginRepository; import org.sonar.server.plugins.ServerPluginRepository;
import org.sonar.server.plugins.UpdateCenterMatrixFactory;
import org.sonar.updatecenter.common.Plugin;


import static com.google.common.collect.ImmutableSortedSet.copyOf; import static com.google.common.collect.ImmutableSortedSet.copyOf;
import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_PLUGIN_METADATA_COMPARATOR; import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_PLUGIN_METADATA_COMPARATOR;
import static org.sonar.server.plugins.ws.PluginWSCommons.compatiblePluginsByKey;


/** /**
* Implementation of the {@code installed} action for the Plugins WebService. * Implementation of the {@code installed} action for the Plugins WebService.
Expand All @@ -40,10 +44,12 @@ public class InstalledAction implements PluginsWsAction {


private final ServerPluginRepository pluginRepository; private final ServerPluginRepository pluginRepository;
private final PluginWSCommons pluginWSCommons; private final PluginWSCommons pluginWSCommons;
private final UpdateCenterMatrixFactory updateCenterMatrixFactory;


public InstalledAction(ServerPluginRepository pluginRepository, PluginWSCommons pluginWSCommons) { public InstalledAction(ServerPluginRepository pluginRepository, PluginWSCommons pluginWSCommons, UpdateCenterMatrixFactory updateCenterMatrixFactory) {
this.pluginRepository = pluginRepository; this.pluginRepository = pluginRepository;
this.pluginWSCommons = pluginWSCommons; this.pluginWSCommons = pluginWSCommons;
this.updateCenterMatrixFactory = updateCenterMatrixFactory;
} }


@Override @Override
Expand All @@ -57,28 +63,24 @@ public void define(WebService.NewController controller) {


@Override @Override
public void handle(Request request, Response response) throws Exception { public void handle(Request request, Response response) throws Exception {
Collection<PluginInfo> infos = retrieveAndSortPluginMetadata(); Collection<PluginInfo> pluginInfoList = searchPluginInfoList();


JsonWriter jsonWriter = response.newJsonWriter(); JsonWriter jsonWriter = response.newJsonWriter();
jsonWriter.setSerializeEmptys(false); jsonWriter.setSerializeEmptys(false);
jsonWriter.beginObject(); jsonWriter.beginObject();


writeMetadataList(jsonWriter, infos); writePluginInfoList(jsonWriter, pluginInfoList);


jsonWriter.endObject(); jsonWriter.endObject();
jsonWriter.close(); jsonWriter.close();
} }


private SortedSet<PluginInfo> retrieveAndSortPluginMetadata() { private SortedSet<PluginInfo> searchPluginInfoList() {
return copyOf(NAME_KEY_PLUGIN_METADATA_COMPARATOR, pluginRepository.getPluginInfos()); return copyOf(NAME_KEY_PLUGIN_METADATA_COMPARATOR, pluginRepository.getPluginInfos());
} }


private void writeMetadataList(JsonWriter jsonWriter, Collection<PluginInfo> pluginMetadatas) { private void writePluginInfoList(JsonWriter jsonWriter, Collection<PluginInfo> pluginInfoList) {
jsonWriter.name(ARRAY_PLUGINS); ImmutableMap<String, Plugin> compatiblesPluginsByKeys = compatiblePluginsByKey(updateCenterMatrixFactory);
jsonWriter.beginArray(); pluginWSCommons.writePluginInfoList(jsonWriter, pluginInfoList, compatiblesPluginsByKeys, ARRAY_PLUGINS);
for (PluginInfo pluginMetadata : pluginMetadatas) {
pluginWSCommons.writePluginMetadata(jsonWriter, pluginMetadata);
}
jsonWriter.endArray();
} }
} }

0 comments on commit 24d686a

Please sign in to comment.