diff options
15 files changed, 763 insertions, 0 deletions
diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentConsumer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentConsumer.java new file mode 100644 index 0000000000..b693b69ecb --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentConsumer.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + + +/** + * Represents a component consumer. + * + * @version $$ + */ +public interface ComponentConsumer extends Consumer { + + Consumer getConsumer(); + void setConsumer(Consumer consumer); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentProducer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentProducer.java new file mode 100644 index 0000000000..d8fab68aa5 --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/ComponentProducer.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + + + +/** + * Represents a component producer. + * + * @version $$ + */ +public interface ComponentProducer extends Producer { + + Producer getProducer(); + void setProducer(Producer producer); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeConsumer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeConsumer.java new file mode 100644 index 0000000000..205cc32d7f --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeConsumer.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + + +/** + * Represents a composite consumer. + * + * @version $$ + */ +public interface CompositeConsumer extends Consumer { + + + + /** + * Returns the promoted component. + * + * @return the promoted component. + */ + Component getPromotedComponent(); + + /** + * Sets the promoted component + * + * @param promotedComponent the promoted component. + */ + void setPromotedComponent(Component promotedComponent); + + ComponentConsumer getPromotedConsumer(); + void setPromotedConsumer(ComponentConsumer consumer); +}
\ No newline at end of file diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeProducer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeProducer.java new file mode 100644 index 0000000000..563497a591 --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/CompositeProducer.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + + +/** + * Represents a composite producer. + * + * @version $$ + */ +public interface CompositeProducer extends Producer { + + + + /** + * Returns the promoted component. + * + * @return the promoted component. + */ + Component getPromotedComponent(); + + /** + * Sets the promoted component + * + * @param promotedComponent the promoted component. + */ + void setPromotedComponent(Component promotedComponent); + + ComponentProducer getPromotedProducer(); + void setPromotedProducer(ComponentProducer producer); +}
\ No newline at end of file diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Consumer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Consumer.java new file mode 100644 index 0000000000..1120e7ce87 --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Consumer.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + +import java.util.List; + + +/** + * Represents a consumer. + * + * @version $$ + */ +public interface Consumer extends EventTarget, Contract { + + + List<EventSource> getSources(); + + String getOperationName(); + + void setOperationName(String operationName); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBinding.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBinding.java new file mode 100644 index 0000000000..1c586889be --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBinding.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + +/** + * Represents an Event binding. + * + * @version $$ + */ +public interface EventBinding extends Binding { + + Binding getBaseBinding(); + + void setBaseBinding(Binding baseBinding); +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBindingFactory.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBindingFactory.java new file mode 100644 index 0000000000..f82fb02b1d --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventBindingFactory.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + + +/** + * A factory for the Event binding model + * + * @version $$ + */ +public interface EventBindingFactory { + + /** + * Create a new Event binding. + * + * @return a new Event binding + */ + EventBinding createEventBinding(); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventSource.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventSource.java new file mode 100644 index 0000000000..34e2644ed2 --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventSource.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + +public interface EventSource extends Base, Extensible, Cloneable { + + /** + * Returns the name of the source. + * + * @return the name of the source + */ + String getName(); + + /** + * Sets the name of the source. + * + * @param name the name of the source + */ + void setName(String name); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventTarget.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventTarget.java new file mode 100644 index 0000000000..6334013c2b --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/EventTarget.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + +public interface EventTarget extends Base, Extensible, Cloneable { + + /** + * Returns the name of the target. + * + * @return the name of the target + */ + String getName(); + + /** + * Sets the name of the target. + * + * @param name the name of the target + */ + void setName(String name); + +} diff --git a/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Producer.java b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Producer.java new file mode 100644 index 0000000000..5059a33ee0 --- /dev/null +++ b/sandbox/event/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Producer.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.assembly; + +import java.util.List; + + +/** + * Represents a producer + * + * @version $$ + */ +public interface Producer extends EventSource, Contract { + + List<EventTarget> getTargets(); + + +} diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java new file mode 100644 index 0000000000..f2488a6fa5 --- /dev/null +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.logging.Logger; +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.impl.JavaElementImpl; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; + +/** + * Processes an {@link org.osoa.sca.annotations.Service} annotation and updates + * the component type with corresponding {@link Service}s. Also processes + * related {@link org.osoa.sca.annotations.Callback} annotations. + * + * @version $$ + */ +public class ConsumerProcessor extends BaseJavaClassVisitor { + @SuppressWarnings("unused") + private static final Logger logger = Logger.getLogger(ConsumerProcessor.class.getName()); + + private JavaInterfaceFactory javaFactory; + + public ConsumerProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(assemblyFactory); + this.javaFactory = javaFactory; + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + + org.osoa.sca.annotations.Consumer annotation = method.getAnnotation(org.osoa.sca.annotations.Consumer.class); + if (annotation == null) { + return; + } + if(Modifier.isPrivate(method.getModifiers())) { + throw new IllegalConsumerException("Illegal annotation @Consumer found on "+method, method); + } + if (method.getParameterTypes().length != 1) { + throw new IllegalConsumerException("Consumer must have one parameter", method); + } + JavaElementImpl element = new JavaElementImpl(method.getDeclaringClass()); + try { + createConsumer(type, element, annotation.name(), method.getName()); + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + } + + /** + * @param type + * @param element + * @throws IllegalConsumerException + */ + private void createConsumer(JavaImplementation type, JavaElementImpl element, String consumerName, String operationName) + throws IllegalConsumerException, InvalidInterfaceException { + + Class<?> consumerClass = element.getType(); + + org.apache.tuscany.sca.assembly.Consumer consumer = assemblyFactory.createConsumer(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + consumer.setInterfaceContract(interfaceContract); + consumer.setName(consumerName); + JavaInterface consumerInterface = javaFactory.createJavaInterface(); + consumerInterface.setRemotable(true); + javaFactory.createJavaInterface(consumerInterface, consumerClass); + consumer.getInterfaceContract().setInterface(consumerInterface); + consumer.setOperationName(operationName); + + type.getConsumers().add(consumer); + } + + +} diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateProducerException.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateProducerException.java new file mode 100644 index 0000000000..79801ed2f2 --- /dev/null +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/DuplicateProducerException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Thrown when an implementation has more than one producer injection site with the same name + * + * @version $$ + */ +public class DuplicateProducerException extends IntrospectionException { + private static final long serialVersionUID = 907910648213477158L; + + public DuplicateProducerException(String message) { + super(message); + } + +} diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalConsumerException.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalConsumerException.java new file mode 100644 index 0000000000..a20174f643 --- /dev/null +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalConsumerException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal use of {@link org.osoa.sca.annotations.Consumer} + * + * @version $$ + */ +public class IllegalConsumerException extends IntrospectionException { + private static final long serialVersionUID = -8932525723147700591L; + + public IllegalConsumerException(String message) { + super(message); + } + + public IllegalConsumerException(String message, Member member) { + super(message, member); + } +} diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalProducerException.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalProducerException.java new file mode 100644 index 0000000000..dd4a25ba8f --- /dev/null +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/IllegalProducerException.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import java.lang.reflect.Member; + +import org.apache.tuscany.sca.implementation.java.IntrospectionException; + +/** + * Denotes an illegal producer definition in a component type + * + * @version $$ + */ +public class IllegalProducerException extends IntrospectionException { + + private static final long serialVersionUID = 4612984122225271395L; + + public IllegalProducerException(String message) { + super(message); + } + + public IllegalProducerException(String message, Member member) { + super(message, member); + } +} diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ProducerProcessor.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ProducerProcessor.java new file mode 100644 index 0000000000..b4bde7ac6f --- /dev/null +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ProducerProcessor.java @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.implementation.java.introspect.impl; + +import static org.apache.tuscany.sca.implementation.java.introspect.impl.JavaIntrospectionHelper.getBaseType; + +import java.lang.annotation.ElementType; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.util.List; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.implementation.java.IntrospectionException; +import org.apache.tuscany.sca.implementation.java.JavaImplementation; +import org.apache.tuscany.sca.implementation.java.impl.JavaElementImpl; +import org.apache.tuscany.sca.implementation.java.impl.JavaParameterImpl; +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; +import org.osoa.sca.annotations.Producer; + +/** + * Processes an {@link @Producer} annotation, updating the component type + * + * @version $$ + */ +public class ProducerProcessor extends BaseJavaClassVisitor { + private JavaInterfaceFactory javaFactory; + + public ProducerProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) { + super(assemblyFactory); + this.javaFactory = javaFactory; + } + + @Override + public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException { + Producer annotation = method.getAnnotation(Producer.class); + if (annotation == null) { + return; // Not a producer annotation. + } + if (!JavaIntrospectionHelper.isSetter(method)) { + throw new IllegalProducerException("Annotated method is not a setter: " + method, method); + } + String name = annotation.name(); + if ("".equals(name)) { + name = JavaIntrospectionHelper.toPropertyName(method.getName()); + } + JavaElementImpl prod = type.getReferenceMembers().get(name); + // Setter override field + if (prod != null && prod.getElementType() != ElementType.FIELD) { + throw new DuplicateProducerException(name); + } + removeProducer(prod, type); + + JavaElementImpl element = new JavaElementImpl(method, 0); + org.apache.tuscany.sca.assembly.Producer producer = createProducer(element, name); + type.getProducers().add(producer); + type.getReferenceMembers().put(name, element); + } + + private boolean removeProducer(JavaElementImpl producer, JavaImplementation type) { + if (producer == null) { + return false; + } + List<org.apache.tuscany.sca.assembly.Producer> producers = type.getProducers(); + for (int i = 0; i < producers.size(); i++) { + if (producers.get(i).getName().equals(producer.getName())) { + producers.remove(i); + return true; + } + } + return false; + } + + @Override + public void visitField(Field field, JavaImplementation type) throws IntrospectionException { + Producer annotation = field.getAnnotation(Producer.class); + if (annotation == null) { + return; + } + String name = annotation.name(); + if ("".equals(name)) { + name = field.getName(); + } + JavaElementImpl prod = type.getReferenceMembers().get(name); + if (prod != null && prod.getElementType() == ElementType.FIELD) { + throw new DuplicateProducerException(name); + } + + // Setter method override field + if (prod == null) { + JavaElementImpl element = new JavaElementImpl(field); + org.apache.tuscany.sca.assembly.Producer producer = createProducer(element, name); + type.getProducers().add(producer); + type.getReferenceMembers().put(name, element); + } + } + + @Override + public void visitConstructorParameter(JavaParameterImpl parameter, JavaImplementation type) + throws IntrospectionException { + Producer refAnnotation = parameter.getAnnotation(Producer.class); + if (refAnnotation == null) { + return; + } + String paramName = parameter.getName(); + String name = getProducerName(paramName, parameter.getIndex(), refAnnotation.name()); + JavaElementImpl prod = type.getReferenceMembers().get(name); + + // Setter override field + if (prod != null && prod.getElementType() != ElementType.FIELD) { + throw new DuplicateProducerException(name); + } + + removeProducer(prod, type); + org.apache.tuscany.sca.assembly.Producer producer = createProducer(parameter, name); + type.getProducers().add(producer); + type.getReferenceMembers().put(name, parameter); + parameter.setClassifer(Producer.class); + parameter.setName(name); + } + + private String getProducerName(String paramName, int pos, String name) throws InvalidConstructorException { + if ("".equals(name)) { + name = paramName; + } + if ("".equals(name)) { + return "_producer" + pos; + } + if (!"".equals(paramName) && !name.equals(paramName)) { + throw new InvalidConstructorException("Mismatching names specified for producer parameter " + pos); + } else { + return name; + } + } + + private org.apache.tuscany.sca.assembly.Producer createProducer(JavaElementImpl element, String name) + throws IntrospectionException { + + org.apache.tuscany.sca.assembly.Producer producer = assemblyFactory.createProducer(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + producer.setInterfaceContract(interfaceContract); + producer.setName(name); + Class<?> rawType = element.getType(); + Type genericType = element.getGenericType(); + Class<?> baseType = getBaseType(rawType, genericType); + try { + JavaInterface interfaze = javaFactory.createJavaInterface(baseType); + producer.getInterfaceContract().setInterface(interfaze); + + for (Operation op : interfaze.getOperations()) { + op.setNonBlocking(true); + } + } catch (InvalidInterfaceException e) { + throw new IntrospectionException(e); + } + return producer; + } +} |