1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.storeconfig;
import java.beans.IndexedPropertyDescriptor;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.Iterator;
import org.apache.tomcat.util.IntrospectionUtils;
import org.apache.tomcat.util.descriptor.web.ResourceBase;
import org.apache.tomcat.util.security.Escape;
/**
* StoreAppends generate really the xml tag elements
*/
public class StoreAppender {
/**
* The set of classes that represent persistable properties.
*/
private static final Class<?>[] persistables = { String.class, Integer.class, Integer.TYPE, Boolean.class,
Boolean.TYPE, Byte.class, Byte.TYPE, Character.class, Character.TYPE, Double.class, Double.TYPE,
Float.class, Float.TYPE, Long.class, Long.TYPE, Short.class, Short.TYPE, InetAddress.class };
private int pos = 0;
/**
* Print the closing tag.
*
* @param aWriter The output writer
* @param aDesc Store description of the current element
*
* @throws Exception A store error occurred
*/
public void printCloseTag(PrintWriter aWriter, StoreDescription aDesc) throws Exception {
aWriter.print("</");
aWriter.print(aDesc.getTag());
aWriter.println(">");
}
/**
* Print only the open tag with all attributes.
*
* @param aWriter The output writer
* @param indent Indentation level
* @param bean The current bean that is stored
* @param aDesc Store description of the current element
*
* @throws Exception A store error occurred
*/
public void printOpenTag(PrintWriter aWriter, int indent, Object bean, StoreDescription aDesc) throws Exception {
aWriter.print("<");
aWriter.print(aDesc.getTag());
if (aDesc.isAttributes() && bean != null) {
printAttributes(aWriter, indent, bean, aDesc);
}
aWriter.println(">");
}
/**
* Print tag with all attributes
*
* @param aWriter The output writer
* @param indent Indentation level
* @param bean The current bean that is stored
* @param aDesc Store description of the current element
*
* @throws Exception A store error occurred
*/
public void printTag(PrintWriter aWriter, int indent, Object bean, StoreDescription aDesc) throws Exception {
aWriter.print("<");
aWriter.print(aDesc.getTag());
if (aDesc.isAttributes() && bean != null) {
printAttributes(aWriter, indent, bean, aDesc);
}
aWriter.println("/>");
}
/**
* Print the value from tag as content.
*
* @param aWriter The output writer
* @param tag The element name
* @param content Element content
*
* @throws Exception A store error occurred
*/
public void printTagContent(PrintWriter aWriter, String tag, String content) throws Exception {
aWriter.print("<");
aWriter.print(tag);
aWriter.print(">");
aWriter.print(Escape.xml(content));
aWriter.print("</");
aWriter.print(tag);
aWriter.println(">");
}
/**
* Print an array of values.
*
* @param aWriter The output writer
* @param tag The element name
* @param indent Indentation level
* @param elements Array of element values
*/
public void printTagValueArray(PrintWriter aWriter, String tag, int indent, String[] elements) {
if (elements != null && elements.length > 0) {
printIndent(aWriter, indent + 2);
aWriter.print("<");
aWriter.print(tag);
aWriter.print(">");
for (int i = 0; i < elements.length; i++) {
printIndent(aWriter, indent + 4);
aWriter.print(elements[i]);
if (i + 1 < elements.length) {
aWriter.println(",");
}
}
printIndent(aWriter, indent + 2);
aWriter.print("</");
aWriter.print(tag);
aWriter.println(">");
}
}
/**
* Print an array of elements.
*
* @param aWriter The output writer
* @param tag The element name
* @param indent Indentation level
* @param elements Array of elements
*
* @throws Exception Store error occurred
*/
public void printTagArray(PrintWriter aWriter, String tag, int indent, String[] elements) throws Exception {
if (elements != null) {
for (String element : elements) {
printIndent(aWriter, indent);
printTagContent(aWriter, tag, element);
}
}
}
/**
* Print some spaces.
*
* @param aWriter The output writer
* @param indent The number of spaces
*/
public void printIndent(PrintWriter aWriter, int indent) {
for (int i = 0; i < indent; i++) {
aWriter.print(' ');
}
pos = indent;
}
/**
* Store the relevant attributes of the specified JavaBean, plus a <code>className</code> attribute defining the
* fully qualified Java class name of the bean.
*
* @param writer PrintWriter to which we are storing
* @param indent Indentation level
* @param bean Bean whose properties are to be rendered as attributes,
* @param desc Store description of the current element
*
* @exception Exception if an exception occurs while storing
*/
public void printAttributes(PrintWriter writer, int indent, Object bean, StoreDescription desc) throws Exception {
printAttributes(writer, indent, true, bean, desc);
}
/**
* Store the relevant attributes of the specified JavaBean.
*
* @param writer PrintWriter to which we are storing
* @param indent Indentation level
* @param include Should we include a <code>className</code> attribute?
* @param bean Bean whose properties are to be rendered as attributes,
* @param desc RegistryDescriptor from this bean
*
* @exception Exception if an exception occurs while storing
*/
public void printAttributes(PrintWriter writer, int indent, boolean include, Object bean, StoreDescription desc)
throws Exception {
// Render a className attribute if requested
if (include && !desc.isStandard()) {
writer.print(" className=\"");
writer.print(bean.getClass().getName());
writer.print("\"");
}
// Acquire the list of properties for this bean
PropertyDescriptor[] descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
if (descriptors == null) {
descriptors = new PropertyDescriptor[0];
}
// Create blank instance
Object bean2 = defaultInstance(bean);
for (PropertyDescriptor descriptor : descriptors) {
Object value = checkAttribute(desc, descriptor, descriptor.getName(), bean, bean2);
if (value != null) {
printAttribute(writer, indent, bean, desc, descriptor.getName(), bean2, value);
}
}
if (bean instanceof ResourceBase) {
ResourceBase resource = (ResourceBase) bean;
for (Iterator<String> iter = resource.listProperties(); iter.hasNext();) {
String name = iter.next();
Object value = resource.getProperty(name);
if (!isPersistable(value.getClass())) {
continue;
}
if (desc.isTransientAttribute(name)) {
continue; // Skip the specified exceptions
}
printValue(writer, indent, name, value);
}
}
}
/**
* Check if the attribute should be printed.
*
* @param desc RegistryDescriptor from this bean
* @param descriptor PropertyDescriptor from this bean property
* @param attributeName The attribute name to store
* @param bean The current bean
* @param bean2 A default instance of the bean for comparison
*
* @return null if the value should be skipped, the value to print otherwise
*/
protected Object checkAttribute(StoreDescription desc, PropertyDescriptor descriptor, String attributeName,
Object bean, Object bean2) {
if (descriptor instanceof IndexedPropertyDescriptor) {
return null; // Indexed properties are not persisted
}
if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) ||
(descriptor.getWriteMethod() == null)) {
return null; // Must be a read-write primitive or String
}
if (desc.isTransientAttribute(descriptor.getName())) {
return null; // Skip the specified exceptions
}
Object value = IntrospectionUtils.getProperty(bean, descriptor.getName());
if (value == null) {
return null; // Null values are not persisted
}
Object value2 = IntrospectionUtils.getProperty(bean2, descriptor.getName());
if (value.equals(value2)) {
// The property has its default value
return null;
}
return value;
}
/**
* Store the specified of the specified JavaBean.
*
* @param writer PrintWriter to which we are storing
* @param indent Indentation level
* @param bean The current bean
* @param desc RegistryDescriptor from this bean
* @param attributeName The attribute name to store
* @param bean2 A default instance of the bean for comparison
* @param value The attribute value
*/
protected void printAttribute(PrintWriter writer, int indent, Object bean, StoreDescription desc,
String attributeName, Object bean2, Object value) {
if (isPrintValue(bean, bean2, attributeName, desc)) {
printValue(writer, indent, attributeName, value);
}
}
/**
* Determine if the attribute value needs to be stored.
*
* @param bean original bean
* @param bean2 default bean
* @param attrName attribute name
* @param desc StoreDescription from bean
*
* @return <code>true</code> if the value should be stored
*/
public boolean isPrintValue(Object bean, Object bean2, String attrName, StoreDescription desc) {
return true;
}
/**
* Generate default Instance for the specified bean.
*
* @param bean The bean
*
* @return an object from same class as bean parameter
*
* @throws ReflectiveOperationException Error creating a new instance
*/
public Object defaultInstance(Object bean) throws ReflectiveOperationException {
return bean.getClass().getConstructor().newInstance();
}
/**
* Print an attribute value.
*
* @param writer PrintWriter to which we are storing
* @param indent Indentation level
* @param name Attribute name
* @param value Attribute value
*/
public void printValue(PrintWriter writer, int indent, String name, Object value) {
// Convert IP addresses to strings so they will be persisted
if (value instanceof InetAddress) {
value = ((InetAddress) value).getHostAddress();
}
if (!(value instanceof String)) {
value = value.toString();
}
String strValue = Escape.xml((String) value);
pos = pos + name.length() + strValue.length();
if (pos > 60) {
writer.println();
printIndent(writer, indent + 4);
} else {
writer.print(' ');
}
writer.print(name);
writer.print("=\"");
writer.print(strValue);
writer.print("\"");
}
/**
* Is the specified property type one for which we should generate a persistence attribute?
*
* @param clazz Java class to be tested
*
* @return <code>true</code> if the specified class should be stored
*/
protected boolean isPersistable(Class<?> clazz) {
for (Class<?> persistable : persistables) {
if (persistable == clazz || persistable.isAssignableFrom(clazz)) {
return true;
}
}
return false;
}
}
|