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
|
/*
* 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.persistence.datasource;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URI;
import javax.sql.DataSource;
import org.osoa.sca.annotations.Reference;
import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension;
import org.apache.tuscany.spi.deployer.DeploymentContext;
import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension;
import org.apache.tuscany.spi.model.TypeInfo;
import org.apache.tuscany.spi.idl.java.JavaServiceContract;
import org.apache.tuscany.spi.implementation.java.JavaMappedProperty;
import org.apache.tuscany.spi.loader.LoaderException;
import org.apache.tuscany.spi.loader.LoaderRegistry;
import org.apache.tuscany.spi.model.ComponentType;
import org.apache.tuscany.spi.model.Property;
import org.apache.tuscany.spi.model.ReferenceDefinition;
import org.apache.tuscany.spi.model.Scope;
import org.apache.tuscany.spi.model.ServiceDefinition;
/**
* Loads the component type for a DataSource component. Component type information is currently static, although this
* could be changed in the future to allow for configuration parameter checking for specific providers.
*
* @version $Rev$ $Date$
*/
public class DSComponentTypeLoader extends ComponentTypeLoaderExtension<DataSourceImplementation> {
private SimpleTypeMapperExtension extension = new SimpleTypeMapperExtension();
public DSComponentTypeLoader(@Reference LoaderRegistry loaderRegistry) {
super(loaderRegistry);
}
protected Class<DataSourceImplementation> getImplementationClass() {
return DataSourceImplementation.class;
}
public void load(DataSourceImplementation implementation, DeploymentContext ctx)
throws LoaderException {
ComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> componentType =
new ComponentType<ServiceDefinition, ReferenceDefinition, Property<?>>();
componentType.setImplementationScope(Scope.COMPOSITE);
JavaServiceContract serviceContract = new JavaServiceContract(DataSource.class);
ServiceDefinition service = new ServiceDefinition(URI.create("#DataSource"), serviceContract, false);
componentType.add(service);
componentType.setInitLevel(1);
Class<?> provider;
try {
provider = implementation.getClassLoader().loadClass(implementation.getProviderName());
} catch (ClassNotFoundException e) {
throw new LoaderException(e);
}
introspectProperties(componentType, provider);
implementation.setComponentType(componentType);
}
/**
* Creates properties by introspecting the provider class an d including all JavaBean setters that take a simple
* type parameter
*
* @param componentType
* @param provider
* @throws AmbiguousPropertyException
*/
@SuppressWarnings("unchecked")
private void introspectProperties(ComponentType<ServiceDefinition, ReferenceDefinition, Property<?>> componentType,
Class<?> provider) throws AmbiguousPropertyException {
Method[] methods = provider.getMethods();
for (Method method : methods) {
String name = method.getName();
if (method.getParameterTypes().length == 1 && name.startsWith("set")) {
String propName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
Class<Type> type = (Class<Type>) method.getParameterTypes()[0];
TypeInfo info = extension.getXMLType(type);
if (info != null) {
// only include methods as properties that take simple type parameters
if (componentType.getProperties().containsKey(propName)) {
throw new AmbiguousPropertyException(propName);
}
JavaMappedProperty<Type> property =
new JavaMappedProperty<Type>(propName, info.getQName(), type);
property.setMember(method);
componentType.add(property);
}
}
}
}
}
|