1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.util.Map ; 25 import java.util.HashMap ; 26 27 28 34 final class AliasManager 35 { 36 private static final String RELATION_TABLE_SUFFIX = "_RELATION_TABLE"; 37 38 private final String prefix; 39 private final String suffix; 40 private final int maxLength; 41 private final Map aliases = new HashMap (); 42 private final Map relationTableAliases = new HashMap (); 43 44 private int count = 0; 45 46 public AliasManager(String prefix, String suffix, int maxLength) 47 { 48 this.prefix = prefix; 49 this.suffix = suffix; 50 this.maxLength = maxLength; 51 } 52 53 public String getAlias(String path) 54 { 55 String alias = (String )aliases.get(path); 56 if(alias == null) 57 { 58 alias = createAlias(path); 59 aliases.put(path, alias); 60 } 61 return alias; 62 } 63 64 private String createAlias(String path) 65 { 66 StringBuffer alias = new StringBuffer (); 67 alias.append(prefix).append(count++).append(suffix); 68 alias.append(path.replace('.', '_')); 69 70 return alias.substring(0, Math.min(maxLength, alias.length())); 71 } 72 73 public void addAlias(String path, String alias) 74 { 75 aliases.put(path, alias); 76 } 77 78 public String getRelationTableAlias(String path) 79 { 80 String relationTableAlias = (String )relationTableAliases.get(path); 81 if(relationTableAlias == null) 82 { 83 relationTableAlias = createRelationTableAlias(path); 84 relationTableAliases.put(path, relationTableAlias); 85 } 86 return relationTableAlias; 87 } 88 89 private String createRelationTableAlias(String path) 90 { 91 StringBuffer relationTableAlias = new StringBuffer (); 92 93 relationTableAlias.append(prefix).append(count++).append(suffix); 94 95 relationTableAlias.append(path.replace('.', '_')); 96 relationTableAlias.append(RELATION_TABLE_SUFFIX); 97 98 return relationTableAlias.substring( 99 0, Math.min(maxLength, relationTableAlias.length())); 100 } 101 } 102 | Popular Tags |