KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > config > spring > XBeanProcessor


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

17 package org.apache.servicemix.jbi.config.spring;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.springframework.beans.factory.BeanDefinitionStoreException;
22 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.apache.xbean.spring.context.SpringApplicationContext;
28 import org.apache.xbean.spring.context.SpringXmlPreprocessor;
29
30 import java.io.BufferedReader JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Set JavaDoc;
37
38 public class XBeanProcessor implements SpringXmlPreprocessor {
39
40     public static final String JavaDoc BEAN_NAME_DELIMITERS = ",; ";
41
42     /**
43      * Value of a T/F attribute that represents true.
44      * Anything else represents false. Case seNsItive.
45      */

46     public static final String JavaDoc TRUE_VALUE = "true";
47     public static final String JavaDoc DEFAULT_VALUE = "default";
48     public static final String JavaDoc DESCRIPTION_ELEMENT = "description";
49
50     public static final String JavaDoc AUTOWIRE_BY_NAME_VALUE = "byName";
51     public static final String JavaDoc AUTOWIRE_BY_TYPE_VALUE = "byType";
52     public static final String JavaDoc AUTOWIRE_CONSTRUCTOR_VALUE = "constructor";
53     public static final String JavaDoc AUTOWIRE_AUTODETECT_VALUE = "autodetect";
54
55     public static final String JavaDoc DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all";
56     public static final String JavaDoc DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple";
57     public static final String JavaDoc DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects";
58
59     public static final String JavaDoc DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init";
60     public static final String JavaDoc DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check";
61     public static final String JavaDoc DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire";
62
63     public static final String JavaDoc IMPORT_ELEMENT = "import";
64     public static final String JavaDoc RESOURCE_ATTRIBUTE = "resource";
65
66     public static final String JavaDoc ALIAS_ELEMENT = "alias";
67     public static final String JavaDoc NAME_ATTRIBUTE = "name";
68     public static final String JavaDoc ALIAS_ATTRIBUTE = "alias";
69
70     public static final String JavaDoc BEAN_ELEMENT = "bean";
71     public static final String JavaDoc ID_ATTRIBUTE = "id";
72     public static final String JavaDoc PARENT_ATTRIBUTE = "parent";
73
74     public static final String JavaDoc CLASS_ATTRIBUTE = "class";
75     public static final String JavaDoc ABSTRACT_ATTRIBUTE = "abstract";
76     public static final String JavaDoc SINGLETON_ATTRIBUTE = "singleton";
77     public static final String JavaDoc LAZY_INIT_ATTRIBUTE = "lazy-init";
78     public static final String JavaDoc AUTOWIRE_ATTRIBUTE = "autowire";
79     public static final String JavaDoc DEPENDENCY_CHECK_ATTRIBUTE = "dependency-check";
80     public static final String JavaDoc DEPENDS_ON_ATTRIBUTE = "depends-on";
81     public static final String JavaDoc INIT_METHOD_ATTRIBUTE = "init-method";
82     public static final String JavaDoc DESTROY_METHOD_ATTRIBUTE = "destroy-method";
83     public static final String JavaDoc FACTORY_METHOD_ATTRIBUTE = "factory-method";
84     public static final String JavaDoc FACTORY_BEAN_ATTRIBUTE = "factory-bean";
85
86     public static final String JavaDoc CONSTRUCTOR_ARG_ELEMENT = "constructor-arg";
87     public static final String JavaDoc INDEX_ATTRIBUTE = "index";
88     public static final String JavaDoc TYPE_ATTRIBUTE = "type";
89     public static final String JavaDoc PROPERTY_ELEMENT = "property";
90     public static final String JavaDoc REF_ATTRIBUTE = "ref";
91     public static final String JavaDoc VALUE_ATTRIBUTE = "value";
92     public static final String JavaDoc LOOKUP_METHOD_ELEMENT = "lookup-method";
93
94     public static final String JavaDoc REPLACED_METHOD_ELEMENT = "replaced-method";
95     public static final String JavaDoc REPLACER_ATTRIBUTE = "replacer";
96     public static final String JavaDoc ARG_TYPE_ELEMENT = "arg-type";
97     public static final String JavaDoc ARG_TYPE_MATCH_ATTRIBUTE = "match";
98
99     public static final String JavaDoc REF_ELEMENT = "ref";
100     public static final String JavaDoc IDREF_ELEMENT = "idref";
101     public static final String JavaDoc BEAN_REF_ATTRIBUTE = "bean";
102     public static final String JavaDoc LOCAL_REF_ATTRIBUTE = "local";
103     public static final String JavaDoc PARENT_REF_ATTRIBUTE = "parent";
104
105     public static final String JavaDoc VALUE_ELEMENT = "value";
106     public static final String JavaDoc NULL_ELEMENT = "null";
107     public static final String JavaDoc LIST_ELEMENT = "list";
108     public static final String JavaDoc SET_ELEMENT = "set";
109     public static final String JavaDoc MAP_ELEMENT = "map";
110     public static final String JavaDoc ENTRY_ELEMENT = "entry";
111     public static final String JavaDoc KEY_ELEMENT = "key";
112     public static final String JavaDoc KEY_ATTRIBUTE = "key";
113     public static final String JavaDoc KEY_REF_ATTRIBUTE = "key-ref";
114     public static final String JavaDoc VALUE_REF_ATTRIBUTE = "value-ref";
115     public static final String JavaDoc PROPS_ELEMENT = "props";
116     public static final String JavaDoc PROP_ELEMENT = "prop";
117
118     /**
119      * All the reserved Spring XML element names which cannot be overloaded by an XML extension
120      */

121     protected static final String JavaDoc[] RESERVED_ELEMENT_NAMES = { "beans", DESCRIPTION_ELEMENT, IMPORT_ELEMENT, ALIAS_ELEMENT,
122                                                                BEAN_ELEMENT,
123                                                                CONSTRUCTOR_ARG_ELEMENT, PROPERTY_ELEMENT,
124                                                                LOOKUP_METHOD_ELEMENT, REPLACED_METHOD_ELEMENT,
125                                                                ARG_TYPE_ELEMENT, REF_ELEMENT, IDREF_ELEMENT,
126                                                                VALUE_ELEMENT, NULL_ELEMENT, LIST_ELEMENT,
127                                                                SET_ELEMENT, MAP_ELEMENT, ENTRY_ELEMENT, KEY_ELEMENT,
128                                                                PROPS_ELEMENT, PROP_ELEMENT };
129
130     protected final Log logger = LogFactory.getLog(getClass());
131     
132     private Set JavaDoc reservedElementNames = new HashSet JavaDoc(Arrays.asList(RESERVED_ELEMENT_NAMES));
133     
134     public void preprocess(SpringApplicationContext applicationContext, XmlBeanDefinitionReader reader, Document JavaDoc document) {
135         preprocessXml(reader, document.getDocumentElement());
136     }
137
138     public void preprocessXml(XmlBeanDefinitionReader reader, Element JavaDoc root) {
139         String JavaDoc localName = root.getNodeName();
140         String JavaDoc uri = root.getNamespaceURI();
141         boolean extensible = true;
142         if (uri == null || uri.length() == 0) {
143             if (reservedElementNames.contains(localName)) {
144                 extensible = false;
145             }
146         }
147         if (extensible) {
148             // lets see if we have a custom XML processor
149
ElementProcessor handler = findElementProcessor(uri, localName);
150             if (handler != null) {
151                 handler.processElement(root, reader);
152             }
153         }
154      
155         // lets recurse into any children
156
NodeList JavaDoc nl = root.getChildNodes();
157         for (int i = 0; i < nl.getLength(); i++) {
158             Node JavaDoc node = nl.item(i);
159             if (node instanceof Element JavaDoc) {
160                 Element JavaDoc element = (Element JavaDoc) node;
161                 preprocessXml(reader, element);
162             }
163         }
164     }
165
166     /**
167      * Uses META-INF/services discovery to find an {@link ElementProcessor} for the given
168      * namespace and localNam
169      *
170      * @param namespaceURI the namespace URI of the element
171      * @param localName the local name of the element
172      * @return the custom processor for the given element name if it could be found, otherwise return null
173      */

174     protected ElementProcessor findElementProcessor(String JavaDoc namespaceURI, String JavaDoc localName) throws BeanDefinitionStoreException {
175         String JavaDoc uri = "META-INF/services/org/springframework/config/" + createDiscoveryPathName(namespaceURI, localName);
176      
177         // lets try the thread context class loader first
178
InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri);
179         if (in == null) {
180             in = getClass().getClassLoader().getResourceAsStream(uri);
181             if (in == null) {
182                 logger.warn("Could not find resource: " + uri);
183                 return null;
184             }
185         }
186      
187         // lets load the file
188
BufferedReader JavaDoc reader = null;
189         try {
190             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
191             String JavaDoc line = reader.readLine();
192             if (line == null) {
193                 throw new BeanDefinitionStoreException("Empty file found for: " + uri);
194             }
195             line = line.trim();
196             Class JavaDoc answer = null;
197             try {
198                 answer = loadClass(line);
199             }
200             catch (ClassNotFoundException JavaDoc e) {
201                 throw new BeanDefinitionStoreException("Could not find class: " + line, e);
202             }
203             try {
204                 return (ElementProcessor) answer.newInstance();
205             }
206             catch (Exception JavaDoc e) {
207                 throw new BeanDefinitionStoreException("Failed to instantiate bean of type: " + answer.getName() + ". Reason: " + e, e);
208             }
209         }
210         catch (IOException JavaDoc e) {
211             throw new BeanDefinitionStoreException("Failed to load file for URI: " + uri + ". Reason: " + e, e);
212         }
213         finally {
214             try {
215                 reader.close();
216             }
217             catch (Exception JavaDoc e) {
218                 // ignore
219
}
220         }
221     }
222
223     /**
224      * Converts the namespace and localName into a valid path name we can use on the classpath to discover a text file
225      */

226     protected String JavaDoc createDiscoveryPathName(String JavaDoc uri, String JavaDoc localName) {
227         if (uri == null || uri.length() == 0) {
228             return localName;
229         }
230         // TODO proper encoding required
231
// lets replace any dodgy characters
232
return uri.replaceAll("://", "/").replace(':', '/').replace(' ', '_') + "/" + localName;
233     }
234
235     /**
236      * Attempts to load the class on the current thread context class loader or the class loader which loaded us
237      */

238     protected Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
239         try {
240             return Thread.currentThread().getContextClassLoader().loadClass(name);
241         }
242         catch (ClassNotFoundException JavaDoc e) {
243             return getClass().getClassLoader().loadClass(name);
244         }
245     }
246
247 }
248
Popular Tags