Shows how to create and run composites dynamically
2012-11-22 09:53
274 查看
tuscany-domain-node/DynamicTestCase.java /** * Shows how to create and run composites dynamically */ public class DynamicTestCase { @Test public void testInstalledContribution() throws NoSuchServiceException, NoSuchDomainException, ContributionReadException, ActivationException, ValidationException, MalformedURLException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, InvocationTargetException, IllegalAccessException { // get the various factories that will be needed TuscanyRuntime tuscanyRuntime = TuscanyRuntime.newInstance(); ExtensionPointRegistry extensionPoints = tuscanyRuntime.getExtensionPointRegistry(); FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); // Create a contribution ContributionFactory contributionFactory = modelFactories.getFactory(ContributionFactory.class); Contribution contribution = contributionFactory.createContribution(); contribution.setURI("testContribution"); ModelResolverExtensionPoint modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, modelFactories); contribution.setModelResolver(modelResolver); contribution.setClassLoader(new URLClassLoader(new URL[] {new File("src/test/resources/sample-helloworld.jar").toURI().toURL()})); // Create a composite AssemblyFactory assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); Composite composite = assemblyFactory.createComposite(); composite.setURI("testComposite"); composite.setName(new QName("testComposite")); // create a component Component component = assemblyFactory.createComponent(); component.setName("testComponent"); JavaImplementationFactory javaImplementationFactory = modelFactories.getFactory(JavaImplementationFactory.class); JavaImplementation javaImplementation = javaImplementationFactory.createJavaImplementation(contribution.getClassLoader().loadClass("sample.HelloworldImpl")); javaImplementation.setJavaClass(contribution.getClassLoader().loadClass("sample.HelloworldImpl")); component.setImplementation(javaImplementation); // add the component to the composite composite.getComponents().add(component); // add the composite to the contribution contribution.addComposite(composite); // Now run the composite with a Tuscany Node Node node = tuscanyRuntime.createNode(); node.installContribution(contribution, null); node.startComposite(contribution.getURI(), composite.getURI()); // test that the service has started and can be invoked testService(node, contribution.getClassLoader()); node.stop(); tuscanyRuntime.stop(); } private void testService(Node node, ClassLoader classLoader) throws ClassNotFoundException, NoSuchServiceException, NoSuchDomainException, IllegalArgumentException, InvocationTargetException, IllegalAccessException { Class<?> interfaze = classLoader.loadClass("sample.Helloworld"); Object clientProxy = node.getService(interfaze, "testComponent/Helloworld"); Method m = interfaze.getMethods()[0]; // the helloworld interface just has a single method "sayHello" Object response = m.invoke(clientProxy, new Object[] {"Ariana"}); Assert.assertEquals("Hello Ariana", response); } }
相关文章推荐
- SharePoint 2007 - /_layouts and how to create pages that run in site context
- Images; How to create an HTTP handler to dynamically resize images and change quality.
- How to create VO s and VLs dynamically in OAF
- Images; How to create an HTTP handler to dynamically resize images and change quality.
- 转载:How to create an automation project using MFC and a type library
- How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- [转]How to Create HTML5 Website and Page Templates for Visual Studio 2010
- zhuanzai: AJAX: How to create a "Processing" modal window using UpdateProgress and ModalPopup ASP.net AJAX controls
- (全英文)How to install and run a simple Asp.Net 5 Application in a Docker Container
- a survey about how to make a file dynamically and offer it to download,but still not save the file!
- how to write and run jpa idempotent on mysql
- How To Create a Custom Salesforce Communities Login and Landing Page
- What is junction object and how to create it?
- how to create and submit kernel patch through git step by step
- Howto Dynamically Insert Javascript And CSS
- How to run eclipse in clean mode? and what happens if we do so?
- How to Install and Run a node.js App on Centos 6.4 64bit
- Create intelligent Web spiders -How to use Java network objects and HTML objects
- How to create, host, test and consume a WCF Web Service
- How To Create a New User and Grant Permissions in MySQL