Changeset 6666


Ignore:
Timestamp:
Dec 17, 2014, 9:23:21 AM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #1901: Get rid of code that use System.identityHashCode() and assume that it return unique values

Added DbControl.uniqueRandom() which can be used to generate a random string that is unique within the transaction.

This should take care of the query api and the other remaining places.

Location:
branches/3.4-stable
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/3.4-stable/src/core/net/sf/basedb/core/DbControl.java

    r6576 r6666  
    4141import net.sf.basedb.util.extensions.manager.ExtensionsManager;
    4242
     43import java.util.HashSet;
    4344import java.util.IdentityHashMap;
    4445import java.util.Iterator;
     
    4950import java.util.HashMap;
    5051import java.util.LinkedHashMap;
     52import java.util.Set;
    5153import java.lang.reflect.Constructor;
    5254
     
    146148 
    147149  /**
     150    Holds random strings that must be unique within a transaction.
     151  */
     152  private Set<String> uniqueRandoms;
     153 
     154  /**
    148155    Create a new object.
    149156  */
     
    169176    itemCache = new IdentityHashMap<BasicData, BasicItem>();
    170177    commitQueue = new LinkedHashMap<BasicItem,Transactional.Action>();
     178    uniqueRandoms = new HashSet<String>();
    171179    isClosed = false;
    172180    isConnected = true;
     
    376384    commitQueue.clear();
    377385    itemCache.clear();
     386    uniqueRandoms.clear();
    378387    if (batchers != null) batchers.clear();
    379388    if (saveIfQueue != null) saveIfQueue.clear();
     
    383392    transactionalActions = null;
    384393    itemCache = null;
     394    uniqueRandoms = null;
    385395    batchers = null;
    386396    isClosed = true;
     
    12111221
    12121222  /**
     1223    Generate a random string value that is unique for the given transaction.
     1224    @since 3.4
     1225  */
     1226  public String uniqueRandom()
     1227  {
     1228    String random = null;
     1229    while (true)
     1230    {
     1231      random = Application.generateRandomId(4);
     1232      if (uniqueRandoms.add(random)) break;
     1233    }
     1234    return random;
     1235  }
     1236 
     1237  /**
    12131238    Add a <code>Batcher</code> to the batcher queue.
    12141239    @param batcher The <code>Batcher</code> to add
  • branches/3.4-stable/src/core/net/sf/basedb/core/ItemContext.java

    r6336 r6666  
    17201720      // propertyDef is JEP expression
    17211721      Expression e = Jep.formulaToExpression(propertyDef.substring(1), ch, rawCh, raw, rep, mrep);
    1722       s = Selects.expression(e, "jep" + System.identityHashCode(e));
     1722      s = Selects.expression(e, "jep" + dc.uniqueRandom());
    17231723    }
    17241724    else
  • branches/3.4-stable/src/core/net/sf/basedb/core/PropertyFilter.java

    r6646 r6666  
    515515            if (obj != null)
    516516            {
    517               String parameterName = "p" + System.identityHashCode(obj);
     517              String parameterName = "p" + dc.uniqueRandom();
    518518              Type theValueType = obj instanceof Double ? Type.DOUBLE : getValueType();
    519519              if (theValueType == Type.TIMESTAMP) theValueType = Type.DATE;
     
    526526        else
    527527        {
    528           String parameterName = "p" + System.identityHashCode(this);
     528          String parameterName = "p" + dc.uniqueRandom();
    529529          Expression parameter = null;
    530530          if (getValue() != null)
     
    605605    else
    606606    {
    607       String parameterName = "p" + System.identityHashCode(this);
     607      String parameterName = "p" + dc.uniqueRandom();
    608608      Expression parameter = null;
    609609      if (getValue() != null && !operator.isListOperator())
     
    632632            if (obj != null)
    633633            {
    634               parameterName = "p" + System.identityHashCode(obj);
     634              parameterName = "p" + dc.uniqueRandom();
    635635              Type theValueType = obj instanceof Double ? Type.DOUBLE : getValueType();
    636636              Expression e = Expressions.parameter(parameterName, obj, theValueType);
     
    825825            if (obj != null)
    826826            {
    827               parameterName = "p" + System.identityHashCode(obj);
     827              parameterName = "p" + dc.uniqueRandom();
    828828              Type theValueType = obj instanceof Double ? Type.DOUBLE : getValueType();
    829829              if (theValueType == Type.TIMESTAMP) theValueType = Type.DATE;
     
    877877      propertyExpression = ItemContext.getDynamicExpression(dc, property);
    878878    }
    879     String parameterName = "p" + System.identityHashCode(this);
     879    String parameterName = "p" + dc.uniqueRandom();
    880880   
    881881    String value = getValue();
     
    918918          if (obj != null)
    919919          {
    920             parameterName = "p" + System.identityHashCode(obj);
     920            parameterName = "p" + dc.uniqueRandom();
    921921            e = Expressions.parameter(parameterName, obj);
    922922          }
  • branches/3.4-stable/src/core/net/sf/basedb/core/signal/AbstractSignalReceiver.java

    r6665 r6666  
    298298      synchronized (handlers)
    299299      {
    300         while (id == null)
     300        while (true)
    301301        {
    302302          id = Application.generateRandomId(4);
    303           if (handlers.containsKey(id)) id = null;
     303          if (!handlers.containsKey(id)) break;
    304304        }
    305305        handlerIds.put(handler, id);
  • branches/3.4-stable/www/admin/extensions/tree.jsp

    r6409 r6666  
    4949<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
    5050<%!
    51 JSONObject newJoustExtensionPoint(JSONObject jsonParent, ExtensionsControl ec, ExtensionPoint ep, ExtensionsFile ef)
     51JSONObject newJoustExtensionPoint(DbControl dc, JSONObject jsonParent, ExtensionsControl ec, ExtensionPoint ep, ExtensionsFile ef)
    5252{
    5353  String id = ep.getId();
     
    5959    icon = "ExtensionPointError";
    6060  }
    61   JSONObject json = newJoustEntry(jsonParent, icon, HTML.encodeTags(name), id);
     61  JSONObject json = newJoustEntry(dc, jsonParent, icon, HTML.encodeTags(name), id);
    6262  json.put("type", "extension-point");
    6363  return json;
    6464}
    6565
    66 JSONObject newJoustExtension(JSONObject jsonParent, ExtensionsControl ec, Extension ext, ExtensionsFile ef, boolean inGroup, boolean showFile)
     66JSONObject newJoustExtension(DbControl dc, JSONObject jsonParent, ExtensionsControl ec, Extension ext, ExtensionsFile ef, boolean inGroup, boolean showFile)
    6767{
    6868  String id = ext.getId();
     
    8888  }
    8989 
    90   JSONObject json = newJoustEntry(jsonParent, icon, name, id);
     90  JSONObject json = newJoustEntry(dc, jsonParent, icon, name, id);
    9191  json.put("type", "extension");
    9292  return json;
    9393}
    9494
    95 JSONObject newJoustFile(JSONObject jsonParent, ExtensionsFile ef, ExtensionsControl ec)
     95JSONObject newJoustFile(DbControl dc, JSONObject jsonParent, ExtensionsFile ef, ExtensionsControl ec)
    9696{
    9797  String efName = ef.getName();
     
    107107  if (ef.checkModified()) icon += "Modified";
    108108 
    109   JSONObject json = newJoustEntry(jsonParent, icon, HTML.encodeTags(efName), efName);
     109  JSONObject json = newJoustEntry(dc, jsonParent, icon, HTML.encodeTags(efName), efName);
    110110  json.put("id", efName);
    111111  json.put("type", "file");
     
    114114}
    115115
    116 JSONObject newJoustExtensionGroup(JSONObject jsonParent, ExtensionsControl ec, Extension ext, String groupId)
     116JSONObject newJoustExtensionGroup(DbControl dc, JSONObject jsonParent, ExtensionsControl ec, Extension ext, String groupId)
    117117{
    118118  String id = ext.getId();
     
    120120  String name = about == null || about.getName() == null ? id : about.getName();
    121121  String icon = "Folder";
    122   JSONObject json = newJoustEntry(jsonParent, icon, HTML.encodeTags(name), groupId);
     122  JSONObject json = newJoustEntry(dc, jsonParent, icon, HTML.encodeTags(name), groupId);
    123123  json.put("type", "group");
    124124  return json;
    125125}
    126126
    127 JSONObject newJoustPlugin(JSONObject jsonParent, ExtensionsControl ex, PluginInfo info, ExtensionsFile ef)
     127JSONObject newJoustPlugin(DbControl dc, JSONObject jsonParent, ExtensionsControl ex, PluginInfo info, ExtensionsFile ef)
    128128{
    129129  String id = info.getClassName();
     
    140140  }
    141141 
    142   JSONObject json = newJoustEntry(jsonParent, icon, name, id);
     142  JSONObject json = newJoustEntry(dc, jsonParent, icon, name, id);
    143143  json.put("type", "plugin");
    144144  json.put("className", info.getClassName());
     
    146146}
    147147
    148 JSONObject newJoustEntry(JSONObject jsonParent, String icon, String text, String id)
     148JSONObject newJoustEntry(DbControl dc, JSONObject jsonParent, String icon, String text, String id)
    149149{
    150150  JSONObject jsonJoust = new JSONObject();
    151151  jsonJoust.put("icon", icon);
    152152  jsonJoust.put("text", text);
    153   jsonJoust.put("id", "joust-"+System.identityHashCode(jsonJoust));
     153  jsonJoust.put("id", "joust-"+dc.uniqueRandom());
    154154  jsonJoust.put("externalId", id);
    155155  if (jsonParent != null)
     
    172172DbControl dc = null;
    173173
    174 // Build the Joust menu tree
    175 JSONArray jsonJoust = new JSONArray();
    176 
    177 // Two ROOT items:
    178 JSONObject jsonByExtPoint = newJoustEntry(null, "Root", "By extension point", "root.by-extension-point");
    179 jsonByExtPoint.put("isOpen", 1);
    180 JSONObject jsonByFile = newJoustEntry(null, "Root", "By file", "root.by-file");
    181 jsonByFile.put("isOpen", 1);
    182 
    183 // Group some extension points inside sub-folders
    184 JSONObject jsonToolbars = newJoustEntry(jsonByExtPoint, "Folder", "Toolbars", "by-extension-point.toolbars");
    185 JSONObject jsonEditDialogs = newJoustEntry(jsonByExtPoint, "Folder", "Edit dialogs", "by-extension-point.edit-dialogs");
    186 JSONObject jsonListColumns = newJoustEntry(jsonByExtPoint, "Folder", "List columns", "by-extension-point.list-columns");
    187 
    188 jsonJoust.add(jsonByExtPoint);
    189 jsonJoust.add(jsonByFile);
    190 
    191 ExtensionsControl ec = ExtensionsControl.get(dc);
    192 List<ExtensionPoint<?>> extensionPoints = ec.getExtensionPoints();
    193 Collections.sort(extensionPoints, Registry.EXTENSIONPOINT_COMPARATOR);
    194 for (ExtensionPoint ep : extensionPoints)
    195 {
    196   String id = ep.getId();
    197   JSONObject jsonParent = jsonByExtPoint;
    198   if (id.startsWith("net.sf.basedb.clients.web.toolbar.")) jsonParent = jsonToolbars;
    199   if (id.startsWith("net.sf.basedb.clients.web.tabcontrol.edit.")) jsonParent = jsonEditDialogs;
    200   if (id.startsWith("net.sf.basedb.clients.web.onsave.")) jsonParent = jsonEditDialogs;
    201   if (id.startsWith("net.sf.basedb.clients.web.listcolumn.")) jsonParent = jsonListColumns;
    202  
    203   JSONObject jsonEp = newJoustExtensionPoint(jsonParent, ec, ep, ec.getFileByObjectKey(new ExtensionPointKey(ep)));
    204 
    205   List<Extension<?>> extensions = ec.getExtensions(ep.getId());
    206   Collections.sort(extensions, Registry.EXTENSION_COMPARATOR);
    207   for (Extension ext : extensions)
    208   {
    209     newJoustExtension(jsonEp, ec, ext, ec.getFileByObjectKey(new ExtensionKey(ext)), false, true);
    210   }
    211 }
    212 
    213 for (ExtensionsFile ef : ec.getFiles())
    214 {
    215   if (!ef.isInstalled()) continue;
    216  
    217   JSONObject jsonFile = newJoustFile(jsonByFile, ef, ec);
    218 
    219   List<ExtensionPoint> eps = ef.getObjectsOfClass(ExtensionPoint.class);
    220   Collections.sort(eps, Registry.EXTENSIONPOINT_COMPARATOR);
    221   for (ExtensionPoint ep : eps)
    222   {
    223     newJoustExtensionPoint(jsonFile, ec, ep, ef);
    224   }
    225  
    226   List<Extension> exts = ef.getObjectsOfClass(Extension.class);
    227   Collections.sort(exts, Registry.EXTENSION_COMPARATOR);
    228   String currentGroupId = null;
    229   JSONObject jsonGroup = null;
    230  
    231   for (Extension ext : exts)
    232   {
    233     int groupIndex = ext.getId().indexOf(":");
    234     // ID:s containing ':' should be grouped
    235     if (groupIndex > 0)
    236     {
    237       String groupId = ext.getId().substring(0, groupIndex);
    238       if (!groupId.equals(currentGroupId))
     174try
     175{
     176  dc = sc.newDbControl();
     177  // Build the Joust menu tree
     178  JSONArray jsonJoust = new JSONArray();
     179 
     180  // Two ROOT items:
     181  JSONObject jsonByExtPoint = newJoustEntry(dc, null, "Root", "By extension point", "root.by-extension-point");
     182  jsonByExtPoint.put("isOpen", 1);
     183  JSONObject jsonByFile = newJoustEntry(dc, null, "Root", "By file", "root.by-file");
     184  jsonByFile.put("isOpen", 1);
     185 
     186  // Group some extension points inside sub-folders
     187  JSONObject jsonToolbars = newJoustEntry(dc, jsonByExtPoint, "Folder", "Toolbars", "by-extension-point.toolbars");
     188  JSONObject jsonEditDialogs = newJoustEntry(dc, jsonByExtPoint, "Folder", "Edit dialogs", "by-extension-point.edit-dialogs");
     189  JSONObject jsonListColumns = newJoustEntry(dc, jsonByExtPoint, "Folder", "List columns", "by-extension-point.list-columns");
     190 
     191  jsonJoust.add(jsonByExtPoint);
     192  jsonJoust.add(jsonByFile);
     193 
     194  ExtensionsControl ec = ExtensionsControl.get(dc);
     195  List<ExtensionPoint<?>> extensionPoints = ec.getExtensionPoints();
     196  Collections.sort(extensionPoints, Registry.EXTENSIONPOINT_COMPARATOR);
     197  for (ExtensionPoint ep : extensionPoints)
     198  {
     199    String id = ep.getId();
     200    JSONObject jsonParent = jsonByExtPoint;
     201    if (id.startsWith("net.sf.basedb.clients.web.toolbar.")) jsonParent = jsonToolbars;
     202    if (id.startsWith("net.sf.basedb.clients.web.tabcontrol.edit.")) jsonParent = jsonEditDialogs;
     203    if (id.startsWith("net.sf.basedb.clients.web.onsave.")) jsonParent = jsonEditDialogs;
     204    if (id.startsWith("net.sf.basedb.clients.web.listcolumn.")) jsonParent = jsonListColumns;
     205   
     206    JSONObject jsonEp = newJoustExtensionPoint(dc, jsonParent, ec, ep, ec.getFileByObjectKey(new ExtensionPointKey(ep)));
     207 
     208    List<Extension<?>> extensions = ec.getExtensions(ep.getId());
     209    Collections.sort(extensions, Registry.EXTENSION_COMPARATOR);
     210    for (Extension ext : extensions)
     211    {
     212      newJoustExtension(dc, jsonEp, ec, ext, ec.getFileByObjectKey(new ExtensionKey(ext)), false, true);
     213    }
     214  }
     215 
     216  for (ExtensionsFile ef : ec.getFiles())
     217  {
     218    if (!ef.isInstalled()) continue;
     219   
     220    JSONObject jsonFile = newJoustFile(dc, jsonByFile, ef, ec);
     221 
     222    List<ExtensionPoint> eps = ef.getObjectsOfClass(ExtensionPoint.class);
     223    Collections.sort(eps, Registry.EXTENSIONPOINT_COMPARATOR);
     224    for (ExtensionPoint ep : eps)
     225    {
     226      newJoustExtensionPoint(dc, jsonFile, ec, ep, ef);
     227    }
     228   
     229    List<Extension> exts = ef.getObjectsOfClass(Extension.class);
     230    Collections.sort(exts, Registry.EXTENSION_COMPARATOR);
     231    String currentGroupId = null;
     232    JSONObject jsonGroup = null;
     233   
     234    for (Extension ext : exts)
     235    {
     236      int groupIndex = ext.getId().indexOf(":");
     237      // ID:s containing ':' should be grouped
     238      if (groupIndex > 0)
    239239      {
    240         currentGroupId = groupId;
    241         jsonGroup = newJoustExtensionGroup(jsonFile, ec, ext, currentGroupId);
     240        String groupId = ext.getId().substring(0, groupIndex);
     241        if (!groupId.equals(currentGroupId))
     242        {
     243          currentGroupId = groupId;
     244          jsonGroup = newJoustExtensionGroup(dc, jsonFile, ec, ext, currentGroupId);
     245        }
    242246      }
    243     }
    244     else
    245     {
    246       currentGroupId = null;
    247       jsonGroup = null;
    248     }
    249     newJoustExtension(jsonGroup != null ? jsonGroup : jsonFile, ec, ext, ef, jsonGroup != null, false);
    250   }
    251   List<PluginInfo> plugins = ef.getObjectsOfClass(PluginInfo.class);
    252   Collections.sort(plugins, PluginInfo.NAME_COMPARATOR);
    253   for (PluginInfo info : plugins)
    254   {
    255     newJoustPlugin(jsonFile, ec, info, ef);
    256   }
    257 }
    258 
    259 
    260 try
    261 {
    262   dc = sc.newDbControl();
     247      else
     248      {
     249        currentGroupId = null;
     250        jsonGroup = null;
     251      }
     252      newJoustExtension(dc, jsonGroup != null ? jsonGroup : jsonFile, ec, ext, ef, jsonGroup != null, false);
     253    }
     254    List<PluginInfo> plugins = ef.getObjectsOfClass(PluginInfo.class);
     255    Collections.sort(plugins, PluginInfo.NAME_COMPARATOR);
     256    for (PluginInfo info : plugins)
     257    {
     258      newJoustPlugin(dc, jsonFile, ec, info, ef);
     259    }
     260  }
    263261  %>
    264262  <base:page title="" type="iframe">
  • branches/3.4-stable/www/common/overview/failures.js

    r6400 r6666  
    4343    else
    4444    {
    45       var failureId = Data.get(target, 'failure-id');
    4645      var nodeId = Data.get(target, 'node-id');
    4746      window.parent.frames['tree'].Tree.selectNode(nodeId);
  • branches/3.4-stable/www/common/overview/failures.jsp

    r6621 r6666  
    157157          {
    158158            index++;
    159             String failureId = "F" + System.identityHashCode(failure);
    160159            Node node = failure.getNode();
    161160            BasicItem item = node.getItem();
    162161            %>
    163             <tr id="<%=failureId%>" class="failure highlight auto-init" data-auto-init="failure"
    164               data-failure-id="<%=failureId%>" data-node-id="<%=node.getId()%>"
     162            <tr class="failure highlight auto-init" data-auto-init="failure"
     163              data-node-id="<%=node.getId()%>"
    165164              <%
    166165              if (item != null)
Note: See TracChangeset for help on using the changeset viewer.