From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- sandbox/jboynes/sdoproxy/pom.xml | 64 ++++ .../apache/tuscany/sdo/helper/DOMHelperImpl.java | 105 ++++++ .../tuscany/sdo/impl/AbstractDataObject.java | 59 ++++ .../tuscany/sdo/impl/AbstractPropertyImpl.java | 48 +++ .../apache/tuscany/sdo/impl/DataFactoryImpl.java | 49 +++ .../org/apache/tuscany/sdo/impl/DataTypeImpl.java | 148 ++++++++ .../apache/tuscany/sdo/impl/InstanceFactory.java | 27 ++ .../apache/tuscany/sdo/impl/ListPropertyImpl.java | 40 +++ .../tuscany/sdo/impl/POJOInstanceFactory.java | 41 +++ .../org/apache/tuscany/sdo/impl/PropertyImpl.java | 39 +++ .../apache/tuscany/sdo/impl/ProxyClassLoader.java | 33 ++ .../org/apache/tuscany/sdo/impl/SDOGenerator.java | 371 +++++++++++++++++++++ .../tuscany/sdo/impl/SDOInstanceFactory.java | 59 ++++ .../apache/tuscany/sdo/impl/TypeHelperImpl.java | 236 +++++++++++++ .../java/org/apache/tuscany/sdo/impl/TypeImpl.java | 73 ++++ .../src/main/java/org/osoa/sdo/DataObject.java | 31 ++ .../java/org/osoa/sdo/DuplicateTypeException.java | 27 ++ .../src/main/java/org/osoa/sdo/Property.java | 29 ++ .../sdoproxy/src/main/java/org/osoa/sdo/Type.java | 62 ++++ .../main/java/org/osoa/sdo/annotation/Type.java | 34 ++ .../main/java/org/osoa/sdo/helper/DataFactory.java | 34 ++ .../main/java/org/osoa/sdo/helper/TypeHelper.java | 52 +++ .../tuscany/sdo/proxy/DOMHelperTestCase.java | 94 ++++++ .../tuscany/sdo/proxy/DataTypesTestCase.java | 130 ++++++++ .../tuscany/sdo/proxy/GetSetOnlyTestCase.java | 49 +++ .../org/apache/tuscany/sdo/proxy/ListTestCase.java | 45 +++ .../tuscany/sdo/proxy/PrimitivesTestCase.java | 202 +++++++++++ .../tuscany/sdo/proxy/SimpleNestingTestCase.java | 64 ++++ .../tuscany/sdo/proxy/interfaces/DataTypes.java | 37 ++ .../tuscany/sdo/proxy/interfaces/GetSetOnly.java | 27 ++ .../tuscany/sdo/proxy/interfaces/ListTypes.java | 27 ++ .../tuscany/sdo/proxy/interfaces/Primitives.java | 55 +++ .../sdo/proxy/interfaces/SimpleNesting.java | 29 ++ 33 files changed, 2420 insertions(+) create mode 100644 sandbox/jboynes/sdoproxy/pom.xml create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/helper/DOMHelperImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractDataObject.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractPropertyImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataFactoryImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/InstanceFactory.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ListPropertyImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/POJOInstanceFactory.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/PropertyImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ProxyClassLoader.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOGenerator.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOInstanceFactory.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeHelperImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeImpl.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DataObject.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DuplicateTypeException.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Property.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Type.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/annotation/Type.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/DataFactory.java create mode 100644 sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/TypeHelper.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DOMHelperTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DataTypesTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/GetSetOnlyTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/ListTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/PrimitivesTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/SimpleNestingTestCase.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/DataTypes.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/GetSetOnly.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/ListTypes.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/Primitives.java create mode 100644 sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/SimpleNesting.java (limited to 'sandbox/jboynes/sdoproxy') diff --git a/sandbox/jboynes/sdoproxy/pom.xml b/sandbox/jboynes/sdoproxy/pom.xml new file mode 100644 index 0000000000..619fb2e685 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/pom.xml @@ -0,0 +1,64 @@ + + + + + org.apache.tuscany + tuscany-sdo + SNAPSHOT + + 4.0.0 + tuscany-sdo-proxy + Tuscany SDO Proxies + Tuscany implementation of proxies for SDO + SNAPSHOT + + + + commonj + sdo-api + SNAPSHOT + compile + + + + asm + asm + 2.2 + compile + + + asm + asm-util + 2.2 + test + + + asm + asm-attrs + 2.2 + test + + + + + junit + junit + 3.8.1 + test + + + diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/helper/DOMHelperImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/helper/DOMHelperImpl.java new file mode 100644 index 0000000000..7480151202 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/helper/DOMHelperImpl.java @@ -0,0 +1,105 @@ +/** + * + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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.sdo.helper; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; + +import javax.xml.namespace.QName; + +import org.osoa.sdo.DataObject; +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; +import org.osoa.sdo.helper.DataFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * @version $Rev$ $Date$ + */ +public class DOMHelperImpl { + private final DataFactory dataFactory; + + public DOMHelperImpl(DataFactory dataFactory) { + this.dataFactory = dataFactory; + } + + public DataObject load(Document doc) { + Element rootElement = doc.getDocumentElement(); + QName name = new QName(rootElement.getNamespaceURI(), rootElement.getLocalName()); + DataObject rootObject = dataFactory.create(name); + for (Node node = rootElement.getFirstChild() ; node != null; node = node.getNextSibling()) { + if (node.getNodeType() != Node.ELEMENT_NODE) { + continue; + } + String propName = node.getLocalName(); + String value = node.getTextContent(); + Property prop = rootObject.getType().getProperty(propName); + rootObject.setString(prop.getIndex(), value); + } + return rootObject; + } + + public void save(DataObject dataObject, Document doc) { + Type t = dataObject.getType(); + Element element = doc.createElementNS(t.getName().getNamespaceURI(), t.getName().getLocalPart()); + int i = 0; + for (Property property : dataObject.getType().getProperties()) { + String value = dataObject.getString(i++); + if (value != null) { + Element propElement = doc.createElementNS(null, property.getName()); + if (property.getType().isDataType()) { + propElement.appendChild(doc.createTextNode(value)); + } + element.appendChild(propElement); + } + } + doc.appendChild(element); + } + + public void writeTo(OutputStream os, Document doc) throws IOException { + PrintWriter writer = new PrintWriter(os); + writeTo(writer, doc.getDocumentElement()); + writer.flush(); + } + + private static void writeTo(PrintWriter writer, Element e) throws IOException { + writer.append('<'); + writer.append(e.getLocalName()); + if (e.hasChildNodes()) { + writer.append('>'); + for (Node node = e.getFirstChild(); node != null; node = node.getNextSibling()) { + switch (node.getNodeType()) { + case Node.TEXT_NODE: + writer.append(node.getTextContent()); + break; + case Node.ELEMENT_NODE: + writeTo(writer, (Element) node); + break; + } + } + writer.append('<'); + writer.append('/'); + writer.append(e.getLocalName()); + writer.append('>'); + } else { + writer.append("/>"); + } + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractDataObject.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractDataObject.java new file mode 100644 index 0000000000..ca9782e269 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractDataObject.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.DataObject; +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public abstract class AbstractDataObject implements DataObject { + private final TypeImpl type; + + protected AbstractDataObject(TypeImpl type) { + this.type = type; + } + + public Type getType() { + return type; + } + + public String getString(int index) { + Object value = get(index); + if (value == null) { + return null; + } + Type propType = type.getProperties().get(index).getType(); + if (!propType.isDataType()) { + throw new UnsupportedOperationException(); + } + return ((DataTypeImpl) propType).toString(value); + } + + public void setString(int index, String s) { + if (s == null) { + set(index, null); + return; + } + Type propType = type.getProperties().get(index).getType(); + if (!propType.isDataType()) { + throw new UnsupportedOperationException(); + } + set(index, ((DataTypeImpl) propType).fromString(s)); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractPropertyImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractPropertyImpl.java new file mode 100644 index 0000000000..7ee220f3b4 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/AbstractPropertyImpl.java @@ -0,0 +1,48 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public abstract class AbstractPropertyImpl implements Property { + private final String name; + private final Type type; + private final int index; + + public AbstractPropertyImpl(String name, Type type, int index) { + this.name = name; + this.type = type; + this.index = index; + } + + public String getName() { + return name; + } + + public Type getType() { + return type; + } + + public int getIndex() { + return index; + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataFactoryImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataFactoryImpl.java new file mode 100644 index 0000000000..76d25d8c2f --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataFactoryImpl.java @@ -0,0 +1,49 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import javax.xml.namespace.QName; + +import org.osoa.sdo.DataObject; +import org.osoa.sdo.Type; +import org.osoa.sdo.helper.DataFactory; + +/** + * @version $Rev$ $Date$ + */ +public class DataFactoryImpl implements DataFactory { + private final TypeHelperImpl typeHelper; + + public DataFactoryImpl(TypeHelperImpl typeHelper) { + this.typeHelper = typeHelper; + } + + public T create(Class interfaceClass) { + Type type = typeHelper.getType(interfaceClass); + return type.newInstance(); + } + + public T create(Type type) { + return type.newInstance(); + } + + public DataObject create(QName name) { + Type type = typeHelper.getType(name); + return (DataObject) type.newInstance(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java new file mode 100644 index 0000000000..f62c90292d --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/DataTypeImpl.java @@ -0,0 +1,148 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.TimeZone; +import javax.xml.namespace.QName; + +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +abstract class DataTypeImpl extends TypeImpl { + public static final Type BOOLEAN = new BooleanDataType(Type.BOOLEAN, Boolean.TYPE); + public static final Type BOOLEAN_OBJECT = new BooleanDataType(Type.BOOLEAN_OBJECT, Boolean.class); + public static final Type CHARACTER = new CharacterDataType(Type.CHARACTER, Character.TYPE); + public static final Type CHARACTER_OBJECT = new CharacterDataType(Type.CHARACTER_OBJECT, Character.class); + + private static class BooleanDataType extends DataTypeImpl { + public BooleanDataType(QName name, Class instanceClass) { + super(name, instanceClass); + } + + public Boolean fromString(String s) { + return Boolean.valueOf(s); + } + } + + private static class CharacterDataType extends DataTypeImpl { + public CharacterDataType(QName name, Class instanceClass) { + super(name, instanceClass); + } + + public Character fromString(String s) { + return s.charAt(0); + } + } + + public static final Type BYTE = new DataTypeImpl(Type.BYTE, byte.class) { + public Byte fromString(String s) { + return Byte.valueOf(s); + } + }; + public static final Type BYTES = new DataTypeImpl(Type.BYTES, byte[].class) { + public byte[] fromString(String s) { + byte[] value = new byte[s.length() >> 1]; + for (int i = 0; i < value.length; i++) { + value[i] = (byte) (Character.digit(s.charAt(i << 1), 16) << 4 | Character.digit(s.charAt((i << 1) + 1), 16)); + } + return value; + } + + public String toString(byte[] value) { + StringBuilder buf = new StringBuilder(value.length << 1); + for (byte b : value) { + buf.append(HEX[b >>> 4]); + buf.append(HEX[b & 0xf]); + } + return buf.toString(); + + } + }; + + public static final Type DATE = new DataTypeImpl(Type.DATE, Date.class) { + public Date fromString(String s) { + try { + DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSSz"); + f.setTimeZone(TimeZone.getTimeZone("GMT")); + return f.parse(s); + } catch (ParseException e) { + throw (IllegalArgumentException) new IllegalArgumentException().initCause(e); + } + } + + public String toString(Date value) { + DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSSz"); + f.setTimeZone(TimeZone.getTimeZone("GMT")); + return f.format(value); + + } + }; + public static final Type DOUBLE = new DataTypeImpl(Type.DOUBLE, double.class) { + public Double fromString(String s) { + return Double.valueOf(s); + } + }; + public static final Type FLOAT = new DataTypeImpl(Type.FLOAT, float.class) { + public Float fromString(String s) { + return Float.valueOf(s); + } + }; + public static final Type INT = new DataTypeImpl(Type.INT, int.class) { + public Integer fromString(String s) { + return Integer.valueOf(s); + } + }; + public static final Type LONG = new DataTypeImpl(Type.LONG, long.class) { + public Long fromString(String s) { + return Long.valueOf(s); + } + }; + public static final Type SHORT = new DataTypeImpl(Type.SHORT, short.class) { + public Short fromString(String s) { + return Short.valueOf(s); + } + }; + public static final Type STRING = new DataTypeImpl(Type.STRING, String.class) { + public String fromString(String s) { + return s; + } + }; + + public DataTypeImpl(QName name, Class instanceClass) { + super(name, instanceClass, Collections.EMPTY_LIST, new POJOInstanceFactory(instanceClass)); + } + + public boolean isDataType() { + return true; + } + + public abstract T fromString(String s); + + public String toString(T value) { + return String.valueOf(value); + } + + private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/InstanceFactory.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/InstanceFactory.java new file mode 100644 index 0000000000..3406327a54 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/InstanceFactory.java @@ -0,0 +1,27 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public interface InstanceFactory { + T newInstance(Typetype); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ListPropertyImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ListPropertyImpl.java new file mode 100644 index 0000000000..bae24b23d5 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ListPropertyImpl.java @@ -0,0 +1,40 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public class ListPropertyImpl extends AbstractPropertyImpl { + private final java.lang.reflect.Type javaType; + + public ListPropertyImpl(String name, Type type, java.lang.reflect.Type javaType, int index) { + super(name, type, index); + this.javaType = javaType; + } + + public boolean isMany() { + return true; + } + + public java.lang.reflect.Type getJavaType() { + return javaType; + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/POJOInstanceFactory.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/POJOInstanceFactory.java new file mode 100644 index 0000000000..3dc44e4440 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/POJOInstanceFactory.java @@ -0,0 +1,41 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public class POJOInstanceFactory implements InstanceFactory { + private final Class clazz; + + public POJOInstanceFactory(Class clazz) { + this.clazz = clazz; + } + + public T newInstance(Type type) { + try { + return clazz.newInstance(); + } catch (InstantiationException e) { + throw (AssertionError) new AssertionError("Unable to instantiate " + clazz.getName()).initCause(e); + } catch (IllegalAccessException e) { + throw (AssertionError) new AssertionError("Unable to instantiate " + clazz.getName()).initCause(e); + } + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/PropertyImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/PropertyImpl.java new file mode 100644 index 0000000000..b607e227a4 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/PropertyImpl.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public class PropertyImpl extends AbstractPropertyImpl { + + public PropertyImpl(String name, Type type, int index) { + super(name, type, index); + } + + public boolean isMany() { + return false; + } + + public java.lang.reflect.Type getJavaType() { + return getType().getJavaType(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ProxyClassLoader.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ProxyClassLoader.java new file mode 100644 index 0000000000..fdbffa8fef --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/ProxyClassLoader.java @@ -0,0 +1,33 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.security.SecureClassLoader; + +/** + * @version $Rev$ $Date$ + */ +class ProxyClassLoader extends SecureClassLoader { + ProxyClassLoader(ClassLoader parent) { + super(parent); + } + + Class addProxy(String name, byte[] bytes) { + return defineClass(name, bytes, 0, bytes.length); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOGenerator.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOGenerator.java new file mode 100644 index 0000000000..e477665c06 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOGenerator.java @@ -0,0 +1,371 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.util.ArrayList; +import java.util.List; +import java.lang.reflect.ParameterizedType; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import static org.objectweb.asm.Opcodes.ACC_PRIVATE; +import static org.objectweb.asm.Opcodes.ACC_PUBLIC; +import static org.objectweb.asm.Opcodes.ACC_SUPER; +import static org.objectweb.asm.Opcodes.ARETURN; +import static org.objectweb.asm.Opcodes.ATHROW; +import static org.objectweb.asm.Opcodes.CHECKCAST; +import static org.objectweb.asm.Opcodes.DUP; +import static org.objectweb.asm.Opcodes.GETFIELD; +import static org.objectweb.asm.Opcodes.ILOAD; +import static org.objectweb.asm.Opcodes.INVOKESPECIAL; +import static org.objectweb.asm.Opcodes.INVOKESTATIC; +import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL; +import static org.objectweb.asm.Opcodes.IRETURN; +import static org.objectweb.asm.Opcodes.NEW; +import static org.objectweb.asm.Opcodes.PUTFIELD; +import static org.objectweb.asm.Opcodes.RETURN; +import static org.objectweb.asm.Opcodes.V1_5; +import org.objectweb.asm.Type; +import org.objectweb.asm.signature.SignatureWriter; +import org.objectweb.asm.signature.SignatureVisitor; +import org.osoa.sdo.Property; + +/** + * @version $Rev$ $Date$ + */ +public class SDOGenerator { + private static final String SDO_MARKER = "$$SDOImpl"; + private static final int ALOAD_0 = 42; + private static final int ALOAD_1 = 43; + private static final int ALOAD_2 = 44; +// private static final int ILOAD_1 = 27; + private static final Type[] NO_ARGS = {}; + + private final String name; + private final List> props; + private ClassWriter cw; + private String internalInterfaceName; + + public SDOGenerator(Class interfaceClass) { + internalInterfaceName = getInternalName(interfaceClass.getName()); + name = internalInterfaceName + SDO_MARKER; + props = new ArrayList>(); + } + + public void addProperty(Property property) { + props.add(property); + } + + public byte[] toByteArray() { + cw = new ClassWriter(false); + cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name, null, "org/apache/tuscany/sdo/impl/AbstractDataObject", new String[]{internalInterfaceName}); + + for (Property property : props) { + generateProperty(property); + } + + generateGetIndex(); + generateSetIndex(); + generateConstructor(); + + cw.visitEnd(); + return cw.toByteArray(); + } + + private void generateGetIndex() { + MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(I)Ljava/lang/Object;", null, null); + mv.visitCode(); + mv.visitVarInsn(ILOAD, 1); + Label[] labels = new Label[props.size()]; + for (int i=0; i < labels.length; i++) { + labels[i] = new Label(); + } + Label def = new Label(); + mv.visitTableSwitchInsn(0, labels.length-1, def, labels); + int i = 0; + for (Property property : props) { + String propertyName = property.getName(); + java.lang.reflect.Type javaType = property.getType().getJavaType(); + + mv.visitLabel(labels[i++]); + mv.visitInsn(ALOAD_0); + + if (javaType instanceof Class) { + Type type = Type.getType((Class) javaType); + mv.visitFieldInsn(GETFIELD, name, propertyName, type.getDescriptor()); + autobox(mv, type); + } else { + throw new UnsupportedOperationException(); + } + + mv.visitInsn(ARETURN); + } + + mv.visitLabel(def); + mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException"); + mv.visitInsn(DUP); + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "", "()V"); + mv.visitInsn(ATHROW); + mv.visitMaxs(2, 2); + mv.visitEnd(); + } + + private void generateSetIndex() { + MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set", "(ILjava/lang/Object;)V", null, null); + mv.visitCode(); + mv.visitVarInsn(ILOAD, 1); + Label[] labels = new Label[props.size()]; + for (int i=0; i < labels.length; i++) { + labels[i] = new Label(); + } + Label def = new Label(); + mv.visitTableSwitchInsn(0, labels.length-1, def, labels); + int i = 0; + for (Property property : props) { + String propertyName = property.getName(); + java.lang.reflect.Type javaType = property.getType().getJavaType(); + + mv.visitLabel(labels[i++]); + mv.visitInsn(ALOAD_0); + mv.visitInsn(ALOAD_2); + + if (javaType instanceof Class) { + Class propertyClass = (Class) javaType; + Type propertyType = Type.getType(propertyClass); + autounbox(mv, propertyType); + mv.visitFieldInsn(PUTFIELD, name, propertyName, propertyType.getDescriptor()); + } else { + throw new UnsupportedOperationException(); + } + mv.visitInsn(RETURN); + } + + mv.visitLabel(def); + mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException"); + mv.visitInsn(DUP); + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "", "()V"); + mv.visitInsn(ATHROW); + mv.visitMaxs(3, 3); + mv.visitEnd(); + } + + private void generateConstructor() { + int stack = 2; + MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "(Lorg/apache/tuscany/sdo/impl/TypeImpl;)V", null, null); + mv.visitCode(); + mv.visitInsn(ALOAD_0); + mv.visitInsn(ALOAD_1); + mv.visitMethodInsn(INVOKESPECIAL, "org/apache/tuscany/sdo/impl/AbstractDataObject", "", "(Lorg/apache/tuscany/sdo/impl/TypeImpl;)V"); + for (Property prop : props) { + if (!prop.isMany()) { + continue; + } + stack = 3; + mv.visitInsn(ALOAD_0); + mv.visitTypeInsn(NEW, "java/util/ArrayList"); + mv.visitInsn(DUP); + mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "", "()V"); + mv.visitFieldInsn(PUTFIELD, name, prop.getName(), "Ljava/util/List;"); + + } + mv.visitInsn(RETURN); + mv.visitMaxs(stack, 2); + mv.visitEnd(); + } + + public void generateProperty(Property property) { + generateField(property); + generateGetter(property); + generateSetter(property); + } + + private void generateField(Property property) { + String propertyName = property.getName(); + java.lang.reflect.Type javaType = property.getJavaType(); + String descriptor; + String signature; + if (javaType instanceof Class) { + descriptor = Type.getType((Class) javaType).getDescriptor(); + signature = null; + } else if (javaType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) javaType; + Type rawType = Type.getType((Class) type.getRawType()); + descriptor = rawType.getDescriptor(); + signature = getSignature(type, rawType); + } else { + throw new UnsupportedOperationException(); + } + FieldVisitor fv = cw.visitField(ACC_PRIVATE, propertyName, descriptor, signature, null); + fv.visitEnd(); + } + + private static String getSignature(ParameterizedType type, Type rawType) { + SignatureWriter sw = new SignatureWriter(); + sw.visitClassType(rawType.getInternalName()); + for (java.lang.reflect.Type typeArg : type.getActualTypeArguments()) { + if (typeArg instanceof Class) { + SignatureVisitor sv = sw.visitTypeArgument(SignatureVisitor.INSTANCEOF); + sv.visitClassType(Type.getType((Class) typeArg).getInternalName()); + sv.visitEnd(); + } + } + sw.visitEnd(); + return sw.toString(); + } + + private void generateGetter(Property property) { + String propertyName = property.getName(); + java.lang.reflect.Type javaType = property.getJavaType(); + String methodName = (Boolean.TYPE.equals(javaType) ? "is" : "get") + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); + + Type rawType; + String signature; + if (javaType instanceof Class) { + rawType = Type.getType((Class) javaType); + signature = null; + } else if (javaType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) javaType; + rawType = Type.getType((Class) type.getRawType()); + signature = getSignature(type, rawType); + } else { + throw new UnsupportedOperationException(); + } + + String descriptor = rawType.getDescriptor(); + + MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "()" + descriptor, signature, null); + mv.visitCode(); + mv.visitInsn(ALOAD_0); + mv.visitFieldInsn(GETFIELD, name, propertyName, descriptor); + mv.visitInsn(rawType.getOpcode(IRETURN)); + mv.visitMaxs(rawType.getSize(), 1); + mv.visitEnd(); + } + + private void generateSetter(Property property) { + String propertyName = property.getName(); + java.lang.reflect.Type javaType = property.getJavaType(); + String methodName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); + + Type rawType; + String signature; + if (javaType instanceof Class) { + rawType = Type.getType((Class) javaType); + signature = null; + } else if (javaType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) javaType; + rawType = Type.getType((Class) type.getRawType()); + signature = getSignature(type, rawType); + } else { + throw new UnsupportedOperationException(); + } + + String descriptor = rawType.getDescriptor(); + + MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, '(' + descriptor + ")V", signature, null); + mv.visitCode(); + mv.visitInsn(ALOAD_0); +// mv.visitInsn(rawType.getOpcode(ILOAD_1)); todo make this work + mv.visitVarInsn(rawType.getOpcode(ILOAD), 1); + mv.visitFieldInsn(PUTFIELD, name, propertyName, descriptor); + mv.visitInsn(RETURN); + mv.visitMaxs(1 + rawType.getSize(), 1 + rawType.getSize()); + mv.visitEnd(); + } + + private static void autobox(MethodVisitor mv, Type type) { + switch (type.getSort()) { + case Type.BOOLEAN: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"); + break; + case Type.CHAR: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;"); + break; + case Type.BYTE: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"); + break; + case Type.SHORT: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"); + break; + case Type.INT: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); + break; + case Type.LONG: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"); + break; + case Type.FLOAT: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"); + break; + case Type.DOUBLE: + mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"); + break; + } + } + + @SuppressWarnings({"OverlyLongMethod", "OverlyComplexMethod"}) + private static void autounbox(MethodVisitor mv, Type type) { + switch (type.getSort()) { + case Type.BOOLEAN: + mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); + break; + case Type.CHAR: + mv.visitTypeInsn(CHECKCAST, "java/lang/Character"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C"); + break; + case Type.BYTE: + mv.visitTypeInsn(CHECKCAST, "java/lang/Byte"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B"); + break; + case Type.SHORT: + mv.visitTypeInsn(CHECKCAST, "java/lang/Short"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S"); + break; + case Type.INT: + mv.visitTypeInsn(CHECKCAST, "java/lang/Integer"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I"); + break; + case Type.LONG: + mv.visitTypeInsn(CHECKCAST, "java/lang/Long"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J"); + break; + case Type.FLOAT: + mv.visitTypeInsn(CHECKCAST, "java/lang/Float"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F"); + break; + case Type.DOUBLE: + mv.visitTypeInsn(CHECKCAST, "java/lang/Double"); + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D"); + break; + case Type.ARRAY: + mv.visitTypeInsn(CHECKCAST, type.getDescriptor()); + break; + case Type.OBJECT: + mv.visitTypeInsn(CHECKCAST, type.getInternalName()); + break; + default: + throw new AssertionError(); + } + } + + private static String getInternalName(String name) { + return name.replace('.', '/'); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOInstanceFactory.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOInstanceFactory.java new file mode 100644 index 0000000000..be20e24502 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/SDOInstanceFactory.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public class SDOInstanceFactory implements InstanceFactory { + private Class implementationClass; + private Constructor ctr; + + public void setImplementationClass(Class implementationClass) { + this.implementationClass = implementationClass; + try { + ctr = implementationClass.getConstructor(TypeImpl.class); + } catch (NoSuchMethodException e) { + throw new AssertionError(); + } + } + + public T newInstance(Type type) { + try { + return ctr.newInstance(type); + } catch (InstantiationException e) { + throw (AssertionError) new AssertionError("Unable to instantiate " + implementationClass.getName()).initCause(e); + } catch (IllegalAccessException e) { + throw (AssertionError) new AssertionError("Unable to instantiate " + implementationClass.getName()).initCause(e); + } catch (InvocationTargetException e) { + Throwable t = e.getCause(); + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } else if (t instanceof Error) { + throw (Error) t; + } else { + throw new AssertionError("SDO Implementation threw checked exception"); + } + } + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeHelperImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeHelperImpl.java new file mode 100644 index 0000000000..bb5c2561d8 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeHelperImpl.java @@ -0,0 +1,236 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.QName; + +import org.osoa.sdo.DuplicateTypeException; +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; +import org.osoa.sdo.helper.TypeHelper; + +/** + * @version $Rev$ $Date$ + */ +public class TypeHelperImpl implements TypeHelper { + private final Map> typesByClass; + private final Map> typesByName; + private final ProxyClassLoader cl; + + @SuppressWarnings({"ClassLoader2Instantiation"}) + public TypeHelperImpl(ClassLoader cl) { + this.cl = new ProxyClassLoader(cl); + typesByName = new HashMap>(); + typesByClass = new HashMap>(); + mapType(DataTypeImpl.BOOLEAN); + mapType(DataTypeImpl.BYTE); + mapType(DataTypeImpl.BYTES); + mapType(DataTypeImpl.CHARACTER); + mapType(DataTypeImpl.DATE); + mapType(DataTypeImpl.DOUBLE); + mapType(DataTypeImpl.FLOAT); + mapType(DataTypeImpl.INT); + mapType(DataTypeImpl.LONG); + mapType(DataTypeImpl.SHORT); + mapType(DataTypeImpl.STRING); + + mapType(DataTypeImpl.BOOLEAN_OBJECT); + mapType(DataTypeImpl.CHARACTER_OBJECT); + } + + private void mapType(Type type) { + typesByName.put(type.getName(), type); + + java.lang.reflect.Type javaType = type.getJavaType(); + if (javaType != null) { + typesByClass.put(javaType, type); + } + } + + public Type define(Class interfaceClass) { + if (typesByClass.containsKey(interfaceClass)) { + throw new DuplicateTypeException(interfaceClass.getName()); + } + + String namespace; + String name; + org.osoa.sdo.annotation.Type ann = interfaceClass.getAnnotation(org.osoa.sdo.annotation.Type.class); + if (ann != null) { + namespace = ann.namespace(); + name = ann.name(); + } else { + namespace = Type.JAVA_NAMESPACE; + name = interfaceClass.getName(); + } + return define(new QName(namespace, name), interfaceClass); + } + + public Type define(QName typeName, Class interfaceClass) { + if (!interfaceClass.isInterface()) { + throw new IllegalArgumentException("Not an interface: " + interfaceClass); + } + + Method[] methods = interfaceClass.getMethods(); + Map props = new LinkedHashMap(methods.length >> 1); + for (Method method : methods) { + String methodName = method.getName(); + Class[] params = method.getParameterTypes(); + String propName; + java.lang.reflect.Type propType; + if (Void.TYPE.equals(method.getReturnType()) && + methodName.startsWith("set") && + methodName.length() > 3 && + params.length == 1) { + propName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); + propType = params[0]; + } else if (Boolean.TYPE.equals(method.getReturnType()) && + methodName.startsWith("is") && + methodName.length() > 2 && + params.length == 0) { + propName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3); + propType = Boolean.TYPE; + } else if (!Void.TYPE.equals(method.getReturnType()) && + methodName.startsWith("get") && + methodName.length() > 3 && + params.length == 0) { + propName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); + propType = method.getGenericReturnType(); + } else { + throw new IllegalArgumentException("Non-accessor method on interface: " + method); + } + + defineProperty(props, propName, propType); + } + return define(typeName, interfaceClass, new ArrayList(props.values())); + } + + private void defineProperty(Map props, String propName, java.lang.reflect.Type propType) { + Property prop = props.get(propName); + if (prop != null) { + /* TODO should check this + if (!propClass.equals(prop.getType().getInstanceClass())) { + throw new IllegalArgumentException("set/get types do not match for property: " + propName); + } + */ + return; + } + + int index = props.size(); + + if (propType instanceof Class) { + prop = getPropertyFromClass(propName, index, propType); + } else if (propType instanceof ParameterizedType) { + prop = getPropertyFromParameterizedType(propName, index, propType); + } else { + throw new UnsupportedOperationException(); + } + props.put(propName, prop); + } + + private Property getPropertyFromParameterizedType(String propName, int index, java.lang.reflect.Type propType) { + ParameterizedType parameterizedType = (ParameterizedType) propType; + if (List.class.equals(parameterizedType.getRawType())) { + java.lang.reflect.Type actualPropType = parameterizedType.getActualTypeArguments()[0]; + if (!(actualPropType instanceof Class)) { + throw new IllegalArgumentException("Actual type of list property must not be generic: " + propName); + } + Class propClass = (Class) actualPropType; + Type type = getType(propClass); + if (type == null) { + type = define(propClass); + } + return new ListPropertyImpl(propName, type, parameterizedType,index); + } else { + throw new IllegalArgumentException("Invalid generic type: " + parameterizedType); + } + } + + private Property getPropertyFromClass(String propName, int index, java.lang.reflect.Type propType) { + Class propClass = (Class) propType; + if (propClass.isArray() && !byte[].class.equals(propClass)) { + throw new IllegalArgumentException("Property cannot be an array type: " + propName); + } + + Type type = getType(propClass); + if (type == null) { + type = define(propClass); + } + return new PropertyImpl(propName, type, index); + } + + private Type define(QName typeName, Class instanceClass, List properties) { + if (typeName == null) { + throw new IllegalArgumentException("typeName is null"); + } + String namespace = typeName.getNamespaceURI(); + String name = typeName.getLocalPart(); + if (namespace == null || Type.SDO_NAMESPACE.equals(namespace)) { + throw new IllegalArgumentException("Invalid namespace: " + namespace); + } + if (name == null) { + throw new IllegalArgumentException("name is null"); + } + if (Type.JAVA_NAMESPACE.equals(namespace) && !instanceClass.getName().equals(name)) { + throw new IllegalArgumentException("in Java namespace, name must equal instanceClass name"); + } + + SDOInstanceFactory instanceFactory = new SDOInstanceFactory(); + Type type = new TypeImpl(typeName, instanceClass, properties, instanceFactory); + + SDOGenerator gen = new SDOGenerator(instanceClass); + for (Property property : properties) { + gen.addProperty(property); + } + byte[] bytes = gen.toByteArray(); +// dumpClass(typeName.getLocalPart(), bytes); + Class implementationClass = (Class) cl.addProxy(null, bytes); + instanceFactory.setImplementationClass(implementationClass); + + mapType(type); + return type; + } + + public Type getType(Class interfaceClass) { + return (Type) typesByClass.get(interfaceClass); + } + + public Type getType(QName name) { + return typesByName.get(name); + } + + private static void dumpClass(String name, byte[] bytes) { + File file = new File("/tmp/dump/" + name.replace('.', '/') + ".class"); + file.getParentFile().mkdirs(); + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(bytes); + fos.close(); + } catch (IOException e) { + } + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeImpl.java b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeImpl.java new file mode 100644 index 0000000000..eb661be334 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany/sdo/impl/TypeImpl.java @@ -0,0 +1,73 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public class TypeImpl implements Type { + private final QName name; + private final java.lang.reflect.Type javaType; + private final List propList; + private final InstanceFactory instanceFactory; + + TypeImpl(QName name, java.lang.reflect.Type javaType, List props, InstanceFactory instanceFactory) { + this.name = name; + this.javaType = javaType; + this.propList = Collections.unmodifiableList(new ArrayList(props)); + this.instanceFactory = instanceFactory; + } + + public QName getName() { + return name; + } + + public java.lang.reflect.Type getJavaType() { + return javaType; + } + + public T newInstance() { + return instanceFactory.newInstance(this); + } + + public List getProperties() { + return propList; + } + + public boolean isDataType() { + return false; +} + + public Property getProperty(String name) { + for (Property prop : propList) { + if (prop.getName().equals(name)) { + return prop; + } + } + return null; + } +} \ No newline at end of file diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DataObject.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DataObject.java new file mode 100644 index 0000000000..e18ceb1171 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DataObject.java @@ -0,0 +1,31 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo; + +/** + * @version $Rev$ $Date$ + */ +public interface DataObject { + Object get(int index); + void set(int index, Object value); + + Type getType(); + + String getString(int index); + void setString(int index, String value); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DuplicateTypeException.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DuplicateTypeException.java new file mode 100644 index 0000000000..5f96160870 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/DuplicateTypeException.java @@ -0,0 +1,27 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo; + +/** + * @version $Rev$ $Date$ + */ +public class DuplicateTypeException extends IllegalArgumentException { + public DuplicateTypeException(String message) { + super(message); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Property.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Property.java new file mode 100644 index 0000000000..ed2646259d --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Property.java @@ -0,0 +1,29 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo; + +/** + * @version $Rev$ $Date$ + */ +public interface Property { + String getName(); + Type getType(); + int getIndex(); + boolean isMany(); + java.lang.reflect.Type getJavaType(); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Type.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Type.java new file mode 100644 index 0000000000..924ddc696d --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/Type.java @@ -0,0 +1,62 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo; + +import java.util.List; +import java.lang.reflect.*; +import javax.xml.namespace.QName; + +/** + * @version $Rev$ $Date$ + */ +public interface Type { + static final String SDO_NAMESPACE = "http://www.osoa.org/sdo"; + static final QName BOOLEAN = new QName(SDO_NAMESPACE, "Boolean"); + static final QName BYTE = new QName(SDO_NAMESPACE, "Byte"); + static final QName BYTES = new QName(SDO_NAMESPACE, "Bytes"); + static final QName CHARACTER = new QName(SDO_NAMESPACE, "Character"); + static final QName DATE = new QName(SDO_NAMESPACE, "Date"); + static final QName DOUBLE = new QName(SDO_NAMESPACE, "Double"); + static final QName FLOAT = new QName(SDO_NAMESPACE, "Float"); + static final QName INT = new QName(SDO_NAMESPACE, "Int"); + static final QName LONG = new QName(SDO_NAMESPACE, "Long"); + static final QName SHORT = new QName(SDO_NAMESPACE, "Short"); + static final QName STRING = new QName(SDO_NAMESPACE, "String"); + + static final String JAVA_NAMESPACE = "http://www.osoa.org/sdo/java"; + static final QName BOOLEAN_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Boolean"); + static final QName CHARACTER_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Character"); + static final QName BYTE_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Byte"); + static final QName SHORT_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Short"); + static final QName INTEGER_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Integer"); + static final QName LONG_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Long"); + static final QName FLOAT_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Float"); + static final QName DOUBLE_OBJECT = new QName(JAVA_NAMESPACE, "java.lang.Double"); + + QName getName(); + + java.lang.reflect.Type getJavaType(); + + T newInstance(); + + List getProperties(); + + boolean isDataType(); + + Property getProperty(String name); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/annotation/Type.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/annotation/Type.java new file mode 100644 index 0000000000..20bacf2dad --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/annotation/Type.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo.annotation; + +import static java.lang.annotation.ElementType.TYPE; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** + * @version $Rev$ $Date$ + */ +@Target({TYPE}) +@Retention(RUNTIME) +public @interface Type { + String namespace(); + + String name(); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/DataFactory.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/DataFactory.java new file mode 100644 index 0000000000..9f234129bc --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/DataFactory.java @@ -0,0 +1,34 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo.helper; + +import javax.xml.namespace.QName; + +import org.osoa.sdo.Type; +import org.osoa.sdo.DataObject; + +/** + * @version $Rev$ $Date$ + */ +public interface DataFactory { + T create(Class interfaceClass); + + T create(Type type); + + DataObject create(QName name); +} diff --git a/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/TypeHelper.java b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/TypeHelper.java new file mode 100644 index 0000000000..1e953cb6ee --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/main/java/org/osoa/sdo/helper/TypeHelper.java @@ -0,0 +1,52 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.osoa.sdo.helper; + +import java.util.List; +import javax.xml.namespace.QName; + +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +public interface TypeHelper { + /** + * Define a Type in the Java namespace based on a Java interface. + * Equivalent to define(new QName(Type.JAVA_NAMESPACE, interfaceClass.getName()), interfaceClass) + * + * @param interfaceClass a Java interface that defines the type + * @return a new Type defined from the Java interface + */ + Type define(Class interfaceClass); + + /** + * Define a Type based on a Java interface. + * The name of the Type will be the interface's class name. + * + * @param name the name of the type + * @param interfaceClass a Java interface that defines the type + * @return a new Type defined from the Java interface + */ + Type define(QName name, Class interfaceClass); + + Type getType(QName name); + + Type getType(Class interfaceClass); +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DOMHelperTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DOMHelperTestCase.java new file mode 100644 index 0000000000..3898f02a66 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DOMHelperTestCase.java @@ -0,0 +1,94 @@ +/** + * + * Copyright 2005 The Apache Software Foundation + * + * Licensed 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.sdo.proxy; + +import java.io.IOException; +import java.util.Date; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import junit.framework.TestCase; +import org.osoa.sdo.DataObject; +import org.osoa.sdo.Type; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.apache.tuscany.sdo.helper.DOMHelperImpl; +import org.apache.tuscany.sdo.impl.DataFactoryImpl; +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.DataTypes; +import org.apache.tuscany.sdo.proxy.interfaces.Primitives; + +/** + * @version $Rev$ $Date$ + */ +public class DOMHelperTestCase extends TestCase { + private Primitives primitives; + private DataTypes dataTypes; + private DOMHelperImpl domHelper; + private Document doc; + + public void testSavePrimitives() throws IOException { + domHelper.save((DataObject) primitives, doc); + Element e = doc.getDocumentElement(); + assertEquals(Type.JAVA_NAMESPACE, e.getNamespaceURI()); + assertEquals(Primitives.class.getName(), e.getLocalName()); + assertEquals(8, e.getChildNodes().getLength()); +// domHelper.writeTo(System.out, doc); + } + + public void testLoadPrimitives() { + Element rootElement = doc.createElementNS(Type.JAVA_NAMESPACE, Primitives.class.getName()); + appendElement(rootElement, "boolean", "true"); + doc.appendChild(rootElement); + Primitives primitives = (Primitives) domHelper.load(doc); + assertNotNull(primitives); + assertEquals(true, primitives.isBoolean()); + } + + private void appendElement(Element parent, String name, String text) { + Element e = doc.createElementNS(null, name); + e.appendChild(doc.createTextNode(text)); + parent.appendChild(e); + } + + public void testSaveDataTypes() throws IOException { + dataTypes.setString("Hello World"); + dataTypes.setBytes(new byte[]{ 0 , 1, 2, 3}); + dataTypes.setDate(new Date()); + domHelper.save((DataObject) dataTypes, doc); + Element e = doc.getDocumentElement(); + assertEquals(Type.JAVA_NAMESPACE, e.getNamespaceURI()); + assertEquals(DataTypes.class.getName(), e.getLocalName()); + assertEquals(3, e.getChildNodes().getLength()); +// domHelper.writeTo(System.out, doc); + } + + protected void setUp() throws Exception { + super.setUp(); + TypeHelperImpl typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + DataFactoryImpl dataFactory = new DataFactoryImpl(typeHelper); + domHelper = new DOMHelperImpl(dataFactory); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + doc = builder.newDocument(); + + primitives = typeHelper.define(Primitives.class).newInstance(); + dataTypes = typeHelper.define(DataTypes.class).newInstance(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DataTypesTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DataTypesTestCase.java new file mode 100644 index 0000000000..d097b22f17 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/DataTypesTestCase.java @@ -0,0 +1,130 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy; + +import java.util.Arrays; +import java.util.Date; + +import junit.framework.TestCase; +import org.osoa.sdo.Type; +import org.osoa.sdo.Property; +import org.osoa.sdo.DataObject; +import org.osoa.sdo.helper.TypeHelper; + +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.DataTypes; + +/** + * @version $Rev$ $Date$ + */ +@SuppressWarnings({"CastToIncompatibleInterface"}) +public class DataTypesTestCase extends TestCase { + private DataTypes dataTypes; + private Type type; + + public void testString() { + dataTypes.setString("Hello World"); + assertEquals("Hello World", dataTypes.getString()); + } + + public void testBytes() { + byte[] bytes = new byte[]{0, 1, 2, 3}; + dataTypes.setBytes(bytes ); + assertTrue(Arrays.equals(bytes, dataTypes.getBytes())); + } + + public void testDate() { + Date date = new Date(); + dataTypes.setDate(date); + assertEquals(date, dataTypes.getDate()); + } + + public void testGetIndex() { + DataObject o = (DataObject) dataTypes; + int i = 0; + for (Property property : type.getProperties()) { + String name = property.getName(); + if ("string".equals(name)) { + dataTypes.setString("Hello World"); + assertEquals("Hello World", o.get(i)); + } else if ("bytes".equals(name)) { + byte[] bytes = new byte[]{0, 1, 2, 3}; + dataTypes.setBytes(bytes ); + assertTrue(Arrays.equals(bytes, (byte[]) o.get(i))); + } else if ("date".equals(name)) { + Date date = new Date(); + dataTypes.setDate(date); + assertEquals(date, o.get(i)); + } else { + fail(); + } + i++; + } + } + + public void testSetIndex() { + DataObject o = (DataObject) dataTypes; + int i = 0; + for (Property property : type.getProperties()) { + String name = property.getName(); + if ("string".equals(name)) { + o.set(i, "Hello World"); + assertEquals("Hello World", dataTypes.getString()); + } else if ("bytes".equals(name)) { + byte[] bytes = new byte[]{0, 1, 2, 3}; + o.set(i, bytes); + assertTrue(Arrays.equals(bytes, dataTypes.getBytes())); + } else if ("date".equals(name)) { + Date date = new Date(); + o.set(i, date); + assertEquals(date, dataTypes.getDate()); + } else { + fail(); + } + i++; + } + } + + public void testSetNull() { + DataObject o = (DataObject) dataTypes; + int i = 0; + for (Property property : type.getProperties()) { + String name = property.getName(); + if ("string".equals(name)) { + o.set(i, null); + assertNull(dataTypes.getString()); + } else if ("bytes".equals(name)) { + o.set(i, null); + assertNull(dataTypes.getBytes()); + } else if ("date".equals(name)) { + o.set(i, null); + assertNull(dataTypes.getDate()); + } else { + fail(); + } + i++; + } + } + + protected void setUp() throws Exception { + super.setUp(); + TypeHelper typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + type = typeHelper.define(DataTypes.class); + dataTypes = type.newInstance(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/GetSetOnlyTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/GetSetOnlyTestCase.java new file mode 100644 index 0000000000..dae76dd099 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/GetSetOnlyTestCase.java @@ -0,0 +1,49 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy; + +import java.util.List; + +import junit.framework.TestCase; +import org.osoa.sdo.helper.TypeHelper; +import org.osoa.sdo.Type; +import org.osoa.sdo.Property; + +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.GetSetOnly; + +/** + * @version $Rev$ $Date$ + */ +public class GetSetOnlyTestCase extends TestCase { + private Type type; + private List properties; + + public void testProperties() { + assertEquals(2, properties.size()); + assertNotNull(type.getProperty("readOnly")); + assertNotNull(type.getProperty("writeOnly")); + } + + protected void setUp() throws Exception { + super.setUp(); + TypeHelper typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + type = typeHelper.define(GetSetOnly.class); + properties = type.getProperties(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/ListTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/ListTestCase.java new file mode 100644 index 0000000000..87d2786778 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/ListTestCase.java @@ -0,0 +1,45 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy; + +import junit.framework.TestCase; +import org.osoa.sdo.Type; +import org.osoa.sdo.helper.TypeHelper; + +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.ListTypes; + +/** + * @version $Rev$ $Date$ + */ +public class ListTestCase extends TestCase { + private Type type; + private ListTypes listTypes; + + public void testDefaultList() throws NoSuchMethodException { + assertNotNull(listTypes.getBooleans()); + assertTrue(listTypes.getBooleans().isEmpty()); + } + + protected void setUp() throws Exception { + super.setUp(); + TypeHelper typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + type = typeHelper.define(ListTypes.class); + listTypes = type.newInstance(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/PrimitivesTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/PrimitivesTestCase.java new file mode 100644 index 0000000000..ea2c031d8b --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/PrimitivesTestCase.java @@ -0,0 +1,202 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy; + +import junit.framework.TestCase; +import org.osoa.sdo.Type; +import org.osoa.sdo.DataObject; +import org.osoa.sdo.Property; +import org.osoa.sdo.helper.TypeHelper; + +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.Primitives; + +/** + * @version $Rev$ $Date$ + */ +@SuppressWarnings({"InstanceofIncompatibleInterface", "CastToIncompatibleInterface"}) +public class PrimitivesTestCase extends TestCase { + private Primitives primitives; + private Type type; + + public void testIsDataObject() { + assertTrue(primitives instanceof DataObject); + DataObject o = (DataObject) primitives; + Type type = o.getType(); + assertNotNull(type); + assertEquals(this.type, type); + assertEquals(Primitives.class, type.getJavaType()); + } + + public void testBoolean() { + primitives.setBoolean(false); + assertEquals(false, primitives.isBoolean()); + primitives.setBoolean(true); + assertEquals(true, primitives.isBoolean()); + } + + public void testCharacter() { + primitives.setCharacter('A'); + assertEquals('A', primitives.getCharacter()); + } + + public void testByte() { + primitives.setByte((byte) 12); + assertEquals((byte) 12, primitives.getByte()); + } + + public void testShort() { + primitives.setShort((short) 1234); + assertEquals((short) 1234, primitives.getShort()); + } + + public void testInt() { + primitives.setInt(1234); + assertEquals(1234, primitives.getInt()); + } + + public void testLong() { + primitives.setLong(1234l); + assertEquals(1234l, primitives.getLong()); + } + + public void testFloat() { + primitives.setFloat(1234f); + assertEquals(1234f, primitives.getFloat()); + } + + public void testDouble() { + primitives.setDouble(1234d); + assertEquals(1234d, primitives.getDouble()); + } + + public void testGetIndex() { + DataObject o = (DataObject) primitives; + int i = 0; + for (Property prop : type.getProperties()) { + String name = prop.getName(); + if ("boolean".equals(name)) { + primitives.setBoolean(true); + assertEquals(true, o.get(i)); + } else if ("character".equals(name)) { + primitives.setCharacter('A'); + assertEquals('A', o.get(i)); + } else if ("byte".equals(name)) { + primitives.setByte((byte) 12); + assertEquals((byte) 12, o.get(i)); + } else if ("short".equals(name)) { + primitives.setShort((short) 123); + assertEquals((short)123, o.get(i)); + } else if ("int".equals(name)) { + primitives.setInt(1234); + assertEquals(1234, o.get(i)); + } else if ("long".equals(name)) { + primitives.setLong(12345l); + assertEquals(12345l, o.get(i)); + } else if ("float".equals(name)) { + primitives.setFloat(1234.56f); + assertEquals(1234.56f, o.get(i)); + } else if ("double".equals(name)) { + primitives.setDouble(1234.5678d); + assertEquals(1234.5678d, o.get(i)); + } else { + fail(); + } + i++; + } + } + + public void testSetIndex() { + DataObject o = (DataObject) primitives; + int i = 0; + for (Property prop : type.getProperties()) { + String name = prop.getName(); + if ("boolean".equals(name)) { + o.set(i, true); + assertEquals(true, primitives.isBoolean()); + } else if ("character".equals(name)) { + o.set(i, 'A'); + assertEquals('A', primitives.getCharacter()); + } else if ("byte".equals(name)) { + o.set(i, (byte) 12); + assertEquals((byte)12, primitives.getByte()); + } else if ("short".equals(name)) { + o.set(i, (short)123); + assertEquals((short)123, primitives.getShort()); + } else if ("int".equals(name)) { + o.set(i, 1234); + assertEquals(1234, primitives.getInt()); + } else if ("long".equals(name)) { + o.set(i, 12345l); + assertEquals(12345l, primitives.getLong()); + } else if ("float".equals(name)) { + o.set(i, 1234.56f); + assertEquals(1234.56f, primitives.getFloat()); + } else if ("double".equals(name)) { + o.set(i, 1234.5678d); + assertEquals(1234.5678d, primitives.getDouble()); + } else { + fail(); + } + i++; + } + } + + public void testCannotSetNull() { + DataObject o = (DataObject) primitives; + assertNotNull(o); + for (int i=0 ; i < type.getProperties().size(); i++) { + try { + o.set(i, null); + fail(); + } catch (NullPointerException e) { + } + } + } + + public void testIndexRange() { + DataObject o = (DataObject) primitives; + try { + o.get(-1); + fail(); + } catch (IllegalArgumentException e) { + } + try { + o.get(8); + fail(); + } catch (IllegalArgumentException e) { + } + try { + o.set(-1, null); + fail(); + } catch (IllegalArgumentException e) { + } + try { + o.set(8, null); + fail(); + } catch (IllegalArgumentException e) { + } + } + + protected void setUp() throws Exception { + super.setUp(); + TypeHelper typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + type = typeHelper.define(Primitives.class); + primitives = type.newInstance(); + } +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/SimpleNestingTestCase.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/SimpleNestingTestCase.java new file mode 100644 index 0000000000..ddcd98ae97 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/SimpleNestingTestCase.java @@ -0,0 +1,64 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy; + +import javax.xml.namespace.QName; + +import junit.framework.TestCase; +import org.osoa.sdo.Property; +import org.osoa.sdo.Type; +import org.osoa.sdo.helper.TypeHelper; + +import org.apache.tuscany.sdo.impl.TypeHelperImpl; +import org.apache.tuscany.sdo.proxy.interfaces.Primitives; +import org.apache.tuscany.sdo.proxy.interfaces.SimpleNesting; + +/** + * @version $Rev$ $Date$ + */ +public class SimpleNestingTestCase extends TestCase { + private Type simpleNestingType; + private SimpleNesting simpleNesting; + private TypeHelper typeHelper; + + public void testGetSetPrimitives() { + Primitives prim = simpleNesting.getPrimitives(); + assertNull(prim); + prim = typeHelper.getType(Primitives.class).newInstance(); + simpleNesting.setPrimitives(prim); + assertSame(prim, simpleNesting.getPrimitives()); + } + + public void testPropertyType() { + Property prop = (Property) simpleNestingType.getProperty("primitives"); + assertNotNull(prop); + assertEquals("primitives", prop.getName()); + Type primType = prop.getType(); + assertEquals(new QName(Type.JAVA_NAMESPACE, Primitives.class.getName()), primType.getName()); + assertEquals(Primitives.class, primType.getJavaType()); + } + + protected void setUp() throws Exception { + super.setUp(); + typeHelper = new TypeHelperImpl(getClass().getClassLoader()); + simpleNestingType = typeHelper.define(SimpleNesting.class); + + simpleNesting = simpleNestingType.newInstance(); + } + +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/DataTypes.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/DataTypes.java new file mode 100644 index 0000000000..3549fb0ead --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/DataTypes.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy.interfaces; + +import java.util.Date; + +/** + * @version $Rev$ $Date$ + */ +public interface DataTypes { + String getString(); + + void setString(String value); + + byte[] getBytes(); + + void setBytes(byte[] value); + + Date getDate(); + + void setDate(Date value); +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/GetSetOnly.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/GetSetOnly.java new file mode 100644 index 0000000000..da846c4938 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/GetSetOnly.java @@ -0,0 +1,27 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy.interfaces; + +/** + * @version $Rev$ $Date$ + */ +public interface GetSetOnly { + int getReadOnly(); + + void setWriteOnly(int value); +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/ListTypes.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/ListTypes.java new file mode 100644 index 0000000000..ea19f7ea36 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/ListTypes.java @@ -0,0 +1,27 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy.interfaces; + +import java.util.List; + +/** + * @version $Rev$ $Date$ + */ +public interface ListTypes { + List getBooleans(); +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/Primitives.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/Primitives.java new file mode 100644 index 0000000000..257076c4a1 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/Primitives.java @@ -0,0 +1,55 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy.interfaces; + +/** + * @version $Rev$ $Date$ + */ +public interface Primitives { + boolean isBoolean(); + + void setBoolean(boolean value); + + char getCharacter(); + + void setCharacter(char value); + + byte getByte(); + + void setByte(byte value); + + int getInt(); + + void setInt(int value); + + short getShort(); + + void setShort(short value); + + long getLong(); + + void setLong(long value); + + float getFloat(); + + void setFloat(float value); + + double getDouble(); + + void setDouble(double value); +} diff --git a/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/SimpleNesting.java b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/SimpleNesting.java new file mode 100644 index 0000000000..c5db66fe64 --- /dev/null +++ b/sandbox/jboynes/sdoproxy/src/test/java/org/apache/tuscany/sdo/proxy/interfaces/SimpleNesting.java @@ -0,0 +1,29 @@ +/** + * + * Copyright 2005 BEA Systems Inc. + * Copyright 2005 International Business Machines Corporation + * + * Licensed 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.sdo.proxy.interfaces; + +/** + * @version $Rev$ $Date$ + */ +public interface SimpleNesting { + Primitives getPrimitives(); + void setPrimitives(Primitives primitives); + + DataTypes getDataTypes(); + void setDataTypes(DataTypes dataTypes); +} -- cgit v1.2.3