1 19 20 package org.netbeans.modules.j2ee.persistence.provider; 21 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.netbeans.api.db.explorer.DatabaseConnection; 28 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Property; 29 30 35 public abstract class Provider { 36 37 public static final String TABLE_GENERATION_CREATE = "tableGenerationCreate"; 39 public static final String TABLE_GENERATION_DROPCREATE = "tableGenerationDropCreate"; 40 public static final String TABLE_GENERATTION_UNKOWN = "tableGenerationUnknown"; 41 42 43 46 private final String providerClass; 47 48 private final Set vendorSpecificProperties; 49 50 51 54 protected Provider(String providerClass) { 55 assert !(null == providerClass || "".equals(providerClass.trim())) : "Provider class must be given!"; 56 this.providerClass = providerClass; 57 this.vendorSpecificProperties = initPropertyNames(); 58 } 59 60 public abstract String getDisplayName(); 61 62 65 public final String getProviderClass(){ 66 return this.providerClass; 67 } 68 69 private Set initPropertyNames(){ 70 Set result = new HashSet (); 71 result.add(getJdbcDriver()); 72 result.add(getJdbcUsername()); 73 result.add(getJdbcUrl()); 74 result.add(getJdbcPassword()); 75 result.add(getTableGenerationPropertyName()); 76 for (Iterator it = getUnresolvedVendorSpecificProperties().keySet().iterator(); it.hasNext();) { 77 String propertyName = (String ) it.next(); 78 result.add(propertyName); 79 } 80 return result; 81 } 82 83 87 public Set getPropertyNames(){ 88 return this.vendorSpecificProperties; 89 } 90 91 94 public final Property getTableGenerationProperty(String strategy){ 95 if ("".equals(getTableGenerationPropertyName())){ 96 return null; 98 } 99 Property result = new Property(); 100 result.setName(getTableGenerationPropertyName()); 101 if (TABLE_GENERATION_CREATE.equals(strategy)){ 102 result.setValue(getTableGenerationCreateValue()); 103 } else if (TABLE_GENERATION_DROPCREATE.equals(strategy)){ 104 result.setValue(getTableGenerationDropCreateValue()); 105 } else { 106 return null; 107 } 108 return result; 109 } 110 111 114 public final String getDefaultJtaDatasource(){ 115 return "jdbc/__default"; 116 } 117 118 121 public abstract String getJdbcUrl(); 122 123 126 public abstract String getJdbcDriver(); 127 128 131 public abstract String getJdbcUsername(); 132 133 136 public abstract String getJdbcPassword(); 137 138 141 public abstract String getTableGenerationPropertyName(); 142 143 146 public abstract String getTableGenerationCreateValue(); 147 148 151 public abstract String getTableGenerationDropCreateValue(); 152 153 156 public abstract Map getUnresolvedVendorSpecificProperties(); 157 158 162 public abstract Map getDefaultVendorSpecificProperties(); 163 164 172 public final Map <String , String > getConnectionPropertiesMap(DatabaseConnection connection){ 173 Map <String , String > result = new HashMap <String , String >(); 174 result.put(getJdbcDriver(), connection != null ? connection.getDriverClass() : ""); 175 result.put(getJdbcUrl(), connection != null ? connection.getDatabaseURL() : ""); 176 result.put(getJdbcUsername(), connection != null ? connection.getUser(): ""); 177 result.put(getJdbcPassword(), 180 connection != null && connection.getPassword() != null ? connection.getPassword() : ""); 181 return result; 182 } 183 184 187 public final boolean supportsTableGeneration(){ 188 return getTableGenerationPropertyName() != null && !"".equals(getTableGenerationPropertyName().trim()); 189 } 190 191 public String toString() { 192 return getDisplayName(); 193 } 194 195 public int hashCode() { 196 return providerClass.hashCode(); 197 } 198 199 public boolean equals(Object obj) { 200 if (obj == null || !(obj instanceof Provider)){ 201 return false; 202 } 203 Provider that = (Provider) obj; 204 return getClass().equals(that.getClass()) && providerClass.equals(that.providerClass); 205 } 206 207 208 } 209 | Popular Tags |