Add getUtility by key to UtilityExtensionPoint

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@794124 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-07-15 00:35:12 +00:00
parent 336d75a67a
commit 9c0aac00b4
5 changed files with 163 additions and 38 deletions

View file

@ -23,10 +23,9 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -40,11 +39,9 @@ import org.apache.tuscany.sca.extensibility.ServiceDiscovery;
* @version $Rev$ $Date$
*/
public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
private Map<Class<?>, Object> utilities = new ConcurrentHashMap<Class<?>, Object>();
private Map<Object, Object> utilities = new ConcurrentHashMap<Object, Object>();
private ExtensionPointRegistry extensionPoints;
private List<Object> unmapped = new ArrayList<Object>();
/**
* Constructs a new extension point.
*/
@ -61,16 +58,25 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
* @throws IllegalArgumentException if utility is null
*/
public void addUtility(Object utility) {
addUtility(null, utility);
}
public void addUtility(Object key, Object utility) {
if (utility == null) {
throw new IllegalArgumentException("Cannot register null as a Service");
}
if(utility instanceof LifeCycleListener) {
((LifeCycleListener) utility).start();
if (utility instanceof LifeCycleListener) {
((LifeCycleListener)utility).start();
}
Set<Class<?>> interfaces = getAllInterfaces(utility.getClass());
for (Class<?> i : interfaces) {
utilities.put(i, utility);
if (key == null) {
Set<Class<?>> interfaces = getAllInterfaces(utility.getClass());
for (Class<?> i : interfaces) {
utilities.put(i, utility);
}
} else {
utilities.put(key, utility);
}
}
@ -102,7 +108,7 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
* @throws IllegalArgumentException if utilityType is null
*/
public <T> T getUtility(Class<T> utilityType) {
return getUtility(utilityType, false);
return getUtility(utilityType, null);
}
/**
@ -120,10 +126,12 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
if(utility instanceof LifeCycleListener) {
((LifeCycleListener) utility).stop();
}
Set<Class<?>> interfaces = getAllInterfaces(utility.getClass());
for (Class<?> i : interfaces) {
utilities.remove(i);
for (Iterator<Map.Entry<Object, Object>> i = utilities.entrySet().iterator(); i.hasNext();) {
Map.Entry<Object, Object> entry = i.next();
if (entry.getValue() == utility) {
i.remove();
}
}
}
@ -134,6 +142,7 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
private static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
Set<Class<?>> implemented = new HashSet<Class<?>>();
getAllInterfaces(clazz, implemented);
implemented.remove(LifeCycleListener.class);
return implemented;
}
@ -151,15 +160,17 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
}
}
public <T> T getUtility(Class<T> utilityType, boolean newInstance) {
public <T> T getUtility(Class<T> utilityType, Object key) {
if (utilityType == null) {
throw new IllegalArgumentException("Cannot lookup Service of type null");
}
Object utility = null;
if (!newInstance) {
utility = utilities.get(utilityType);
if (key == null) {
key = utilityType;
}
Object utility = utilities.get(key);
if (utility == null) {
// Dynamically load a utility class declared under META-INF/services/"utilityType"
@ -188,9 +199,10 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
}
}
// Cache the loaded utility
addUtility(utility);
if (newInstance) {
unmapped.add(utility);
if (key == utilityType) {
addUtility(utility);
} else {
addUtility(key, utility);
}
}
} catch (InvocationTargetException e) {
@ -206,8 +218,7 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
}
}
return utilityType.cast(utility);
}
}
public void start() {
// NOOP
}
@ -221,12 +232,6 @@ public class DefaultUtilityExtensionPoint implements UtilityExtensionPoint {
map.put(listener, listener);
}
}
for (Object util : unmapped) {
if (util instanceof LifeCycleListener) {
LifeCycleListener listener = (LifeCycleListener)util;
map.put(listener, listener);
}
}
for (LifeCycleListener listener : map.values()) {
listener.stop();
}

View file

@ -34,6 +34,14 @@ public interface UtilityExtensionPoint extends LifeCycleListener {
* @throws IllegalArgumentException if utility is null
*/
void addUtility(Object utility);
/**
* Add a utility to the extension point for a given key
* @param utility The instance of the utility
*
* @throws IllegalArgumentException if utility is null
*/
void addUtility(Object key, Object utility);
/**
* Get the utility by the interface
@ -45,14 +53,15 @@ public interface UtilityExtensionPoint extends LifeCycleListener {
<T> T getUtility(Class<T> utilityType);
/**
* Get a new instance of the utility by the interface
* Get an instance of the utility by the interface and key
* @param utilityType The lookup key (utility interface)
* @param newInstance A new instance is required
* @param key A key associated with the utility, if it is null,
* then the utilityType is used as the key
* @return The instance of the utility
*
* @throws IllegalArgumentException if utilityType is null
*/
<T> T getUtility(Class<T> utilityType, boolean newInstance);
<T> T getUtility(Class<T> utilityType, Object key);
/**
* Remove a utility

View file

@ -0,0 +1,111 @@
/*
* 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.extensibility;
import java.io.Serializable;
import org.apache.tuscany.sca.core.DefaultUtilityExtensionPoint;
import org.apache.tuscany.sca.core.LifeCycleListener;
import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
*/
public class DefaultUtilityExtensionPointTestCase {
private static UtilityExtensionPoint ep;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ep = new DefaultUtilityExtensionPoint(null);
ep.start();
}
@Test
public void testGet() {
MyUtilityImpl my = new MyUtilityImpl();
ep.addUtility(my);
Assert.assertTrue(my.started);
Utility1 u1 = ep.getUtility(Utility1.class);
Assert.assertSame(my, u1);
Utility2 u2 = ep.getUtility(Utility2.class);
Assert.assertSame(my, u2);
ep.removeUtility(my);
Assert.assertFalse(my.started);
u1 = ep.getUtility(Utility1.class);
Assert.assertNull(u1);
ep.addUtility("1", my);
u1= ep.getUtility(Utility1.class);
Assert.assertNull(u1);
u1= ep.getUtility(Utility1.class, "1");
Assert.assertNotNull(u1);
ep.removeUtility(my);
u1= ep.getUtility(Utility1.class, "1");
Assert.assertNull(u1);
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
ep.stop();
}
public static interface Utility1 extends Serializable {
void op1();
}
public static interface Utility2 extends Serializable {
void op2();
}
public static class MyUtilityImpl implements Utility1, Utility2, LifeCycleListener {
public boolean started;
public void start() {
System.out.println("start");
started = true;
}
public void stop() {
System.out.println("stop");
started = false;
}
public void op1() {
System.out.println("op1");
}
public void op2() {
System.out.println("op2");
}
}
}

View file

@ -97,9 +97,6 @@ import org.apache.tuscany.sca.node.configuration.ContributionConfiguration;
import org.apache.tuscany.sca.node.configuration.DeploymentComposite;
import org.apache.tuscany.sca.node.configuration.NodeConfiguration;
import org.apache.tuscany.sca.node.configuration.xml.NodeConfigurationProcessor;
import org.apache.tuscany.sca.provider.DefinitionsProvider;
import org.apache.tuscany.sca.provider.DefinitionsProviderException;
import org.apache.tuscany.sca.provider.DefinitionsProviderExtensionPoint;
import org.apache.tuscany.sca.work.WorkScheduler;
import org.oasisopen.sca.ServiceRuntimeException;
@ -436,6 +433,7 @@ public class NodeFactoryImpl extends NodeFactory {
DefinitionsUtil.aggregate(defs, systemDefinitions);
}
/*
// Load the system definitions.xml from all of the loaded extension points
DefinitionsProviderExtensionPoint definitionsProviders = extensionPoints.getExtensionPoint(DefinitionsProviderExtensionPoint.class);
@ -447,6 +445,7 @@ public class NodeFactoryImpl extends NodeFactory {
} catch (DefinitionsProviderException e) {
throw new IllegalStateException(e);
}
*/
// create a system contribution to hold the definitions. The contribution
// will be extended later with definitions from application contributions

View file

@ -83,7 +83,7 @@ public class NodeImpl implements Node, Client {
this.proxyFactory = manager.proxyFactory;
this.compositeActivator =
manager.extensionPoints.getExtensionPoint(UtilityExtensionPoint.class).getUtility(CompositeActivator.class,
true);
this);
try {
// get the top level composite for this node
compositeActivator.setDomainComposite(manager.configureNode(configuration));
@ -151,6 +151,7 @@ public class NodeImpl implements Node, Client {
} // end if
manager.removeNode(configuration);
manager.extensionPoints.getExtensionPoint(UtilityExtensionPoint.class).removeUtility(compositeActivator);
this.compositeActivator = null;
this.proxyFactory = null;