crafterm 2002/10/14 09:17:51
Modified: xfc build.xml
xfc/src/java/org/apache/excalibur/xfc/model Model.java
RoleRef.java
xfc/src/java/org/apache/excalibur/xfc/modules
AbstractModule.java ECM.java Fortress.java
xfc/src/test/org/apache/excalibur/xfc/test xfcTestCase.java
Added: xfc/src/java/org/apache/excalibur/xfc/model Instance.java
xfc/src/test/org/apache/excalibur/xfc/test ecm.xconf
fortress.xconf
Log:
First stab at converting .xconf files for ECM and Fortress.
Several improvements/refactorings to existing codebase are coming.
Revision Changes Path
1.7 +2 -2 jakarta-avalon-excalibur/xfc/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/xfc/build.xml,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- build.xml 8 Oct 2002 16:30:18 -0000 1.6
+++ build.xml 14 Oct 2002 16:17:50 -0000 1.7
@@ -205,9 +205,9 @@
<xfc>
<input module="ecm"
-
context="build/testclasses/org/apache/excalibur/xfc/test/ecm.roles:ecm.xconf"/>
+
context="build/testclasses/org/apache/excalibur/xfc/test/ecm.roles:build/testclasses/org/apache/excalibur/xfc/test/ecm.xconf"/>
<output module="fortress"
-
context="build/tests/fortress-ant-generated.roles:fortress-generated.xconf"/>
+
context="build/tests/fortress-ant-generated.roles:build/tests/fortress-ant-generated.xconf"/>
</xfc>
</target>
1.3 +47 -2
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/model/Model.java
Index: Model.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/model/Model.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Model.java 4 Oct 2002 14:36:24 -0000 1.2
+++ Model.java 14 Oct 2002 16:17:50 -0000 1.3
@@ -62,6 +62,7 @@
public final class Model
{
private final List m_definitions = new ArrayList();
+ private final List m_instances = new ArrayList();
/**
* Adds a new [EMAIL PROTECTED] RoleRef} object to the system. A
@@ -76,8 +77,20 @@
}
/**
+ * Adds a new [EMAIL PROTECTED] Instance} object to the system. An
+ * [EMAIL PROTECTED] Instance} object contains the definition of a
+ * particular instance of a role.
+ *
+ * @param instance an [EMAIL PROTECTED] Instance} instance
+ */
+ public void addInstance( final Instance instance )
+ {
+ m_instances.add( instance );
+ }
+
+ /**
* Obtain all definitions this Model contains, as an
- * array of RoleRef objects.
+ * array of [EMAIL PROTECTED] RoleRef} objects.
*
* @return a [EMAIL PROTECTED] RoleRef}[] array
*/
@@ -85,6 +98,38 @@
{
return (RoleRef[]) m_definitions.toArray(
new RoleRef[ m_definitions.size() ]
+ );
+ }
+
+ /**
+ * Method to locate a [EMAIL PROTECTED] RoleRef} object by shorthand
name.
+ *
+ * @param shorthand shorthand name
+ * @return a [EMAIL PROTECTED] RoleRef} object or null if none could be
found
+ */
+ public RoleRef findByShorthand( final String shorthand )
+ {
+ RoleRef[] refs = getDefinitions();
+
+ for ( int i = 0; i < refs.length; ++i )
+ {
+ if ( refs[i].getShorthand().equals( shorthand ) )
+ return refs[i];
+ }
+
+ return null;
+ }
+
+ /**
+ * Obtain all instances this Model contains, as an
+ * array of [EMAIL PROTECTED] Instance} objects.
+ *
+ * @return an @link Instance}[] array
+ */
+ public Instance[] getInstances()
+ {
+ return (Instance[]) m_instances.toArray(
+ new Instance[ m_instances.size() ]
);
}
}
1.4 +2 -2
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/model/RoleRef.java
Index: RoleRef.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/model/RoleRef.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RoleRef.java 7 Oct 2002 17:13:17 -0000 1.3
+++ RoleRef.java 14 Oct 2002 16:17:50 -0000 1.4
@@ -88,7 +88,7 @@
* Creates a new <code>RoleRef</code> instance.
*
* @param role role name
- * @param role shorthand name
+ * @param shorthand shorthand name
* @param definitions a [EMAIL PROTECTED] Definition} array
*/
public RoleRef( final String role, final String shorthand, final
Definition[] definitions )
1.1
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/model/Instance.java
Index: Instance.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software
Foundation"
must not be used to endorse or promote products derived from this
software
without prior written permission. For written permission, please contact
[EMAIL PROTECTED]
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.excalibur.xfc.model;
import java.util.ArrayList;
import java.util.List;
import org.apache.avalon.framework.configuration.Configuration;
/**
* Class which represents an instance of a particular component in an xconf
file.
*
* <p>
* This class can contain a:
*
* <ul>
* <li>Single role based instance definition.
* <li>Single non-role based instance definition (ie. complete role &
instance
* definition).
* <li>Multi role based instance definition (ie. role based component
selector
* definition).
* <li>Multi non-role based instance definition (ie. complete role & sub
instance
* definition of a component selector).
* </ul>
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Marcus Crafter</a>
* @version CVS $Id: Instance.java,v 1.1 2002/10/14 16:17:50 crafterm Exp $
*/
public final class Instance
{
// instance configuration
private final Configuration[] m_configuration;
private final String m_shorthand;
// instance roles/override class/and subinstance definitions, if any
private final String m_class;
private final String m_role;
private final Instance[] m_subinstances;
private final String m_handler;
/**
* Creates a new [EMAIL PROTECTED] Instance} instance. This constructor
creates
* an instance definition of a particular role, indexed by shorthand name.
*
* @param shorthand shorthand name
* @param config instance <code>Configuration</code> as an array, if any
* @param clazz override class, if any
*/
public Instance(
final String shorthand,
final Configuration[] config,
final String clazz,
final String handler
)
{
m_configuration = config;
m_shorthand = shorthand;
m_class = clazz;
m_role = null;
m_subinstances = null;
m_handler = handler;
}
/**
* Creates a new [EMAIL PROTECTED] Instance} object for role
* based ComponentSelector.
*
* @param shorthand a <code>String</code> value
* @param subinstances an <code>Instance[]</code> value
*/
public Instance(
final String shorthand,
final Instance[] subinstances
)
{
m_configuration = null;
m_shorthand = shorthand;
m_subinstances = subinstances;
m_class = null;
m_role = null;
m_handler = null;
}
/**
* Creates a new [EMAIL PROTECTED] Instance} instance. This constructor
creates
* an instance definition of a given role.
*
* @param config instance <code>Configuration</code> as an array, if any
* @param clazz component implementation class name
* @param role role name
*/
public Instance(
final Configuration[] config,
final String clazz,
final String role,
final String handler
)
{
m_configuration = config;
m_class = clazz;
m_role = role;
m_shorthand = null;
m_subinstances = null;
m_handler = handler;
}
/**
* Creates a new [EMAIL PROTECTED] Instance} instance. This constructor
creates
* an instance definition of a given role that contains sub instances
* (ie. non role manager component selector definitions).
*
* @param clazz implementing class name
* @param role role name
* @param subinstances an <code>Instance[]</code> array
*/
public Instance(
final String clazz,
final String role,
final Instance[] subinstances,
final String handler
)
{
m_class = clazz;
m_role = role;
m_subinstances = subinstances;
m_configuration = null;
m_shorthand = null;
m_handler = handler;
}
/**
* Obtain this Instance's configuration
*
* @return a <code>Configuration[]</code> value
*/
public Configuration[] getConfiguration()
{
return m_configuration;
}
/**
* Obtain this Instance's implementing class, or override class name
*
* @return a <code>String</code> value
*/
public String getClassImpl()
{
return m_class;
}
/**
* Obtain this Instance's role name
*
* @return a <code>String</code> value
*/
public String getRole()
{
return m_role;
}
/**
* Obtain this Instance's shorthand name
*
* @return a <code>String</code> value
*/
public String getShorthand()
{
return m_shorthand;
}
/**
* Obtain this Instance's list of sub instances.
*
* @return an <code>Instance[]</code> value
*/
public Instance[] getSubInstances()
{
return m_subinstances;
}
/**
* Obtain this Instance's normalized handler
*
* @return normalized handler name
*/
public String getHandler()
{
return m_handler;
}
}
1.3 +3 -1
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/AbstractModule.java
Index: AbstractModule.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/AbstractModule.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractModule.java 4 Oct 2002 14:46:35 -0000 1.2
+++ AbstractModule.java 14 Oct 2002 16:17:50 -0000 1.3
@@ -80,6 +80,8 @@
protected static final char CONTEXT_SEPARATOR = ':';
+ protected static final String COMPONENT = "component";
+
protected final DefaultConfigurationSerializer m_serializer;
protected final DefaultConfigurationBuilder m_builder;
1.8 +271 -6
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/ECM.java
Index: ECM.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/ECM.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ECM.java 8 Oct 2002 15:04:40 -0000 1.7
+++ ECM.java 14 Oct 2002 16:17:50 -0000 1.8
@@ -58,6 +58,7 @@
import org.apache.excalibur.xfc.model.Model;
import org.apache.excalibur.xfc.model.Definition;
+import org.apache.excalibur.xfc.model.Instance;
import org.apache.excalibur.xfc.model.RoleRef;
/**
@@ -98,6 +99,8 @@
private static final String ECS =
"org.apache.avalon.excalibur.component.ExcaliburComponentSelector";
+ private static final String COMPONENT_INSTANCE = "component-instance";
+
private static Map m_handlers = new HashMap();
// Normalized mappings for ECM lifestyles
@@ -129,9 +132,11 @@
{
validateContext( context );
- Configuration[] roles = getRoles( getRoleFile( context ) );
Model model = new Model();
+ // locate all roles
+ Configuration[] roles = getRoles( getRoleFile( context ) );
+
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "Identified total of " + roles.length + "
roles" );
@@ -143,6 +148,22 @@
model.addRoleRef( buildRoleRef( roles[i] ) );
}
+ // locate all component instances
+ Configuration[] instances = getInstanceList( getConfigurationFile(
context ) );
+
+ if ( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug(
+ "Identified total of " + instances.length + " component
instances"
+ );
+ }
+
+ for ( int i = 0; i < instances.length; ++i )
+ {
+ model.addInstance( buildInstance( instances[i], model ) );
+ }
+
+ // finished
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "Model built" );
@@ -157,7 +178,7 @@
* @param context a <code>String</code> value
* @return a <code>File</code> value
*/
- private File getRoleFile( final String context )
+ protected File getRoleFile( final String context )
{
int i = context.indexOf( CONTEXT_SEPARATOR );
return new File( context.substring( 0, i ) );
@@ -169,7 +190,7 @@
* @param context a <code>String</code> value
* @return a <code>File</code> value
*/
- private File getConfigurationFile( final String context )
+ protected File getConfigurationFile( final String context )
{
int i = context.indexOf( CONTEXT_SEPARATOR );
return new File( context.substring( i + 1 ) );
@@ -183,7 +204,7 @@
* @return a <code>Configuration[]</code> value
* @exception Exception if an error occurs
*/
- private Configuration[] getRoles( final File input )
+ protected Configuration[] getRoles( final File input )
throws Exception
{
Configuration config = m_builder.buildFromFile( input );
@@ -191,6 +212,21 @@
}
/**
+ * Helper method for obtaining the instances defined in
+ * a particular input file
+ *
+ * @param input a <code>File</code> value
+ * @return a <code>Configuration[]</code> value
+ * @exception Exception if an error occurs
+ */
+ protected Configuration[] getInstanceList( final File input )
+ throws Exception
+ {
+ Configuration config = m_builder.buildFromFile( input );
+ return config.getChildren();
+ }
+
+ /**
* Method to construct a [EMAIL PROTECTED] RoleRef} object from
* a Role definition.
*
@@ -403,6 +439,124 @@
return null;
}
+ protected Instance buildInstance( final Configuration i, final Model
model )
+ throws Exception
+ {
+ if ( i.getName().equals( COMPONENT ) )
+ {
+ Configuration[] kids = i.getChildren( COMPONENT_INSTANCE );
+
+ if ( kids.length > 0 )
+ {
+ // build non-role component selector
+ return buildNonRoleComponentSelectorInstance( i );
+ }
+
+ // build non-role component
+ return buildNonRoleComponentInstance( i );
+ }
+
+ if ( isComponentSelectorXConf( i.getName(), model ) )
+ {
+ // build multi role based component
+ return buildRoleComponentSelectorInstance( i );
+ }
+
+ // build single role based component
+ return buildRoleComponentInstance( i );
+ }
+
+ private boolean isComponentSelectorXConf(
+ final String shorthand, final Model model
+ )
+ throws Exception
+ {
+ // check if shorthand corresponds to ECM
+ RoleRef ref = model.findByShorthand( shorthand );
+
+ if ( ref != null && ref.getProviders().length > 1 )
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ protected Instance buildNonRoleComponentSelectorInstance(
+ final Configuration i
+ )
+ throws Exception
+ {
+ final Configuration[] kids = i.getChildren( COMPONENT_INSTANCE );
+ final Instance[] subinstances = new Instance[ kids.length ];
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ subinstances[j] = buildSubInstance( kids[j] );
+ }
+
+ return new Instance(
+ ECS, i.getAttribute( "role" ), subinstances, SINGLETON
+ );
+ }
+
+ protected Instance buildNonRoleComponentInstance( final Configuration i )
+ throws Exception
+ {
+ return new Instance(
+ i.getChildren(),
+ getOverrideClass( i ),
+ i.getAttribute( "role" ),
+ getHandler( getOverrideClass( i ) )
+ );
+ }
+
+ protected Instance buildRoleComponentInstance( final Configuration i )
+ throws Exception
+ {
+ String cl = getOverrideClass( i );
+
+ return new Instance(
+ i.getName(),
+ i.getChildren(),
+ cl, cl == null ? null : getHandler( cl )
+ );
+ }
+
+ protected Instance buildRoleComponentSelectorInstance( final
Configuration i )
+ {
+ // get the subinstances
+ Configuration[] kids = i.getChildren();
+ Instance[] subinstances = new Instance[ kids.length ];
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ subinstances[j] =
+ new Instance( kids[j].getName(), kids[j].getChildren(),
null, null );
+ }
+
+ // create the root instance
+ return new Instance( i.getName(), subinstances );
+ }
+
+ private Instance buildSubInstance( final Configuration i )
+ throws Exception
+ {
+ String cl = i.getAttribute( "class" );
+
+ return new Instance(
+ i.getAttribute( "name" ),
+ i.getChildren(),
+ cl, cl == null ? null : getHandler( cl )
+ );
+ }
+
+ private String getOverrideClass( final Configuration i )
+ {
+ // return null if optional class attribute not specified
+ return i.getAttribute( "class", null );
+ }
+
/**
* Serializes a [EMAIL PROTECTED] Model} definition, ECM style, to an
* output context.
@@ -416,6 +570,7 @@
{
validateContext( context );
+ // create the role file
RoleRef[] rolerefs = model.getDefinitions();
DefaultConfiguration roles = new DefaultConfiguration( "role-list",
"" );
@@ -426,6 +581,18 @@
}
m_serializer.serializeToFile( getRoleFile( context ), roles );
+
+ // create the xconf file
+ Instance[] instances = model.getInstances();
+ DefaultConfiguration xconf = new DefaultConfiguration( "xconf", "" );
+
+ // for each instance object generate an xconf file entry
+ for ( int j = 0; j < instances.length; ++j )
+ {
+ xconf.addChild( buildXConf( instances[j] ) );
+ }
+
+ m_serializer.serializeToFile( getConfigurationFile( context ), xconf
);
}
/**
@@ -505,6 +672,104 @@
return role;
}
+ private Configuration buildXConf( final Instance i )
+ throws Exception
+ {
+ // has shorthand
+ if ( i.getShorthand() != null )
+ {
+ return buildSingleRoleXConf( i );
+ }
+
+ if ( i.getSubInstances() == null )
+ {
+ // has no shorthand, no subinstances
+ return buildNonRoleSingleXConf( i );
+ }
+
+ // has no shorthand, has subinstances
+ return buildNonRoleMultiXConf( i );
+
+ // return buildMultiRoleXConf();
+ }
+
+ private Configuration buildSingleRoleXConf( final Instance i )
+ throws Exception
+ {
+ DefaultConfiguration conf = new DefaultConfiguration(
i.getShorthand(), "" );
+
+ if ( i.getConfiguration() != null )
+ {
+ Configuration[] kids = i.getConfiguration();
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ conf.addChild( kids[j] );
+ }
+ }
+
+ if ( i.getClassImpl() != null )
+ {
+ conf.setAttribute( "class", i.getClassImpl() );
+ }
+
+ return conf;
+ }
+
+ private Configuration buildNonRoleSingleXConf( final Instance i )
+ throws Exception
+ {
+ DefaultConfiguration conf = new DefaultConfiguration( "component",
"" );
+
+ conf.setAttribute( "role", i.getRole() );
+ conf.setAttribute( "class", i.getClassImpl() );
+
+ if ( i.getConfiguration() != null )
+ {
+ Configuration[] kids = i.getConfiguration();
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ conf.addChild( kids[j] );
+ }
+ }
+
+ return conf;
+ }
+
+ private Configuration buildNonRoleMultiXConf( final Instance i )
+ throws Exception
+ {
+ DefaultConfiguration conf = new DefaultConfiguration( "component",
"" );
+
+ conf.setAttribute( "role", i.getRole() );
+ conf.setAttribute( "class", ECS );
+
+ Instance[] subs = i.getSubInstances();
+
+ for ( int j = 0; j < subs.length; ++j )
+ {
+ DefaultConfiguration child =
+ new DefaultConfiguration( "component-instance", "" );
+ child.setAttribute( "class", subs[j].getClassImpl() );
+ child.setAttribute( "name", subs[j].getShorthand() );
+
+ if ( subs[j].getConfiguration() != null )
+ {
+ Configuration[] kids = subs[j].getConfiguration();
+
+ for ( int k = 0; k < kids.length; ++k )
+ {
+ child.addChild( kids[k] );
+ }
+ }
+
+ conf.addChild( child );
+ }
+
+ return conf;
+ }
+
/**
* Helper method to validate the input & output context's
* given to this module.
@@ -512,7 +777,7 @@
* @param context a <code>String</code> context value
* @exception Exception if an error occurs
*/
- private void validateContext( final String context )
+ protected void validateContext( final String context )
throws Exception
{
if ( context.indexOf( CONTEXT_SEPARATOR ) == -1 )
1.5 +197 -4
jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/Fortress.java
Index: Fortress.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/java/org/apache/excalibur/xfc/modules/Fortress.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Fortress.java 8 Oct 2002 10:42:32 -0000 1.4
+++ Fortress.java 14 Oct 2002 16:17:50 -0000 1.5
@@ -56,6 +56,8 @@
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.excalibur.xfc.model.Definition;
+import org.apache.excalibur.xfc.model.Instance;
+import org.apache.excalibur.xfc.model.Model;
import org.apache.excalibur.xfc.model.RoleRef;
/**
@@ -159,7 +161,7 @@
);
}
- return new RoleRef( getRole( role ), "UNKNOWN-REVISIT", definitions
);
+ return new RoleRef( getRole( role ), "UNKNOWN", definitions );
}
/**
@@ -208,7 +210,7 @@
protected String getHandler( final Configuration role )
throws Exception
{
- return getLifestyleType( role.getAttribute( "handler" ), TRANSIENT );
+ return getLifestyleType( role.getAttribute( "handler" ), FACTORY );
}
/**
@@ -241,6 +243,41 @@
return defaultValue;
}
+ protected Instance buildInstance( final Configuration i )
+ throws Exception
+ {
+ if ( i.getName().equals( COMPONENT ) )
+ {
+ // build non-role component
+ return buildNonRoleComponentInstance( i );
+ }
+
+ // build role based component
+ return buildRoleComponentInstance( i );
+ }
+
+ protected Instance buildNonRoleComponentInstance( final Configuration i )
+ throws Exception
+ {
+ return new Instance(
+ i.getChildren(),
+ i.getAttribute( "class" ),
+ i.getAttribute( "role" ),
+ null
+ );
+ }
+
+ protected Instance buildRoleComponentInstance( final Configuration i )
+ throws Exception
+ {
+ return new Instance(
+ i.getName(),
+ i.getChildren(),
+ null,
+ null
+ );
+ }
+
/**
* Builds a single component Role definition from a [EMAIL PROTECTED]
RoleRef}
* definition.
@@ -276,7 +313,7 @@
hint.setAttribute( "shorthand", defs[i].getShorthand() );
hint.setAttribute( "class", defs[i].getDefaultClass() );
hint.setAttribute(
- "handler", getLifestyleType( defs[i].getHandler(), TRANSIENT
)
+ "handler", getLifestyleType( defs[i].getHandler(), FACTORY )
);
role.addChild( hint );
@@ -285,5 +322,161 @@
role.setAttribute( "name", ref.getRole() );
return role;
+ }
+
+ /**
+ * Serializes a [EMAIL PROTECTED] Model} definition, ECM style, to an
+ * output context.
+ *
+ * @param model a [EMAIL PROTECTED] Model} instance
+ * @param context ECM output Context
+ * @exception Exception if an error occurs
+ */
+ public void serialize( final Model model, final String context )
+ throws Exception
+ {
+ validateContext( context );
+
+ // create the role file
+ RoleRef[] rolerefs = model.getDefinitions();
+ DefaultConfiguration roles = new DefaultConfiguration( "role-list",
"" );
+
+ // for each type object generate a roles file entry
+ for ( int i = 0; i < rolerefs.length; ++i )
+ {
+ roles.addChild( buildRole( rolerefs[i] ) );
+ }
+
+ m_serializer.serializeToFile( getRoleFile( context ), roles );
+
+ // create the xconf file
+ Instance[] instances = model.getInstances();
+ DefaultConfiguration xconf = new DefaultConfiguration( "xconf", "" );
+
+ // for each instance object generate an xconf file entry
+ for ( int j = 0; j < instances.length; ++j )
+ {
+ Configuration[] xconfs = buildXConf( instances[j] );
+
+ for ( int k = 0; k < xconfs.length; ++k )
+ {
+ xconf.addChild( xconfs[k] );
+ }
+ }
+
+ m_serializer.serializeToFile( getConfigurationFile( context ), xconf
);
+ }
+
+ protected Configuration[] buildXConf( final Instance i )
+ throws Exception
+ {
+ if ( i.getShorthand() != null )
+ {
+
+ if ( i.getSubInstances() == null )
+ {
+ // has shorthand, single component
+ return new Configuration[] { buildSingleRoleXConf( i ) };
+ }
+
+ // has shorthand, multi component
+ return buildMultiRoleXConf( i );
+ }
+
+ if ( i.getSubInstances() == null )
+ {
+ // has no shorthand, no subinstances
+ return new Configuration[] { buildNonRoleSingleXConf( i ) };
+ }
+
+ // has no shorthand, has subinstances
+ return buildNonRoleMultiXConf( i );
+ }
+
+ private Configuration buildSingleRoleXConf( final Instance i )
+ throws Exception
+ {
+ DefaultConfiguration conf = new DefaultConfiguration(
i.getShorthand(), "" );
+
+ if ( i.getConfiguration() != null )
+ {
+ Configuration[] kids = i.getConfiguration();
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ conf.addChild( kids[j] );
+ }
+ }
+
+ conf.setAttribute( "id", i.getShorthand() );
+
+ return conf;
+ }
+
+ private Configuration[] buildMultiRoleXConf( final Instance i )
+ throws Exception
+ {
+ Instance[] subinstances = i.getSubInstances();
+ Configuration[] xconf = new Configuration[ subinstances.length ];
+
+ for ( int j = 0; j < subinstances.length; ++j )
+ {
+ xconf[j] = buildSingleRoleXConf( subinstances[j] );
+ }
+
+ return xconf;
+ }
+
+ private Configuration buildNonRoleSingleXConf( final Instance i )
+ throws Exception
+ {
+ DefaultConfiguration conf = new DefaultConfiguration( "component",
"" );
+
+ conf.setAttribute( "role", i.getRole() );
+ conf.setAttribute( "class", i.getClassImpl() );
+ conf.setAttribute( "handler", getLifestyleType( i.getHandler(),
FACTORY ) );
+ conf.setAttribute( "id", "UNKNOWN" );
+
+ if ( i.getConfiguration() != null )
+ {
+ Configuration[] kids = i.getConfiguration();
+
+ for ( int j = 0; j < kids.length; ++j )
+ {
+ conf.addChild( kids[j] );
+ }
+ }
+
+ return conf;
+ }
+
+ private Configuration[] buildNonRoleMultiXConf( final Instance i )
+ {
+ Instance[] subs = i.getSubInstances();
+ Configuration[] xconfs = new Configuration[ subs.length ];
+
+ for ( int j = 0; j < subs.length; ++j )
+ {
+ DefaultConfiguration conf = new DefaultConfiguration( COMPONENT,
"" );
+
+ conf.setAttribute( "role", i.getRole() );
+ conf.setAttribute( "class", subs[j].getClassImpl() );
+ conf.setAttribute( "handler", getLifestyleType(
subs[j].getHandler(), FACTORY ) );
+ conf.setAttribute( "id", subs[j].getShorthand() );
+
+ if ( subs[j].getConfiguration() != null )
+ {
+ Configuration[] kids = subs[j].getConfiguration();
+
+ for ( int k = 0; k < kids.length; ++k )
+ {
+ conf.addChild( kids[k] );
+ }
+ }
+
+ xconfs[j] = conf;
+ }
+
+ return xconfs;
}
}
1.6 +8 -8
jakarta-avalon-excalibur/xfc/src/test/org/apache/excalibur/xfc/test/xfcTestCase.java
Index: xfcTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/xfc/src/test/org/apache/excalibur/xfc/test/xfcTestCase.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- xfcTestCase.java 8 Oct 2002 15:02:02 -0000 1.5
+++ xfcTestCase.java 14 Oct 2002 16:17:50 -0000 1.6
@@ -81,12 +81,14 @@
// location of ECM roles/xconf configuration data
private static final String ECM_ROLES =
"../testclasses/org/apache/excalibur/xfc/test/ecm.roles";
- private static final String ECM_XCONF = "ecm.xconf";
+ private static final String ECM_XCONF =
+ "../testclasses/org/apache/excalibur/xfc/test/ecm.xconf";
// location of Fortress roles/xconf configuration data
private static final String FORTRESS_ROLES =
"../testclasses/org/apache/excalibur/xfc/test/fortress.roles";
- private static final String FORTRESS_XCONF = "fortress.xconf";
+ private static final String FORTRESS_XCONF =
+ "../testclasses/org/apache/excalibur/xfc/test/fortress.xconf";
// misc internals
private DefaultConfigurationBuilder m_builder = new
DefaultConfigurationBuilder();
@@ -208,7 +210,8 @@
public void testXFC_ECM_serialize()
throws Exception
{
- String ECM_ROLES_GENERATED = "ecm-generated.roles";
+ final String ECM_ROLES_GENERATED = "ecm-generated.roles";
+ final String ECM_XCONF_GENERATED = "ecm-generated.xconf";
// create an ECM module test rig instance
ECMTestRig ecm = new ECMTestRig();
@@ -218,7 +221,7 @@
Model model = ecm.generate( ECM_ROLES + ":" + ECM_XCONF );
// serialize the model out to a temporary file
- ecm.serialize( model, ECM_ROLES_GENERATED + ":" + ECM_XCONF );
+ ecm.serialize( model, ECM_ROLES_GENERATED + ":" +
ECM_XCONF_GENERATED );
// compare original with generated copy, they should be equal
Configuration master = m_builder.buildFromFile( ECM_ROLES );
@@ -400,9 +403,6 @@
// generate model from predefined ECM configuration
Model model = ecm.generate( ECM_ROLES + ":" + ECM_XCONF );
-
- // serialize the model out to a Fortress temporary file
- //fortress.serialize( model, FORTRESS_ROLES_GENERATED + ":" +
FORTRESS_XCONF );
// load the same config and manually verify that model is correct
Configuration[] rolesREAL =
1.1
jakarta-avalon-excalibur/xfc/src/test/org/apache/excalibur/xfc/test/ecm.xconf
Index: ecm.xconf
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<ecm-xconf>
<!-- XML Parser -->
<xml-parser class="org.apache.avalon.excalibur.xml.JaxpParser">
<parameter name="validate" value="false"/>
<parameter name="namespace-prefixes" value="false"/>
<parameter name="stop-on-warning" value="true"/>
<parameter name="stop-on-recoverable-error" value="true"/>
<parameter name="reuse-parsers" value="false"/>
</xml-parser>
<!-- XSLT Processor -->
<xslt-processor>
<parameter name="use-store" value="false"/>
<parameter name="incremental-processing" value="true"/>
</xslt-processor>
<!-- Xpath Processor -->
<xpath-processor
class="org.apache.avalon.excalibur.xml.xpath.XPathProcessorImpl"/>
<!-- Datasources -->
<datasources>
<jdbc name="default">
<dburl>jdbc:hsqldb:hsql://localhost:9002</dburl>
<user>sa</user>
<password/>
</jdbc>
</datasources>
<!-- component selector, non roles -->
<component role="my.cs.role"
class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector" >
<component-instance name="one" class="my.first.impl">
<config><values/></config>
</component-instance>
<component-instance name="two" class="my.second.impl">
<config><values/></config>
</component-instance>
</component>
<!-- component, non role -->
<component role="my.role" class="my.class">
<config><values/></config>
</component>
</ecm-xconf>
1.1
jakarta-avalon-excalibur/xfc/src/test/org/apache/excalibur/xfc/test/fortress.xconf
Index: fortress.xconf
===================================================================
<?xml version="1.0" encoding="iso-8859-1" ?>
<!-- Example Fortress configuration file -->
<fortress-xconf>
<xml-parser id="xml-parser">
<parameter name="validate" value="false"/>
<parameter name="namespace-prefixes" value="false"/>
<parameter name="stop-on-warning" value="true"/>
<parameter name="stop-on-recoverable-error" value="true"/>
<parameter name="reuse-parsers" value="false"/>
</xml-parser>
<xslt-processor id="xslt-processor">
<parameter name="use-store" value="false"/>
<parameter name="incremental-processing" value="true"/>
</xslt-processor>
<xpath-processor id="xpath-processor"/>
<jdbc id="personnel">
<dburl>jdbc:hsqldb:hsql://localhost:9002</dburl>
<user>sa</user>
<password/>
</jdbc>
<component role="org.apache.avalon.excalibur.datasource.DataSourceComponent"
id="default-connection"
class="org.apache.avalon.excalibur.datasource.JdbcDataSourceComponent"
handler="org.apache.excalibur.fortress.handler.ThreadSafeComponentHandler">
<omponent-specific-configuration/>
</component>
</fortress-xconf>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>