KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > verifier > tests > JdbcConnectionPoolTest


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.verifier.tests;
32
33 /* Test Case to check the validity of JdbcConnection Pool fields
34  * Author : srini@sun.com
35  **/

36
37 // 8.0 XML Verifier
38
//1import com.sun.enterprise.tools.verifier.Result;
39
import com.sun.enterprise.config.serverbeans.Server;
40 import com.sun.enterprise.config.serverbeans.*;
41 import com.sun.enterprise.config.serverbeans.Resources;
42 import com.sun.enterprise.config.serverbeans.Applications;
43 import com.sun.enterprise.config.ConfigContext;
44 import com.sun.enterprise.config.ConfigContextEvent;
45 import com.sun.enterprise.config.ConfigException;
46 import com.sun.enterprise.config.serverbeans.JdbcResource;
47 import com.sun.enterprise.config.serverbeans.JdbcConnectionPool;
48
49 import com.sun.enterprise.admin.verifier.*;
50
51 // Logging
52
import java.util.logging.Logger JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import com.sun.logging.LogDomains;
55
56
57 public class JdbcConnectionPoolTest extends ServerXmlTest implements ServerCheck {
58     
59      // Logging
60
static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.APPVERIFY_LOGGER);
61
62     static int minPool=0;
63     static String JavaDoc connValReqd=null;
64     static String JavaDoc connValMethod=null;
65     
66     public JdbcConnectionPoolTest() {
67     }
68  
69     // check method called by command line verifier
70
public Result check(ConfigContext context) {
71         Result result;
72         result = super.getInitializedResult();
73         // 8.0 XML Verifier
74
/*try {
75             Server server = (Server)context.getRootConfigBean();
76             Resources resource = server.getResources();
77             JdbcConnectionPool[] pool = resource.getJdbcConnectionPool();
78             for(int i=0;i<pool.length;i++){
79                 int minPool=0, maxPool=0;
80                 try {
81                     minPool = Integer.parseInt(pool[i].getSteadyPoolSize());
82                     maxPool = Integer.parseInt(pool[i].getMaxPoolSize());
83                 } catch(NumberFormatException e) {
84                     result.failed("Bad Number");
85                 }
86                 if(maxPool < minPool)
87                     result.failed("Steady Pool size should be less than or equal to Maximum Pool size");
88                 if(pool[i].isIsConnectionValidationRequired() && pool[i].getConnectionValidationMethod().equals("table")) {
89                         if(pool[i].getValidationTableName() == null || pool[i].getValidationTableName().equals(""))
90                             result.failed("Required Table Name if Connection validation method is Table for " + pool[i].getName() );
91                         else
92                             result.passed("Validation Table Name for " + pool[i].getName() );
93                 }
94                 else
95                         result.passed("Validation Passed for Connection Pool" + pool[i].getName());
96             }
97         }
98         catch(Exception ex) {
99             // Logging
100             _logger.log(Level.FINE, "serverxmlverifier.exception", ex);
101             result.failed("Exception : " + ex.getMessage());
102         }*/

103         return result;
104     }
105     
106      //check method called by iasadmin and adminGUI
107
public Result check(ConfigContextEvent ccce) {
108         Result result = new Result();
109         result.passed("Passed **");
110         ConfigContext context = ccce.getConfigContext();
111         Object JavaDoc value = ccce.getObject();
112         String JavaDoc choice = ccce.getChoice();
113         
114         String JavaDoc beanName = ccce.getBeanName();
115         if(beanName!=null) {
116                String JavaDoc name = ccce.getName();
117                return testSave(name,(String JavaDoc)value);
118         }
119         
120         JdbcConnectionPool pool = (JdbcConnectionPool)value;
121         String JavaDoc poolName = pool.getName();
122         
123         // check if connction pool name is valid object name Bug : 4698687 : start
124
if(StaticTest.checkObjectName(poolName, result))
125             result.passed("Valid Object Name");
126         else {
127             result.failed("Connection Pool Name Invalid ");
128             return result;
129         }
130         // End Bug : 4698687
131

132         String JavaDoc datasourceClassname = pool.getDatasourceClassname();
133         
134         // to be uncommented once warning is implemented
135
/*if(isInValidDataSource(result, datasourceClassname))
136             return result;*/

137         
138         if (choice != null && choice.equals("DELETE")){
139             try{
140                     // 8.0 XML Verifier
141
//Server server = (Server)context.getRootConfigBean();
142
//Resources resource = server.getResources();
143
Domain domain = (Domain)context.getRootConfigBean();
144                     Resources resource = domain.getResources();
145                     JdbcResource[] jdbcResource = resource.getJdbcResource();
146                     if(jdbcResource.length == 0)
147                             result.passed("Connect Pool not used by data source");
148                     for(int i=0;i<jdbcResource.length;i++){
149                         if(jdbcResource[i].getPoolName().equals(poolName)) {
150                                 result.failed("Connection Pool Used by Existing data source, cannot delete pool");
151                                 break;
152                         }
153                         else
154                                result.passed("Connect Pool not used by data source");
155                     }
156             }
157             catch(Exception JavaDoc e) {
158             }
159         }
160         else {
161                  if(pool.isIsConnectionValidationRequired() && pool.getConnectionValidationMethod().equals("table")) {
162                         if(pool.getValidationTableName() == null || pool.getValidationTableName().equals(""))
163                                result.failed("Required Table Name if Connection validation method is Table");
164                         else
165                                result.passed("Validation Table Name");
166                 }
167                 else
168                      result.passed("****** Passed Validation Table Name");
169         }
170         return result;
171      }
172      
173      public Result testSave(String JavaDoc name, String JavaDoc value) {
174            Result result = new Result();
175            result.passed("Passed **");
176            if(name.equals(ServerTags.STEADY_POOL_SIZE)) {
177                try {
178                     JdbcConnectionPoolTest.minPool = Integer.parseInt(value);
179                     result.passed("Passed ");
180                }catch(NumberFormatException JavaDoc e) {
181                    result.failed("Bad Number : Steady pool size");
182                }
183            }
184            if(name.equals(ServerTags.MAX_POOL_SIZE)){
185                try {
186                     int maxPool = Integer.parseInt(value);
187                     if(maxPool < JdbcConnectionPoolTest.minPool)
188                         result.failed("Steady Pool size must be less than or equal to Maximum Pool size");
189                     else
190                         result.passed("Passed ");
191                }catch(NumberFormatException JavaDoc e) {
192                    result.failed("Bad Number : Max pool size");
193                }
194            }
195            if(name.equals(ServerTags.IS_CONNECTION_VALIDATION_REQUIRED)){
196                 JdbcConnectionPoolTest.connValReqd = value;
197            }
198            if(name.equals(ServerTags.CONNECTION_VALIDATION_METHOD)){
199                 JdbcConnectionPoolTest.connValMethod = value;
200            }
201            if(name.equals(ServerTags.VALIDATION_TABLE_NAME)){
202                if(connValReqd.equals("true")) {
203                    if(connValMethod.equals("table")) {
204                        if(value == null || value.equals(""))
205                             result.failed("Required table name");
206                        else
207                            result.passed("Passed ***");
208                    }
209                    else
210                        result.passed("Passed ***");
211                }
212                else
213                    result.passed("Passed ***");
214            }// to be uncommented once warning is implemented
215
/*if(name.equals(ServerTags.DATASOURCE_CLASSNAME)) {
216                 if(isInValidDataSource(result, value)) {
217                     return result;
218                 }
219            }*/

220             // Check for transaction-isolation-level
221
if (name.equals(ServerTags.TRANSACTION_ISOLATION_LEVEL)) {
222                 if (value != null) {
223                     String JavaDoc isolation = (String JavaDoc) value;
224                 
225                      if (isolation.equals(""))
226                         result.failed("Transaction Isolation Level not specified");
227                      else if ((isolation.equals("read-uncommitted")) ||
228                         (isolation.equals("read-committed")) ||
229                         (isolation.equals("repeatable-read")) ||
230                         (isolation.equals("serializable")))
231                          result.passed("Valid Transaction Isolation Level");
232                      else result.failed("Invalid Transaction Isolation Level: " + isolation);
233                 }
234            }
235            // Bug 4675624
236
if(name.equals(ServerTags.IDLE_TIMEOUT_IN_SECONDS)) {
237                if(value != null) {
238                    try {
239                        Integer.parseInt(value);
240                        result.passed("Passed ***");
241                    } catch(NumberFormatException JavaDoc e) {
242                        result.failed("Idle Timeout : invalid number");
243                    }
244                }
245            }
246            return result;
247      }
248      
249      public boolean isInValidDataSource(Result result, String JavaDoc datasourceClassname) {
250          boolean failed = true;
251          try {
252             Class JavaDoc c1 = Class.forName(datasourceClassname);
253             Object JavaDoc obj = c1.newInstance();
254             if (obj instanceof javax.sql.DataSource JavaDoc) {
255                 result.passed("Valid Data Source");
256                 failed = false;
257             }
258             else if(obj instanceof javax.sql.XADataSource JavaDoc) {
259                 result.passed("Valid XA DataSource");
260                 failed = false;
261             }
262             else
263                 result.failed("Invalid Data Source Class not implementing, javax.sql.DataSource or javax.sql.XADataSource");
264          }catch(Exception JavaDoc e) {
265          // Logging
266
_logger.log(Level.FINE, "serverxmlverifier.error_instantiation", e.getMessage());
267              result.failed("Invalid DataSource class");
268          }
269          return failed;
270      }
271 }
272
Popular Tags