KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > ConnectorStoreAppender


1 /**
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.catalina.storeconfig;
17
18 import java.beans.IndexedPropertyDescriptor JavaDoc;
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
38  * Store the Connector attributes. Connector has really special design. A
39  * Connector is only a startup Wrapper for a ProtocolHandler. This meant that
40  * ProtocolHandler get all there attribtues from the Connector attribtue map.
41  * Strange is that some attributes change there name and the attribute
42  * sslProtocol need a sepzial handling
43  *
44  * @author Peter Rossbach
45  *
46  */

47 public class ConnectorStoreAppender extends StoreAppender {
48
49     protected static HashMap JavaDoc replacements = new HashMap JavaDoc();
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     /**
66      * Store the relevant attributes of the specified JavaBean.
67      *
68      * @param writer
69      * PrintWriter to which we are storing
70      * @param include
71      * Should we include a <code>className</code> attribute?
72      * @param bean
73      * Bean whose properties are to be rendered as attributes,
74      * @param desc
75      * RegistryDescrpitor from this bean
76      *
77      * @exception Exception
78      * if an exception occurs while storing
79      */

80     public void printAttributes(PrintWriter JavaDoc writer, int indent,
81             boolean include, Object JavaDoc bean, StoreDescription desc)
82             throws Exception JavaDoc {
83
84         // Render the relevant properties of this bean
85
String JavaDoc className = bean.getClass().getName();
86
87         // Render a className attribute if requested
88
if (include && desc != null && !desc.isStandard()) {
89             writer.print(" className=\"");
90             writer.print(bean.getClass().getName());
91             writer.print("\"");
92         }
93
94         List JavaDoc propertyKeys = getPropertyKeys((Connector) bean);
95         // Create blank instance
96
Object JavaDoc bean2 = defaultInstance(bean);
97         for (Iterator JavaDoc propertyIterator = propertyKeys.iterator(); propertyIterator
98                 .hasNext();) {
99             String JavaDoc key = (String JavaDoc) propertyIterator.next();
100             Object JavaDoc value = (Object JavaDoc) IntrospectionUtils.getProperty(bean, key);
101
102             if (desc.isTransientAttribute(key)) {
103                 continue; // Skip the specified exceptions
104
}
105             if (value == null) {
106                 continue; // Null values are not persisted
107
}
108             if (!isPersistable(value.getClass())) {
109                 continue;
110             }
111             Object JavaDoc value2 = IntrospectionUtils.getProperty(bean2, key);
112             if (value.equals(value2)) {
113                 // The property has its default value
114
continue;
115             }
116             if (isPrintValue(bean, bean2, key, desc))
117                 printValue(writer, indent, key, value);
118         }
119         String JavaDoc protocol = ((Connector) bean).getProtocol();
120         if (protocol != null && !"HTTP/1.1".equals(protocol))
121             super.printValue(writer, indent, "protocol", protocol);
122
123     }
124
125     /**
126      * Get all properties from Connector and current ProtocolHandler
127      *
128      * @param bean
129      * @return List of Connector Properties
130      * @throws IntrospectionException
131      */

132     protected List JavaDoc getPropertyKeys(Connector bean)
133             throws IntrospectionException JavaDoc {
134         ArrayList JavaDoc propertyKeys = new ArrayList JavaDoc();
135         // Acquire the list of properties for this bean
136
ProtocolHandler protocolHandler = bean.getProtocolHandler();
137         // Acquire the list of properties for this bean
138
PropertyDescriptor JavaDoc descriptors[] = Introspector.getBeanInfo(
139                 bean.getClass()).getPropertyDescriptors();
140         if (descriptors == null) {
141             descriptors = new PropertyDescriptor JavaDoc[0];
142         }
143         for (int i = 0; i < descriptors.length; i++) {
144             if (descriptors[i] instanceof IndexedPropertyDescriptor JavaDoc) {
145                 continue; // Indexed properties are not persisted
146
}
147             if (!isPersistable(descriptors[i].getPropertyType())
148                     || (descriptors[i].getReadMethod() == null)
149                     || (descriptors[i].getWriteMethod() == null)) {
150                 continue; // Must be a read-write primitive or String
151
}
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 JavaDoc propertyIterator = protocolHandler.getAttributeNames(); propertyIterator
159                 .hasNext();) {
160             Object JavaDoc key = propertyIterator.next();
161             if (propertyKeys.contains(key))
162                 continue;
163             propertyKeys.add(key);
164         }
165         return propertyKeys;
166     }
167
168     /**
169      * print Attributes
170      *
171      * @param aWriter
172      * @param indent
173      * @param bean
174      * @param aDesc
175      * @throws Exception
176      */

177     protected void storeConnectorAttribtues(PrintWriter JavaDoc aWriter, int indent,
178             Object JavaDoc bean, StoreDescription aDesc) throws Exception JavaDoc {
179         if (aDesc.isAttributes()) {
180             printAttributes(aWriter, indent, false, bean, aDesc);
181         }
182     }
183
184     /*
185      * Print the open tag for connector attributes (override)
186      *
187      * @see org.apache.catalina.storeconfig.StoreAppender#printOpenTag(java.io.PrintWriter,
188      * int, java.lang.Object,
189      * org.apache.catalina.storeconfig.StoreDescription)
190      */

191     public void printOpenTag(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc bean,
192             StoreDescription aDesc) throws Exception JavaDoc {
193         aWriter.print("<");
194         aWriter.print(aDesc.getTag());
195         storeConnectorAttribtues(aWriter, indent, bean, aDesc);
196         aWriter.println(">");
197     }
198
199     /**
200      * print a tag for connector attributes (override)
201      *
202      * @see org.apache.catalina.storeconfig.StoreAppender#printTag(java.io.PrintWriter,
203      * int, java.lang.Object,
204      * org.apache.catalina.storeconfig.StoreDescription)
205      */

206     public void printTag(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc bean,
207             StoreDescription aDesc) throws Exception JavaDoc {
208         aWriter.print("<");
209         aWriter.print(aDesc.getTag());
210         storeConnectorAttribtues(aWriter, indent, bean, aDesc);
211         aWriter.println("/>");
212     }
213
214     /**
215      * print a value but replace attribute name
216      *
217      * @param writer
218      * @param name
219      * @param value
220      * @see org.apache.catalina.storeconfig.StoreAppender#printValue(java.io.PrintWriter,
221      * int, java.lang.String, java.lang.Object)
222      */

223     public void printValue(PrintWriter JavaDoc writer, int indent, String JavaDoc name,
224             Object JavaDoc value) {
225         String JavaDoc repl = name;
226         if (replacements.get(name) != null) {
227             repl = (String JavaDoc) replacements.get(name);
228         }
229         super.printValue(writer, indent, repl, value);
230     }
231     
232     /*
233      * Print Context Values. <ul><li> Spezial handling to default workDir.
234      * </li><li> Don't save path at external context.xml </li><li> Don't
235      * generate docBase for host.appBase webapps <LI></ul>
236      *
237      * @see org.apache.catalina.config.StoreAppender#isPrintValue(java.lang.Object,
238      * java.lang.Object, java.lang.String,
239      * org.apache.catalina.config.StoreDescription)
240      */

241     public boolean isPrintValue(Object JavaDoc bean, Object JavaDoc bean2, String JavaDoc 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 JavaDoc catalinaBase = getCatalinaBase();
248                 File JavaDoc jkHomeBase = getJkHomeBase((String JavaDoc) connector
249                         .getProperty("jkHome"), catalinaBase);
250                 isPrint = !catalinaBase.equals(jkHomeBase);
251
252             }
253         }
254         return isPrint;
255     }
256
257     protected File JavaDoc getCatalinaBase() {
258
259         File JavaDoc appBase;
260         File JavaDoc file = new File JavaDoc(System.getProperty("catalina.base"));
261         try {
262             file = file.getCanonicalFile();
263         } catch (IOException JavaDoc e) {
264         }
265         return (file);
266     }
267
268     protected File JavaDoc getJkHomeBase(String JavaDoc jkHome, File JavaDoc appBase) {
269
270         File JavaDoc jkHomeBase;
271         File JavaDoc file = new File JavaDoc(jkHome);
272         if (!file.isAbsolute())
273             file = new File JavaDoc(appBase, jkHome);
274         try {
275             jkHomeBase = file.getCanonicalFile();
276         } catch (IOException JavaDoc e) {
277             jkHomeBase = file;
278         }
279         return (jkHomeBase);
280     }
281
282 }
Popular Tags