You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptimizedWidgetsetPanel.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.debug.internal;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.user.client.ui.FlowPanel;
  21. import com.google.gwt.user.client.ui.HTML;
  22. import com.vaadin.client.ApplicationConfiguration;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.ServerConnector;
  25. import com.vaadin.client.Util;
  26. import com.vaadin.client.ui.UnknownComponentConnector;
  27. import com.vaadin.client.ui.UnknownExtensionConnector;
  28. /**
  29. * Optimized widgetset view panel of the debug window.
  30. *
  31. * @since 7.1.4
  32. */
  33. public class OptimizedWidgetsetPanel extends FlowPanel {
  34. /**
  35. * Update the panel contents based on the connectors that have been used so
  36. * far on this execution of the application.
  37. */
  38. public void update() {
  39. clear();
  40. HTML h = new HTML("Getting used connectors");
  41. add(h);
  42. String s = "";
  43. for (ApplicationConnection ac : ApplicationConfiguration
  44. .getRunningApplications()) {
  45. ApplicationConfiguration conf = ac.getConfiguration();
  46. s += "<h1>Used connectors for "
  47. + Util.escapeHTML(conf.getServiceUrl()) + "</h1>";
  48. for (String connectorName : getUsedConnectorNames(conf)) {
  49. s += Util.escapeHTML(connectorName) + "<br/>";
  50. }
  51. s += "<h2>To make an optimized widgetset based on these connectors:</h2>";
  52. s += "<h3>1. Add the following <b>to the end</b> of your widgetset.gwt.xml file:</h3>";
  53. s += "<textarea rows=\"3\" style=\"width:90%\">";
  54. s += "<generate-with class=\"OptimizedConnectorBundleLoaderFactory\">\n";
  55. s += " <when-type-assignable class=\"com.vaadin.client.metadata.ConnectorBundleLoader\" />\n";
  56. s += "</generate-with>\n";
  57. s += "</textarea>";
  58. s += "<h3>2. Add the following code into OptimizedConnectorBundleLoaderFactory.java:</h3>";
  59. s += "<textarea rows=\"5\" style=\"width:90%\">";
  60. s += generateOptimizedWidgetSet(getUsedConnectorNames(conf));
  61. s += "</textarea>";
  62. s += "<h3>3. Recompile your widgetset. For example with Maven: 'mvn compile vaadin:compile'</h3>";
  63. }
  64. h.setHTML(s);
  65. }
  66. private Set<String> getUsedConnectorNames(
  67. ApplicationConfiguration configuration) {
  68. int tag = 0;
  69. Set<String> usedConnectors = new HashSet<>();
  70. while (true) {
  71. String serverSideClass = configuration
  72. .getServerSideClassNameForTag(tag);
  73. if (serverSideClass == null) {
  74. break;
  75. }
  76. Class<? extends ServerConnector> connectorClass = configuration
  77. .getConnectorClassByEncodedTag(tag);
  78. if (connectorClass == null) {
  79. break;
  80. }
  81. if (connectorClass != UnknownComponentConnector.class
  82. && connectorClass != UnknownExtensionConnector.class) {
  83. usedConnectors.add(connectorClass.getName());
  84. }
  85. tag++;
  86. if (tag > 10000) {
  87. // Sanity check
  88. getLogger().severe(
  89. "Search for used connector classes was forcefully terminated");
  90. break;
  91. }
  92. }
  93. return usedConnectors;
  94. }
  95. public String generateOptimizedWidgetSet(Set<String> usedConnectors) {
  96. String s = "import java.util.HashSet;\n";
  97. s += "import java.util.Set;\n";
  98. s += "import com.google.gwt.core.ext.typeinfo.JClassType;\n";
  99. s += "import com.vaadin.client.ui.ui.UIConnector;\n";
  100. s += "import com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory;\n";
  101. s += "import com.vaadin.shared.ui.Connect.LoadStyle;\n\n";
  102. s += "public class OptimizedConnectorBundleLoaderFactory extends\n";
  103. s += " ConnectorBundleLoaderFactory {\n";
  104. s += " private Set<String> eagerConnectors = new HashSet<String>();\n";
  105. s += " {\n";
  106. for (String c : usedConnectors) {
  107. s += " eagerConnectors.add(" + Util.escapeHTML(c)
  108. + ".class.getName());\n";
  109. }
  110. s += " }\n";
  111. s += "\n";
  112. s += " @Override\n";
  113. s += " protected LoadStyle getLoadStyle(JClassType connectorType) {\n";
  114. s += " if (eagerConnectors.contains(connectorType.getQualifiedBinaryName())) {\n";
  115. s += " return LoadStyle.EAGER;\n";
  116. s += " } else {\n";
  117. s += " // Loads all other connectors immediately after the initial view has\n";
  118. s += " // been rendered\n";
  119. s += " return LoadStyle.DEFERRED;\n";
  120. s += " }\n";
  121. s += " }\n";
  122. s += "}\n";
  123. return s;
  124. }
  125. private static Logger getLogger() {
  126. return Logger.getLogger(OptimizedWidgetsetPanel.class.getName());
  127. }
  128. }