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   * @author Jason van Zyl
38   */
39  public class PlexusTools {
40      public static PlexusConfiguration buildConfiguration(String resourceName, Reader configuration)
41              throws PlexusConfigurationException {
42          try {
43              XmlPlexusConfigurationReader reader = new XmlPlexusConfigurationReader();
44  
45              PlexusConfiguration result = reader.read(configuration);
46  
47              return result;
48          } catch (PlexusConfigurationException e) {
49              throw new PlexusConfigurationException(
50                      "PlexusConfigurationException building configuration from: " + resourceName, e);
51          } catch (IOException e) {
52              throw new PlexusConfigurationException("IO error building configuration from: " + resourceName, e);
53          }
54      }
55  
56      public static PlexusConfiguration buildConfiguration(String configuration) throws PlexusConfigurationException {
57          return buildConfiguration("<String Memory Resource>", new StringReader(configuration));
58      }
59  
60      public static ComponentDescriptor<?> buildComponentDescriptor(String configuration, ClassRealm realm)
61              throws PlexusConfigurationException {
62          return buildComponentDescriptor(buildConfiguration(configuration), realm);
63      }
64  
65      public static ComponentDescriptor<?> buildComponentDescriptor(PlexusConfiguration configuration)
66              throws PlexusConfigurationException {
67          return buildComponentDescriptorImpl(configuration, null);
68      }
69  
70      public static ComponentDescriptor<?> buildComponentDescriptor(PlexusConfiguration configuration, ClassRealm realm)
71              throws PlexusConfigurationException {
72          if (realm == null) {
73              throw new NullPointerException("realm is null");
74          }
75  
76          return buildComponentDescriptorImpl(configuration, realm);
77      }
78  
79      private static ComponentDescriptor<?> buildComponentDescriptorImpl(
80              PlexusConfiguration configuration, ClassRealm realm) throws PlexusConfigurationException {
81          String implementation = configuration.getChild("implementation").getValue();
82          if (implementation == null) {
83              throw new PlexusConfigurationException("implementation is null");
84          }
85  
86          ComponentDescriptor<?> cd;
87          try {
88              if (realm != null) {
89                  Class<?> implementationClass = realm.loadClass(implementation);
90                  cd = new ComponentDescriptor(implementationClass, realm);
91              } else {
92                  cd = new ComponentDescriptor();
93                  cd.setImplementation(implementation);
94              }
95          } catch (Throwable e) {
96              throw new PlexusConfigurationException(
97                      "Can not load implementation class " + implementation + " from realm " + realm, e);
98          }
99  
100         cd.setRole(configuration.getChild("role").getValue());
101 
102         cd.setRoleHint(configuration.getChild("role-hint").getValue());
103 
104         cd.setVersion(configuration.getChild("version").getValue());
105 
106         cd.setComponentType(configuration.getChild("component-type").getValue());
107 
108         cd.setInstantiationStrategy(
109                 configuration.getChild("instantiation-strategy").getValue());
110 
111         cd.setLifecycleHandler(configuration.getChild("lifecycle-handler").getValue());
112 
113         cd.setComponentProfile(configuration.getChild("component-profile").getValue());
114 
115         cd.setComponentComposer(configuration.getChild("component-composer").getValue());
116 
117         cd.setComponentConfigurator(
118                 configuration.getChild("component-configurator").getValue());
119 
120         cd.setComponentFactory(configuration.getChild("component-factory").getValue());
121 
122         cd.setDescription(configuration.getChild("description").getValue());
123 
124         cd.setAlias(configuration.getChild("alias").getValue());
125 
126         String s = configuration.getChild("isolated-realm").getValue();
127 
128         if (s != null) {
129             cd.setIsolatedRealm(s.equals("true") ? true : false);
130         }
131 
132         // ----------------------------------------------------------------------
133         // Here we want to look for directives for inlining external
134         // configurations. we probably want to take them from files or URLs.
135         // ----------------------------------------------------------------------
136 
137         cd.setConfiguration(configuration.getChild("configuration"));
138 
139         // ----------------------------------------------------------------------
140         // Requirements
141         // ----------------------------------------------------------------------
142 
143         PlexusConfiguration[] requirements =
144                 configuration.getChild("requirements").getChildren("requirement");
145 
146         for (PlexusConfiguration requirement : requirements) {
147             ComponentRequirement cr;
148 
149             PlexusConfiguration[] hints = requirement.getChild("role-hints").getChildren("role-hint");
150             if (hints != null && hints.length > 0) {
151                 cr = new ComponentRequirementList();
152 
153                 List<String> hintList = new LinkedList<String>();
154                 for (PlexusConfiguration hint : hints) {
155                     hintList.add(hint.getValue());
156                 }
157 
158                 ((ComponentRequirementList) cr).setRoleHints(hintList);
159             } else {
160                 cr = new ComponentRequirement();
161 
162                 cr.setRoleHint(requirement.getChild("role-hint").getValue());
163             }
164 
165             cr.setRole(requirement.getChild("role").getValue());
166 
167             cr.setOptional(Boolean.parseBoolean(requirement.getChild("optional").getValue()));
168 
169             cr.setFieldName(requirement.getChild("field-name").getValue());
170 
171             cd.addRequirement(cr);
172         }
173 
174         return cd;
175     }
176 
177     public static ComponentSetDescriptor buildComponentSet(PlexusConfiguration c) throws PlexusConfigurationException {
178         return buildComponentSet(c, null);
179     }
180 
181     public static ComponentSetDescriptor buildComponentSet(PlexusConfiguration c, ClassRealm realm)
182             throws PlexusConfigurationException {
183         ComponentSetDescriptor csd = new ComponentSetDescriptor();
184 
185         // ----------------------------------------------------------------------
186         // Components
187         // ----------------------------------------------------------------------
188 
189         PlexusConfiguration[] components = c.getChild("components").getChildren("component");
190 
191         for (PlexusConfiguration component : components) {
192             csd.addComponentDescriptor(buildComponentDescriptorImpl(component, realm));
193         }
194 
195         // ----------------------------------------------------------------------
196         // Dependencies
197         // ----------------------------------------------------------------------
198 
199         PlexusConfiguration[] dependencies = c.getChild("dependencies").getChildren("dependency");
200 
201         for (PlexusConfiguration d : dependencies) {
202             ComponentDependency cd = new ComponentDependency();
203 
204             cd.setArtifactId(d.getChild("artifact-id").getValue());
205 
206             cd.setGroupId(d.getChild("group-id").getValue());
207 
208             String type = d.getChild("type").getValue();
209             if (type != null) {
210                 cd.setType(type);
211             }
212 
213             cd.setVersion(d.getChild("version").getValue());
214 
215             csd.addDependency(cd);
216         }
217 
218         return csd;
219     }
220 
221     public static void writeConfiguration(PrintStream out, PlexusConfiguration configuration)
222             throws PlexusConfigurationException {
223         writeConfiguration(out, configuration, "");
224     }
225 
226     private static void writeConfiguration(PrintStream out, PlexusConfiguration configuration, String indent)
227             throws PlexusConfigurationException {
228         out.print(indent + "<" + configuration.getName());
229         String[] atts = configuration.getAttributeNames();
230 
231         if (atts.length > 0) {
232             for (String att : atts) {
233                 out.print("\n" + indent + "  " + att + "='" + configuration.getAttribute(att) + "'");
234             }
235         }
236 
237         PlexusConfiguration[] pc = configuration.getChildren();
238 
239         if ((configuration.getValue() != null && configuration.getValue().trim().length() > 0) || pc.length > 0) {
240             out.print(">"
241                     + (configuration.getValue() == null
242                             ? ""
243                             : configuration.getValue().trim()));
244 
245             if (pc.length > 0) {
246                 out.println();
247                 for (PlexusConfiguration aPc : pc) {
248                     writeConfiguration(out, aPc, indent + "  ");
249                 }
250                 out.print(indent);
251             }
252 
253             out.println("</" + configuration.getName() + ">");
254         } else {
255             out.println("/>");
256         }
257     }
258 }