KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > JdbcConnectionPoolHandlers


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  * JdbcHandlers.java
26  *
27  * Created on November 21, 2003, 10:22 AM
28  */

29
30 package com.sun.enterprise.tools.admingui.handlers;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36
37 import javax.servlet.http.HttpSession JavaDoc;
38 import com.iplanet.jato.RequestContext;
39 import com.iplanet.jato.RequestManager;
40 import com.iplanet.jato.model.DefaultModel;
41 import com.iplanet.jato.model.Model;
42 import com.iplanet.jato.view.View;
43 import com.iplanet.jato.view.ViewBase;
44 import com.iplanet.jato.model.ModelControlException;
45
46
47 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
48 import com.sun.enterprise.tools.guiframework.model.ModelManager;
49 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
50 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
51 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
52
53 import com.sun.web.ui.model.CCActionTableModelInterface;
54 import com.sun.web.ui.view.html.CCCheckBox;
55 import com.sun.web.ui.view.html.CCTextField;
56
57 import com.sun.enterprise.connectors.util.ConnectionDefinitionUtils;
58
59 public class JdbcConnectionPoolHandlers {
60     
61     public static final String JavaDoc PROPERTY_NAME = "propertyName";
62     public static final String JavaDoc PROPERTY_VALUE = "propertyValue";
63     
64     /** Creates a new instance of JdbcHandlers */
65     
66     public void getValuesFromStep1(RequestContext ctx, HandlerContext handlerCtx) throws ModelControlException {
67     View view = handlerCtx.getView();
68     DescriptorContainerView descView = (DescriptorContainerView)(((ViewBase)view));
69     ViewDescriptor vd = descView.getViewDescriptor();
70     HttpSession JavaDoc session = RequestManager.getSession();
71         DefaultModel step1Model = (DefaultModel)(session.getValue("connectionPoolStep1.model"));
72         DefaultModel step3Model = (DefaultModel)(session.getValue("connectionPoolStep3.model"));
73         String JavaDoc resourceType = (String JavaDoc)step1Model.getValue("ResourceType");
74         String JavaDoc vendor = (String JavaDoc)step1Model.getValue("Vendor");
75         boolean changed = true;
76         if (step3Model != null) {
77             if (resourceType != null &&
78                 resourceType.equals(step3Model.getValue("ResourceType")) &&
79                 vendor != null &&
80                 vendor.equals(step3Model.getValue("Vendor"))) {
81                     changed = false;
82             }
83         }
84         if (changed) {
85             String JavaDoc datasourceClassName = getDatasourceClassName(vendor, resourceType);
86             if (datasourceClassName != null) {
87                 descView.setDisplayFieldValue("DatasourceClassName", datasourceClassName.trim());
88             }
89             CCActionTableModelInterface model = (CCActionTableModelInterface) handlerCtx.getInputValue("propertiesModel");
90             populateProperties(datasourceClassName, model);
91         }
92         descView.setDisplayFieldValue("Name", step1Model.getValue("Name"));
93         descView.setDisplayFieldValue("ResourceType", resourceType);
94         descView.setDisplayFieldValue("Vendor", vendor);
95     }
96
97     public void setDatasourceClassName(RequestContext ctx, HandlerContext handlerCtx) {
98     DescriptorContainerView descView = (DescriptorContainerView)(handlerCtx.getView());
99     ViewDescriptor vd = descView.getViewDescriptor();
100         String JavaDoc vendor = (String JavaDoc)descView.getDisplayFieldValue("Vendor");
101         String JavaDoc resourceType = (String JavaDoc)descView.getDisplayFieldValue("ResourceType");
102         String JavaDoc datasourceClassName = getDatasourceClassName(vendor, resourceType);
103         if (datasourceClassName != null) {
104             descView.setDisplayFieldValue("DatasourceClassName", datasourceClassName.trim());
105         }
106     }
107     
108     public void enableGuaranteeIsolation(RequestContext ctx, HandlerContext handlerCtx) {
109     View view = handlerCtx.getView();
110     CCCheckBox guaranteeIsolation = (CCCheckBox)view;
111     DescriptorContainerView descView = (DescriptorContainerView)view.getParent();
112     String JavaDoc transIsolation = (String JavaDoc)descView.getDisplayFieldValue("TransactionIsolation");
113     
114     if (transIsolation != null && transIsolation.length() > 0) {
115         guaranteeIsolation.setDisabled(false);
116     }
117     else {
118         guaranteeIsolation.setValue("false");
119         guaranteeIsolation.setDisabled(true);
120     }
121     }
122     
123     public void enableTableName(RequestContext ctx, HandlerContext handlerCtx) {
124     View view = handlerCtx.getView();
125     CCTextField tableName = (CCTextField)view;
126     DescriptorContainerView descView = (DescriptorContainerView)view.getParent();
127     String JavaDoc validMethod = (String JavaDoc)descView.getDisplayFieldValue("ConnectionValidationMethod");
128     
129     if (validMethod != null && validMethod.equals("table")) {
130         tableName.setDisabled(false);
131     }
132     else {
133         tableName.setDisabled(true);
134     }
135     }
136
137     public static String JavaDoc getDatasourceClassName(String JavaDoc vendor, String JavaDoc resourceType) {
138         ResourceBundle JavaDoc bundle =
139             ResourceBundle.getBundle("com.sun.enterprise.tools.admingui.resources.JdbcVendors");
140         if (vendor != null) {
141             try {
142                 /** properties file should be of the format :
143                  *<vendorname>,<resourceType>=<datasourceclassname>
144                  **/

145                 return bundle.getString(vendor.toLowerCase()+","+resourceType);
146             } catch (Exception JavaDoc ex) {
147                 return null; // unknown vendor/resourceType
148
}
149         }
150         return null;
151     }
152     
153     public void populateProperties(String JavaDoc dataSource,
154             CCActionTableModelInterface model) throws ModelControlException{
155         String JavaDoc[] defaultProperties = {
156             "databaseName", "serverName", "port", "networkProtocol",
157             "user", "password", "roleName", "datasourceName" };
158         RequestContext ctx = RequestManager.getRequestContext();
159         Map JavaDoc properties = null;
160         try {
161             properties = ConnectionDefinitionUtils.getConnectionDefinitionPropertiesAndDefaults(dataSource.trim());
162         } catch (Exception JavaDoc ex) {
163             /** deliberately ignore exception when trying to get properties.
164              *this may be because the classname is not in path. default properties
165              *would be loaded in this case.
166              **/

167         }
168         if (properties == null || properties.size() == 0) {
169             ((DefaultModel)model).clear();
170             model.beforeFirst();
171             for (int i = 0; i < defaultProperties.length; i++) {
172                 model.appendRow();
173                 model.setValue(PROPERTY_NAME, defaultProperties[i]);
174                 model.setValue(PROPERTY_VALUE, "");
175                 model.setRowSelected(false);
176             }
177         }
178         else {
179             Iterator JavaDoc keys = properties.keySet().iterator();
180             ((DefaultModel)model).clear();
181             model.beforeFirst();
182             while (keys.hasNext()) {
183                 model.appendRow();
184                 String JavaDoc name = (String JavaDoc)keys.next();
185                 model.setValue(PROPERTY_NAME, name);
186                 model.setValue(PROPERTY_VALUE, properties.get(name));
187                 model.setRowSelected(false);
188             }
189         }
190     }
191     
192     public static void main(String JavaDoc __args[]) {
193         //test
194
System.out.println("DS = " + getDatasourceClassName("sybase" , "javax.sql.XADataSource"));
195     }
196 }
197
Popular Tags