You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polygene.apache.org by ni...@apache.org on 2017/04/11 12:11:05 UTC
[4/5] polygene-java git commit: POLYGENE-45 : The plugin proposal
that this test is trying to verify disappeared years ago,
and we might re-visit "plugin patterns" later.
POLYGENE-45 : The plugin proposal that this test is trying to verify disappeared years ago, and we might re-visit "plugin patterns" later.
Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/d7fc3065
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/d7fc3065
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/d7fc3065
Branch: refs/heads/develop
Commit: d7fc306584c1e42311c6ff09f6174942e89afb39
Parents: a6fb0cd
Author: niclas <ni...@spicter.com>
Authored: Tue Apr 11 20:04:14 2017 +0800
Committer: niclas <ni...@spicter.com>
Committed: Tue Apr 11 20:04:14 2017 +0800
----------------------------------------------------------------------
.../apache/polygene/api/common/PluginTest.java | 368 -------------------
1 file changed, 368 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/polygene-java/blob/d7fc3065/core/runtime/src/test/java/org/apache/polygene/api/common/PluginTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/api/common/PluginTest.java b/core/runtime/src/test/java/org/apache/polygene/api/common/PluginTest.java
deleted file mode 100644
index dcab32f..0000000
--- a/core/runtime/src/test/java/org/apache/polygene/api/common/PluginTest.java
+++ /dev/null
@@ -1,368 +0,0 @@
-/*
- * 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.polygene.api.common;
-
-import java.lang.reflect.Proxy;
-import java.time.Instant;
-import java.util.Collections;
-import java.util.List;
-import org.apache.polygene.api.injection.scope.Service;
-import org.apache.polygene.api.injection.scope.Structure;
-import org.apache.polygene.api.mixin.Mixins;
-import org.apache.polygene.api.service.ImportedServiceDescriptor;
-import org.apache.polygene.api.service.ServiceActivation;
-import org.apache.polygene.api.service.ServiceComposite;
-import org.apache.polygene.api.service.ServiceFinder;
-import org.apache.polygene.api.service.ServiceImporter;
-import org.apache.polygene.api.service.ServiceImporterException;
-import org.apache.polygene.api.service.ServiceReference;
-import org.apache.polygene.api.structure.Application;
-import org.apache.polygene.api.time.SystemTime;
-import org.apache.polygene.bootstrap.ApplicationAssembler;
-import org.apache.polygene.bootstrap.ApplicationAssembly;
-import org.apache.polygene.bootstrap.ApplicationAssemblyFactory;
-import org.apache.polygene.bootstrap.Assembler;
-import org.apache.polygene.bootstrap.AssemblyException;
-import org.apache.polygene.bootstrap.Energy4Java;
-import org.apache.polygene.bootstrap.LayerAssembly;
-import org.apache.polygene.bootstrap.LayerName;
-import org.apache.polygene.bootstrap.ModuleAssembly;
-import org.apache.polygene.bootstrap.ModuleName;
-import org.junit.Ignore;
-import org.junit.Test;
-
-/**
- * Sample of how a plugin architecture could work.
- * The plugins can use services in the main application and the main
- * application can use services from the plugins
- */
-public class PluginTest
-{
- @Test
- @Ignore( "Must fix the TODOs below. This example relied on ability to set Service MetaInfo at runtime, it seems it's not possible anymore." )
- public void testPlugins()
- throws Exception
- {
- Energy4Java runtime = new Energy4Java();
- Application app = runtime.newApplication( new MainApplicationAssembler() );
- app.activate();
- }
-
- // Main application
-
- class MainApplicationAssembler
- implements ApplicationAssembler
- {
- public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory )
- throws AssemblyException
- {
- return applicationFactory.newApplicationAssembly( new Assembler[][][]
- {
- {
- {
- new PluginAssembler(),
- new UIAssembler(),
- }
- },
- {
- {
- new ServiceAssembler()
- }
- }
- } );
- }
- }
-
- // The UI uses the plugins
-
- class UIAssembler
- implements Assembler
- {
- public void assemble( ModuleAssembly module )
- throws AssemblyException
- {
- module.services( PluginTesterService.class ).instantiateOnStartup();
- module.importedServices( Plugin.class )
- .importedBy( ServiceFinderImporter.class )
- .visibleIn( Visibility.layer );
- }
- }
-
- // Get a reference to a plugin and use it
-
- @Mixins( PluginTesterService.PluginTesterMixin.class )
- interface PluginTesterService
- extends ServiceActivation, ServiceComposite
- {
- class PluginTesterMixin
- implements ServiceActivation
- {
- @Service
- Plugin plugin;
-
- @Service
- PluginsService plugins;
-
- public void activateService()
- throws Exception
- {
- // Use plugin
- System.out.println( plugin.say( "Hello", "World" ) );
-
- // Restart plugin
- plugins.passivateService();
-
- // Plugin is now unavailable
- try
- {
- System.out.println( plugin.say( "Hello", "World" ) );
- }
- catch( ServiceImporterException e )
- {
- // Ignore
- }
-
- plugins.activateService();
-
- // Use plugin
- System.out.println( plugin.say( "Hello", "World" ) );
- }
-
- public void passivateService()
- throws Exception
- {
- }
- }
- }
-
- // Assemble the base service that the plugin can use
-
- class ServiceAssembler
- implements Assembler
- {
- public void assemble( ModuleAssembly module )
- throws AssemblyException
- {
- module.services( HelloWorldService.class ).visibleIn( Visibility.application );
- }
- }
-
- // Plugins can access service instances with this interface
-
- interface HelloWorld
- {
- String say( String phrase, String name );
- }
-
- // Implementation of service that the plugin can use
-
- @Mixins( HelloWorldService.HelloWorldMixin.class )
- interface HelloWorldService
- extends HelloWorld, ServiceComposite
- {
- class HelloWorldMixin
- implements HelloWorld
- {
- public String say( String phrase, String name )
- {
- return phrase + " " + name;
- }
- }
- }
-
- // Assemble the plugins module
-
- class PluginAssembler
- implements Assembler
- {
- public void assemble( ModuleAssembly module )
- throws AssemblyException
- {
- module.services( PluginsService.class ).instantiateOnStartup();
- }
- }
-
-// Plugin service
-// This assembles and activates a separate application for the plugins
-// The plugins can look up services in the host application and the
-
- // plugins can be looked up by the host application
-
- @Mixins( PluginsService.PluginsMixin.class )
- interface PluginsService
- extends ServiceComposite, ServiceActivation
- {
- class PluginsMixin
- implements ServiceActivation
- {
- @Structure
- ServiceFinder finder;
- @Service
- ServiceReference<Plugin> plugin;
- private Application app;
-
- public void activateService()
- throws Exception
- {
- Energy4Java runtime = new Energy4Java();
- app = runtime.newApplication( new PluginApplicationAssembler( finder ) );
-
- app.activate();
- ServiceFinder pluginFinder = app.findModule( "Plugin layer", "Plugin module" );
-
- // TODO: Niclas wrote: No clue how all this Test is supposed to work, and can't figure out to create a workaround for this.
-// finder.findService(Plugin.class).metaInfo().add(ServiceFinder.class, pluginFinder);
- }
-
- public void passivateService()
- throws Exception
- {
- // TODO: Niclas wrote: No clue how all this Test is supposed to work, and can't figure out to create a workaround for this.
-// plugin.metaInfo().remove(ServiceFinder.class);
-
- app.passivate();
- }
- }
- }
-
- // Assemble the plugin application
-
- public static class PluginApplicationAssembler
- implements ApplicationAssembler
- {
- private ServiceFinder finder;
-
- public PluginApplicationAssembler( ServiceFinder finder )
- {
- this.finder = finder;
- }
-
- public ApplicationAssembly assemble( ApplicationAssemblyFactory applicationFactory )
- throws AssemblyException
- {
- return applicationFactory.newApplicationAssembly( new Assembler()
- {
- public void assemble( ModuleAssembly module )
- throws AssemblyException
- {
- new LayerName( "Plugin layer" ).assemble( module );
- new ModuleName( "Plugin module" ).assemble( module );
-
- LayerAssembly layer = module.layer();
-
- // In a real case you would "detect" the plugins somehow. Here the plugin assembler is hardcoded
- List<Assembler> pluginAssemblers = Collections.singletonList( new SimonAssembler() );
-
- for( int i = 0; i < pluginAssemblers.size(); i++ )
- {
- ModuleAssembly pluginModule = layer.module( "Plugin " + ( i + 1 ) );
- Assembler assembler = pluginAssemblers.get( i );
- assembler.assemble( pluginModule );
- }
-
- // Import host services
- module.importedServices( HelloWorld.class )
- .importedBy( ServiceFinderImporter.class )
- .setMetaInfo( finder )
- .visibleIn( Visibility.layer );
- }
- } );
- }
- }
-
- // Service importer that uses a ServiceFinder
-
- public static class ServiceFinderImporter
- implements ServiceImporter
- {
- public Object importService( final ImportedServiceDescriptor serviceDescriptor )
- throws ServiceImporterException
- {
- final Class<?> mainType = serviceDescriptor.types().findFirst().orElse(null);
-
- Class[] interfaces = serviceDescriptor.types().toArray( Class[]::new );
- return Proxy.newProxyInstance(
- mainType.getClassLoader(),
- interfaces,
- ( proxy, method, args ) -> {
- ServiceFinder finder = serviceDescriptor.metaInfo( ServiceFinder.class );
- if( finder == null )
- {
- throw new ServiceImporterException( "No ServiceFinder specified for imported service " + serviceDescriptor
- .identity() );
- }
- Object service = finder.findService( mainType ).get();
- return method.invoke( service, args );
- } );
- }
-
- public boolean isAvailable( Object instance )
- {
- return true;
- }
- }
-
-// The plugin interface. Plugins should implement this, and they can then
-
- // be looked up by the host application
-
- interface Plugin
- {
- String say( String phrase, String name );
- }
-
- // Assemble a sample plugin
-
- public static class SimonAssembler
- implements Assembler
- {
- public void assemble( ModuleAssembly module )
- throws AssemblyException
- {
- module.services( SimonSaysPlugin.class ).visibleIn( Visibility.layer );
- }
- }
-
- // Test implementation of the plugin interface
-
- @Mixins( SimonSaysPlugin.SimonSaysMixin.class )
- interface SimonSaysPlugin
- extends Plugin, ServiceComposite
- {
- class SimonSaysMixin
- implements Plugin
- {
- @Service
- HelloWorld helloWorld;
- private Instant time;
-
- public SimonSaysMixin()
- {
- time = SystemTime.now();
- }
-
- public String say( String phrase, String name )
- {
- return "Simon says:" + helloWorld.say( phrase, name ) + " (plugin started at:" + time + ")";
- }
- }
- }
-}