1 16 package org.apache.catalina.storeconfig; 17 18 import java.beans.IndexedPropertyDescriptor ; 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.catalina.Container; 31 import org.apache.catalina.connector.Connector; 32 import org.apache.catalina.core.StandardContext; 33 import org.apache.catalina.core.StandardHost; 34 import org.apache.coyote.ProtocolHandler; 35 import org.apache.tomcat.util.IntrospectionUtils; 36 37 47 public class ConnectorStoreAppender extends StoreAppender { 48 49 protected static HashMap replacements = new HashMap (); 50 static { 51 replacements.put("backlog", "acceptCount"); 52 replacements.put("soLinger", "connectionLinger"); 53 replacements.put("soTimeout", "connectionTimeout"); 54 replacements.put("timeout", "connectionUploadTimeout"); 55 replacements.put("clientauth", "clientAuth"); 56 replacements.put("keystore", "keystoreFile"); 57 replacements.put("randomfile", "randomFile"); 58 replacements.put("rootfile", "rootFile"); 59 replacements.put("keypass", "keystorePass"); 60 replacements.put("keytype", "keystoreType"); 61 replacements.put("protocol", "sslProtocol"); 62 replacements.put("protocols", "sslProtocols"); 63 } 64 65 80 public void printAttributes(PrintWriter writer, int indent, 81 boolean include, Object bean, StoreDescription desc) 82 throws Exception { 83 84 String className = bean.getClass().getName(); 86 87 if (include && desc != null && !desc.isStandard()) { 89 writer.print(" className=\""); 90 writer.print(bean.getClass().getName()); 91 writer.print("\""); 92 } 93 94 List propertyKeys = getPropertyKeys((Connector) bean); 95 Object bean2 = defaultInstance(bean); 97 for (Iterator propertyIterator = propertyKeys.iterator(); propertyIterator 98 .hasNext();) { 99 String key = (String ) propertyIterator.next(); 100 Object value = (Object ) IntrospectionUtils.getProperty(bean, key); 101 102 if (desc.isTransientAttribute(key)) { 103 continue; } 105 if (value == null) { 106 continue; } 108 if (!isPersistable(value.getClass())) { 109 continue; 110 } 111 Object value2 = IntrospectionUtils.getProperty(bean2, key); 112 if (value.equals(value2)) { 113 continue; 115 } 116 if (isPrintValue(bean, bean2, key, desc)) 117 printValue(writer, indent, key, value); 118 } 119 String protocol = ((Connector) bean).getProtocol(); 120 if (protocol != null && !"HTTP/1.1".equals(protocol)) 121 super.printValue(writer, indent, "protocol", protocol); 122 123 } 124 125 132 protected List getPropertyKeys(Connector bean) 133 throws IntrospectionException { 134 ArrayList propertyKeys = new ArrayList (); 135 ProtocolHandler protocolHandler = bean.getProtocolHandler(); 137 PropertyDescriptor descriptors[] = Introspector.getBeanInfo( 139 bean.getClass()).getPropertyDescriptors(); 140 if (descriptors == null) { 141 descriptors = new PropertyDescriptor [0]; 142 } 143 for (int i = 0; i < descriptors.length; i++) { 144 if (descriptors[i] instanceof IndexedPropertyDescriptor ) { 145 continue; } 147 if (!isPersistable(descriptors[i].getPropertyType()) 148 || (descriptors[i].getReadMethod() == null) 149 || (descriptors[i].getWriteMethod() == null)) { 150 continue; } 152 if ("protocol".equals(descriptors[i].getName()) 153 || "protocolHandlerClassName".equals(descriptors[i] 154 .getName())) 155 continue; 156 propertyKeys.add(descriptors[i].getName()); 157 } 158 for (Iterator propertyIterator = protocolHandler.getAttributeNames(); propertyIterator 159 .hasNext();) { 160 Object key = propertyIterator.next(); 161 if (propertyKeys.contains(key)) 162 continue; 163 propertyKeys.add(key); 164 } 165 return propertyKeys; 166 } 167 168 177 protected void storeConnectorAttribtues(PrintWriter aWriter, int indent, 178 Object bean, StoreDescription aDesc) throws Exception { 179 if (aDesc.isAttributes()) { 180 printAttributes(aWriter, indent, false, bean, aDesc); 181 } 182 } 183 184 191 public void printOpenTag(PrintWriter aWriter, int indent, Object bean, 192 StoreDescription aDesc) throws Exception { 193 aWriter.print("<"); 194 aWriter.print(aDesc.getTag()); 195 storeConnectorAttribtues(aWriter, indent, bean, aDesc); 196 aWriter.println(">"); 197 } 198 199 206 public void printTag(PrintWriter aWriter, int indent, Object bean, 207 StoreDescription aDesc) throws Exception { 208 aWriter.print("<"); 209 aWriter.print(aDesc.getTag()); 210 storeConnectorAttribtues(aWriter, indent, bean, aDesc); 211 aWriter.println("/>"); 212 } 213 214 223 public void printValue(PrintWriter writer, int indent, String name, 224 Object value) { 225 String repl = name; 226 if (replacements.get(name) != null) { 227 repl = (String ) replacements.get(name); 228 } 229 super.printValue(writer, indent, repl, value); 230 } 231 232 241 public boolean isPrintValue(Object bean, Object bean2, String attrName, 242 StoreDescription desc) { 243 boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc); 244 if (isPrint) { 245 if ("jkHome".equals(attrName)) { 246 Connector connector = ((Connector) bean); 247 File catalinaBase = getCatalinaBase(); 248 File jkHomeBase = getJkHomeBase((String ) connector 249 .getProperty("jkHome"), catalinaBase); 250 isPrint = !catalinaBase.equals(jkHomeBase); 251 252 } 253 } 254 return isPrint; 255 } 256 257 protected File getCatalinaBase() { 258 259 File appBase; 260 File file = new File (System.getProperty("catalina.base")); 261 try { 262 file = file.getCanonicalFile(); 263 } catch (IOException e) { 264 } 265 return (file); 266 } 267 268 protected File getJkHomeBase(String jkHome, File appBase) { 269 270 File jkHomeBase; 271 File file = new File (jkHome); 272 if (!file.isAbsolute()) 273 file = new File (appBase, jkHome); 274 try { 275 jkHomeBase = file.getCanonicalFile(); 276 } catch (IOException e) { 277 jkHomeBase = file; 278 } 279 return (jkHomeBase); 280 } 281 282 } | Popular Tags |