KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.catalina.cluster.tcp.ReplicationTransmitter;
28 import org.apache.tomcat.util.IntrospectionUtils;
29
30 /**
31  * Store the ReplicationTransmitter attributes.
32  *
33  * @author Peter Rossbach
34  *
35  */

36 public class ReplicationTransmitterStoreAppender extends StoreAppender {
37
38     /**
39      * Store the relevant attributes of the specified JavaBean.
40      *
41      * @param writer
42      * PrintWriter to which we are storing
43      * @param include
44      * Should we include a <code>className</code> attribute?
45      * @param bean
46      * Bean whose properties are to be rendered as attributes,
47      * @param desc
48      * RegistryDescrpitor from this bean
49      *
50      * @exception Exception
51      * if an exception occurs while storing
52      */

53     public void printAttributes(PrintWriter JavaDoc writer, int indent,
54             boolean include, Object JavaDoc bean, StoreDescription desc)
55             throws Exception JavaDoc {
56
57         // Render the relevant properties of this bean
58
String JavaDoc className = bean.getClass().getName();
59
60         // Render a className attribute if requested
61
if (include && desc != null && !desc.isStandard()) {
62             writer.print(" className=\"");
63             writer.print(bean.getClass().getName());
64             writer.print("\"");
65         }
66
67         List JavaDoc propertyKeys = getPropertyKeys((ReplicationTransmitter) bean);
68         // Create blank instance
69
Object JavaDoc bean2 = defaultInstance(bean);
70         for (Iterator JavaDoc propertyIterator = propertyKeys.iterator(); propertyIterator
71                 .hasNext();) {
72             String JavaDoc key = (String JavaDoc) propertyIterator.next();
73             Object JavaDoc value = (Object JavaDoc) IntrospectionUtils.getProperty(bean, key);
74
75             if (desc.isTransientAttribute(key)) {
76                 continue; // Skip the specified exceptions
77
}
78             if (value == null) {
79                 continue; // Null values are not persisted
80
}
81             if (!isPersistable(value.getClass())) {
82                 continue;
83             }
84             Object JavaDoc value2 = IntrospectionUtils.getProperty(bean2, key);
85             if (value.equals(value2)) {
86                 // The property has its default value
87
continue;
88             }
89             if (isPrintValue(bean, bean2, key, desc))
90                 printValue(writer, indent, key, value);
91         }
92     }
93
94     /**
95      * Get all properties from ReplicationTransmitter (also dynamic properties)
96      *
97      * @param bean
98      * @return List of Connector Properties
99      * @throws IntrospectionException
100      */

101     protected List JavaDoc getPropertyKeys(ReplicationTransmitter bean)
102             throws IntrospectionException JavaDoc {
103         ArrayList JavaDoc propertyKeys = new ArrayList JavaDoc();
104         // Acquire the list of properties for this bean
105
PropertyDescriptor JavaDoc descriptors[] = Introspector.getBeanInfo(
106                 bean.getClass()).getPropertyDescriptors();
107         if (descriptors == null) {
108             descriptors = new PropertyDescriptor JavaDoc[0];
109         }
110         for (int i = 0; i < descriptors.length; i++) {
111             if (descriptors[i] instanceof IndexedPropertyDescriptor JavaDoc) {
112                 continue; // Indexed properties are not persisted
113
}
114             if (!isPersistable(descriptors[i].getPropertyType())
115                     || (descriptors[i].getReadMethod() == null)
116                     || (descriptors[i].getWriteMethod() == null)) {
117                 continue; // Must be a read-write primitive or String
118
}
119             propertyKeys.add(descriptors[i].getName());
120         }
121         for (Iterator JavaDoc propertyIterator = bean.getPropertyNames(); propertyIterator
122                 .hasNext();) {
123             Object JavaDoc key = propertyIterator.next();
124             if (propertyKeys.contains(key))
125                 continue;
126             if ("className".equals(key))
127                 continue;
128             propertyKeys.add(key);
129         }
130         return propertyKeys;
131     }
132
133 }
Popular Tags