You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2002/05/23 08:08:58 UTC
cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder DefaultProject.java DefaultProjectBuilder.java
donaldp 02/05/22 23:08:58
Modified: container/src/java/org/apache/myrmidon/components/builder
DefaultProject.java DefaultProjectBuilder.java
Log:
Make DefaultProject mostly immutable by specifying all values in constructor and removing need for setters/adders
Revision Changes Path
1.17 +64 -66 jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProject.java
Index: DefaultProject.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProject.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- DefaultProject.java 23 May 2002 04:31:15 -0000 1.16
+++ DefaultProject.java 23 May 2002 06:08:58 -0000 1.17
@@ -10,8 +10,8 @@
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
-import org.apache.avalon.excalibur.i18n.ResourceManager;
-import org.apache.avalon.excalibur.i18n.Resources;
+import java.util.Map;
+import java.util.Iterator;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.interfaces.executor.ExecutionFrame;
import org.apache.myrmidon.interfaces.model.TargetMetaData;
@@ -24,47 +24,88 @@
* Default project implementation.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.16 $ $Date: 2002/05/23 04:31:15 $
+ * @version $Revision: 1.17 $ $Date: 2002/05/23 06:08:58 $
*/
public class DefaultProject
implements Project, TargetMetaData
{
- private static final Resources REZ =
- ResourceManager.getPackageResources( DefaultProject.class );
-
- ///The project references in this project
- private final HashMap m_references = new HashMap();
-
- ///The targets contained by this project
- private final HashMap m_targets = new HashMap();
+ /**
+ * The project name
+ */
+ private final String m_name;
- ///The implicit target (not present in m_targets)
- private Target m_implicitTarget;
+ /**
+ * The URI of this project file.
+ */
+ private final String m_uri;
- ///The name of the default target
- private String m_defaultTarget;
+ /**
+ * The name of the default target
+ */
+ private final String m_defaultTarget;
- ///The base directory of project
- private File m_baseDirectory;
+ /**
+ * The base directory of project
+ */
+ private final File m_baseDirectory;
- ///The project name
- private String m_name;
+ /**
+ * The project references in this project
+ */
+ private final HashMap m_references;
/**
- * The URI of this project file.
+ * The targets contained by this project
*/
- private String m_uri;
+ private final HashMap m_targets;
+
private Workspace m_workspace;
public DefaultProject( final String name,
final String uri,
final File baseDirectory,
- final String defaultTarget )
+ final String defaultTarget,
+ final Map references,
+ final Map targets )
{
m_name = name;
m_uri = uri;
m_baseDirectory = baseDirectory;
m_defaultTarget = defaultTarget;
+
+ m_references = new HashMap();
+ final Iterator refKeys = references.keySet().iterator();
+ while( refKeys.hasNext() )
+ {
+ final Object key = refKeys.next();
+ final Object ref = references.get( key );
+ if( ref instanceof ProjectRef )
+ {
+ m_references.put( key, ref );
+ }
+ else
+ {
+ final String message = key + " instanceof " + ref.getClass().getName();
+ throw new ClassCastException( message );
+ }
+ }
+
+ m_targets = new HashMap();
+ final Iterator targetKeys = targets.keySet().iterator();
+ while( targetKeys.hasNext() )
+ {
+ final Object key = targetKeys.next();
+ final Object target = targets.get( key );
+ if( target instanceof Target )
+ {
+ m_targets.put( key, target );
+ }
+ else
+ {
+ final String message = key + " instanceof " + target.getClass().getName();
+ throw new ClassCastException( message );
+ }
+ }
}
public String getURI()
@@ -102,18 +143,9 @@
*/
public final Target getImplicitTarget()
{
- return m_implicitTarget;
+ return getTarget( Project.IMPLICIT_TARGET_NAME );
}
- /**
- * Set ImplicitTarget.
- *
- * @param target the implicit target
- */
- public final void setImplicitTarget( final Target target )
- {
- m_implicitTarget = target;
- }
/**
* Retrieve a target by name.
@@ -137,26 +169,6 @@
}
/**
- * Add a target.
- *
- * @param name the name of target
- * @param target the Target
- * @throws IllegalArgumentException if target already exists with same name
- */
- public final void addTarget( final String name, final Target target )
- {
- if( null != m_targets.get( name ) )
- {
- final String message = REZ.getString( "duplicate-target.error", name );
- throw new IllegalArgumentException( message );
- }
- else
- {
- m_targets.put( name, target );
- }
- }
-
- /**
* Retrieve the name of the Target.
*
* @return the name of the Target.
@@ -219,19 +231,5 @@
public void setWorkspace( final Workspace workspace )
{
m_workspace = workspace;
- }
-
- public void addProjectRef( final ProjectRef ref )
- {
- if( null != m_references.get( ref.getName() ) )
- {
- final String message =
- REZ.getString( "duplicate-project.error", ref.getName() );
- throw new IllegalArgumentException( message );
- }
- else
- {
- m_references.put( ref.getName(), ref );
- }
}
}
1.63 +70 -38 jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java
Index: DefaultProjectBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -r1.62 -r1.63
--- DefaultProjectBuilder.java 23 May 2002 05:25:14 -0000 1.62
+++ DefaultProjectBuilder.java 23 May 2002 06:08:58 -0000 1.63
@@ -11,6 +11,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.HashMap;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.excalibur.io.FileUtil;
@@ -32,7 +33,7 @@
* Default implementation to construct project from a build file.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
- * @version $Revision: 1.62 $ $Date: 2002/05/23 05:25:14 $
+ * @version $Revision: 1.63 $ $Date: 2002/05/23 06:08:58 $
*
* @ant.type type="project-builder" name="ant2"
*/
@@ -122,13 +123,29 @@
transformProject( model );
model.makeReadOnly();
- // Build the project model and add to cache
- final DefaultProject project = buildProject( file, model );
+ verifyTopLevelProject( model );
+
+ //get project-level attributes
+ final String projectName = getProjectName( model, file );
+ final File baseDirectory = calcBaseDir( file, model.getAttribute( "basedir" ) );
+ final String defaultTarget = model.getAttribute( "default", "main" );
+ final Version version = getVersion( model );
+ final String uri = getURI( file );
+
+ verifyVersion( version );
+
+ final HashMap refs = new HashMap();
+ final HashMap targets = new HashMap();
+ buildTopLevelProject( baseDirectory, model, refs, targets );
- // Build using all top-level attributes
- buildTopLevelProject( project, model );
+ // Build the project model and add to cache
- return project;
+ return new DefaultProject( projectName,
+ uri,
+ baseDirectory,
+ defaultTarget,
+ refs,
+ targets );
}
catch( Exception e )
{
@@ -158,37 +175,52 @@
}
/**
- * build project from {@link ModelElement} tree.
+ * Verify that the actual version complies with expected
+ * version. See {@link Version#complies(Version)} for details
+ * on version compliance.
*
- * @param file the file from which configuration was loaded
- * @param model the project model
- * @return the created Project
- * @throws ProjectException if an error occurs building the project
+ * @param version the version to check
+ * @throws ProjectException if specified version is not compliant
*/
- private DefaultProject buildProject( final File file,
- final ModelElement model )
+ private void verifyVersion( final Version version )
throws ProjectException
{
- if( !model.getName().equals( "project" ) )
+ if( !VERSION.complies( version ) )
{
- final String message = REZ.getString( "ant.no-project-element.error" );
+ final String message =
+ REZ.getString( "ant.bad-version.error",
+ VERSION,
+ version );
throw new ProjectException( message );
}
+ }
- //get project-level attributes
- final String projectName = getProjectName( model, file );
- final String baseDirectoryName = model.getAttribute( "basedir" );
- final String defaultTarget = model.getAttribute( "default", "main" );
- final Version version = getVersion( model );
- final String uri = getURI( file );
-
- if( !VERSION.complies( version ) )
+ /**
+ * Verify "project" is the name of the top level element.
+ *
+ * @param model the model to check
+ * @throws ProjectException if top level element is not "project"
+ */
+ private void verifyTopLevelProject( final ModelElement model )
+ throws ProjectException
+ {
+ if( !model.getName().equals( "project" ) )
{
final String message =
- REZ.getString( "ant.bad-version.error", VERSION, version );
+ REZ.getString( "ant.no-project-element.error" );
throw new ProjectException( message );
}
+ }
+ /**
+ * Calculate Basedirectory for project.
+ *
+ * @param file the file loading project from
+ * @param baseDirectoryName the basedir specified in project file
+ * @return
+ */
+ private File calcBaseDir( final File file, final String baseDirectoryName )
+ {
//determine base directory for project. Use the directory containing
//the build file as the default.
File baseDirectory = file.getParentFile();
@@ -204,8 +236,7 @@
file, baseDirectory );
getLogger().debug( message );
}
-
- return new DefaultProject( projectName, uri, baseDirectory, defaultTarget );
+ return baseDirectory;
}
/**
@@ -298,17 +329,18 @@
/**
* Handle all top level elements in a project model.
*
- * @param project the project
+ * @param baseDirectory the basedir of project
* @param model the model
* @throws ProjectException if an error occurs
*/
- private void buildTopLevelProject( final DefaultProject project,
- final ModelElement model )
+ private void buildTopLevelProject( final File baseDirectory,
+ final ModelElement model,
+ final HashMap refs,
+ final HashMap targets )
throws Exception
{
- final ModelElement implicit = new ModelElement( "target", model.getLocation() );
+ final ModelElement implicit = new ModelElement( "target", model.getLocation() );
implicit.setAttribute( "name", "<init>" );
- project.setImplicitTarget( new Target( implicit ) );
final ModelElement[] children = model.getChildren();
@@ -323,7 +355,8 @@
{
if( name.equals( "projectref" ) )
{
- buildProjectRef( project, element );
+ final ProjectRef ref = buildProjectRef( baseDirectory, element );
+ refs.put( ref.getName(), ref );
continue;
}
else
@@ -349,7 +382,7 @@
if( name.equals( "target" ) )
{
final String target = element.getAttribute( "name" );
- project.addTarget( target, new Target( element ) );
+ targets.put( target, new Target( element ) );
}
else
{
@@ -359,10 +392,11 @@
throw new ProjectException( message );
}
}
+ targets.put( "<init>", new Target( implicit ) );
}
- private void buildProjectRef( final DefaultProject project,
- final ModelElement element )
+ private ProjectRef buildProjectRef( final File baseDirectory,
+ final ModelElement element )
throws ProjectException
{
final String name = element.getAttribute( "name" );
@@ -397,14 +431,12 @@
}
// Build the URL of the referenced projects
- final File baseDirectory = project.getBaseDirectory();
final File file = FileUtil.resolveFile( baseDirectory, location );
try
{
final String uri = file.getCanonicalFile().toString();
- final ProjectRef ref = new ProjectRef( name, uri );
- project.addProjectRef( ref );
+ return new ProjectRef( name, uri );
}
catch( final IOException ioe )
{
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>