KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > tools > URLCheck


1 /*
2
3    Derby - Class org.apache.derby.tools.URLCheck
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.tools;
23
24 import org.apache.derby.iapi.reference.Attribute;
25 import org.apache.derby.iapi.tools.i18n.LocalizedResource;
26 import org.apache.derby.impl.tools.ij.AttributeHolder;
27 import java.util.Vector JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.lang.reflect.Field JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34 /**
35  * This class takes a string used for a connection URL and checks for
36  * correctness.
37  * To turn off output in ij, use the command line
38  * property of -DURLCheck=false.
39  *
40  * param anURL The URL used to connect to a database.
41  *
42  */

43
44 public class URLCheck {
45
46   public Vector JavaDoc attributes;
47   public static Vector JavaDoc booleanAttributes;
48   //Need so that AppUI class does not get garbage collected
49
LocalizedResource langUtil = LocalizedResource.getInstance();
50   Vector JavaDoc validProps;
51
52   public URLCheck(String JavaDoc anURL) {
53
54     try {
55       //Initialize the AppUI class
56

57       //Parse the URL string into properties.
58
Properties JavaDoc props = getAttributes(anURL, new Properties JavaDoc());
59       check();
60     }
61     catch (Exception JavaDoc ex) {
62       ex.printStackTrace();
63     }
64   }
65  
66
67   public static void main(String JavaDoc[] args) {
68     if (args.length > 0) {
69       //Get the first argument passed in.
70
URLCheck aCheck = new URLCheck(args[0]);
71     }
72   }
73   public void check(){
74     Enumeration JavaDoc e = attributes.elements();
75     while (e.hasMoreElements()) {
76       AttributeHolder anAttribute = (AttributeHolder)e.nextElement();
77       //The check for duplicate must be done at the URLCheck level
78
//and not by each specific attribute. Only URLCheck knowns about
79
//all of the attributes and names.
80
checkForDuplicate(anAttribute);
81       //Have each attribute check as much about themself as possible.
82
anAttribute.check( validProps);
83     }
84   }
85   public void checkForDuplicate(AttributeHolder anAttribute){
86     Enumeration JavaDoc e = attributes.elements();
87     while (e.hasMoreElements()) {
88       AttributeHolder aHolder = (AttributeHolder)e.nextElement();
89       //If a duplicate is found, make sure that the message is only shown
90
//once for each attribute.
91
if (anAttribute != aHolder && anAttribute.getName().equals(aHolder.getName())) {
92         anAttribute.addError(langUtil.getTextMessage("TL_dupAtt"));
93       }
94     }
95
96   }
97     public Properties JavaDoc getAttributes(String JavaDoc url, Properties JavaDoc props) throws Exception JavaDoc {
98         
99         String JavaDoc protocol = "";
100
101         if( url.startsWith( "jdbc:derby:net:") ||
102             url.startsWith( "jdbc:derby://"))
103         {
104             validProps = null;
105         }
106         else if( url.startsWith( "jdbc:derby:"))
107         {
108             protocol = "jdbc:derby:";
109             validProps = getValidDerbyProps();
110         }
111         else
112             validProps = null;
113
114         
115         //Parse the url into attributes and put them in a Properties object.
116
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url.substring(protocol.length()), ";:\"");
117         attributes = new Vector JavaDoc();
118         while (st.hasMoreTokens()) {
119       AttributeHolder anAttribute = new AttributeHolder();
120       String JavaDoc anAtt = "";
121       String JavaDoc aValue = "";
122       String JavaDoc aToken = st.nextToken();
123       //The "=" is the seperator between key and value.
124
int eqPos = aToken.indexOf('=');
125       if (eqPos == -1) {
126           //If there is no "=" this is not an attribute
127
continue;
128       }
129       else {
130         anAtt = (aToken.substring(0, eqPos)).trim();
131         aValue = (aToken.substring(eqPos + 1)).trim();
132
133       }
134       anAttribute.setName(anAtt);
135       anAttribute.setValue(aValue);
136       anAttribute.setToken(aToken);
137       attributes.addElement(anAttribute);
138       props.put(anAtt, aToken);
139     }
140         return props;
141     }
142
143   public static Vector JavaDoc getBooleanAttributes(){
144     if (booleanAttributes == null) {
145       booleanAttributes = new Vector JavaDoc();
146           booleanAttributes.addElement(Attribute.DATA_ENCRYPTION);
147           booleanAttributes.addElement(Attribute.CREATE_ATTR);
148           booleanAttributes.addElement(Attribute.SHUTDOWN_ATTR);
149           booleanAttributes.addElement(Attribute.UPGRADE_ATTR);
150     }
151     return booleanAttributes;
152   }
153
154     private static Vector JavaDoc validDerbyProps;
155     private Vector JavaDoc getValidDerbyProps()
156     {
157         if( validDerbyProps == null)
158         {
159             try
160             {
161                 Vector JavaDoc props = new Vector JavaDoc();
162                 Class JavaDoc att = Attribute.class;
163                 //Use reflection to get the list of valid keys from the Attribute class.
164
//The Attribute class is an interface and therefore all the field
165
//for it are public.
166
Field JavaDoc[] fields = att.getFields();
167                 for (int i = 0; i < fields.length; i++)
168                 {
169                     Field JavaDoc aField = (Field JavaDoc)fields[i];
170                     props.addElement(aField.get(att));
171                 }
172                 validDerbyProps = props;
173             }
174             catch (Exception JavaDoc ex)
175             {
176                 ex.printStackTrace();
177             }
178         }
179         return validDerbyProps;
180     } // end of getValidDerbyProps
181

182 }
183
Popular Tags