01: /*
02: * Copyright 2007 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.outerj.daisy.runtime.component;
17:
18: import org.springframework.beans.factory.xml.NamespaceHandler;
19: import org.springframework.beans.factory.xml.ParserContext;
20: import org.springframework.beans.factory.config.BeanDefinition;
21: import org.springframework.beans.factory.config.BeanDefinitionHolder;
22: import org.springframework.beans.factory.config.ConstructorArgumentValues;
23: import org.springframework.beans.factory.support.RootBeanDefinition;
24: import org.w3c.dom.Element;
25: import org.w3c.dom.Node;
26: import org.outerj.daisy.runtime.DaisyRTException;
27:
28: public class DaisyRuntimeNamespaceHandler implements NamespaceHandler {
29:
30: public void init() {
31: }
32:
33: public BeanDefinition parse(Element element,
34: ParserContext parserContext) {
35: if (element.getLocalName().equals("import-service")) {
36: try {
37: String service = element.getAttribute("service");
38: Class serviceClass = parserContext.getReaderContext()
39: .getBeanClassLoader().loadClass(service);
40: Object component = ContainerConfigImpl.BUILD_CONTEXT_RUNTIME
41: .get().getService(serviceClass);
42:
43: String id = element.getAttribute("id");
44: RootBeanDefinition def = new RootBeanDefinition(
45: ServiceImportFactoryBean.class);
46:
47: ConstructorArgumentValues args = new ConstructorArgumentValues();
48: args.addIndexedArgumentValue(0, serviceClass);
49: args.addIndexedArgumentValue(1, component);
50:
51: def.setConstructorArgumentValues(args);
52: def.setLazyInit(false);
53: parserContext.getRegistry().registerBeanDefinition(id,
54: def);
55: } catch (Throwable e) {
56: throw new DaisyRTException(
57: "Error handling import-service directive.", e);
58: }
59: } else if (element.getLocalName().equals("export-service")) {
60: try {
61: String service = element.getAttribute("service");
62: Class serviceClass = parserContext.getReaderContext()
63: .getBeanClassLoader().loadClass(service);
64:
65: String beanName = element.getAttribute("ref");
66:
67: ContainerConfigImpl.BUILD_CONTEXT_EXPORTS.get().put(
68: serviceClass, beanName);
69: } catch (Throwable e) {
70: throw new DaisyRTException(
71: "Error handling import-service directive.", e);
72: }
73: }
74: return null;
75: }
76:
77: public BeanDefinitionHolder decorate(Node node,
78: BeanDefinitionHolder beanDefinitionHolder,
79: ParserContext parserContext) {
80: return null;
81: }
82: }
|