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
|
/*
* 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.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.catalina.Globals;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
import org.apache.tomcat.util.IntrospectionUtils;
import org.apache.tomcat.util.net.SocketProperties;
/**
* Store the Connector attributes. Connector has really special design. A Connector is only a startup Wrapper for a
* ProtocolHandler. This meant that ProtocolHandler get all there attributes from the Connector attribute map. Strange
* is that some attributes change their name.
*/
public class ConnectorStoreAppender extends StoreAppender {
protected static final HashMap<String,String> replacements = new HashMap<>();
protected static final Set<String> internalExecutorAttributes = new HashSet<>();
static {
replacements.put("timeout", "connectionUploadTimeout");
replacements.put("randomfile", "randomFile");
internalExecutorAttributes.add("maxThreads");
internalExecutorAttributes.add("minSpareThreads");
internalExecutorAttributes.add("threadPriority");
}
@Override
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("\"");
}
Connector connector = (Connector) bean;
String protocol = connector.getProtocol();
List<String> propertyKeys = getPropertyKeys(connector);
// Create blank instance
Object bean2 = new Connector(protocol);// defaultInstance(bean);
for (String key : propertyKeys) {
Object value = IntrospectionUtils.getProperty(bean, key);
if (desc.isTransientAttribute(key)) {
continue; // Skip the specified exceptions
}
if (value == null) {
continue; // Null values are not persisted
}
if (!isPersistable(value.getClass())) {
continue;
}
Object value2 = IntrospectionUtils.getProperty(bean2, key);
if (value.equals(value2)) {
// The property has its default value
continue;
}
if (isPrintValue(bean, bean2, key, desc)) {
printValue(writer, indent, key, value);
}
}
if (protocol != null && !"HTTP/1.1".equals(protocol)) {
super.printValue(writer, indent, "protocol", protocol);
}
String executorName = connector.getExecutorName();
if (!Connector.INTERNAL_EXECUTOR_NAME.equals(executorName)) {
super.printValue(writer, indent, "executor", executorName);
}
}
/**
* Get all properties from Connector and current ProtocolHandler.
*
* @param bean The connector
*
* @return List of Connector property names
*
* @throws IntrospectionException Error introspecting connector
*/
protected List<String> getPropertyKeys(Connector bean) throws IntrospectionException {
List<String> propertyKeys = new ArrayList<>();
// Acquire the list of properties for this bean
ProtocolHandler protocolHandler = bean.getProtocolHandler();
// Acquire the list of properties for this bean
PropertyDescriptor[] descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
if (descriptors == null) {
descriptors = new PropertyDescriptor[0];
}
for (PropertyDescriptor descriptor : descriptors) {
if (descriptor instanceof IndexedPropertyDescriptor) {
continue; // Indexed properties are not persisted
}
if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) ||
(descriptor.getWriteMethod() == null)) {
continue; // Must be a read-write primitive or String
}
if ("protocol".equals(descriptor.getName()) || "protocolHandlerClassName".equals(descriptor.getName())) {
continue;
}
propertyKeys.add(descriptor.getName());
}
// Add the properties of the protocol handler
descriptors = Introspector.getBeanInfo(protocolHandler.getClass()).getPropertyDescriptors();
if (descriptors == null) {
descriptors = new PropertyDescriptor[0];
}
for (PropertyDescriptor descriptor : descriptors) {
if (descriptor instanceof IndexedPropertyDescriptor) {
continue; // Indexed properties are not persisted
}
if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) ||
(descriptor.getWriteMethod() == null)) {
continue; // Must be a read-write primitive or String
}
String key = descriptor.getName();
if (!Connector.INTERNAL_EXECUTOR_NAME.equals(bean.getExecutorName()) &&
internalExecutorAttributes.contains(key)) {
continue;
}
if (replacements.get(key) != null) {
key = replacements.get(key);
}
if (!propertyKeys.contains(key)) {
propertyKeys.add(key);
}
}
// Add the properties for the socket
final String socketName = "socket.";
descriptors = Introspector.getBeanInfo(SocketProperties.class).getPropertyDescriptors();
if (descriptors == null) {
descriptors = new PropertyDescriptor[0];
}
for (PropertyDescriptor descriptor : descriptors) {
if (descriptor instanceof IndexedPropertyDescriptor) {
continue; // Indexed properties are not persisted
}
if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) ||
(descriptor.getWriteMethod() == null)) {
continue; // Must be a read-write primitive or String
}
String key = descriptor.getName();
if (replacements.get(key) != null) {
key = replacements.get(key);
}
if (!propertyKeys.contains(key)) {
// Add socket.[original name] if this is not a property
// that could be set elsewhere
propertyKeys.add(socketName + descriptor.getName());
}
}
return propertyKeys;
}
/**
* Print Attributes for the connector
*
* @param aWriter Current writer
* @param indent Indentation level
* @param bean The connector bean
* @param aDesc The connector description
*
* @throws Exception Store error occurred
*/
protected void storeConnectorAttributes(PrintWriter aWriter, int indent, Object bean, StoreDescription aDesc)
throws Exception {
if (aDesc.isAttributes()) {
printAttributes(aWriter, indent, false, bean, aDesc);
}
}
@Override
public void printOpenTag(PrintWriter aWriter, int indent, Object bean, StoreDescription aDesc) throws Exception {
aWriter.print("<");
aWriter.print(aDesc.getTag());
storeConnectorAttributes(aWriter, indent, bean, aDesc);
aWriter.println(">");
}
@Override
public void printTag(PrintWriter aWriter, int indent, Object bean, StoreDescription aDesc) throws Exception {
aWriter.print("<");
aWriter.print(aDesc.getTag());
storeConnectorAttributes(aWriter, indent, bean, aDesc);
aWriter.println("/>");
}
@Override
public void printValue(PrintWriter writer, int indent, String name, Object value) {
String repl = name;
if (replacements.get(name) != null) {
repl = replacements.get(name);
}
super.printValue(writer, indent, repl, value);
}
/**
* Print Connector Values.
* <ul>
* <li>Special handling to default jkHome.</li>
* <li>Don't save catalina.base path at server.xml</li>
* <li>
* </ul>
*
* @see org.apache.catalina.storeconfig.StoreAppender#isPrintValue(Object, Object, String, StoreDescription)
*/
@Override
public boolean isPrintValue(Object bean, Object bean2, String attrName, StoreDescription desc) {
boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
if (isPrint) {
if ("jkHome".equals(attrName)) {
Connector connector = (Connector) bean;
File catalinaBase = getCatalinaBase();
File jkHomeBase = getJkHomeBase((String) connector.getProperty("jkHome"), catalinaBase);
isPrint = !catalinaBase.equals(jkHomeBase);
}
}
return isPrint;
}
protected File getCatalinaBase() {
File file = new File(System.getProperty(Globals.CATALINA_BASE_PROP));
try {
file = file.getCanonicalFile();
} catch (IOException ioe) {
// Ignore
}
return file;
}
protected File getJkHomeBase(String jkHome, File appBase) {
File jkHomeBase;
File file = new File(jkHome);
if (!file.isAbsolute()) {
file = new File(appBase, jkHome);
}
try {
jkHomeBase = file.getCanonicalFile();
} catch (IOException ioe) {
jkHomeBase = file;
}
return jkHomeBase;
}
}
|