View Javadoc
1   package org.codehaus.plexus.component.repository.io;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.PrintStream;
21  import java.io.Reader;
22  import java.io.StringReader;
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import org.codehaus.plexus.classworlds.realm.ClassRealm;
27  import org.codehaus.plexus.component.repository.ComponentDependency;
28  import org.codehaus.plexus.component.repository.ComponentDescriptor;
29  import org.codehaus.plexus.component.repository.ComponentRequirement;
30  import org.codehaus.plexus.component.repository.ComponentRequirementList;
31  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
32  import org.codehaus.plexus.configuration.PlexusConfiguration;
33  import org.codehaus.plexus.configuration.PlexusConfigurationException;
34  import org.codehaus.plexus.configuration.io.XmlPlexusConfigurationReader;
35  
36  
37  /**
38   * @author Jason van Zyl 
39   */
40  public class PlexusTools
41  {
42      public static PlexusConfiguration buildConfiguration( String resourceName, Reader configuration )
43          throws PlexusConfigurationException
44      {
45          try
46          {
47              XmlPlexusConfigurationReader reader = new XmlPlexusConfigurationReader();
48              
49              PlexusConfiguration result = reader.read( configuration );
50  
51              return result;
52          }
53          catch ( PlexusConfigurationException e )
54          {
55              throw new PlexusConfigurationException( "PlexusConfigurationException building configuration from: " + resourceName, e );
56          }
57          catch ( IOException e )
58          {
59              throw new PlexusConfigurationException( "IO error building configuration from: " + resourceName, e );
60          }
61      }
62  
63      public static PlexusConfiguration buildConfiguration( String configuration )
64          throws PlexusConfigurationException
65      {
66          return buildConfiguration( "<String Memory Resource>", new StringReader( configuration ) );
67      }
68  
69      public static ComponentDescriptor<?> buildComponentDescriptor( String configuration, ClassRealm realm  )
70          throws PlexusConfigurationException
71      {
72          return buildComponentDescriptor( buildConfiguration( configuration ), realm );
73      }
74  
75      public static ComponentDescriptor<?> buildComponentDescriptor( PlexusConfiguration configuration )
76          throws PlexusConfigurationException
77      {
78          return buildComponentDescriptorImpl( configuration, null );
79      }
80      
81      public static ComponentDescriptor<?> buildComponentDescriptor( PlexusConfiguration configuration, ClassRealm realm )
82          throws PlexusConfigurationException
83      {
84          if ( realm == null )
85          {
86              throw new NullPointerException( "realm is null" );
87          }
88  
89          return buildComponentDescriptorImpl( configuration, realm );
90      }
91  
92      private static ComponentDescriptor<?> buildComponentDescriptorImpl( PlexusConfiguration configuration,
93                                                                          ClassRealm realm )
94          throws PlexusConfigurationException
95      {
96          String implementation = configuration.getChild( "implementation" ).getValue();
97          if (implementation == null)
98          {
99              throw new PlexusConfigurationException( "implementation is null" );
100         }
101 
102         ComponentDescriptor<?> cd;
103         try
104         {
105             if ( realm != null )
106             {
107                 Class<?> implementationClass = realm.loadClass( implementation );
108                 cd = new ComponentDescriptor(implementationClass, realm);
109             }
110             else
111             {
112                 cd = new ComponentDescriptor();
113                 cd.setImplementation( implementation );
114             }
115         }
116         catch ( Throwable e )
117         {
118             throw new PlexusConfigurationException("Can not load implementation class " + implementation +
119                 " from realm " + realm, e);
120         }
121 
122         cd.setRole( configuration.getChild( "role" ).getValue() );
123 
124         cd.setRoleHint( configuration.getChild( "role-hint" ).getValue() );
125 
126         cd.setVersion( configuration.getChild( "version" ).getValue() );
127 
128         cd.setComponentType( configuration.getChild( "component-type" ).getValue() );
129 
130         cd.setInstantiationStrategy( configuration.getChild( "instantiation-strategy" ).getValue() );
131 
132         cd.setLifecycleHandler( configuration.getChild( "lifecycle-handler" ).getValue() );
133 
134         cd.setComponentProfile( configuration.getChild( "component-profile" ).getValue() );
135 
136         cd.setComponentComposer( configuration.getChild( "component-composer" ).getValue() );
137 
138         cd.setComponentConfigurator( configuration.getChild( "component-configurator" ).getValue() );
139 
140         cd.setComponentFactory( configuration.getChild( "component-factory" ).getValue() );
141 
142         cd.setDescription( configuration.getChild( "description" ).getValue() );
143 
144         cd.setAlias( configuration.getChild( "alias" ).getValue() );
145 
146         String s = configuration.getChild( "isolated-realm" ).getValue();
147 
148         if ( s != null )
149         {
150             cd.setIsolatedRealm( s.equals( "true" ) ? true : false );
151         }
152 
153         // ----------------------------------------------------------------------
154         // Here we want to look for directives for inlining external
155         // configurations. we probably want to take them from files or URLs.
156         // ----------------------------------------------------------------------
157 
158         cd.setConfiguration( configuration.getChild( "configuration" ) );
159 
160         // ----------------------------------------------------------------------
161         // Requirements
162         // ----------------------------------------------------------------------
163 
164         PlexusConfiguration[] requirements = configuration.getChild( "requirements" ).getChildren( "requirement" );
165 
166         for (PlexusConfiguration requirement : requirements) {
167             ComponentRequirement cr;
168 
169             PlexusConfiguration[] hints = requirement.getChild("role-hints").getChildren("role-hint");
170             if (hints != null && hints.length > 0) {
171                 cr = new ComponentRequirementList();
172 
173                 List<String> hintList = new LinkedList<String>();
174                 for (PlexusConfiguration hint : hints) {
175                     hintList.add(hint.getValue());
176                 }
177 
178                 ((ComponentRequirementList) cr).setRoleHints(hintList);
179             } else {
180                 cr = new ComponentRequirement();
181 
182                 cr.setRoleHint(requirement.getChild("role-hint").getValue());
183             }
184 
185             cr.setRole(requirement.getChild("role").getValue());
186 
187             cr.setOptional(Boolean.parseBoolean(requirement.getChild("optional").getValue()));
188 
189             cr.setFieldName(requirement.getChild("field-name").getValue());
190 
191             cd.addRequirement(cr);
192         }
193 
194         return cd;
195     }
196 
197     public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c )
198         throws PlexusConfigurationException
199     {
200         return buildComponentSet( c, null );
201     }
202 
203     public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c, ClassRealm realm )
204         throws PlexusConfigurationException
205     {
206         ComponentSetDescriptor csd = new ComponentSetDescriptor();
207 
208         // ----------------------------------------------------------------------
209         // Components
210         // ----------------------------------------------------------------------
211 
212         PlexusConfiguration[] components = c.getChild( "components" ).getChildren( "component" );
213 
214         for (PlexusConfiguration component : components) {
215             csd.addComponentDescriptor(buildComponentDescriptorImpl(component, realm));
216         }
217 
218         // ----------------------------------------------------------------------
219         // Dependencies
220         // ----------------------------------------------------------------------
221 
222         PlexusConfiguration[] dependencies = c.getChild( "dependencies" ).getChildren( "dependency" );
223 
224         for (PlexusConfiguration d : dependencies) {
225             ComponentDependency cd = new ComponentDependency();
226 
227             cd.setArtifactId(d.getChild("artifact-id").getValue());
228 
229             cd.setGroupId(d.getChild("group-id").getValue());
230 
231             String type = d.getChild("type").getValue();
232             if (type != null) {
233                 cd.setType(type);
234             }
235 
236             cd.setVersion(d.getChild("version").getValue());
237 
238             csd.addDependency(cd);
239         }
240 
241         return csd;
242     }
243 
244     public static void writeConfiguration( PrintStream out, PlexusConfiguration configuration )
245         throws PlexusConfigurationException
246     {
247         writeConfiguration( out, configuration, "" );
248     }
249 
250     private static void writeConfiguration( PrintStream out, PlexusConfiguration configuration, String indent )
251         throws PlexusConfigurationException
252     {
253         out.print( indent + "<" + configuration.getName() );
254         String[] atts = configuration.getAttributeNames();
255 
256         if ( atts.length > 0 )
257         {
258             for (String att : atts) {
259                 out.print("\n" + indent + "  " + att + "='" + configuration.getAttribute(att) + "'");
260             }
261         }
262 
263         PlexusConfiguration[] pc = configuration.getChildren();
264 
265         if ( ( configuration.getValue() != null && configuration.getValue().trim().length() > 0 ) || pc.length > 0 )
266         {
267             out.print( ">" + ( configuration.getValue() == null ? "" : configuration.getValue().trim() ) );
268 
269             if ( pc.length > 0 )
270             {
271                 out.println();
272                 for (PlexusConfiguration aPc : pc) {
273                     writeConfiguration(out, aPc, indent + "  ");
274                 }
275                 out.print( indent );
276             }
277 
278             out.println( "</" + configuration.getName() + ">" );
279         }
280         else
281         {
282             out.println( "/>" );
283         }
284     }
285 
286 }