darrell 02/05/07 21:10:27
Modified: antlib/src/java/org/apache/antlib/project TypeLibTask.java
antlib/src/java/org/apache/antlib/runtime Import.java
container/src/java/org/apache/myrmidon/components/deployer
CompoundTypeDeployer.java
ConverterTypeDeployer.java
DefaultTypeLibraryDeployer.java
container/src/java/org/apache/myrmidon/components/embeddor
DefaultEmbeddor.java
container/src/java/org/apache/myrmidon/components/type
DefaultTypeManager.java MultiSourceTypeFactory.java
Resources.properties
container/src/java/org/apache/myrmidon/interfaces/deployer
ConverterDefinition.java DefaultTypeDeployer.java
TypeDeployer.java TypeLibraryDeployer.java
container/src/java/org/apache/myrmidon/interfaces/type
TypeManager.java
container/src/test/org/apache/myrmidon/components/deployer/test
DefaultDeployerTestCase.java
framework/src/java/org/apache/myrmidon/framework
AbstractTypeDef.java DataTypeDeployer.java
myrmidon project.xml
Added: antlib/src/java/org/apache/antlib/selftest LogTask2.java
LogTask3.java
container/src/java/org/apache/myrmidon/components/type
NamespaceAwareTypeFactory.java TypeName.java
container/src/test/org/apache/myrmidon/components/type/test
DefaultTypeManagerTestCase.java TestRole.java
TestType1.java TestType2.java
myrmidon/src/samples namespace-test.ant
Log:
Initial cut of adding Namespace support for type name resolution.
* Types can now be registered with the TypeManager under a namespace.
If a qualified name is when looking up a type, only the specified
namespace is searched for the type name requested.
If an unqualified name is used, all namespaces are searced.
(myrmidon/src/samples/namespace-test.ant provides examples).
* TypeLibDeployer now provides methods for deploying a TypeLib
under a namespace.
* TypeLib, Import and TaskDef tasks all support an optional
"namespace" parameter.
* Added a set of tests for DefaultTypeManager.
Revision Changes Path
1.3 +8 -2
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/project/TypeLibTask.java
Index: TypeLibTask.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/project/TypeLibTask.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TypeLibTask.java 3 May 2002 06:56:10 -0000 1.2
+++ TypeLibTask.java 8 May 2002 04:10:26 -0000 1.3
@@ -29,6 +29,7 @@
ResourceManager.getPackageResources( TypeLibTask.class );
private String m_library;
+ private String m_namespace;
private String m_name;
private String m_role;
@@ -37,6 +38,11 @@
m_library = library;
}
+ public void setNamespace( final String namespace )
+ {
+ m_namespace = namespace;
+ }
+
public void setName( final String name )
{
m_name = name;
@@ -104,12 +110,12 @@
if( null == m_role )
{
// Deploy everything in the typelib
- typeDeployer.deployAll();
+ typeDeployer.deployAll( m_namespace );
}
else
{
// Deploy the specified type
- typeDeployer.deployType( m_role, m_name );
+ typeDeployer.deployType( m_role, m_namespace, m_name );
}
}
catch( final Exception de )
1.4 +7 -1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/runtime/Import.java
Index: Import.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/runtime/Import.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Import.java 3 May 2002 06:56:10 -0000 1.3
+++ Import.java 8 May 2002 04:10:26 -0000 1.4
@@ -30,12 +30,18 @@
ResourceManager.getPackageResources( Import.class );
private File m_lib;
+ private String m_namespace;
public void setLib( final File lib )
{
m_lib = lib;
}
+ public void setNamespace( final String namespace )
+ {
+ m_namespace = namespace;
+ }
+
public void execute()
throws TaskException
{
@@ -51,7 +57,7 @@
final Library library = libraryManager.createLibrary( new File[]
{ m_lib } );
final Deployer deployer = (Deployer)getService( Deployer.class );
final TypeLibraryDeployer typeDeployer =
deployer.createDeployer( library );
- typeDeployer.deployAll();
+ typeDeployer.deployAll( m_namespace );
}
catch( final Exception e )
{
1.1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/selftest/LogTask2.java
Index: LogTask2.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.selftest;
import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.framework.LogLevel;
/**
* An override version of the "log" task, to test namespace resolution.
* This is a copy of org.apache.antlib.core.Log, with minor alterations,
* which allows a prefixed message to be logged.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @ant.task name="log"
*/
public class LogTask2
extends AbstractTask
{
/**
* The prefix which is prepended to all log messages.
*/
public static final String m_prefix = "[Log2]";
/**
* The message to printout when logging
*/
private String m_message;
/**
* The level at which to print out messages.
*/
private LogLevel m_level = LogLevel.INFO;
/**
* Set the level at which the message will be logged.
*
* @param level the level at which message will be logged
*/
public void setLevel( final LogLevel level )
{
m_level = level;
}
/**
* Set the message to print out when logging message
*/
public void setMessage( final String message )
{
checkNullMessage();
m_message = message;
}
/**
* Set the message to print out when logging message
*/
public void addContent( final String message )
{
checkNullMessage();
m_message = message;
}
/**
* Log message at specified level.
*/
public void execute()
throws TaskException
{
String modifiedMessage = getOverrideMessage( m_message );
LogLevel.log( getContext(), m_level, modifiedMessage );
}
/**
* Utility message to verify that the message has not already been set.
*/
private void checkNullMessage()
{
if( null != m_message )
{
final String message = "Message can only be set once by " +
"either nested content or the message attribute";
throw new IllegalStateException( message );
}
}
/**
* Method for creating the special log message, which differentiates
* this task from the core Log task.
* @param message
* @return
*/
protected String getOverrideMessage( final String message )
{
return m_prefix + message;
}
}
1.1
jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/selftest/LogTask3.java
Index: LogTask3.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.selftest;
/**
* A version of Log task, which is used to test namespace resolution.
* This task is not declared as part of the antlib (no xdoclet tag),
* so it can be expicitly type-def'd.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:26 $
*/
public class LogTask3
extends LogTask2
{
/**
* The prefix which is prepended to all log messages.
*/
public static final String m_prefix = "[Log3]: ";
/**
* @see LogTask2#getOverrideMessage( String )
*/
protected String getOverrideMessage( String message )
{
return m_prefix + message;
}
}
1.4 +4 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/CompoundTypeDeployer.java
Index: CompoundTypeDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/CompoundTypeDeployer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CompoundTypeDeployer.java 26 Apr 2002 03:22:24 -0000 1.3
+++ CompoundTypeDeployer.java 8 May 2002 04:10:26 -0000 1.4
@@ -17,7 +17,7 @@
* A type deployer that delegates to a type deployer per role.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.3 $ $Date: 2002/04/26 03:22:24 $
+ * @version $Revision: 1.4 $ $Date: 2002/05/08 04:10:26 $
*/
class CompoundTypeDeployer
implements TypeDeployer
@@ -41,7 +41,8 @@
/**
* Deploys a type.
*/
- public void deployType( final TypeDefinition typeDefinition,
+ public void deployType( final String namespace,
+ final TypeDefinition typeDefinition,
final TypeFactory typeFactory )
throws Exception
{
@@ -52,6 +53,6 @@
{
deployer = m_defaultDeployer;
}
- deployer.deployType( typeDefinition, typeFactory );
+ deployer.deployType( namespace, typeDefinition, typeFactory );
}
}
1.4 +4 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/ConverterTypeDeployer.java
Index: ConverterTypeDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/ConverterTypeDeployer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ConverterTypeDeployer.java 26 Apr 2002 03:22:24 -0000 1.3
+++ ConverterTypeDeployer.java 8 May 2002 04:10:26 -0000 1.4
@@ -24,7 +24,7 @@
* manager and the converter registry.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.3 $ $Date: 2002/04/26 03:22:24 $
+ * @version $Revision: 1.4 $ $Date: 2002/05/08 04:10:26 $
*/
class ConverterTypeDeployer
extends DefaultTypeDeployer
@@ -45,7 +45,8 @@
/**
* Deploys a type.
*/
- public void deployType( final TypeDefinition typeDefinition,
+ public void deployType( final String namespace,
+ final TypeDefinition typeDefinition,
final TypeFactory typeFactory )
throws Exception
{
@@ -68,6 +69,6 @@
m_converterRegistry.registerConverter( name, source, destination );
// Register the converter as a type
- super.deployType( typeDefinition, typeFactory );
+ super.deployType( namespace, typeDefinition, typeFactory );
}
}
1.5 +28 -8
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/DefaultTypeLibraryDeployer.java
Index: DefaultTypeLibraryDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/deployer/DefaultTypeLibraryDeployer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DefaultTypeLibraryDeployer.java 3 May 2002 06:56:10 -0000 1.4
+++ DefaultTypeLibraryDeployer.java 8 May 2002 04:10:26 -0000 1.5
@@ -32,7 +32,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.4 $ $Date: 2002/05/03 06:56:10 $
+ * @version $Revision: 1.5 $ $Date: 2002/05/08 04:10:26 $
*/
class DefaultTypeLibraryDeployer
extends AbstractLogEnabled
@@ -137,11 +137,17 @@
public void deployAll()
throws DeploymentException
{
+ deployAll( null );
+ }
+
+ public void deployAll( String namespace )
+ throws DeploymentException
+ {
// Deploy types
for( int i = 0; i < m_descriptors.length; i++ )
{
TypeDescriptor descriptor = m_descriptors[ i ];
- deployTypes( descriptor );
+ deployTypes( namespace, descriptor );
}
}
@@ -151,6 +157,12 @@
public void deployType( final String roleName, final String typeName )
throws DeploymentException
{
+ deployType( roleName, null, typeName );
+ }
+
+ public void deployType( String roleName, String namespace, String
typeName )
+ throws DeploymentException
+ {
try
{
// Locate the definition for the type
@@ -167,7 +179,7 @@
// Found the definition - deploy it. Note that we
// keep looking for matching types, and let the
deployer
// deal with duplicates
- doDeployType( definition );
+ doDeployType( namespace, definition );
}
}
}
@@ -185,9 +197,15 @@
public void deployType( final TypeDefinition typeDef )
throws DeploymentException
{
+ deployType( null, typeDef );
+ }
+
+ public void deployType( String namespace, TypeDefinition typeDef )
+ throws DeploymentException
+ {
try
{
- doDeployType( typeDef );
+ doDeployType( namespace, typeDef );
}
catch( Exception e )
{
@@ -200,7 +218,8 @@
/**
* Deploys a type.
*/
- private void doDeployType( final TypeDefinition typeDef ) throws
Exception
+ private void doDeployType( final String namespace, final TypeDefinition
typeDef )
+ throws Exception
{
// Validate the type definition
final String typeName = typeDef.getName();
@@ -227,7 +246,7 @@
factory.addNameClassMapping( typeName, className );
// Deploy
- m_typeDeployer.deployType( typeDef, factory );
+ m_typeDeployer.deployType( namespace, typeDef, factory );
if( getLogger().isDebugEnabled() )
{
@@ -352,7 +371,8 @@
/**
* Deploys all types from a typelib descriptor.
*/
- private void deployTypes( final TypeDescriptor descriptor )
+ private void deployTypes( final String namespace,
+ final TypeDescriptor descriptor )
throws DeploymentException
{
try
@@ -369,7 +389,7 @@
for( int i = 0; i < definitions.length; i++ )
{
final TypeDefinition definition = definitions[ i ];
- doDeployType( definition );
+ doDeployType( namespace, definition );
}
}
catch( final Exception e )
1.60 +10 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java
Index: DefaultEmbeddor.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/embeddor/DefaultEmbeddor.java,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -r1.59 -r1.60
--- DefaultEmbeddor.java 3 May 2002 09:26:13 -0000 1.59
+++ DefaultEmbeddor.java 8 May 2002 04:10:26 -0000 1.60
@@ -64,7 +64,7 @@
* Instantiate this to embed inside other applications.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.59 $ $Date: 2002/05/03 09:26:13 $
+ * @version $Revision: 1.60 $ $Date: 2002/05/08 04:10:26 $
*/
public class DefaultEmbeddor
extends AbstractLogEnabled
@@ -76,6 +76,12 @@
/** Package containing the default component implementations. */
private static final String PREFIX = "org.apache.myrmidon.components.";
+ /** Namespace for the container types */
+ private static final String CONTAINER_TYPE_NAMESPACE = "myrmidon";
+
+ /** Namespace for the core antlibs */
+ private static final String CORE_TYPE_NAMESPACE = "ant";
+
private Deployer m_deployer;
private TypeManager m_typeManager;
private LibraryManager m_libraryManager;
@@ -215,15 +221,16 @@
final ClassLoader containerClassLoader = getClass().getClassLoader();
final Library containerLib = m_libraryManager.createLibrary(
containerClassLoader );
final TypeLibraryDeployer containerTypeDeployer =
m_deployer.createDeployer( containerLib );
- containerTypeDeployer.deployAll();
+ containerTypeDeployer.deployAll( CONTAINER_TYPE_NAMESPACE );
// Deploy all core type libraries in the lib directory
+ // TODO: Deploy these under library name, instead of "core"?
final Library[] coreLibs =
m_typeLibraryManager.getCoreTypeLibraries();
for( int i = 0; i < coreLibs.length; i++ )
{
final Library library = coreLibs[ i ];
final TypeLibraryDeployer libDeployer =
m_deployer.createDeployer( library );
- libDeployer.deployAll();
+ libDeployer.deployAll( CORE_TYPE_NAMESPACE );
}
}
1.21 +47 -32
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/DefaultTypeManager.java
Index: DefaultTypeManager.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/DefaultTypeManager.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- DefaultTypeManager.java 24 Apr 2002 02:21:00 -0000 1.20
+++ DefaultTypeManager.java 8 May 2002 04:10:26 -0000 1.21
@@ -23,7 +23,8 @@
* The interface that is used to manage types.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.20 $ $Date: 2002/04/24 02:21:00 $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
+ * @version $Revision: 1.21 $ $Date: 2002/05/08 04:10:26 $
*/
public class DefaultTypeManager
implements TypeManager, Serviceable
@@ -34,7 +35,7 @@
///Parent type manager to inherit values from.
private final DefaultTypeManager m_parent;
- ///Maps role Class to MultiSourceTypeFactory.
+ ///Maps role Class to NamespaceAwareTypeFactory.
private final HashMap m_roleMap = new HashMap();
private RoleManager m_roleManager;
@@ -44,6 +45,10 @@
this( null );
}
+ /**
+ * Create a chained DefaultTypeManager which wraps a parent TypeManager.
+ * @param parent The parent TypeManager - unknown types are delegated to
the parent.
+ */
private DefaultTypeManager( final DefaultTypeManager parent )
{
m_parent = parent;
@@ -53,37 +58,57 @@
}
}
+ /**
+ * @see Serviceable#service( ServiceManager )
+ */
public void service( final ServiceManager serviceManager )
throws ServiceException
{
m_roleManager = (RoleManager)serviceManager.lookup( RoleManager.ROLE
);
}
+ /**
+ * @see TypeManager#registerType( String, String, TypeFactory )
+ */
+ public void registerType( final String roleName,
+ final String shorthandName,
+ final TypeFactory factory )
+ throws TypeException
+ {
+ final NamespaceAwareTypeFactory nsFactory = createFactory( roleName
);
+ nsFactory.register( shorthandName, factory );
+ }
+
+ /**
+ * @see TypeManager#registerType( String, String, String, TypeFactory )
+ */
public void registerType( final String roleName,
+ final String namespace,
final String shorthandName,
final TypeFactory factory )
throws TypeException
{
- final MultiSourceTypeFactory msFactory = createFactory( roleName );
- msFactory.register( shorthandName, factory );
+ final NamespaceAwareTypeFactory nsFactory = createFactory( roleName
);
+ nsFactory.register( namespace, shorthandName, factory );
}
+ /**
+ * @see TypeManager#getFactory( String )
+ */
public TypeFactory getFactory( final String roleName )
throws TypeException
{
return createFactory( roleName );
}
+ /**
+ * @see TypeManager#createChildTypeManager()
+ */
public TypeManager createChildTypeManager()
{
return new DefaultTypeManager( this );
}
- private final MultiSourceTypeFactory lookupFactory( final String
roleName )
- {
- return (MultiSourceTypeFactory)m_roleMap.get( roleName );
- }
-
/**
* Get a factory of appropriate role.
* Create a Factory if none exists with same name.
@@ -92,24 +117,26 @@
* @return the Factory for interface
* @exception TypeException role does not specify accessible work
interface
*/
- private MultiSourceTypeFactory createFactory( final String roleName )
+ private NamespaceAwareTypeFactory createFactory( final String roleName )
throws TypeException
{
- MultiSourceTypeFactory factory =
(MultiSourceTypeFactory)m_roleMap.get( roleName );
+ // If a TypeFactory has already been created for this role, return
it.
+ NamespaceAwareTypeFactory factory =
(NamespaceAwareTypeFactory)m_roleMap.get( roleName );
if( null != factory )
{
return factory;
}
- final MultiSourceTypeFactory parentFactory = getParentTypedFactory(
roleName );
- if( null != parentFactory )
- {
- factory = new MultiSourceTypeFactory( parentFactory );
+ // If we have a parent, we need to chain from the parent TypeFactory.
+ // This is OK because DefaultTypeManager hierarchies share a common
RoleManager.
+ if ( m_parent != null )
+ {
+ final NamespaceAwareTypeFactory parentFactory =
+ m_parent.createFactory( roleName );
+ factory = new NamespaceAwareTypeFactory( parentFactory );
}
-
- ///If we haven't got factory try to create a new one
- if( null == factory )
- {
+ // Otherwise, try to create a new one
+ else {
// Lookup the role type
final RoleInfo role = m_roleManager.getRole( roleName );
if( role == null )
@@ -117,23 +144,11 @@
final String message = REZ.getString( "unknown-role.error",
roleName );
throw new TypeException( message );
}
- factory = new MultiSourceTypeFactory(
role.getImplementationClass() );
+ factory = new NamespaceAwareTypeFactory( role );
}
m_roleMap.put( roleName, factory );
return factory;
- }
-
- private MultiSourceTypeFactory getParentTypedFactory( final String
roleName )
- {
- if( null != m_parent )
- {
- return m_parent.lookupFactory( roleName );
- }
- else
- {
- return null;
- }
}
}
1.17 +2 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java
Index: MultiSourceTypeFactory.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- MultiSourceTypeFactory.java 1 Apr 2002 09:56:26 -0000 1.16
+++ MultiSourceTypeFactory.java 8 May 2002 04:10:26 -0000 1.17
@@ -17,9 +17,9 @@
* This factory acts as a proxy to set of object factories.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.16 $ $Date: 2002/04/01 09:56:26 $
+ * @version $Revision: 1.17 $ $Date: 2002/05/08 04:10:26 $
*/
-public class MultiSourceTypeFactory
+class MultiSourceTypeFactory
implements TypeFactory
{
private static final Resources REZ =
1.5 +3 -0
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/Resources.properties
Index: Resources.properties
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/Resources.properties,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Resources.properties 15 Mar 2002 02:48:20 -0000 1.4
+++ Resources.properties 8 May 2002 04:10:26 -0000 1.5
@@ -7,3 +7,6 @@
no-factory.error=Failed to locate factory for {0}.
mismatched-type.error=Factory for type {0} created an object of incompatible
type {1}.
no-work-interface.error=Role {0} does not specify accessible work interface.
+
+# NamespaceAwareTypeFactory
+invalid-type-name.error=Invalid type name '{0}'. Type names may not contain
the namespace separator character '{1}'.
1.1
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/NamespaceAwareTypeFactory.java
Index: NamespaceAwareTypeFactory.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.interfaces.role.RoleInfo;
import org.apache.myrmidon.interfaces.type.TypeException;
import org.apache.myrmidon.interfaces.type.TypeFactory;
import org.apache.myrmidon.interfaces.type.TypeManager;
/**
* A type factory which registers types under a name and namespace, and allows
* lookup of types under both fully-qualified names and short-names.
* When looking up a type based on an unqualified name, all namespaces are
* searched for a type matching the type name.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:26 $
*/
class NamespaceAwareTypeFactory
implements TypeFactory
{
private static final Resources REZ =
ResourceManager.getPackageResources( NamespaceAwareTypeFactory.class
);
/**
* When no namespace is specified, types are registered under the default
namespace.
* Since this is an empty string, it is not possible to lookup these
types with a
* fully qualified name.
*/
private static final String DEFAULT_NAMESPACE = new String( "" );
private RoleInfo m_role;
private NamespaceAwareTypeFactory m_parent;
private Map m_namespaceFactories = new HashMap();
/**
* A cache which stores a map of TypeName -> TypeFactory instance.
* For simplicity, the cache is cleared whenever a new type is registered.
*/
private Map m_typeFactoryCache = new HashMap();
/**
* Create a empty type factory for the specified role.
* @param role The role for types provided by this TypeFactory.
*/
NamespaceAwareTypeFactory( RoleInfo role )
{
m_parent = null;
m_role = role;
}
/**
* Create a nested type factory, which first searches locally registered
types,
* before delegating lookups to the parent.
* @param parent The parent type factory.
*/
NamespaceAwareTypeFactory( NamespaceAwareTypeFactory parent )
{
m_parent = parent;
m_role = parent.m_role;
}
// public void register( String namespace, TypeRegistry registry )
// {
// m_namespaces.add( namespace );
// m_namespaceFactories.put( namespace, registry.getFactory(
m_role ) );
// }
/**
* Register a named type under the default namespace ("").
* Equivelant to calling <code>register( null, name, factory )</code>
* @param name The name of the type.
* @param factory The factory for instances of this type.
* @throws TypeException If the type name is invalid.
*/
public void register( String name, TypeFactory factory )
throws TypeException
{
register( null, name, factory );
}
/**
* Register a named type under the specified namespace.
* @param namespace The namespace for the type's registration.
* @param name The name of the type.
* @param factory The factory for instances of this type.
* @throws TypeException If the type name is invalid.
*/
public void register( String namespace, String name, TypeFactory factory )
throws TypeException
{
// Type short-names may not contain the namespace separator.
if ( parseName( name ).hasNamespace() )
{
String message = REZ.getString( "invalid-type-name.error", name,
String.valueOf(
TypeManager.NAMESPACE_SEPARATOR ) );
throw new TypeException( message );
}
// Use the default namespace if none provided.
if ( namespace == null )
{
namespace = DEFAULT_NAMESPACE;
}
// Get a multi-source factory for this namespace/role,
// creating and adding to the namespace -> factory map if necessary.
MultiSourceTypeFactory namespaceFactory =
(MultiSourceTypeFactory)m_namespaceFactories.get( namespace );
if( namespaceFactory == null )
{
namespaceFactory = new MultiSourceTypeFactory(
m_role.getImplementationClass() );
m_namespaceFactories.put( namespace, namespaceFactory );
}
// Register the type with the namespace-specific multisource factory.
namespaceFactory.register( name, factory );
// Clear the cache of type factories.
m_typeFactoryCache.clear();
}
/**
* @param name Can be either a fully qualified name with namespace,
* or a simple type name.
* @see TypeFactory#canCreate( String )
*/
public boolean canCreate( String name )
{
TypeName qname = parseName( name );
TypeFactory factory = findFactory( qname );
if( factory != null )
{
return true;
}
else
{
return false;
}
}
/**
* @param name Can be either a fully qualified name with namespace,
* or a simple type name.
* @see TypeFactory#create( String )
*/
public Object create( String name )
throws TypeException
{
TypeName qname = parseName( name );
TypeFactory factory = findFactory( qname );
if( factory != null )
{
return factory.create( qname.getShortName() );
}
else
{
final String message = REZ.getString( "no-factory.error", name );
throw new TypeException( message );
}
}
/**
* Find the factory for the types, first looking in locally registered
* types, and then delegating to the parent.
*
* @param qname The qualified TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
*/
private TypeFactory findFactory( TypeName qname )
{
TypeFactory factory = findLocalFactoryUseCache( qname );
if ( factory == null && m_parent != null )
{
factory = m_parent.findFactory( qname );
}
return factory;
}
/**
* Looks for a TypeFactory in the cache, if present, otherwise creates
* the factory and adds it to the cache.
* @param qname The qualified TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
*/
private TypeFactory findLocalFactoryUseCache( TypeName qname )
{
if ( m_typeFactoryCache.containsKey( qname ) )
{
return (TypeFactory)m_typeFactoryCache.get( qname );
}
else
{
TypeFactory factory = findLocalFactory( qname );
m_typeFactoryCache.put( qname, factory );
return factory;
}
}
/**
* Finds the specific TypeFactory which can create an instance of the type
* specified by a TypeName.
* If the TypeName contains a namespace, only the factory for that
namespace
* is searched, otherwise all namespaces are searched.
*
* @param qname The TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
*/
private TypeFactory findLocalFactory( TypeName qname )
{
TypeFactory factory;
if( qname.hasNamespace() )
{
String namespace = qname.getNamespace();
factory = (TypeFactory)m_namespaceFactories.get( namespace );
if( factory != null && factory.canCreate( qname.getShortName() ) )
{
return factory;
}
}
else
{
// Search all namespaces
// TODO: make sure that this lookup is not ambiguous.
Iterator factories = m_namespaceFactories.values().iterator();
while( factories.hasNext() )
{
factory = (TypeFactory)factories.next();
if( factory.canCreate( qname.getShortName() ) )
{
return factory;
}
}
}
return null;
}
protected TypeName parseName( String name )
{
return new TypeName( name );
}
}
1.1
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/TypeName.java
Index: TypeName.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type;
import org.apache.myrmidon.interfaces.type.TypeManager;
/**
* A simple class which breaks a fully-qualified string name into
* it's namespace and type-name components.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:26 $
*/
final class TypeName
{
private String m_fullName;
private String m_shortName;
private String m_namespace;
/**
* Create a TypeName with the supplied string name.
* @param typeName The name of the type, which may or may not
* contain a namespace prefix.
*/
TypeName( final String typeName )
{
if ( typeName == null )
{
throw new NullPointerException( "typeName" );
}
m_fullName = typeName;
int pos = typeName.indexOf( TypeManager.NAMESPACE_SEPARATOR );
if( pos == -1 )
{
m_shortName = typeName;
m_namespace = null;
}
else
{
m_shortName = typeName.substring( pos + 1 );
m_namespace = typeName.substring( 0, pos );
}
}
/**
* @return The shortname for the type.
*/
String getShortName()
{
return m_shortName;
}
/**
* @return The namespace for the type.
*/
String getNamespace()
{
return m_namespace;
}
/**
* @return <code>true</code> if the TypeName has a namespace specified.
*/
boolean hasNamespace()
{
return ( m_namespace != null );
}
public int hashCode()
{
return m_fullName.hashCode();
}
public boolean equals( Object o )
{
if ( o instanceof TypeName )
{
String otherFullName = ((TypeName)o).m_fullName;
return ( otherFullName.equals( m_fullName ) );
}
return false;
}
}
1.6 +8 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/ConverterDefinition.java
Index: ConverterDefinition.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/ConverterDefinition.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ConverterDefinition.java 24 Apr 2002 02:21:00 -0000 1.5
+++ ConverterDefinition.java 8 May 2002 04:10:27 -0000 1.6
@@ -8,12 +8,13 @@
package org.apache.myrmidon.interfaces.deployer;
import org.apache.aut.converter.Converter;
+import org.apache.myrmidon.interfaces.type.TypeManager;
/**
* A specialised TypeDefinition which defines a converter.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.5 $ $Date: 2002/04/24 02:21:00 $
+ * @version $Revision: 1.6 $ $Date: 2002/05/08 04:10:27 $
*/
public class ConverterDefinition
extends TypeDefinition
@@ -31,7 +32,12 @@
final String sourceType,
final String destinationType )
{
- super( className, Converter.ROLE, className );
+ // Hack the className to make sure that the type name doesn't
+ // contain a namespace character.
+ // TODO: Once the TypeDeployer is namespace-aware,
+ // this shouldn't be necessary?
+ super( className.replace( TypeManager.NAMESPACE_SEPARATOR, '_' ),
+ Converter.ROLE, className );
m_sourceType = sourceType;
m_destinationType = destinationType;
}
1.2 +4 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/DefaultTypeDeployer.java
Index: DefaultTypeDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/DefaultTypeDeployer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultTypeDeployer.java 25 Apr 2002 09:34:45 -0000 1.1
+++ DefaultTypeDeployer.java 8 May 2002 04:10:27 -0000 1.2
@@ -19,7 +19,7 @@
* manager.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.1 $ $Date: 2002/04/25 09:34:45 $
+ * @version $Revision: 1.2 $ $Date: 2002/05/08 04:10:27 $
*/
public class DefaultTypeDeployer
implements TypeDeployer, Serviceable
@@ -40,12 +40,13 @@
/**
* Deploys a type.
*/
- public void deployType( final TypeDefinition typeDefinition,
+ public void deployType( final String namespace,
+ final TypeDefinition typeDefinition,
final TypeFactory typeFactory )
throws Exception
{
final String roleName = typeDefinition.getRole();
final String typeName = typeDefinition.getName();
- m_typeManager.registerType( roleName, typeName, typeFactory );
+ m_typeManager.registerType( roleName, namespace, typeName,
typeFactory );
}
}
1.7 +4 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeDeployer.java
Index: TypeDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeDeployer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TypeDeployer.java 25 Apr 2002 09:34:45 -0000 1.6
+++ TypeDeployer.java 8 May 2002 04:10:27 -0000 1.7
@@ -14,7 +14,7 @@
* Deploys the types of a particular role.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.6 $ $Date: 2002/04/25 09:34:45 $
+ * @version $Revision: 1.7 $ $Date: 2002/05/08 04:10:27 $
*/
public interface TypeDeployer
{
@@ -25,6 +25,8 @@
* @param typeFactory The factory to use for instantiating the type.
* @throws java.lang.Exception On error.
*/
- void deployType( TypeDefinition typeDefinition, TypeFactory typeFactory )
+ void deployType( String namespace,
+ TypeDefinition typeDefinition,
+ TypeFactory typeFactory )
throws Exception;
}
1.2 +46 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeLibraryDeployer.java
Index: TypeLibraryDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/deployer/TypeLibraryDeployer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TypeLibraryDeployer.java 25 Apr 2002 01:41:50 -0000 1.1
+++ TypeLibraryDeployer.java 8 May 2002 04:10:27 -0000 1.2
@@ -12,12 +12,13 @@
* library to be deployed.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.1 $ $Date: 2002/04/25 01:41:50 $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
+ * @version $Revision: 1.2 $ $Date: 2002/05/08 04:10:27 $
*/
public interface TypeLibraryDeployer
{
/**
- * Deploys everything in the type library.
+ * Deploys everything in the type library, using an empty namespace.
* @throws DeploymentException
* If the library cannot be deployed.
*/
@@ -25,6 +26,15 @@
throws DeploymentException;
/**
+ * Deploys everything in the type library under the specified namespace.
+ * @param namespace The namespace under which the types should be
deployed.
+ * @throws DeploymentException
+ * If the library cannot be deployed.
+ */
+ void deployAll( String namespace )
+ throws DeploymentException;
+
+ /**
* Deploys a single type from the type library. The type definition is
* read from the type library descriptor.
*
@@ -41,6 +51,25 @@
throws DeploymentException;
/**
+ * Deploys a single type from the type library under the specified
namespace.
+ * The type definition is read from the type library descriptor.
+ *
+ * @param roleName
+ * The role name.
+ *
+ * @param namespace
+ * The namespace to deploy the type under.
+ *
+ * @param typeName
+ * The type name.
+ *
+ * @throws DeploymentException
+ * If the type cannot be deployed.
+ */
+ void deployType( String roleName, String namespace, String typeName )
+ throws DeploymentException;
+
+ /**
* Deploys a single type from the type library.
*
* @param typeDef
@@ -51,4 +80,19 @@
*/
void deployType( TypeDefinition typeDef )
throws DeploymentException;
+
+ /**
+ * Deploys a single type from the type library, under the specified
namespace.
+ *
+ * @param typeDef
+ * The type definition.
+ *
+ * @param namespace
+ * The namespace to deploy the type under.
+ *
+ * @throws DeploymentException
+ * If the type cannot be deployed.
+ */
+ void deployType( String namespace, TypeDefinition typeDef )
+ throws DeploymentException;
}
1.11 +20 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeManager.java
Index: TypeManager.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/type/TypeManager.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- TypeManager.java 1 Apr 2002 09:56:28 -0000 1.10
+++ TypeManager.java 8 May 2002 04:10:27 -0000 1.11
@@ -11,7 +11,8 @@
* The interface that is used to manage types.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.10 $ $Date: 2002/04/01 09:56:28 $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
+ * @version $Revision: 1.11 $ $Date: 2002/05/08 04:10:27 $
*/
public interface TypeManager
{
@@ -19,7 +20,12 @@
String ROLE = TypeManager.class.getName();
/**
- * Registers a new type.
+ * Character used to separate namespace from type name.
+ */
+ char NAMESPACE_SEPARATOR = '.';
+
+ /**
+ * Registers a new type, with no namespace specified.
*
* @param roleName The role for the type.
* @param shorthandName The shorthand name for the type.
@@ -27,6 +33,18 @@
* @throws TypeException If an error occurs.
*/
void registerType( String roleName, String shorthandName, TypeFactory
factory )
+ throws TypeException;
+
+ /**
+ * Registers a new type under the specified namespace.
+ * @param roleName The role for the type.
+ * @param namespace The namespace to register the type under
+ * @param shorthandName The shorthand name for the type.
+ * @param factory The type factory for instances of this type.
+ * @throws TypeException If an error occurs.
+ */
+ void registerType( String roleName, String namespace,
+ String shorthandName, TypeFactory factory )
throws TypeException;
/**
1.7 +29 -0
jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java
Index: DefaultDeployerTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/deployer/test/DefaultDeployerTestCase.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DefaultDeployerTestCase.java 3 May 2002 06:56:11 -0000 1.6
+++ DefaultDeployerTestCase.java 8 May 2002 04:10:27 -0000 1.7
@@ -21,6 +21,7 @@
import org.apache.myrmidon.interfaces.library.LibraryManager;
import org.apache.myrmidon.interfaces.type.TypeException;
import org.apache.myrmidon.interfaces.type.TypeFactory;
+import org.apache.myrmidon.interfaces.type.TypeManager;
/**
* Test cases for the default deployer.
@@ -32,6 +33,8 @@
{
private static final String TEST_TYPE1_NAME = "test-type1";
+ private static final String TEST_NAMESPACE = "test-namespace";
+
private Deployer m_deployer;
private Converter m_converter;
@@ -149,6 +152,32 @@
// Make sure the test types have been deployed
assertTypesRegistered();
+ }
+
+ /**
+ * Tests deployment of all types from a typelib descriptor under
+ * a specified namespace.
+ */
+ public void testDeployUnderNamespace() throws Exception
+ {
+ final TypeLibraryDeployer typeDeployer = getTestDeployer();
+
+ // Make sure the test types have not been deployed. Do this after
+ // the deployer has been created
+ assertTypesNotRegistered();
+
+ // Deploy all the types from the descriptor
+ typeDeployer.deployAll( TEST_NAMESPACE );
+
+ // Make sure the test types can be accessed without specifying a
namespace.
+ assertTypesRegistered();
+
+ // Check that the Type can be accessed with a namespace qualified
name.
+ String qname = TEST_NAMESPACE + TypeManager.NAMESPACE_SEPARATOR +
TEST_TYPE1_NAME;
+ final TypeFactory typeFactory = getTypeManager().getFactory(
TestDataType.ROLE );
+ final Object result = typeFactory.create( qname );
+ assertTrue( result instanceof TestType1 );
+
}
/**
1.1
jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/DefaultTypeManagerTestCase.java
Index: DefaultTypeManagerTestCase.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type.test;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.myrmidon.components.AbstractComponentTest;
import org.apache.myrmidon.components.type.DefaultTypeManager;
import org.apache.myrmidon.interfaces.type.DefaultTypeFactory;
import org.apache.myrmidon.interfaces.type.TypeException;
import org.apache.myrmidon.interfaces.type.TypeFactory;
import org.apache.myrmidon.interfaces.type.TypeManager;
import org.apache.myrmidon.interfaces.role.RoleInfo;
/**
* Test cases for the DefaultTypeManager
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:27 $
*/
public class DefaultTypeManagerTestCase
extends AbstractComponentTest
{
private static final Resources REZ =
getResourcesForTested( DefaultTypeManagerTestCase.class );
private static final String UNKNOWN_ROLE = "unknown-role";
private static final String UNKNOWN_TYPE = "unknown-type";
private static final String TEST_NAMESPACE = "test-namespace";
private static final String TEST_ROLE = TestRole.ROLE;
private static final String TYPE_NAME1 = "test-type1";
private static final String TYPE_NAME2 = "test-type2";
private static final Class TYPE_CLASS1 = TestType1.class;
private static final Class TYPE_CLASS2 = TestType2.class;
private DefaultTypeManager m_typeManager;
public DefaultTypeManagerTestCase( String name )
{
super( name );
}
protected void setUp() throws Exception
{
super.setUp();
registerRole( new RoleInfo( TestRole.ROLE, TestRole.class ) );
m_typeManager = new DefaultTypeManager();
m_typeManager.service( getServiceManager() );
}
public void testRegister() throws Exception
{
// Make sure it's not there to start with.
assertCantCreate( m_typeManager, TEST_ROLE, TYPE_NAME1 );
// Register a type with no namespace
registerType( m_typeManager, TEST_ROLE, null, TYPE_NAME1, TYPE_CLASS1
);
// Lookup the type with the shortname
assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, TYPE_CLASS1 );
}
public void testRegisterNS() throws Exception
{
String qname = makeQName( TEST_NAMESPACE, TYPE_NAME1 );
// Check pre-registration.
assertCantCreate( m_typeManager, TEST_ROLE, TYPE_NAME1 );
assertCantCreate( m_typeManager, TEST_ROLE, qname );
// Register a type under a namespace
registerType( m_typeManager, TEST_ROLE, TEST_NAMESPACE, TYPE_NAME1,
TYPE_CLASS1 );
// Lookup the type with the shortName and qname.
assertCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, TYPE_CLASS1 );
assertCreate( m_typeManager, TEST_ROLE, qname, TYPE_CLASS1 );
}
public void testLookupUnknown() throws Exception
{
// Unknown role.
try
{
m_typeManager.getFactory( UNKNOWN_ROLE );
fail( "Lookup unknown role should fail." );
}
catch( TypeException te )
{
final String message = REZ.getString( "unknown-role.error",
UNKNOWN_ROLE );
assertSameMessage( message, te );
}
// Unknown type name for known role.
assertCantCreate( m_typeManager, TEST_ROLE, UNKNOWN_TYPE );
}
public void testLookupUnknownQName() throws Exception
{
// Unknown qualified type name for known role.
String qname = makeQName( TEST_NAMESPACE, UNKNOWN_TYPE );
assertCantCreate( m_typeManager, TEST_ROLE, qname );
// Register Type1 with no namespace,
// and try to look it up with a namespace - can't be found.
qname = makeQName( TEST_NAMESPACE, TYPE_NAME1 );
registerType( m_typeManager, TEST_ROLE, null, TYPE_NAME1, TYPE_CLASS1
);
assertCantCreate( m_typeManager, TEST_ROLE, qname );
// Register Type2 with a namespace,
// and try to look it up with a different namespace - cant' be found.
qname = makeQName( TEST_NAMESPACE, TYPE_NAME2 );
registerType( m_typeManager, TEST_ROLE, "another-namespace",
TYPE_NAME2, TYPE_CLASS2 );
assertCantCreate( m_typeManager, TEST_ROLE, qname );
}
public void testNamespaceSeparation() throws Exception
{
// Register Type1 and Type2 with same name under different namespaces.
String shortName = "my-type";
String ns1 = "ns1";
String ns2 = "ns2";
registerType( m_typeManager, TEST_ROLE, ns1, shortName, TYPE_CLASS1 );
registerType( m_typeManager, TEST_ROLE, ns2, shortName, TYPE_CLASS2 );
String qname1 = makeQName( ns1, shortName);
String qname2 = makeQName( ns2, shortName);
assertCreate( m_typeManager, TEST_ROLE, qname1, TYPE_CLASS1 );
assertCreate( m_typeManager, TEST_ROLE, qname2, TYPE_CLASS2 );
// TODO; implement this functionality.
/*
// Type names are ambiguous, should fail on lookup.
TypeFactory factory = m_typeManager.getFactory( KNOWN_ROLE );
assertTrue( ! factory.canCreate( shortName ) );
try
{
factory.create( shortName );
fail();
}
catch( TypeException e )
{
// Check exception.
}
*/
}
public void testChildIndependence() throws Exception
{
DefaultTypeManager parent = m_typeManager;
DefaultTypeManager child1 = createChild( parent );
DefaultTypeManager child2 = createChild( parent );
final String typeName = "test-type";
// Check pre-registration
assertCantCreate( child1, TEST_ROLE, typeName );
assertCantCreate( child2, TEST_ROLE, typeName );
// Register TYPE1 with child1
registerType( child1, TEST_ROLE, null, typeName, TYPE_CLASS1 );
assertCreate( child1, TEST_ROLE, typeName, TYPE_CLASS1 );
assertCantCreate( child2, TEST_ROLE, typeName );
registerType( child2, TEST_ROLE, null, typeName, TYPE_CLASS2 );
assertCreate( child1, TEST_ROLE, typeName, TYPE_CLASS1 );
assertCreate( child2, TEST_ROLE, typeName, TYPE_CLASS2 );
}
public void testInheritance() throws Exception
{
DefaultTypeManager parent = m_typeManager;
DefaultTypeManager child = createChild( parent );
assertCantCreate( child, TEST_ROLE, TYPE_NAME1 );
assertCantCreate( child, TEST_ROLE, TYPE_NAME2 );
// Register name1 with parent -> type1 and
// check that the child inherits the type.
String typeName = TYPE_NAME1;
registerType( parent, TEST_ROLE, null, typeName, TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS1 );
// Now register name1 with child -> type2 and make sure
// that parent registration is overridden.
registerType( child, TEST_ROLE, null, typeName, TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS2 );
// Register name2 with child first, then parent, and make
// sure that parent doesn't override child if set after.
typeName = TYPE_NAME2;
registerType( child, TEST_ROLE, null, typeName, TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS1 );
registerType( parent, TEST_ROLE, null, typeName, TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS1 );
}
public void testInheritanceQName() throws Exception
{
DefaultTypeManager parent = m_typeManager;
DefaultTypeManager child = createChild( parent );
assertCantCreate( child, TEST_ROLE, TYPE_NAME1 );
assertCantCreate( child, TEST_ROLE, TYPE_NAME2 );
// Register name1 with parent -> type1 and
// check that the child inherits the type.
String typeName = TYPE_NAME1;
String qname = makeQName( TEST_NAMESPACE, typeName );
registerType( parent, TEST_ROLE, TEST_NAMESPACE, typeName,
TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, qname, TYPE_CLASS1 );
// Now register name1 with child -> type2 and make sure
// that parent registration is overridden.
registerType( child, TEST_ROLE, TEST_NAMESPACE, typeName, TYPE_CLASS2
);
assertCreate( child, TEST_ROLE, qname, TYPE_CLASS2 );
// Register name2 with child first, then parent, and make
// sure that parent doesn't override child if set after.
typeName = TYPE_NAME2;
qname = makeQName( TEST_NAMESPACE, typeName );
registerType( child, TEST_ROLE, TEST_NAMESPACE, typeName, TYPE_CLASS1
);
assertCreate( child, TEST_ROLE, qname, TYPE_CLASS1 );
registerType( parent, TEST_ROLE, TEST_NAMESPACE, typeName,
TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, qname, TYPE_CLASS1 );
}
public void testInheritanceNS() throws Exception
{
DefaultTypeManager parent = m_typeManager;
DefaultTypeManager child = createChild( parent );
final String nsParent = "parent";
final String nsChild = "child";
final String typeName = "type";
final String parentQName = makeQName( nsParent, typeName );
final String childQName = makeQName( nsChild, typeName );
assertCantCreate( child, TEST_ROLE, typeName );
assertCantCreate( child, TEST_ROLE, parentQName );
assertCantCreate( child, TEST_ROLE, childQName );
// Register type1 with the parent, and make sure it's accessible
// in the child, both qualified and not.
registerType( parent, TEST_ROLE, nsParent, typeName, TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, parentQName, TYPE_CLASS1 );
assertCantCreate( child, TEST_ROLE, childQName );
// Now register type2 with the child, under the same name
registerType( child, TEST_ROLE, nsChild, typeName, TYPE_CLASS2 );
// With no namespace, or child namespace,
// should get type2 from child.
// With the parent namespace, should get type1.
assertCreate( child, TEST_ROLE, typeName, TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, childQName, TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, parentQName, TYPE_CLASS1 );
}
public void testDeepInheritance() throws Exception
{
DefaultTypeManager grandparent = m_typeManager;
DefaultTypeManager parent = createChild( grandparent);
DefaultTypeManager child = createChild( parent);
assertCantCreate( child, TEST_ROLE, TYPE_NAME1 );
// Register with grandparent, and check available.
registerType( grandparent, TEST_ROLE, null, TYPE_NAME1, TYPE_CLASS1 );
assertCreate( child, TEST_ROLE, TYPE_NAME1, TYPE_CLASS1 );
// Register with parent, should override grandparent.
registerType( parent, TEST_ROLE, null, TYPE_NAME1, TYPE_CLASS2 );
assertCreate( child, TEST_ROLE, TYPE_NAME1, TYPE_CLASS2 );
}
/**
* Combines the namespace and the typename, separated by the namespace
separator.
*/
private String makeQName( final String namespace, final String typeName )
{
return namespace + TypeManager.NAMESPACE_SEPARATOR + typeName;
}
/**
* Registers a type with the provided TypeManager.
*/
private void registerType( final TypeManager typeManager,
final String roleName,
final String namespace,
final String typeName,
final Class type )
throws TypeException
{
DefaultTypeFactory factory = new DefaultTypeFactory(
type.getClassLoader() );
factory.addNameClassMapping( typeName, type.getName() );
if( namespace == null )
{
typeManager.registerType( roleName, typeName, factory );
}
else
{
typeManager.registerType( roleName, namespace, typeName, factory
);
}
}
/**
* Checks that the named type can be created by the TypeManager provided,
* and that the returned type is of the correct class.
*/
private void assertCreate( final TypeManager typeManager,
final String roleName,
final String typeName,
final Class expectedClass )
throws Exception
{
TypeFactory factory = typeManager.getFactory( roleName );
boolean canCreate = factory.canCreate( typeName );
assertTrue( "Could not create", canCreate );
Object created = factory.create( typeName );
assertTrue( "Wrong class created", expectedClass.isInstance( created
) );
}
/**
* Checks that the named type cannot be created by the TypeManager
provided,
* failing with a "no factory" error.
*/
private void assertCantCreate( final TypeManager typeManager,
final String roleName,
final String typeName )
throws Exception
{
TypeFactory factory = typeManager.getFactory( roleName );
assertTrue( !factory.canCreate( typeName ) );
try
{
factory.create( typeName );
fail();
}
catch( TypeException te )
{
String message = REZ.getString( "no-factory.error", typeName );
assertSameMessage( message, te );
}
}
private DefaultTypeManager createChild( DefaultTypeManager parent )
{
return (DefaultTypeManager)parent.createChildTypeManager();
}
}
1.1
jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/TestRole.java
Index: TestRole.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type.test;
/**
* A role for testing the TypeManager
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:27 $
*/
public interface TestRole
{
String ROLE = "test-role";
}
1.1
jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/TestType1.java
Index: TestType1.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type.test;
/**
* A Type for testing the TypeManager
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:27 $
*/
public class TestType1 implements TestRole
{
}
1.1
jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/TestType2.java
Index: TestType2.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.type.test;
/**
* A Type for testing the TypeManager
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
* @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:27 $
*/
public class TestType2 implements TestRole
{
}
1.4 +13 -2
jakarta-ant-myrmidon/framework/src/java/org/apache/myrmidon/framework/AbstractTypeDef.java
Index: AbstractTypeDef.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/framework/src/java/org/apache/myrmidon/framework/AbstractTypeDef.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractTypeDef.java 3 May 2002 06:56:11 -0000 1.3
+++ AbstractTypeDef.java 8 May 2002 04:10:27 -0000 1.4
@@ -25,7 +25,7 @@
* TODO: Make this support classpath sub-element in future
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.3 $ $Date: 2002/05/03 06:56:11 $
+ * @version $Revision: 1.4 $ $Date: 2002/05/08 04:10:27 $
*/
public abstract class AbstractTypeDef
extends AbstractContainerTask
@@ -35,6 +35,7 @@
private Path m_classpath = new Path();
private String m_classname;
+ private String m_namespace = null;
public void setClassname( final String classname )
{
@@ -57,6 +58,16 @@
}
/**
+ * Sets the namespace under which the type is registered.
+ * This parameter is optional.
+ * @param namespace
+ */
+ public void setNamespace( final String namespace )
+ {
+ m_namespace = namespace;
+ }
+
+ /**
* Executes the task.
*/
public void execute()
@@ -76,7 +87,7 @@
final Deployer deployer = (Deployer)getService( Deployer.class );
final TypeLibraryDeployer typeDeployer =
deployer.createDeployer( library );
final TypeDefinition typeDef = createTypeDefinition();
- typeDeployer.deployType( typeDef );
+ typeDeployer.deployType( m_namespace, typeDef );
}
catch( final Exception e )
{
1.2 +5 -4
jakarta-ant-myrmidon/framework/src/java/org/apache/myrmidon/framework/DataTypeDeployer.java
Index: DataTypeDeployer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/framework/src/java/org/apache/myrmidon/framework/DataTypeDeployer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DataTypeDeployer.java 25 Apr 2002 09:34:45 -0000 1.1
+++ DataTypeDeployer.java 8 May 2002 04:10:27 -0000 1.2
@@ -19,7 +19,7 @@
* task with the same name.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
- * @version $Revision: 1.1 $ $Date: 2002/04/25 09:34:45 $
+ * @version $Revision: 1.2 $ $Date: 2002/05/08 04:10:27 $
*/
public class DataTypeDeployer
extends DefaultTypeDeployer
@@ -30,14 +30,15 @@
/**
* Deploys a type.
*/
- public void deployType( final TypeDefinition typeDefinition,
+ public void deployType( final String namespace,
+ final TypeDefinition typeDefinition,
final TypeFactory typeFactory )
throws Exception
{
// Register the type itself
- super.deployType( typeDefinition, typeFactory );
+ super.deployType( namespace, typeDefinition, typeFactory );
// Register the instantiating task
- getTypeManager().registerType( Task.ROLE, typeDefinition.getName(),
m_factory );
+ getTypeManager().registerType( Task.ROLE, namespace,
typeDefinition.getName(), m_factory );
}
}
1.3 +1 -1 jakarta-ant-myrmidon/myrmidon/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/myrmidon/project.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- project.xml 22 Apr 2002 06:52:31 -0000 1.2
+++ project.xml 8 May 2002 04:10:27 -0000 1.3
@@ -13,7 +13,6 @@
<fileset dir="../container/build/lib"
includes="myrmidon-container-api-*.jar"/>
<fileset dir="../framework/build/lib" includes="*.jar, *.atl"/>
<fileset dir="../antlib/build/lib" includes="*.jar, *.atl"
excludes="selftest*"/>
- <fileset dir="../ant1compat/build/lib" includes="*.jar, *.atl"/>
<fileset dir="../lib" includes="*.jar"/>
<fileset dir="../tools/xalan" includes="*.jar"/>
</copy>
@@ -42,6 +41,7 @@
<copy todir="${dist.dir}/ext">
<fileset dir="../antlib/build/lib" includes="selftest*"/>
<fileset dir="../lib/ext" includes="*.jar"/>
+ <fileset dir="../ant1compat/build/lib" includes="*.jar, *.atl"/>
</copy>
<!-- Samples and docs -->
1.1
jakarta-ant-myrmidon/myrmidon/src/samples/namespace-test.ant
Index: namespace-test.ant
===================================================================
<?xml version="1.0"?>
<!--
==============================================================================
Basic tests build file
Authors:
Darrell DeBoer <[EMAIL PROTECTED]>
Legal:
Copyright (c) 2000 The Apache Software Foundation. All Rights Reserved.
==============================================================================
-->
<project version="2.0.0">
<target name="main" depends="log-no-import, log-import-selftest.atl,
log-taskdef-newlog"/>
<target name="log-no-import">
<!-- With no overriding imports, both <log> and <ant.log> refer to
the core Log task. -->
<log message="message"/>
<ant.log message="message"/>
</target>
<target name="log-import-selftest.atl">
<!-- Import selftest.atl with the "test" namespace. -->
<typelib library="selftest" namespace="test"/>
<!-- The version of <log> in selftest.atl overrides the core Log task
-->
<log message="message"/> <!-- selftest.LogTask2 -->
<test.log message="message"/> <!-- selftest.LogTask2 -->
<ant.log message="message"/> <!-- core.Log -->
</target>
<target name="log-taskdef-newlog">
<type-def
name="log"
namespace="task-deffed"
role="task"
classname="org.apache.antlib.selftest.LogTask3"
classpath="../../dist/ext/selftest.atl" />
<log message="message"/> <!-- selftest.LogTask3 -->
<ant.log message="message"/> <!-- core.Log -->
<test.log message="message"/> <!-- selftest.LogTask2 -->
<task-deffed.log message="message"/> <!-- selftest.LogTask3 -->
</target>
</project>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>