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 --- .../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 ++++ 14 files changed, 1328 insertions(+) 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 (limited to 'sandbox/jboynes/sdoproxy/src/main/java/org/apache/tuscany') 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 -- cgit v1.2.3