Coverage Report - org.codehaus.plexus.component.repository.io.PlexusTools
 
Classes in this File Line Coverage Branch Coverage Complexity
PlexusTools
66 %
62/93
40 %
16/40
3,9
 
 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  0
 public class PlexusTools
 41  
 {
 42  
     public static PlexusConfiguration buildConfiguration( String resourceName, Reader configuration )
 43  
         throws PlexusConfigurationException
 44  
     {
 45  
         try
 46  
         {
 47  611
             XmlPlexusConfigurationReader reader = new XmlPlexusConfigurationReader();
 48  
             
 49  611
             PlexusConfiguration result = reader.read( configuration );
 50  
 
 51  609
             return result;
 52  
         }
 53  2
         catch ( PlexusConfigurationException e )
 54  
         {
 55  2
             throw new PlexusConfigurationException( "PlexusConfigurationException building configuration from: " + resourceName, e );
 56  
         }
 57  0
         catch ( IOException e )
 58  
         {
 59  0
             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  10
         return buildConfiguration( "<String Memory Resource>", new StringReader( configuration ) );
 67  
     }
 68  
 
 69  
     public static ComponentDescriptor<?> buildComponentDescriptor( String configuration, ClassRealm realm  )
 70  
         throws PlexusConfigurationException
 71  
     {
 72  9
         return buildComponentDescriptor( buildConfiguration( configuration ), realm );
 73  
     }
 74  
 
 75  
     public static ComponentDescriptor<?> buildComponentDescriptor( PlexusConfiguration configuration )
 76  
         throws PlexusConfigurationException
 77  
     {
 78  0
         return buildComponentDescriptorImpl( configuration, null );
 79  
     }
 80  
     
 81  
     public static ComponentDescriptor<?> buildComponentDescriptor( PlexusConfiguration configuration, ClassRealm realm )
 82  
         throws PlexusConfigurationException
 83  
     {
 84  2357
         if ( realm == null )
 85  
         {
 86  0
             throw new NullPointerException( "realm is null" );
 87  
         }
 88  
 
 89  2354
         return buildComponentDescriptorImpl( configuration, realm );
 90  
     }
 91  
 
 92  
     private static ComponentDescriptor<?> buildComponentDescriptorImpl( PlexusConfiguration configuration,
 93  
                                                                         ClassRealm realm )
 94  
         throws PlexusConfigurationException
 95  
     {
 96  2356
         String implementation = configuration.getChild( "implementation" ).getValue();
 97  2356
         if (implementation == null)
 98  
         {
 99  0
             throw new PlexusConfigurationException( "implementation is null" );
 100  
         }
 101  
 
 102  
         ComponentDescriptor<?> cd;
 103  
         try
 104  
         {
 105  2355
             if ( realm != null )
 106  
             {
 107  2355
                 Class<?> implementationClass = realm.loadClass( implementation );
 108  2358
                 cd = new ComponentDescriptor(implementationClass, realm);
 109  2359
             }
 110  
             else
 111  
             {
 112  0
                 cd = new ComponentDescriptor();
 113  0
                 cd.setImplementation( implementation );
 114  
             }
 115  
         }
 116  0
         catch ( Throwable e )
 117  
         {
 118  0
             throw new PlexusConfigurationException("Can not load implementation class " + implementation +
 119  
                 " from realm " + realm, e);
 120  2359
         }
 121  
 
 122  2359
         cd.setRole( configuration.getChild( "role" ).getValue() );
 123  
 
 124  2360
         cd.setRoleHint( configuration.getChild( "role-hint" ).getValue() );
 125  
 
 126  2359
         cd.setVersion( configuration.getChild( "version" ).getValue() );
 127  
 
 128  2359
         cd.setComponentType( configuration.getChild( "component-type" ).getValue() );
 129  
 
 130  2360
         cd.setInstantiationStrategy( configuration.getChild( "instantiation-strategy" ).getValue() );
 131  
 
 132  2359
         cd.setLifecycleHandler( configuration.getChild( "lifecycle-handler" ).getValue() );
 133  
 
 134  2360
         cd.setComponentProfile( configuration.getChild( "component-profile" ).getValue() );
 135  
 
 136  2360
         cd.setComponentComposer( configuration.getChild( "component-composer" ).getValue() );
 137  
 
 138  2360
         cd.setComponentConfigurator( configuration.getChild( "component-configurator" ).getValue() );
 139  
 
 140  2360
         cd.setComponentFactory( configuration.getChild( "component-factory" ).getValue() );
 141  
 
 142  2360
         cd.setDescription( configuration.getChild( "description" ).getValue() );
 143  
 
 144  2359
         cd.setAlias( configuration.getChild( "alias" ).getValue() );
 145  
 
 146  2359
         String s = configuration.getChild( "isolated-realm" ).getValue();
 147  
 
 148  2360
         if ( s != null )
 149  
         {
 150  0
             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  2360
         cd.setConfiguration( configuration.getChild( "configuration" ) );
 159  
 
 160  
         // ----------------------------------------------------------------------
 161  
         // Requirements
 162  
         // ----------------------------------------------------------------------
 163  
 
 164  2360
         PlexusConfiguration[] requirements = configuration.getChild( "requirements" ).getChildren( "requirement" );
 165  
 
 166  3113
         for (PlexusConfiguration requirement : requirements) {
 167  
             ComponentRequirement cr;
 168  
 
 169  754
             PlexusConfiguration[] hints = requirement.getChild("role-hints").getChildren("role-hint");
 170  754
             if (hints != null && hints.length > 0) {
 171  84
                 cr = new ComponentRequirementList();
 172  
 
 173  84
                 List<String> hintList = new LinkedList<String>();
 174  252
                 for (PlexusConfiguration hint : hints) {
 175  168
                     hintList.add(hint.getValue());
 176  
                 }
 177  
 
 178  84
                 ((ComponentRequirementList) cr).setRoleHints(hintList);
 179  84
             } else {
 180  670
                 cr = new ComponentRequirement();
 181  
 
 182  670
                 cr.setRoleHint(requirement.getChild("role-hint").getValue());
 183  
             }
 184  
 
 185  754
             cr.setRole(requirement.getChild("role").getValue());
 186  
 
 187  754
             cr.setOptional(Boolean.parseBoolean(requirement.getChild("optional").getValue()));
 188  
 
 189  754
             cr.setFieldName(requirement.getChild("field-name").getValue());
 190  
 
 191  754
             cd.addRequirement(cr);
 192  
         }
 193  
 
 194  2359
         return cd;
 195  
     }
 196  
 
 197  
     public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c )
 198  
         throws PlexusConfigurationException
 199  
     {
 200  0
         return buildComponentSet( c, null );
 201  
     }
 202  
 
 203  
     public static ComponentSetDescriptor buildComponentSet( PlexusConfiguration c, ClassRealm realm )
 204  
         throws PlexusConfigurationException
 205  
     {
 206  1
         ComponentSetDescriptor csd = new ComponentSetDescriptor();
 207  
 
 208  
         // ----------------------------------------------------------------------
 209  
         // Components
 210  
         // ----------------------------------------------------------------------
 211  
 
 212  1
         PlexusConfiguration[] components = c.getChild( "components" ).getChildren( "component" );
 213  
 
 214  2
         for (PlexusConfiguration component : components) {
 215  1
             csd.addComponentDescriptor(buildComponentDescriptorImpl(component, realm));
 216  
         }
 217  
 
 218  
         // ----------------------------------------------------------------------
 219  
         // Dependencies
 220  
         // ----------------------------------------------------------------------
 221  
 
 222  1
         PlexusConfiguration[] dependencies = c.getChild( "dependencies" ).getChildren( "dependency" );
 223  
 
 224  2
         for (PlexusConfiguration d : dependencies) {
 225  1
             ComponentDependency cd = new ComponentDependency();
 226  
 
 227  1
             cd.setArtifactId(d.getChild("artifact-id").getValue());
 228  
 
 229  1
             cd.setGroupId(d.getChild("group-id").getValue());
 230  
 
 231  1
             String type = d.getChild("type").getValue();
 232  1
             if (type != null) {
 233  0
                 cd.setType(type);
 234  
             }
 235  
 
 236  1
             cd.setVersion(d.getChild("version").getValue());
 237  
 
 238  1
             csd.addDependency(cd);
 239  
         }
 240  
 
 241  1
         return csd;
 242  
     }
 243  
 
 244  
     public static void writeConfiguration( PrintStream out, PlexusConfiguration configuration )
 245  
         throws PlexusConfigurationException
 246  
     {
 247  0
         writeConfiguration( out, configuration, "" );
 248  0
     }
 249  
 
 250  
     private static void writeConfiguration( PrintStream out, PlexusConfiguration configuration, String indent )
 251  
         throws PlexusConfigurationException
 252  
     {
 253  0
         out.print( indent + "<" + configuration.getName() );
 254  0
         String[] atts = configuration.getAttributeNames();
 255  
 
 256  0
         if ( atts.length > 0 )
 257  
         {
 258  0
             for (String att : atts) {
 259  0
                 out.print("\n" + indent + "  " + att + "='" + configuration.getAttribute(att) + "'");
 260  
             }
 261  
         }
 262  
 
 263  0
         PlexusConfiguration[] pc = configuration.getChildren();
 264  
 
 265  0
         if ( ( configuration.getValue() != null && configuration.getValue().trim().length() > 0 ) || pc.length > 0 )
 266  
         {
 267  0
             out.print( ">" + ( configuration.getValue() == null ? "" : configuration.getValue().trim() ) );
 268  
 
 269  0
             if ( pc.length > 0 )
 270  
             {
 271  0
                 out.println();
 272  0
                 for (PlexusConfiguration aPc : pc) {
 273  0
                     writeConfiguration(out, aPc, indent + "  ");
 274  
                 }
 275  0
                 out.print( indent );
 276  
             }
 277  
 
 278  0
             out.println( "</" + configuration.getName() + ">" );
 279  
         }
 280  
         else
 281  
         {
 282  0
             out.println( "/>" );
 283  
         }
 284  0
     }
 285  
 
 286  
 }