inline UpdatesXmlLoader

This commit is contained in:
Vladimir Krivosheev
2014-11-11 18:10:02 +01:00
parent 16f81a97df
commit 3f40aa3cd8
2 changed files with 35 additions and 70 deletions
@@ -41,9 +41,7 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.StandardFileSystems;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Function;
import com.intellij.util.HttpRequests;
import com.intellij.util.PlatformUtils;
import com.intellij.util.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.UrlConnectionUtil;
import com.intellij.util.net.HttpConfigurable;
@@ -461,11 +459,44 @@ public final class UpdateChecker {
return pluginFile.getUrl();
}
@Nullable
private static UpdatesInfo loadUpdatesInfo(@Nullable final String updateUrl) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("load update xml (UPDATE_URL='" + updateUrl + "' )");
}
if (StringUtil.isEmpty(updateUrl)) {
LOG.debug("update url is empty: updates will not be checked");
return null;
}
return HttpRequests.request(updateUrl.startsWith("file:") ? updateUrl : updateUrl + '?' + prepareUpdateCheckArgs())
.connectTimeout(5 * Time.SECOND)
.readTimeout(5 * Time.SECOND)
.get(new ThrowableConvertor<URLConnection, UpdatesInfo, Exception>() {
@Override
public UpdatesInfo convert(URLConnection connection) throws Exception {
InputStream inputStream = connection.getInputStream();
try {
return new UpdatesInfo(JDOMUtil.loadDocument(inputStream).getRootElement());
}
catch (JDOMException e) {
// Broken xml downloaded. Don't bother telling user.
LOG.info(e);
return null;
}
finally {
inputStream.close();
}
}
});
}
@NotNull
private static CheckForUpdateResult checkForUpdates(final UpdateSettings settings) {
UpdatesInfo info;
try {
info = UpdatesXmlLoader.loadUpdatesInfo(getUpdateUrl());
info = loadUpdatesInfo(getUpdateUrl());
if (info == null) {
return new CheckForUpdateResult(UpdateStrategy.State.NOTHING_LOADED);
}
@@ -1,66 +0,0 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.updateSettings.impl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.HttpRequests;
import com.intellij.util.ThrowableConvertor;
import com.intellij.util.Time;
import org.jdom.JDOMException;
import org.jetbrains.annotations.Nullable;
import java.io.InputStream;
import java.net.URLConnection;
class UpdatesXmlLoader {
private static final Logger LOG = Logger.getInstance(UpdatesXmlLoader.class);
@Nullable
public static UpdatesInfo loadUpdatesInfo(@Nullable final String updateUrl) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("load update xml (UPDATE_URL='" + updateUrl + "' )");
}
if (StringUtil.isEmpty(updateUrl)) {
LOG.debug("update url is empty: updates will not be checked");
return null;
}
return HttpRequests.request(updateUrl.startsWith("file:") ? updateUrl : updateUrl + '?' + UpdateChecker.prepareUpdateCheckArgs())
.connectTimeout(5 * Time.SECOND)
.readTimeout(5 * Time.SECOND)
.get(new ThrowableConvertor<URLConnection, UpdatesInfo, Exception>() {
@Override
public UpdatesInfo convert(URLConnection connection) throws Exception {
InputStream inputStream = connection.getInputStream();
try {
return new UpdatesInfo(JDOMUtil.loadDocument(inputStream).getRootElement());
}
catch (JDOMException e) {
// Broken xml downloaded. Don't bother telling user.
LOG.info(e);
return null;
}
finally {
inputStream.close();
}
}
});
}
}