1 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 ; 28 import java.util.Properties ; 29 import java.util.Enumeration ; 30 import java.util.StringTokenizer ; 31 import java.lang.reflect.Field ; 32 import java.sql.SQLException ; 33 34 43 44 public class URLCheck { 45 46 public Vector attributes; 47 public static Vector booleanAttributes; 48 LocalizedResource langUtil = LocalizedResource.getInstance(); 50 Vector validProps; 51 52 public URLCheck(String anURL) { 53 54 try { 55 57 Properties props = getAttributes(anURL, new Properties ()); 59 check(); 60 } 61 catch (Exception ex) { 62 ex.printStackTrace(); 63 } 64 } 65 66 67 public static void main(String [] args) { 68 if (args.length > 0) { 69 URLCheck aCheck = new URLCheck(args[0]); 71 } 72 } 73 public void check(){ 74 Enumeration e = attributes.elements(); 75 while (e.hasMoreElements()) { 76 AttributeHolder anAttribute = (AttributeHolder)e.nextElement(); 77 checkForDuplicate(anAttribute); 81 anAttribute.check( validProps); 83 } 84 } 85 public void checkForDuplicate(AttributeHolder anAttribute){ 86 Enumeration e = attributes.elements(); 87 while (e.hasMoreElements()) { 88 AttributeHolder aHolder = (AttributeHolder)e.nextElement(); 89 if (anAttribute != aHolder && anAttribute.getName().equals(aHolder.getName())) { 92 anAttribute.addError(langUtil.getTextMessage("TL_dupAtt")); 93 } 94 } 95 96 } 97 public Properties getAttributes(String url, Properties props) throws Exception { 98 99 String 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 StringTokenizer st = new StringTokenizer (url.substring(protocol.length()), ";:\""); 117 attributes = new Vector (); 118 while (st.hasMoreTokens()) { 119 AttributeHolder anAttribute = new AttributeHolder(); 120 String anAtt = ""; 121 String aValue = ""; 122 String aToken = st.nextToken(); 123 int eqPos = aToken.indexOf('='); 125 if (eqPos == -1) { 126 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 getBooleanAttributes(){ 144 if (booleanAttributes == null) { 145 booleanAttributes = new Vector (); 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 validDerbyProps; 155 private Vector getValidDerbyProps() 156 { 157 if( validDerbyProps == null) 158 { 159 try 160 { 161 Vector props = new Vector (); 162 Class att = Attribute.class; 163 Field [] fields = att.getFields(); 167 for (int i = 0; i < fields.length; i++) 168 { 169 Field aField = (Field )fields[i]; 170 props.addElement(aField.get(att)); 171 } 172 validDerbyProps = props; 173 } 174 catch (Exception ex) 175 { 176 ex.printStackTrace(); 177 } 178 } 179 return validDerbyProps; 180 } 182 } 183 | Popular Tags |