View Javadoc

1   /*$Id: CompositeApplicationDescriptionLibrary.java,v 1.5 2005/01/23 12:52:26 jdt Exp $
2    * Created on 09-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.description;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import org.astrogrid.applications.description.exception.ApplicationDescriptionNotFoundException;
17  import org.astrogrid.component.descriptor.ComponentDescriptor;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Set;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  /*** An {@link org.astrogrid.applications.description.ApplicationDescriptionLibrary} that composes together other <tt>ApplicationDescriptionLibrary</tt>s 
30   * @author Noel Winstanley nw@jb.man.ac.uk 09-Jun-2004
31   *
32   */
33  public class CompositeApplicationDescriptionLibrary implements ApplicationDescriptionLibrary, ComponentDescriptor {
34      /***
35       * Commons Logger for this class
36       */
37      private static final Log logger = LogFactory
38              .getLog(CompositeApplicationDescriptionLibrary.class);
39  
40      /*** Construct a new CompositeApplicationDescriptionLibrary
41       * 
42       */
43      public CompositeApplicationDescriptionLibrary() {
44          super();
45      }
46      protected final Set libs = new HashSet();
47      /***
48       * @see org.astrogrid.applications.description.ApplicationDescriptionLibrary#getDescription(java.lang.String)
49       */
50      public ApplicationDescription getDescription(String name) throws ApplicationDescriptionNotFoundException {
51          logger.debug("Getting description for "+name+" from composite library");
52          logger.debug("Composite library contains "+libs.size()+" libraries");
53          for (Iterator i = libs.iterator(); i.hasNext(); ) {
54              ApplicationDescriptionLibrary lib = (ApplicationDescriptionLibrary)i.next();
55              logger.debug("Checking library "+lib+".  This library has "+lib.getApplicationNames().length+" entries");
56              try {
57                  ApplicationDescription desc = lib.getDescription(name);
58                  logger.debug("Found description");
59                  // shouldn't return null, but sanity check.
60                  if (desc != null) {
61                      return desc;
62                  }
63              } catch (ApplicationDescriptionNotFoundException e) {
64                  // oh well, not there then.
65              }
66          }
67          // not found
68          throw new ApplicationDescriptionNotFoundException(name);
69      }
70      /***
71       * @see org.astrogrid.applications.description.ApplicationDescriptionLibrary#getApplicationNames()
72       */
73      public String[] getApplicationNames() {
74          List results = new ArrayList();
75          for (Iterator i = libs.iterator(); i.hasNext(); ) {
76              ApplicationDescriptionLibrary lib = (ApplicationDescriptionLibrary)i.next();
77              String[] names = lib.getApplicationNames();
78              results.addAll(Arrays.asList(names));
79          }
80          return (String[])results.toArray(new String[]{});
81      }
82      
83      /*** add another library to the composite */
84      public void addLibrary(ApplicationDescriptionLibrary lib) {
85          libs.add(lib);
86      }
87      /***
88       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
89       */
90      public String getName() {
91          return "Composite Application Description Library";
92      }
93      /***
94       * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
95       */
96      public String getDescription() {
97          StringBuffer libDescs = new StringBuffer();
98          for (Iterator i = libs.iterator(); i.hasNext(); ) {
99              ApplicationDescriptionLibrary lib = (ApplicationDescriptionLibrary)i.next();
100             libDescs.append("\n");
101             if (lib instanceof ComponentDescriptor) {
102                  libDescs.append(((ComponentDescriptor)lib).getDescription());
103             } else {
104                 libDescs.append(lib.getClass().getName());
105                 libDescs.append("\n");
106                 libDescs.append(Arrays.asList(lib.getApplicationNames()));                
107             }
108         }
109         return "Composes a set of description libraries into one. Currently contains " + libDescs.toString();
110     }
111     /***
112      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
113      */
114     public Test getInstallationTest() {
115         // join together container installationi tests.
116         TestSuite suite = new TestSuite("Tests for " + this.getName());
117         for (Iterator i = libs.iterator(); i.hasNext(); ) {
118             Object o = i.next();
119             if (o instanceof ComponentDescriptor) {
120                 Test t= ((ComponentDescriptor)o).getInstallationTest();
121                 if (t != null) {
122                     suite.addTest(t);
123                 }
124             }
125         }
126         return suite;
127     }
128 }
129 
130 
131 /* 
132 $Log: CompositeApplicationDescriptionLibrary.java,v $
133 Revision 1.5  2005/01/23 12:52:26  jdt
134 merge from cea_jdt_902
135 
136 Revision 1.4.88.1  2005/01/22 13:57:01  jdt
137 added some logging.
138 
139 Revision 1.4  2004/08/11 16:52:11  nw
140 added logger
141 
142 Revision 1.3  2004/07/26 00:57:46  nw
143 javadoc
144 
145 Revision 1.2  2004/07/01 11:16:22  nw
146 merged in branch
147 nww-itn06-componentization
148 
149 Revision 1.1.2.2  2004/07/01 01:42:46  nw
150 final version, before merge
151 
152 Revision 1.1.2.1  2004/06/14 08:56:58  nw
153 factored applications into sub-projects,
154 got packaging of wars to work again
155  
156 */