KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > threetier > ConnectionPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.threetier;
23
24 import java.util.*;
25 import java.io.*;
26 import oracle.toplink.essentials.internal.helper.*;
27 import oracle.toplink.essentials.sessions.Login;
28 import oracle.toplink.essentials.internal.localization.*;
29
30 /**
31  * <p>
32  * <b>Purpose</b>: Used to specify how a client session's should be allocated.
33  * @see ServerSession
34  */

35 public class ConnectionPolicy implements Cloneable JavaDoc, Serializable {
36     protected Login login;
37     protected String JavaDoc poolName;
38     protected boolean isLazy;
39
40     /** This attribute provides a mechanism to pass connection information to events. */
41     protected Map properties;
42
43     /**
44      * PUBLIC:
45      * A connection policy is used to define how the client session connection should be acquired.
46      */

47     public ConnectionPolicy() {
48         this.isLazy = true;
49     }
50
51     /**
52      * PUBLIC:
53      * A connection policy is used to define how the client session connection should be acquired.
54      */

55     public ConnectionPolicy(String JavaDoc poolName) {
56         this.isLazy = true;
57         this.poolName = poolName;
58     }
59
60     /**
61      * PUBLIC:
62      * A connection policy is used to define how the client session connection should be acquired.
63      */

64     public ConnectionPolicy(Login login) {
65         this.isLazy = false;
66         this.login = login;
67     }
68
69     /**
70      * INTERNAL:
71      * Clone the query
72      */

73     public Object JavaDoc clone() {
74         try {
75             ConnectionPolicy clone = (ConnectionPolicy)super.clone();
76             if (clone.hasLogin()) {
77                 clone.setLogin((Login)clone.getLogin().clone());
78             }
79             return clone;
80         } catch (CloneNotSupportedException JavaDoc e) {
81             return null;
82         }
83     }
84
85     /**
86      * PUBLIC:
87      * A lazy connection only acquires a physical connection
88      * when a transaction is started and releases the connection when the transaction completes.
89      */

90     public void dontUseLazyConnection() {
91         setIsLazy(false);
92     }
93
94     /**
95      * PUBLIC:
96      * Return the login to use for this connection.
97      * Client sessions support using a seperate user login for database modification.
98      */

99     public Login getLogin() {
100         return login;
101     }
102
103     /**
104      * PUBLIC:
105      * Return the pool name or null if not part of a pool.
106      */

107     public String JavaDoc getPoolName() {
108         return poolName;
109     }
110
111     /**
112      * ADVANCED:
113      * This method will return the collection of custom properties set on the Connection
114      * policy. Note that this will cause the lazy initialization of the HashMap.
115      */

116     public Map getProperties() {
117         if (this.properties == null) {
118             this.properties = new HashMap();
119         }
120         return this.properties;
121     }
122
123     /**
124      * PUBLIC:
125      * Returns the property associated with the corresponding key. These properties will be available to
126      * connection events.
127      */

128     public Object JavaDoc getProperty(Object JavaDoc object) {
129         if (this.hasProperties()) {
130             return this.getProperties().get(object);
131         }
132         return null;
133     }
134
135     /**
136      * PUBLIC:
137      * Return if a login is used, only one of login and pool can be used.
138      */

139     public boolean hasLogin() {
140         return login != null;
141     }
142
143     /**
144      * PUBLIC:
145      * Returns true if properties are available on the Connection Policy
146      */

147     public boolean hasProperties() {
148         return (this.properties != null) && (!this.properties.isEmpty());
149     }
150
151     /**
152      * PUBLIC:
153      * Return if a lazy connection should be used, a lazy connection only acquire a physical connection
154      * when a transaction is started and releases the connection when the transaction completes.
155      */

156     public boolean isLazy() {
157         return isLazy;
158     }
159
160     /**
161      * INTERNAL:
162      * Return if part of a connection pool.
163      */

164     public boolean isPooled() {
165         return poolName != null;
166     }
167
168     /**
169      * INTERNAL:
170      * Return if part of a connection pool.
171      */

172     public boolean isUserDefinedConnection() {
173         return poolName == null;
174     }
175
176     /**
177      * PUBLIC:
178      * This method is used to remove a custom property from the Connection Policy.
179      * This method will return the propery removed. If it was not found then null
180      * will be returned.
181      */

182     public Object JavaDoc removeProperty(Object JavaDoc key) {
183         if (this.hasProperties()) {
184             return getProperties().remove(key);
185         }
186         return null;
187     }
188
189     /**
190      * PUBLIC:
191      * Set if a lazy connection should be used, a lazy connection only acquire a physical connection
192      * when a transaction is started and releases the connection when the transaction completes.
193      */

194     public void setIsLazy(boolean isLazy) {
195         this.isLazy = isLazy;
196     }
197
198     /**
199      * PUBLIC:
200      * Set the login to use for this connection.
201      * Client sessions support using a seperate user login for database modification.
202      * Pooled connections must use the pool's login and cannot define their own.
203      */

204     public void setLogin(Login login) {
205         this.login = login;
206     }
207
208     /**
209      * PUBLIC:
210      * Set the pool name or null if not part of a pool.
211      */

212     public void setPoolName(String JavaDoc poolName) {
213         this.poolName = poolName;
214     }
215
216     /**
217      * PUBLIC:
218      * Use this method to set custom properties on the Connection Policy. These
219      * properties will be available from within connection events but have no
220      * effect on the connection directly.
221      */

222     public void setProperty(Object JavaDoc key, Object JavaDoc property) {
223         getProperties().put(key, property);
224     }
225
226     /**
227      * INTERNAL:
228      * return a string representation of this ConnectionPolicy
229      */

230     public String JavaDoc toString() {
231         String JavaDoc type = "";
232         if (isPooled()) {
233             type = "(" + ToStringLocalization.buildMessage("pooled", (Object JavaDoc[])null) + ": " + getPoolName();
234         } else {
235             type = "(" + ToStringLocalization.buildMessage("login", (Object JavaDoc[])null) + ": " + getLogin();
236         }
237         if (isLazy()) {
238             type = type + "," + ToStringLocalization.buildMessage("lazy", (Object JavaDoc[])null) + ")";
239         } else {
240             type = type + "," + ToStringLocalization.buildMessage("non-lazy", (Object JavaDoc[])null) + ")";
241         }
242
243         return Helper.getShortClassName(getClass()) + type;
244     }
245
246     /**
247      * PUBLIC:
248      * A lazy connection only acquires a physical connection
249      * when a transaction is started and releases the connection when the transaction completes.
250      */

251     public void useLazyConnection() {
252         setIsLazy(true);
253     }
254 }
255
Popular Tags