Event prototype: initial model changes
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@706572 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e753f271cc
commit
d02579ef28
15 changed files with 763 additions and 0 deletions
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue