From 1a51f9f3d1c3c4cd4cc0017288faf5e420b092c8 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 18 Aug 2010 18:26:30 +0000 Subject: Add impl spring stub/tie git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@986837 13f79535-47bb-0310-9956-ffa450edef68 --- .../spring/metadata/SpringBeanElement.java | 129 +++++++++++++++++++++ .../metadata/SpringConstructorArgElement.java | 77 ++++++++++++ .../spring/metadata/SpringElementTie.java | 70 +++++++++++ .../spring/metadata/SpringPropertyElement.java | 68 +++++++++++ .../spring/metadata/SpringSCAPropertyElement.java | 59 ++++++++++ .../spring/metadata/SpringSCAReferenceElement.java | 73 ++++++++++++ .../spring/metadata/SpringSCAServiceElement.java | 73 ++++++++++++ .../spring/processor/SpringXMLLoaderTie.java | 57 +++++++++ 8 files changed, 606 insertions(+) create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java create mode 100644 sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java (limited to 'sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache') diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java new file mode 100644 index 0000000000..d954195980 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringBeanElement.java @@ -0,0 +1,129 @@ +/* + * 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.sca.implementation.spring.metadata; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a element in a Spring application-context + * - this has id and className attributes + * - plus zero or more property elements as children + * + * @version $Rev$ $Date$ + */ +public class SpringBeanElement { + + private String id; + private String className = null; + private boolean innerBean = false; + private boolean abstractBean = false; + private boolean parentAttribute = false; + private boolean factoryBeanAttribute = false; + private boolean factoryMethodAttribute = false; + + private List properties = new ArrayList(); + private List constructorargs = new ArrayList(); + + public SpringBeanElement(String id, String className) { + this.id = id; + this.className = className; + } + + public String getClassName() { + return className; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public List getProperties() { + return properties; + } + + public void addProperty(SpringPropertyElement property) { + properties.add(property); + } + + public List getCustructorArgs() { + return constructorargs; + } + + public void addCustructorArgs(SpringConstructorArgElement args) { + constructorargs.add(args); + } + + public boolean isInnerBean() { + return innerBean; + } + + public void setInnerBean(boolean innerBean) { + this.innerBean = innerBean; + } + + public boolean isAbstractBean() { + return abstractBean; + } + + public void setAbstractBean(boolean abstractBean) { + this.abstractBean = abstractBean; + } + + public boolean hasParentAttribute() { + return parentAttribute; + } + + public void setParentAttribute(boolean parentAttribute) { + this.parentAttribute = parentAttribute; + } + + public boolean hasFactoryBeanAttribute() { + return factoryBeanAttribute; + } + + public void setFactoryBeanAttribute(boolean factoryBeanAttribute) { + this.factoryBeanAttribute = factoryBeanAttribute; + } + + public boolean hasFactoryMethodAttribute() { + return factoryMethodAttribute; + } + + public void setFactoryMethodAttribute(boolean factoryMethodAttribute) { + this.factoryMethodAttribute = factoryMethodAttribute; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SpringBeanElement [id=").append(id).append(", className=").append(className) + .append(", innerBean=").append(innerBean).append(", abstractBean=").append(abstractBean) + .append(", parentAttribute=").append(parentAttribute).append(", factoryBeanAttribute=") + .append(factoryBeanAttribute).append(", factoryMethodAttribute=").append(factoryMethodAttribute) + .append(", properties=").append(properties).append(", constructorargs=").append(constructorargs) + .append("]"); + return builder.toString(); + } + +} // end class SpringBeanElement diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java new file mode 100644 index 0000000000..5571029c61 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringConstructorArgElement.java @@ -0,0 +1,77 @@ +/* + * 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.sca.implementation.spring.metadata; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a element in a Spring application-context + * - this has ref attribute + * + * @version $Rev$ $Date$ + */ +public class SpringConstructorArgElement { + + private String type; + private int autoIndex = -1; + private int index = -1; + private List refs = new ArrayList(); + private List values = new ArrayList(); + + public SpringConstructorArgElement(String type) { + this.type = type; + } + + public String getType() { + return this.type; + } + + public List getRefs() { + return this.refs; + } + + public void addRef(String ref) { + this.refs.add(ref); + } + + public int getIndex() { + return this.index; + } + + public void setIndex(int index) { + this.index = index; + } + + public int getAutoIndex() { + return this.autoIndex; + } + + public void setAutoIndex(int index) { + this.autoIndex = index; + } + + public List getValues() { + return this.values; + } + + public void addValue(String value) { + this.values.add(value); + } +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java new file mode 100644 index 0000000000..df4b077819 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringElementTie.java @@ -0,0 +1,70 @@ +/* + * 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.sca.implementation.spring.metadata; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * A hacking utility to copy beans field by field between two class loaders + */ +public class SpringElementTie { + public static T copy(Object source, Class cls, Type genericType) { + if (source == null) { + return null; + } + if (cls.isPrimitive()) { + return (T)source; + } + if (Collection.class.isAssignableFrom(cls)) { + ParameterizedType pType = (ParameterizedType)genericType; + Type itemType = pType.getActualTypeArguments()[0]; + Collection col = (Collection)source; + List target = new ArrayList(); + for (Object item : col) { + target.add(copy(item, (Class)itemType, itemType)); + } + return (T)target; + } + if (cls.isInstance(source)) { + return cls.cast(source); + } + try { + Class sourceClass = source.getClass(); + T target = cls.newInstance(); + for (Field sourceField : sourceClass.getDeclaredFields()) { + sourceField.setAccessible(true); + Field targetField = cls.getDeclaredField(sourceField.getName()); + targetField.setAccessible(true); + Object sourceFieldValue = sourceField.get(source); + Object targetFieldValue = copy(sourceFieldValue, targetField.getType(), targetField.getGenericType()); + targetField.set(target, targetFieldValue); + } + return target; + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java new file mode 100644 index 0000000000..585b3d3381 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringPropertyElement.java @@ -0,0 +1,68 @@ +/* + * 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.sca.implementation.spring.metadata; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a element in a Spring application-context + * - this has name and ref attributes + * + * @version $Rev$ $Date$ + */ +public class SpringPropertyElement { + + private String name; + private List refs = new ArrayList(); + private List values = new ArrayList(); + + public SpringPropertyElement(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public List getRefs() { + return this.refs; + } + + public void addRef(String ref) { + this.refs.add(ref); + } + + public List getValues() { + return this.values; + } + + public void addValue(String value) { + this.values.add(value); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SpringPropertyElement [name=").append(name).append(", refs=").append(refs).append(", values=") + .append(values).append("]"); + return builder.toString(); + } + +} // end class SpringPropertyElement diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java new file mode 100644 index 0000000000..f721b21042 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAPropertyElement.java @@ -0,0 +1,59 @@ +/* + * 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.sca.implementation.spring.metadata; + +/** + * Represents an element in a Spring application-context + * - this has name and type attributes + * @version $Rev$ $Date$ + */ +public class SpringSCAPropertyElement { + + private String name; + private String type; + + public SpringSCAPropertyElement(String name, String type) { + this.name = name; + this.type = type; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SpringSCAPropertyElement [name=").append(name).append(", type=").append(type).append("]"); + return builder.toString(); + } + +} // end class SpringPropertyElement diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java new file mode 100644 index 0000000000..ffa91ad179 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAReferenceElement.java @@ -0,0 +1,73 @@ +/* + * 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.sca.implementation.spring.metadata; + + +/** + * Represents a element in a Spring application-context + * - this has id and className attributes + * - plus zero or more property elements as children + * + * @version $Rev$ $Date$ + */ +public class SpringSCAReferenceElement { + + private String name; + private String type; + private String defaultBean; + + public SpringSCAReferenceElement(String name, String type) { + this.name = name; + this.type = type; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public void setDefaultBean(String defaultBean) { + this.defaultBean = defaultBean; + } + + public String getDefaultBean() { + return defaultBean; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SpringSCAReferenceElement [name=").append(name).append(", type=").append(type) + .append(", defaultBean=").append(defaultBean).append("]"); + return builder.toString(); + } + + +} // end class SpringSCAReferenceElement diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java new file mode 100644 index 0000000000..6dee35f46e --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/metadata/SpringSCAServiceElement.java @@ -0,0 +1,73 @@ +/* + * 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.sca.implementation.spring.metadata; + + +/** + * Represents a element in a Spring application-context + * - this has id and className attributes + * - plus zero or more property elements as children + * + * @version $Rev$ $Date$ + */ +public class SpringSCAServiceElement { + + private String name; + private String type; + private String target; + + + public SpringSCAServiceElement(String name, String target) { + this.name = name; + this.target = target; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public void setTarget(String target) { + this.target = target; + } + + public String getTarget() { + return target; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SpringSCAServiceElement [name=").append(name).append(", type=").append(type) + .append(", target=").append(target).append("]"); + return builder.toString(); + } + +} // end class SpringSCAServiceElement diff --git a/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java new file mode 100644 index 0000000000..b3f0a4a883 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-spring-runtime/src/main/java/org/apache/tuscany/sca/implementation/spring/processor/SpringXMLLoaderTie.java @@ -0,0 +1,57 @@ +/* + * 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.sca.implementation.spring.processor; + +import java.net.URL; +import java.util.List; + +import org.apache.tuscany.sca.implementation.spring.runtime.context.SCAGenericApplicationContext; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.context.ApplicationContext; +import org.springframework.core.io.UrlResource; + +/** + * A tie that allows Tuscany to call Spring library to load the application context for the purpose of introspection + */ +public class SpringXMLLoaderTie { + + public static ApplicationContext createApplicationContext(Object scaParentContext, + ClassLoader classLoader, + List resources) { + if (classLoader == null) { + classLoader = Thread.currentThread().getContextClassLoader(); + } + + SCAGenericApplicationContext appCtx = + new SCAGenericApplicationContext((ApplicationContext)scaParentContext, classLoader); + XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx); + + // REVIEW: [rfeng] How do we control the schema validation + xmlReader.setValidating(false); + + for (URL resource : resources) { + xmlReader.loadBeanDefinitions(new UrlResource(resource)); + } + + return appCtx; + + } + +} -- cgit v1.2.3