View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.tools.tasks;
9   
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.Iterator;
13  import org.apache.tools.ant.BuildException;
14  import org.apache.tools.ant.Project;
15  import org.apache.tools.ant.types.DataType;
16  import org.apache.tools.ant.types.Reference;
17  
18  /***
19   * An Ant type that represents a set of Plugins.
20   *
21   * @author Peter Donald
22   * @version $Revision: 1.3 $ $Date: 2003/11/29 09:28:02 $
23   */
24  public abstract class PluginSet
25      extends DataType
26  {
27      /*** Name for type of elements in set. */
28      private final String m_type;
29  
30      /*** Set of PluginElement objects. */
31      private final ArrayList m_plugins = new ArrayList();
32  
33      /*** Set of PluginSet objects. */
34      private final ArrayList m_sets = new ArrayList();
35  
36      /***
37       * Create plugin set for specified type.
38       *
39       * @param type the type
40       */
41      protected PluginSet( final String type )
42      {
43          if( null == type )
44          {
45              throw new NullPointerException( "type" );
46          }
47          m_type = type;
48      }
49  
50      /***
51       * Add a plugin to set.
52       *
53       * @param element the interceptor definition
54       */
55      void addPlugin( final PluginElement element )
56      {
57          m_plugins.add( element );
58      }
59  
60      /***
61       * Add a plugin to set.
62       *
63       * @param set the set
64       */
65      void addPluginSet( final PluginSet set )
66      {
67          m_sets.add( set );
68      }
69  
70      /***
71       * Convert PluginSet to a collection of plugins.
72       *
73       * @return the collection of Plugins
74       */
75      Collection toPlugins()
76      {
77          final Collection result = new ArrayList();
78          final Iterator elements = m_plugins.iterator();
79          while( elements.hasNext() )
80          {
81              final PluginElement element = (PluginElement)elements.next();
82              if( null == element.getName() )
83              {
84                  throw new BuildException( m_type + " must have a name" );
85              }
86          }
87          result.addAll( m_plugins );
88          final Iterator iterator = m_sets.iterator();
89          while( iterator.hasNext() )
90          {
91              final PluginSet set = (PluginSet)iterator.next();
92              result.addAll( set.toPlugins() );
93          }
94          return result;
95      }
96  
97      /***
98       * Makes this instance in effect a reference to another PluginSet instance.
99       *
100      * <p>You must not set another attribute or nest elements inside this
101      * element if you make it a reference.</p>
102      *
103      * @param reference the reference to which this instance is associated
104      * @throws BuildException if this instance already has been configured.
105      */
106     public void setRefid( final Reference reference )
107         throws BuildException
108     {
109         if( !m_plugins.isEmpty() || !m_sets.isEmpty() )
110         {
111             throw tooManyAttributes();
112         }
113         // change this to get the objects from the other reference
114         final Object object =
115             reference.getReferencedObject( getProject() );
116         log( "Referencing " + object, Project.MSG_DEBUG );
117         final Class clazz = getClass();
118         if( clazz.isInstance( object ) )
119         {
120             final PluginSet other = (PluginSet)object;
121             m_plugins.addAll( other.m_plugins );
122             m_sets.addAll( other.m_sets );
123         }
124         else
125         {
126             final String message = reference.getRefId() +
127                 " doesn\'t refer to a " + clazz.getName();
128             throw new BuildException( message );
129         }
130 
131         super.setRefid( reference );
132     }
133 }