diff --git a/java/java-impl/src/META-INF/libraryJarUsage.xml b/java/java-impl/src/META-INF/libraryJarUsage.xml new file mode 100644 index 000000000000..f161458db995 --- /dev/null +++ b/java/java-impl/src/META-INF/libraryJarUsage.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptor.java b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptor.java new file mode 100644 index 000000000000..e824b8f65956 --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptor.java @@ -0,0 +1,37 @@ +/* + * 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.internal.statistic.libraryJar; + +import com.intellij.util.xmlb.annotations.Attribute; +import com.intellij.util.xmlb.annotations.Tag; + +/** + * @author Ivan Chirkov + */ +@Tag("technology") +public class LibraryJarDescriptor { + /** + * Name of library/framework + */ + @Attribute("name") + public String myName; + + /** + * Class used to identify library/framework + */ + @Attribute("class") + public String myClass; +} diff --git a/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptors.java b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptors.java new file mode 100644 index 000000000000..fbb64676422e --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarDescriptors.java @@ -0,0 +1,34 @@ +/* + * 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.internal.statistic.libraryJar; + +/** + * @author Ivan Chirkov + */ + +import com.intellij.util.xmlb.annotations.AbstractCollection; +import com.intellij.util.xmlb.annotations.Property; + +public class LibraryJarDescriptors { + + @Property(surroundWithTag = false) + @AbstractCollection(surroundWithTag = false) + public LibraryJarDescriptor[] myDescriptors; + + public LibraryJarDescriptor[] getDescriptors() { + return myDescriptors; + } +} diff --git a/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarStatisticsService.java b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarStatisticsService.java new file mode 100644 index 000000000000..7029edecf69e --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarStatisticsService.java @@ -0,0 +1,94 @@ +/* + * 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.internal.statistic.libraryJar; + +import com.intellij.facet.frameworks.SettingsConnectionService; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.net.HttpConfigurable; +import com.intellij.util.xmlb.XmlSerializationException; +import com.intellij.util.xmlb.XmlSerializer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * @author Ivan Chirkov + */ +public class LibraryJarStatisticsService extends SettingsConnectionService { + + private static final String FILE_NAME = "library-jar-statistics.xml"; + private static final String DEFAULT_SETTINGS_URL = "http://jetbrains.com/idea/" + FILE_NAME; + private static final String DEFAULT_SERVICE_URL = "http://frameworks.jetbrains.com/statistics"; + + private static final LibraryJarStatisticsService myInstance = new LibraryJarStatisticsService(); + private LibraryJarDescriptor[] myDescriptors; + + public static LibraryJarStatisticsService getInstance() { + return myInstance; + } + + protected LibraryJarStatisticsService() { + super(DEFAULT_SETTINGS_URL, DEFAULT_SERVICE_URL); + } + + @NotNull + public LibraryJarDescriptor[] getTechnologyDescriptors() { + if (myDescriptors == null) { + final URL url = createVersionsUrl(); + if (url == null) return new LibraryJarDescriptor[0]; + final LibraryJarDescriptors descriptors = deserialize(url); + myDescriptors = descriptors == null ? new LibraryJarDescriptor[0] : descriptors.getDescriptors(); + } + return myDescriptors; + } + + @Nullable + private static LibraryJarDescriptors deserialize(@Nullable URL url) { + if (url == null) return null; + + LibraryJarDescriptors libraryJarDescriptors = null; + try { + libraryJarDescriptors = XmlSerializer.deserialize(url, LibraryJarDescriptors.class); + } + catch (XmlSerializationException e) { + // + } + return libraryJarDescriptors; + } + + @Nullable + private URL createVersionsUrl() { + final String serviceUrl = getServiceUrl(); + if (StringUtil.isNotEmpty(serviceUrl)) { + try { + final String url = serviceUrl + "/" + FILE_NAME; + HttpConfigurable.getInstance().prepareURL(url); + + return new URL(url); + } + catch (MalformedURLException ignored) { + } + catch (IOException e) { + // no route to host, unknown host, etc. + } + } + + return null; + } +} diff --git a/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarUsagesCollector.java b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarUsagesCollector.java new file mode 100644 index 000000000000..d535c3b523d2 --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/LibraryJarUsagesCollector.java @@ -0,0 +1,82 @@ +/* + * 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.internal.statistic.libraryJar; + +import com.intellij.internal.statistic.AbstractApplicationUsagesCollector; +import com.intellij.internal.statistic.CollectUsagesException; +import com.intellij.internal.statistic.beans.GroupDescriptor; +import com.intellij.internal.statistic.beans.UsageDescriptor; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.ProjectScope; +import com.intellij.util.containers.hash.HashSet; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Ivan Chirkov + */ +public class LibraryJarUsagesCollector extends AbstractApplicationUsagesCollector { + public static final Pattern MULTI_DIGIT_VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+).*"); + private static final GroupDescriptor GROUP = GroupDescriptor.create("Libraries by jars", GroupDescriptor.LOWER_PRIORITY); + + private static final Pattern PATH_PATTERN = Pattern.compile(".*/[\\w|\\-|\\.]+-([\\w|\\.]+)jar!/.*"); + + @NotNull + @Override + public Set getProjectUsages(@NotNull final Project project) throws CollectUsagesException { + final LibraryJarDescriptor[] descriptors = LibraryJarStatisticsService.getInstance().getTechnologyDescriptors(); + final Set result = new HashSet(descriptors.length); + ApplicationManager.getApplication().runReadAction(new Runnable() { + @Override + public void run() { + for (LibraryJarDescriptor descriptor : descriptors) { + String className = descriptor.myClass; + if (className != null) { + PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(className, ProjectScope.getLibrariesScope(project)); + if (psiClass != null) { + Matcher matcher = PATH_PATTERN.matcher(psiClass.getContainingFile().getVirtualFile().getPath()); + if (matcher.matches()) { + String version; + String fullVersion = matcher.group(1); + matcher = MULTI_DIGIT_VERSION_PATTERN.matcher(fullVersion); + if (matcher.matches()) { + version = matcher.group(1); + } else { + version = fullVersion.substring(0, fullVersion.indexOf(".")); + } + result.add(new UsageDescriptor(descriptor.myName + "_" + version, 1)); + } + } + } + } + } + }); + return result; + } + + @NotNull + @Override + public GroupDescriptor getGroupId() { + return GROUP; + } + +} diff --git a/java/java-impl/src/com/intellij/internal/statistic/libraryJar/library-jar-statistics.xml b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/library-jar-statistics.xml new file mode 100644 index 000000000000..95b1b7968bc0 --- /dev/null +++ b/java/java-impl/src/com/intellij/internal/statistic/libraryJar/library-jar-statistics.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index c06866b0a63d..658a024f7736 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -21,6 +21,9 @@ + + +