Browse Source

Read in roles from META-INF/ant-roles.xml and remove hardwired mapping for tasks/data-types.

Also fixed so that role-name mapping is 1-to-1


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269096 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 24 years ago
parent
commit
75bc6925af
1 changed files with 48 additions and 11 deletions
  1. +48
    -11
      proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer/DefaultRoleManager.java

+ 48
- 11
proposal/myrmidon/src/java/org/apache/myrmidon/components/deployer/DefaultRoleManager.java View File

@@ -7,23 +7,26 @@
*/
package org.apache.myrmidon.components.deployer;

import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import org.apache.myrmidon.api.DataType;
import org.apache.myrmidon.api.Task;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.xml.sax.InputSource;

/**
* Interface to manage roles and mapping to shorthand names.
* Interface to manage roles and mapping to names.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:ricardo@apache,org">Ricardo Rocha</a>
* @author <a href="mailto:giacomo@apache,org">Giacomo Pati</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
* @version CVS $Revision$ $Date$
*/
public class DefaultRoleManager
implements RoleManager, Initializable
{
private final static String ROLE_DESCRIPTOR = "META-INF/ant-roles.xml";

/** Parent <code>RoleManager</code> for nested resolution */
private final RoleManager m_parent;

@@ -52,14 +55,42 @@ public class DefaultRoleManager
m_parent = parent;
}

/**
* initialize the RoleManager.
* This involves reading all Role descriptors in common classloader.
*
* @exception Exception if an error occurs
*/
public void initialize()
throws Exception
{
///UGLY HACK!!!!!!!!!!!!!!!!!!!!!!!
addNameRoleMapping( "task", Task.ROLE );
addNameRoleMapping( "data-type", DataType.ROLE );
final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();

//getClass().getClassLoader().getResources( "META-INF/ant-types.xml" );
final Enumeration enum = getClass().getClassLoader().getResources( ROLE_DESCRIPTOR );
while( enum.hasMoreElements() )
{
final URL url = (URL)enum.nextElement();
final Configuration descriptor = builder.build( new InputSource( url.toString() ) );
handleDescriptor( descriptor );
}
}

/**
* Configure RoleManager based on contents of single descriptor.
*
* @param descriptor the descriptor
* @exception ConfigurationException if an error occurs
*/
private void handleDescriptor( final Configuration descriptor )
throws ConfigurationException
{
final Configuration[] types = descriptor.getChildren( "role" );
for( int i = 0; i < types.length; i++ )
{
final String name = types[ i ].getAttribute( "shorthand" );
final String role = types[ i ].getAttribute( "name" );
addNameRoleMapping( name, role );
}
}

/**
@@ -109,13 +140,19 @@ public class DefaultRoleManager
throws IllegalArgumentException
{
final String oldRole = (String)m_names.get( name );

if( null != oldRole && oldRole.equals( role ) )
{
throw new IllegalArgumentException( "Name already mapped to another role (" +
oldRole + ")" );
}

final String oldName = (String)m_roles.get( role );
if( null != oldName && oldName.equals( name ) )
{
throw new IllegalArgumentException( "Role already mapped to another name (" +
oldName + ")" );
}

m_names.put( name, role );
m_roles.put( role, name );
}


Loading…
Cancel
Save