KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > provider > Provider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.provider;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.db.explorer.DatabaseConnection;
28 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Property;
29
30 /**
31  * This class represents a persistence provider.
32  *
33  * @author Erno Mononen
34  */

35 public abstract class Provider {
36     
37     // constants for properties
38
public static final String JavaDoc TABLE_GENERATION_CREATE = "tableGenerationCreate";
39     public static final String JavaDoc TABLE_GENERATION_DROPCREATE = "tableGenerationDropCreate";
40     public static final String JavaDoc TABLE_GENERATTION_UNKOWN = "tableGenerationUnknown";
41     
42     
43     /**
44      * Fully qualified class name of the provider.
45      */

46     private final String JavaDoc providerClass;
47     
48     private final Set JavaDoc vendorSpecificProperties;
49     
50     
51     /**
52      * Creates a new instance of Provider
53      */

54     protected Provider(String JavaDoc 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 JavaDoc getDisplayName();
61     
62     /**
63      * @see #providerClass
64      */

65     public final String JavaDoc getProviderClass(){
66         return this.providerClass;
67     }
68     
69     private Set JavaDoc initPropertyNames(){
70         Set JavaDoc result = new HashSet JavaDoc();
71         result.add(getJdbcDriver());
72         result.add(getJdbcUsername());
73         result.add(getJdbcUrl());
74         result.add(getJdbcPassword());
75         result.add(getTableGenerationPropertyName());
76         for (Iterator JavaDoc it = getUnresolvedVendorSpecificProperties().keySet().iterator(); it.hasNext();) {
77             String JavaDoc propertyName = (String JavaDoc) it.next();
78             result.add(propertyName);
79         }
80         return result;
81     }
82     
83     /**
84      * Gets the names of all provider specific properties.
85      * @return Set of Strings representing names of provider specific properties.
86      */

87     public Set JavaDoc getPropertyNames(){
88         return this.vendorSpecificProperties;
89     }
90     
91     /**
92      * @return the property that represents table generation strategy.
93      */

94     public final Property getTableGenerationProperty(String JavaDoc strategy){
95         if ("".equals(getTableGenerationPropertyName())){
96             // provider doesn't support table generation
97
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     /**
112      * @return name of the default datasource.
113      */

114     public final String JavaDoc getDefaultJtaDatasource(){
115         return "jdbc/__default";
116     }
117     
118     /**
119      * @return name of the property representing JDBC URL.
120      */

121     public abstract String JavaDoc getJdbcUrl();
122     
123     /**
124      * @return name of the property representing JDBC driver.
125      */

126     public abstract String JavaDoc getJdbcDriver();
127     
128     /**
129      * @return name of the property representing JDBC user name.
130      */

131     public abstract String JavaDoc getJdbcUsername();
132     
133     /**
134      * @return name of the property representing JDBC password.
135      */

136     public abstract String JavaDoc getJdbcPassword();
137     
138     /**
139      * @return name of the property representing table generation strategy.
140      */

141     public abstract String JavaDoc getTableGenerationPropertyName();
142     
143     /**
144      * @return value of the property that represents <tt>create tables</tt> strategy.
145      */

146     public abstract String JavaDoc getTableGenerationCreateValue();
147     
148     /**
149      * @return value of the property that represents <tt>create and drop tables</tt> strategy.
150      */

151     public abstract String JavaDoc getTableGenerationDropCreateValue();
152     
153     /**
154      * @return Map<String, String> containing vendor specific properties.
155      */

156     public abstract Map JavaDoc getUnresolvedVendorSpecificProperties();
157     
158     /**
159      * @return Map<String, String> containing vendor specific properties
160      * which should be set on a new unit by default.
161      */

162     public abstract Map JavaDoc getDefaultVendorSpecificProperties();
163     
164     /**
165      * Gets a map containing provider specific name / values pairs of given
166      * database connection's properties. If given connection was null, will
167      * return a map containing keys (names) of properties but empty Strings as values.
168      * @param connection
169      * @return Map (key String representing name of the property, value String
170      * representing value of the property).
171      */

172     public final Map JavaDoc<String JavaDoc, String JavaDoc> getConnectionPropertiesMap(DatabaseConnection connection){
173         Map JavaDoc<String JavaDoc, String JavaDoc> result = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
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         // must set an empty string for password if a null password
178
// was returned from the connection, see #81729
179
result.put(getJdbcPassword(),
180                 connection != null && connection.getPassword() != null ? connection.getPassword() : "");
181         return result;
182     }
183     
184     /**
185      * @return true if this provider support table generation, false otherwise.
186      */

187     public final boolean supportsTableGeneration(){
188         return getTableGenerationPropertyName() != null && !"".equals(getTableGenerationPropertyName().trim());
189     }
190     
191     public String JavaDoc toString() {
192         return getDisplayName();
193     }
194     
195     public int hashCode() {
196         return providerClass.hashCode();
197     }
198     
199     public boolean equals(Object JavaDoc 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