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
279
280
281
282
283
|
/*
* 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.tuscany.implementation.spring;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Property;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentProperty;
import org.apache.tuscany.sca.core.invocation.ProxyFactory;
import org.apache.tuscany.sca.factory.ObjectFactory;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
import org.apache.tuscany.sca.runtime.RuntimeWire;
import org.apache.tuscany.sca.implementation.java.context.JavaPropertyValueObjectFactory;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.io.Resource;
/**
* A Spring ParentApplicationContext for a given Spring Implementation
*
* The Parent application context is responsible for handling those entities within a Spring
* application context that actually belong to SCA rather than to Spring. The principal things
* are Properties and References. These may be present either through explicit <sca:property/>
* and <sca:reference/> elements in the application context or they may be implicit through
* unresolved Spring bean <property.../> elements. In either case, it is the Parent application
* context that must provide Spring beans that correspond to the property or reference, as derived
* from the SCA composite in which the Spring application context is an implementation.
* @author MikeEdwards
*
*/
class SCAParentApplicationContext implements ApplicationContext {
// The Spring implementation for which this is the parent application context
private SpringImplementation implementation;
private RuntimeComponent component;
private ProxyFactory proxyService;
private JavaPropertyValueObjectFactory propertyFactory;
private static final String[] EMPTY_ARRAY = new String[0];
public SCAParentApplicationContext( RuntimeComponent component,
SpringImplementation implementation,
ProxyFactory proxyService,
JavaPropertyValueObjectFactory propertyValueObjectFactory) {
this.implementation = implementation;
this.component = component;
this.proxyService = proxyService;
this.propertyFactory = propertyValueObjectFactory;
} // end constructor
public Object getBean(String name) throws BeansException {
return getBean(name, null);
}
/**
* Get a Bean for a reference or for a property..
* @param name - the name of the Bean required
* @param requiredtype - the required type of the Bean (either a Java class or a Java interface)
* @return Object - a Bean which matches the requested bean
*/
public Object getBean(String name, Class requiredType) throws BeansException {
System.out.println("Spring parent context - getBean called for name: " + name );
// The expectation is that the requested Bean is either a reference or a property
// from the Spring context
for ( Reference reference : implementation.getReferences() ) {
if( reference.getName().equals(name) ) {
// Extract the Java interface for the reference (it can't be any other interface type
// for a Spring application context)
if( requiredType == null ) {
JavaInterface javaInterface =
(JavaInterface) reference.getInterfaceContract().getInterface();
requiredType = javaInterface.getJavaClass();
}
// Create and return eturn the proxy for the reference
return getService( requiredType, reference.getName() );
} // end if
} // end for
// For a property, get the name and the required Java type and create a Bean
// of that type with the value inserted.
for ( Property property : implementation.getProperties() ) {
if( property.getName().equals(name) ) {
if( requiredType == null ) {
// The following code only deals with a subset of types and was superceded
// by the information from the implementation (which uses Classes as found
// in the Spring implementation itself.
//requiredType = JavaXMLMapper.getJavaType( property.getXSDType() );
requiredType = implementation.getPropertyClass( name );
}
return getPropertyBean( requiredType, property.getName() );
} // end if
} // end for
throw new NoSuchBeanDefinitionException("Unable to find Bean with name " + name );
} // end method getBean( String, Class )
/**
* Creates a proxy Bean for a reference
* @param <B> the Business interface type for the reference
* @param businessInterface - the business interface as a Class
* @param referenceName - the name of the Reference
* @return an Bean of the type defined by <B>
*/
private <B> B getService(Class<B> businessInterface, String referenceName) {
List<ComponentReference> refs = component.getReferences();
for (ComponentReference ref : refs) {
if (ref.getName().equals(referenceName)) {
RuntimeComponentReference attachPoint = (RuntimeComponentReference)ref;
RuntimeWire wire = attachPoint.getRuntimeWires().get(0);
return proxyService.createProxy(businessInterface, wire);
}
}
return null;
}
/**
* Method to create a Java Bean for a Property value
* @param <B> the class type of the Bean
* @param requiredType - a Class object for the required type
* @param name - the Property name
* @return - a Bean of the specified property, with value set
*/
private <B> B getPropertyBean( Class requiredType, String name) {
B propertyObject = null;
// Get the component's list of properties
List<ComponentProperty> props = component.getProperties();
for ( ComponentProperty prop : props ) {
if ( prop.getName().equals(name) ) {
// On finding the property, create a factory for it and create a Bean using
// the factory
ObjectFactory factory = propertyFactory.createValueFactory( prop,
prop.getValue(),
requiredType );
propertyObject = (B)factory.getInstance();
} // end if
} // end for
return propertyObject;
}
public boolean containsBean(String name) {
// TODO
System.out.println("Spring parent context - containsBean called for name: " + name );
return false;
}
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
// TODO
return false;
}
public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
throw new UnsupportedOperationException();
}
public Class getType(String name) throws NoSuchBeanDefinitionException {
return null;
}
public String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return EMPTY_ARRAY;
}
public ApplicationContext getParent() {
return null;
}
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
return null;
}
public String getDisplayName() {
return implementation.getURI();
}
public long getStartupDate() {
return 0;
}
public boolean containsBeanDefinition(String beanName) {
return false;
}
public int getBeanDefinitionCount() {
return 0;
}
public String[] getBeanDefinitionNames() {
return new String[0];
}
public String[] getBeanNamesForType(Class type) {
return new String[0];
}
public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean includeFactoryBeans) {
return new String[0];
}
public Map getBeansOfType(Class type) throws BeansException {
return null;
}
public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans)
throws BeansException {
return null;
}
public boolean isPrototype( String theString ) {
return false;
}
public BeanFactory getParentBeanFactory() {
return null;
}
public boolean containsLocalBean(String name) {
return false;
}
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
return null;
}
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
return null;
}
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
return null;
}
public void publishEvent(ApplicationEvent event) {
}
public Resource[] getResources(String locationPattern) throws IOException {
return new Resource[0];
}
public Resource getResource(String location) {
return null;
}
public ClassLoader getClassLoader() {
// REVIEW: this is almost certainly flawed, but it's not clear how the SCA runtime's
// resource loading mechanism is exposed right now.
return this.getClass().getClassLoader();
}
}
|