From e814020c243beed03b1590b356abbee2b2ddef4c Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Thu, 15 Nov 2012 16:00:50 +0400 Subject: [PATCH] downloading remote templates --- .../templates/RemoteTemplatesFactory.java | 63 +++++++++++++++++-- .../templates/TemplateModuleBuilder.java | 9 ++- .../intellij/platform/templates/templates.xml | 10 --- 3 files changed, 64 insertions(+), 18 deletions(-) delete mode 100644 java/idea-ui/src/com/intellij/platform/templates/templates.xml diff --git a/java/idea-ui/src/com/intellij/platform/templates/RemoteTemplatesFactory.java b/java/idea-ui/src/com/intellij/platform/templates/RemoteTemplatesFactory.java index 6d49a2b2f7e2..493755127536 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/RemoteTemplatesFactory.java +++ b/java/idea-ui/src/com/intellij/platform/templates/RemoteTemplatesFactory.java @@ -15,20 +15,28 @@ */ package com.intellij.platform.templates; +import com.intellij.ide.plugins.PluginManager; import com.intellij.ide.util.projectWizard.WizardContext; +import com.intellij.openapi.application.ApplicationInfo; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.extensions.PluginId; import com.intellij.openapi.module.ModuleType; import com.intellij.openapi.module.ModuleTypeManager; import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.util.io.StreamUtil; import com.intellij.platform.ProjectTemplate; import com.intellij.platform.ProjectTemplatesFactory; -import com.intellij.util.Function; +import com.intellij.util.NullableFunction; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.net.HttpConfigurable; import org.jdom.Element; import org.jdom.JDOMException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; import java.util.List; import java.util.zip.ZipInputStream; @@ -38,6 +46,8 @@ import java.util.zip.ZipInputStream; */ public class RemoteTemplatesFactory implements ProjectTemplatesFactory { + private static final String URL = "http://download.jetbrains.com/idea/project_templates/"; + @NotNull @Override public String[] getGroups() { @@ -47,21 +57,43 @@ public class RemoteTemplatesFactory implements ProjectTemplatesFactory { @NotNull @Override public ProjectTemplate[] createTemplates(String group, WizardContext context) { + InputStream stream = null; + HttpURLConnection connection = null; + String code = ApplicationInfo.getInstance().getBuild().getProductCode(); try { - return createFromText(""); + connection = getConnection(code + "_templates.xml"); + stream = connection.getInputStream(); + String text = StreamUtil.readText(stream); + return createFromText(text); } catch (Exception e) { + LOG.error(e); return ProjectTemplate.EMPTY_ARRAY; } + finally { + StreamUtil.closeStream(stream); + if (connection != null) { + connection.disconnect(); + } + } } + @SuppressWarnings("unchecked") public static ProjectTemplate[] createFromText(String text) throws IOException, JDOMException { - @SuppressWarnings("unchecked") - List templates = JDOMUtil.loadDocument(text).getRootElement().getChildren("template"); - return ContainerUtil.map2Array(templates, ProjectTemplate.class, new Function() { + + List elements = JDOMUtil.loadDocument(text).getRootElement().getChildren("template"); + + List templates = ContainerUtil.mapNotNull(elements, new NullableFunction() { @Override public ProjectTemplate fun(final Element element) { + List plugins = element.getChildren("requiredPlugin"); + for (Element plugin : plugins) { + String id = plugin.getTextTrim(); + if (!PluginManager.isPluginInstalled(PluginId.getId(id))) { + return null; + } + } String type = element.getChildText("moduleType"); final ModuleType moduleType = ModuleTypeManager.getInstance().findByID(type); return new ArchivedProjectTemplate(element.getChildTextTrim("name")) { @@ -72,7 +104,15 @@ public class RemoteTemplatesFactory implements ProjectTemplatesFactory { @Override public ZipInputStream getStream() throws IOException { - return null; + String path = element.getChildText("path"); + final HttpURLConnection connection = getConnection(path); + return new ZipInputStream(connection.getInputStream()) { + @Override + public void close() throws IOException { + super.close(); + connection.disconnect(); + } + }; } @Nullable @@ -83,5 +123,16 @@ public class RemoteTemplatesFactory implements ProjectTemplatesFactory { }; } }); + return templates.toArray(new ProjectTemplate[templates.size()]); } + + private static HttpURLConnection getConnection(String path) throws IOException { + HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(URL + path); + connection.setConnectTimeout(2000); + connection.setReadTimeout(2000); + connection.connect(); + return connection; + } + + private final static Logger LOG = Logger.getInstance(RemoteTemplatesFactory.class); } diff --git a/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java b/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java index 0928ee59f33e..2e29d83ad5ad 100644 --- a/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java +++ b/java/idea-ui/src/com/intellij/platform/templates/TemplateModuleBuilder.java @@ -31,6 +31,7 @@ import com.intellij.openapi.roots.ui.configuration.ModulesProvider; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.NullableComputable; +import com.intellij.openapi.util.io.StreamUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.RefreshQueue; @@ -128,9 +129,10 @@ public class TemplateModuleBuilder extends ModuleBuilder { } private void unzip(String path, boolean moduleMode) { + File dir = new File(path); + ZipInputStream zipInputStream = null; try { - File dir = new File(path); - ZipInputStream zipInputStream = myTemplate.getStream(); + zipInputStream = myTemplate.getStream(); ZipUtil.unzip(ProgressManager.getInstance().getProgressIndicator(), dir, zipInputStream, moduleMode ? PATH_CONVERTOR : null); String iml = ContainerUtil.find(dir.list(), new Condition() { @Override @@ -154,6 +156,9 @@ public class TemplateModuleBuilder extends ModuleBuilder { catch (IOException e) { throw new RuntimeException(e); } + finally { + StreamUtil.closeStream(zipInputStream); + } } @Nullable diff --git a/java/idea-ui/src/com/intellij/platform/templates/templates.xml b/java/idea-ui/src/com/intellij/platform/templates/templates.xml deleted file mode 100644 index 32eb511d09b4..000000000000 --- a/java/idea-ui/src/com/intellij/platform/templates/templates.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - \ No newline at end of file