Author: bdekruijff at gmail.com Date: Tue Dec 21 18:35:06 2010 New Revision: 525
Log: [sandbox] Seperate module for fabric testing Added: sandbox/bdekruijff/fabrictest/ sandbox/bdekruijff/fabrictest/pom.xml sandbox/bdekruijff/fabrictest/src/ sandbox/bdekruijff/fabrictest/src/main/ sandbox/bdekruijff/fabrictest/src/main/java/ sandbox/bdekruijff/fabrictest/src/main/java/org/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/FabricTestService.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/NonRemotableService.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/RemotableService.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/internal/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/internal/RemotableServiceImpl.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/osgi/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/osgi/Activator.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/service/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/service/FabricTestServiceImpl.java sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/shell/ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/shell/FabricTestCommand.java sandbox/bdekruijff/fabrictest/src/test/ sandbox/bdekruijff/fabrictest/src/test/java/ Added: sandbox/bdekruijff/fabrictest/pom.xml ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/pom.xml Tue Dec 21 18:35:06 2010 @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.amdatu</groupId> + <artifactId>org.amdatu.core</artifactId> + <version>0.1.0-SNAPSHOT</version> + </parent> + <groupId>org.amdatu.core</groupId> + <artifactId>fabrictest</artifactId> + <packaging>bundle</packaging> + <name>Amdatu Core - Service Fabric Test</name> + <description>Amdatu Core - Service Fabric Test</description> + + <dependencies> + <dependency> + <groupId>org.amdatu.core</groupId> + <artifactId>fabric</artifactId> + <type>bundle</type> + <scope>provided</scope> + <version>${pom.version}</version> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.shell</artifactId> + <type>bundle</type> + <scope>provided</scope> + <version>1.4.2</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Bundle-Activator>org.amdatu.core.fabrictest.osgi.Activator</Bundle-Activator> + <Bundle-SymbolicName>org.amdatu.core.fabrictest</Bundle-SymbolicName> + <Export-Package>org.amdatu.core.fabrictest</Export-Package> + <Embed-Dependency>*;scope=compile</Embed-Dependency> + <Embed-Transitive>true</Embed-Transitive> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + +</project> Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/FabricTestService.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/FabricTestService.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,25 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest; + +public interface FabricTestService { + + void registerRemotableService(); + + void unregisterRemotableService(); + +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/NonRemotableService.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/NonRemotableService.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,22 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest; + +public interface NonRemotableService { + + String ping(); +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/RemotableService.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/RemotableService.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,22 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest; + +public interface RemotableService { + + String hello(String name); +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/internal/RemotableServiceImpl.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/internal/RemotableServiceImpl.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,32 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest.internal; + +import org.amdatu.core.fabrictest.NonRemotableService; +import org.amdatu.core.fabrictest.RemotableService; + +public class RemotableServiceImpl implements RemotableService, NonRemotableService { + + public String hello(String name) { + System.err.println("Saying hi to " + name); + return "Hi " + name; + } + + public String ping() { + return "pong"; + } +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/osgi/Activator.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/osgi/Activator.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,59 @@ +/* +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest.osgi; + +import org.amdatu.core.fabrictest.FabricTestService; +import org.amdatu.core.fabrictest.RemotableService; +import org.amdatu.core.fabrictest.service.FabricTestServiceImpl; +import org.amdatu.core.fabrictest.shell.FabricTestCommand; +import org.apache.felix.dm.DependencyActivatorBase; +import org.apache.felix.dm.DependencyManager; +import org.apache.felix.shell.Command; +import org.osgi.framework.BundleContext; +import org.osgi.service.log.LogService; + +public class Activator extends DependencyActivatorBase { + + @Override + public void init(BundleContext context, DependencyManager manager) throws Exception { + + manager.add( + createComponent() + .setImplementation(new FabricTestServiceImpl()) + .setInterface(FabricTestService.class.getName(), null) + .add(createServiceDependency().setService(LogService.class).setRequired(false))); + + manager.add( + createComponent() + .setImplementation(new FabricTestCommand()) + .setInterface(Command.class.getName(), null) + .add( + createServiceDependency().setService(FabricTestService.class).setRequired(true)) + .add( + createServiceDependency().setService(RemotableService.class).setRequired(false) + .setCallbacks("remotableServiceAdded", "remotableServiceRemoved")) + .add( + createServiceDependency().setService(LogService.class).setRequired(false) + )); + + } + + @Override + public void destroy(BundleContext context, DependencyManager manager) throws Exception { + } +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/service/FabricTestServiceImpl.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/service/FabricTestServiceImpl.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,68 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest.service; + +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; + +import org.amdatu.core.fabric.remote.DistributionService; +import org.amdatu.core.fabrictest.FabricTestService; +import org.amdatu.core.fabrictest.NonRemotableService; +import org.amdatu.core.fabrictest.RemotableService; +import org.amdatu.core.fabrictest.internal.RemotableServiceImpl; +import org.apache.felix.dm.Component; +import org.apache.felix.dm.DependencyManager; + +public class FabricTestServiceImpl implements FabricTestService { + + private final Map<String, Component> m_serviceComponents = new HashMap<String, Component>(); + private volatile DependencyManager m_dependencyManager; + + public void stop() { + if (m_serviceComponents.containsKey("c1")) + unregisterRemotableService(); + } + + public void registerRemotableService() { + Dictionary<String, Object> props = new Hashtable<String, Object>(); + + props.put(DistributionService.SERVICE_INTENTS_PROP, new String[] {}); + props.put(DistributionService.SERVICE_EXPORTED_INTENTS_PROP, + new String[] { DistributionService.DISTRIBUTION_INTENT_CONFIDENTIALITY }); + props.put(DistributionService.SERVICE_EXPORTED_INTENTS_EXTRA_PROP, new String[] {}); + props.put(DistributionService.SERVICE_EXPORTED_INTERFACES_PROP, + new String[] { RemotableService.class.getName() }); + props.put(DistributionService.SERVICE_EXPORTED_CONFIGS_PROP, + new String[] { DistributionService.SERVICE_CONFIGURATION_TYPE }); + props.put("org.amdatu.test.prop", "this property should be published on imported services"); + + Component serviceComponent = + m_dependencyManager + .createComponent() + .setInterface(new String[] { RemotableService.class.getName(), NonRemotableService.class.getName() }, + props).setImplementation(new RemotableServiceImpl()); + + m_dependencyManager.add(serviceComponent); + m_serviceComponents.put("c1", serviceComponent); + } + + public void unregisterRemotableService() { + m_dependencyManager.remove(m_serviceComponents.remove("c1")); + } +} Added: sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/shell/FabricTestCommand.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/fabrictest/src/main/java/org/amdatu/core/fabrictest/shell/FabricTestCommand.java Tue Dec 21 18:35:06 2010 @@ -0,0 +1,184 @@ +/* + Copyright (C) 2010 Amdatu.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.amdatu.core.fabrictest.shell; + +import java.io.PrintStream; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.StringTokenizer; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.amdatu.core.fabrictest.FabricTestService; +import org.amdatu.core.fabrictest.RemotableService; +import org.apache.felix.shell.Command; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +public class FabricTestCommand implements Command { + + private volatile FabricTestService m_fabricTestService; + + private final List<RemotableService> m_remotableServices = new LinkedList<RemotableService>(); + private final ReentrantReadWriteLock m_remotableServicesLock = new ReentrantReadWriteLock(); + + BundleContext m_bundleContext; + + public void remotableServiceAdded(ServiceReference serviceReference, Object serviceObject) { + m_remotableServicesLock.writeLock().lock(); + try { + m_remotableServices.add((RemotableService) serviceObject); + } + finally { + m_remotableServicesLock.writeLock().unlock(); + } + } + + public void remotableServiceRemoved(ServiceReference serviceReference, Object serviceObject) { + m_remotableServicesLock.writeLock().lock(); + try { + m_remotableServices.remove((RemotableService) serviceObject); + } + finally { + m_remotableServicesLock.writeLock().unlock(); + } + } + + public String getName() { + return "ftest"; + } + + public String getShortDescription() { + return "Fabric test"; + } + + public String getUsage() { + return "ftest <register|unregister|call>"; + } + + public void execute(String s, PrintStream out, PrintStream err) { + + String serviceName = FabricTestService.class.getName(); + + StringTokenizer st = new StringTokenizer(s, " "); + st.nextToken(); // ignore command itself + + if (st.countTokens() != 1) { + out.println(getUsage()); + return; + } + + String command = st.nextToken(); + + if (command.equals("register")) { + m_fabricTestService.registerRemotableService(); + return; + } + + if (command.equals("unregister")) { + m_fabricTestService.unregisterRemotableService(); + return; + } + + if (command.equals("benchmark1")) { + long startTime = System.currentTimeMillis(); + + Random r = new Random(); + String[] names = new String[] { "Bram", "Ivo", "Mark", "Marcel", "Angelo", "Martijn", "Hans" }; + for (int i = 0; i < 1000; i++) { + for (String name : names) { + m_remotableServicesLock.readLock().lock(); + try { + if (m_remotableServices.size() > 0) { + int q = r.nextInt(m_remotableServices.size()); + String anwser = m_remotableServices.get(q).hello(name); + out.println("got: " + anwser); + } + } + finally { + m_remotableServicesLock.readLock().unlock(); + } + } + } + out.println("Benchmark 1: " + (System.currentTimeMillis() - startTime) + " ms"); + return; + } + + if (command.equals("benchmark2")) { + + List<HelloCallable> helloCallables = new LinkedList<FabricTestCommand.HelloCallable>(); + + Random r = new Random(); + String[] names = new String[] { "Bram", "Ivo", "Mark", "Marcel", "Angelo", "Martijn", "Hans" }; + m_remotableServicesLock.readLock().lock(); + try { + for (int i = 0; i < 1000; i++) { + for (String name : names) { + if (m_remotableServices.size() > 0) { + int q = r.nextInt(m_remotableServices.size()); + helloCallables.add(new HelloCallable(m_remotableServices.get(q), name)); + } + } + } + } + finally { + m_remotableServicesLock.readLock().unlock(); + } + + ExecutorService exe = Executors.newFixedThreadPool(4); + long startTime = System.currentTimeMillis(); + try { + List<Future<String>> helloFutures = exe.invokeAll(helloCallables); + exe.shutdown(); + + for (Future<String> helloFuture : helloFutures) { + out.println("got: " + helloFuture.get()); + } + } + catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + out.println("Benchmark 2: " + (System.currentTimeMillis() - startTime) + " ms"); + return; + } + } + + class HelloCallable implements Callable<String> { + + private final RemotableService m_rs; + private final String m_name; + + public HelloCallable(RemotableService rs, String name) { + m_rs = rs; + m_name = name; + } + + public String call() throws Exception { + return m_rs.hello(m_name); + } + } +}
