1 package org.apache.maven.jelly;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.net.URL;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.commons.jelly.JellyContext;
28 import org.apache.commons.jelly.TagLibrary;
29 import org.apache.commons.jelly.XMLOutput;
30 import org.apache.commons.jelly.expression.Expression;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.maven.MavenConstants;
34 import org.apache.maven.MavenSession;
35 import org.apache.maven.jelly.tags.jeez.MavenJeezTagLibrary;
36 import org.apache.maven.project.Project;
37 import org.apache.maven.werkz.WerkzProject;
38 import org.codehaus.plexus.util.StringUtils;
39
40 /** Specialized version of a <code>JellyContext</code>.
41 *
42 * <p>
43 * This class simply provides hooks for accessing the jelly
44 * run-time execution. Typical idiom simply includes adding
45 * poor-man's-aspects (PMAs) to a <code>JellyContext</code>
46 * an explicitly invoking the super-class implementation
47 * of a given method.
48 * </p>
49 *
50 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
51 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
52 *
53 * @version $Id: MavenJellyContext.java 517014 2007-03-11 21:15:50Z ltheussl $
54 */
55 public class MavenJellyContext
56 extends JellyContext
57 {
58 /** LOGGER for output */
59 private final static Log LOGGER = LogFactory.getLog( MavenJellyContext.class );
60
61
62
63
64
65 /** Construct.
66 */
67 public MavenJellyContext()
68 {
69 super();
70 initializeContext();
71 }
72
73 /** Construct.
74 *
75 * @param rootContext The root context.
76 */
77 public MavenJellyContext( URL rootContext )
78 {
79 super( rootContext );
80 initializeContext();
81 }
82
83 /** Construct.
84 *
85 * @param parent Parent MavenJellyContext
86 */
87 public MavenJellyContext( MavenJellyContext parent )
88 {
89 super( parent );
90 initializeContext();
91 }
92
93 /**
94 * Create a context with this as the parent
95 * @return a new context
96 */
97 protected JellyContext createChildContext()
98 {
99 return new MavenJellyContext( this );
100 }
101
102
103
104
105
106 /**
107 * Initialize the context with any tag libs or other values we need.
108 */
109 private void initializeContext()
110 {
111 MavenJeezTagLibrary jeezTagLib = new MavenJeezTagLibrary();
112
113 registerTagLibrary( "jelly:mavenant", jeezTagLib );
114 registerTagLibrary( "", jeezTagLib );
115
116
117 if ( !isTagLibraryRegistered( "jelly:maven" ) )
118 {
119 registerTagLibrary( "jelly:maven", "org.apache.maven.jelly.tags.maven.MavenTagLibrary" );
120 }
121 }
122
123 /** Register a new tag library.
124 *
125 * <p>
126 * This implementation calls the superclass
127 * implementation after determining that there
128 * is no other tag-lib registered for the
129 * given <code>uri</code>.
130 * </p>
131 *
132 * @param uri Namespace URI to index the taglib.
133 * @param taglib The tag-lib to register.
134 */
135 public void registerTagLibrary( String uri, TagLibrary taglib )
136 {
137 if ( !isTagLibraryRegistered( uri ) )
138 {
139 super.registerTagLibrary( uri, taglib );
140 }
141 }
142
143 /**
144 * Register a new tag library.
145 *
146 * <p>
147 * This implementation calls the superclass
148 * implementation after determining that there
149 * is no other tag-lib registered for the
150 * given <code>namespaceURI</code>.
151 * </p>
152 *
153 * @param namespaceURI Namespace URI to index the taglib.
154 * @param className The class name of the tag-lib to register.
155 */
156 public void registerTagLibrary( String namespaceURI, String className )
157 {
158 if ( !isTagLibraryRegistered( namespaceURI ) )
159 {
160 super.registerTagLibrary( namespaceURI, className );
161 }
162 }
163
164 /**
165 * Retrieve a registered <code>TagLibrary</code> by namespace URI.
166 *
167 * @param uri The namespace URI of the tag-library.
168 *
169 * @return The matching tag-library, or <code>null</code> if none matches.
170 */
171 public TagLibrary getTagLibrary( String uri )
172 {
173 return super.getTagLibrary( uri );
174 }
175
176 /** Retrieve a variable.
177 *
178 * @param name The name of the variable.
179 *
180 * @return The value of the variable, if set, otherwise
181 * <code>null</code>.
182 */
183 public Object getVariable( String name )
184 {
185 Object value;
186
187
188
189 if ( isInherit() )
190 {
191 value = super.findVariable( name );
192 }
193 else
194 {
195 value = super.getVariable( name );
196 }
197
198 if ( value instanceof Expression )
199 {
200
201 Expression expression = (Expression) value;
202 value = expression.evaluate( this );
203 }
204
205 return value;
206 }
207
208 /**
209 * Convert a <code>String</code> property to a
210 * <code>Boolean</code> based on its contents. It would be nice
211 * if Jelly would deal with this automatically.
212 *
213 * @param key The property key to lookup and convert.
214 *
215 * @return The boolean value of the property if convertiable,
216 * otherwise <code>Boolean.FALSE</code>.
217 */
218 public Boolean getBoolean( String key )
219 {
220
221
222
223 Object value = getVariable( key );
224
225 if ( value instanceof Boolean )
226 {
227 return (Boolean) value;
228 }
229
230 String stringValue = (String) value;
231
232 if ( "true".equalsIgnoreCase( stringValue ) || "on".equalsIgnoreCase( stringValue ) || "1".equals( value ) )
233 {
234 return Boolean.TRUE;
235 }
236
237 return Boolean.FALSE;
238 }
239
240
241
242
243
244 /**
245 * Set the mavenSession attribute.
246 *
247 * @param mavenSession the session for the context
248 */
249 public void setMavenSession( MavenSession mavenSession )
250 {
251 setVariable( MavenConstants.SESSION, mavenSession );
252 }
253
254 /**
255 * Get the mavenSession attribute.
256 *
257 * @return The
258 */
259 public MavenSession getMavenSession()
260 {
261 return (MavenSession) getVariable( MavenConstants.SESSION );
262 }
263
264 public void setMavenRepoRemotes( List mavenRepoRemote )
265 {
266 setVariable( MavenConstants.REPO_REMOTE, convertListToCsvString( mavenRepoRemote ) );
267 }
268
269 /**
270 * Get the mavenRepoRemote attribute.
271 *
272 * @return The list of remote repositories.
273 */
274 public List getMavenRepoRemote()
275 {
276
277 return convertCsvStringToList( (String) getVariable( MavenConstants.REPO_REMOTE ) );
278 }
279
280 /**
281 * Convert a CSV list of values into a List.
282 *
283 * @param csvString CVS list of values.
284 * @return The List of value.
285 */
286 private List convertCsvStringToList( String csvString )
287 {
288 ArrayList list = new ArrayList();
289 String[] s = StringUtils.split( csvString, "," );
290
291 for ( int i = 0; i < s.length; i++ )
292 {
293 list.add( s[i] );
294 }
295
296 return list;
297 }
298
299 /**
300 * Convert a List into a CSV list of values.
301 *
302 * @param list list of values.
303 * @return The CVS string
304 */
305 private String convertListToCsvString( List list )
306 {
307 StringBuffer buf = new StringBuffer();
308
309 for ( int i = 0; i < list.size(); i++ )
310 {
311 if ( i > 0 )
312 {
313 buf.append( "," );
314 }
315 buf.append( list.get( i ) );
316 }
317
318 return buf.toString();
319 }
320
321 /**
322 * Set the location of the local maven repository.
323 *
324 * @param mavenRepoLocal The local repository.
325 */
326 public void setMavenRepoLocal( String mavenRepoLocal )
327 {
328 setVariable( MavenConstants.REPO_LOCAL, mavenRepoLocal );
329 }
330
331 /**
332 * Get the location of the local maven repository.
333 *
334 * @return The local repository.
335 */
336 public String getMavenRepoLocal()
337 {
338 return (String) getVariable( MavenConstants.REPO_LOCAL );
339 }
340
341 /**
342 * Set the jelly output sink.
343 *
344 * @param xmlOutput The jelly output sink.
345 */
346 public void setXMLOutput( XMLOutput xmlOutput )
347 {
348 setVariable( MavenConstants.XML_OUTPUT, xmlOutput );
349 }
350
351 /**
352 * Get the jelly output sink.
353 *
354 * @return The jelly output sink.
355 */
356 public XMLOutput getXMLOutput()
357 {
358 return (XMLOutput) getVariable( MavenConstants.XML_OUTPUT );
359 }
360
361 /**
362 * Set the online flag.
363 *
364 * @param online The online flag.
365 */
366 public void setOnline( Boolean online )
367 {
368 setVariable( MavenConstants.ONLINE, online );
369 }
370
371 /**
372 * Get the online flag.
373 *
374 * @return The online flag.
375 */
376 public Boolean getOnline()
377 {
378 return getBoolean( MavenConstants.ONLINE );
379 }
380
381 /**
382 * Set the maven project.
383 *
384 * @param project The maven project.
385 */
386 public void setProject( Project project )
387 {
388 setVariable( MavenConstants.MAVEN_POM, project );
389 }
390
391 /**
392 * Get the maven project.
393 *
394 * @return The maven project.
395 */
396 public Project getProject()
397 {
398 return (Project) getVariable( MavenConstants.MAVEN_POM );
399 }
400
401 /**
402 * Set the proxy host.
403 *
404 * @param proxyHost The proxy host.
405 */
406 public void setProxyHost( String proxyHost )
407 {
408 setVariable( MavenConstants.PROXY_HOST, proxyHost );
409 }
410
411 /**
412 * Get the proxy host.
413 *
414 * @return The proxy host.
415 */
416 public String getProxyHost()
417 {
418 return (String) getVariable( MavenConstants.PROXY_HOST );
419 }
420
421 /**
422 * Set the proxy port.
423 *
424 * @param proxyPort The proxy port.
425 */
426 public void setProxyPort( String proxyPort )
427 {
428 setVariable( MavenConstants.PROXY_PORT, proxyPort );
429 }
430
431 /**
432 * Get the proxy port.
433 *
434 * @return The proxy port.
435 */
436 public String getProxyPort()
437 {
438 return (String) getVariable( MavenConstants.PROXY_PORT );
439 }
440
441 /**
442 * Set the proxy user name.
443 *
444 * @param proxyUserName The user name setting for the proxy.
445 */
446 public void setProxyUserName( String proxyUserName )
447 {
448 setVariable( MavenConstants.PROXY_USERNAME, proxyUserName );
449 }
450
451 /**
452 * Get the proxy user name.
453 *
454 * @return The proxy user name.
455 */
456 public String getProxyUserName()
457 {
458 return (String) getVariable( MavenConstants.PROXY_USERNAME );
459 }
460
461 /**
462 * Set the proxy password.
463 *
464 * @param proxyPassword The proxy password.
465 */
466 public void setProxyPassword( String proxyPassword )
467 {
468 setVariable( MavenConstants.PROXY_PASSWORD, proxyPassword );
469 }
470
471 /**
472 * Get the proxy password.
473 *
474 * @return The ant project.
475 */
476 public String getProxyPassword()
477 {
478 return (String) getVariable( MavenConstants.PROXY_PASSWORD );
479 }
480
481
482
483 /**
484 * Set the debug flag.
485 *
486 * @param debug The debug flag.
487 */
488 public void setDebugOn( Boolean debug )
489 {
490 setVariable( MavenConstants.DEBUG_ON, debug );
491 }
492
493 /**
494 * Get the debug flag.
495 *
496 * @return The debug flag.
497 */
498 public Boolean getDebugOn()
499 {
500 Boolean b = (Boolean) getVariable( MavenConstants.DEBUG_ON );
501
502 if ( b == null )
503 {
504 return Boolean.FALSE;
505 }
506
507 return b;
508 }
509
510 /**
511 * Set the emacs mode flag.
512 *
513 * @param emacsModeOn The emacs mode flag.
514 */
515 public void setEmacsModeOn( Boolean emacsModeOn )
516 {
517 setVariable( MavenConstants.EMACS_MODE_ON, emacsModeOn );
518 }
519
520 /**
521 * Get the emacs mode flag.
522 *
523 * @return The emacs mode flag.
524 */
525 public Boolean getEmacsModeOn()
526 {
527 Boolean b = (Boolean) getVariable( MavenConstants.EMACS_MODE_ON );
528
529 if ( b == null )
530 {
531 return Boolean.FALSE;
532 }
533
534 return b;
535 }
536
537 /**
538 * Set flag indicating if the remote repository is enabled for use.
539 *
540 * @param remoteRepositoryEnabled Remote repository usage flag.
541 */
542 public void setRemoteRepositoryEnabled( Boolean remoteRepositoryEnabled )
543 {
544 setVariable( MavenConstants.REPO_REMOTE_ENABLED, remoteRepositoryEnabled );
545 }
546
547 /**
548 * Set flag indicating if the remote repository is enabled for use.
549 *
550 * @return Remote repository usage flag.
551 */
552 public Boolean getRemoteRepositoryEnabled()
553 {
554 return getBoolean( MavenConstants.REPO_REMOTE_ENABLED );
555 }
556
557 /**
558 * Set the flag indicating the use of the JAR overriding facility.
559 *
560 * @param mavenJarOverride MavenSession jar override flag.
561 */
562 public void setMavenJarOverride( Boolean mavenJarOverride )
563 {
564 setVariable( MavenConstants.JAR_OVERRIDE, mavenJarOverride );
565 }
566
567 /**
568 * Get the flag indicating the use of the JAR overriding facility.
569 *
570 * @return MavenSession jar override flag.
571 */
572 public Boolean getMavenJarOverride()
573 {
574 return getBoolean( MavenConstants.JAR_OVERRIDE );
575 }
576
577 /**
578 * Get the maven jar override value for a particular dependency.
579 *
580 * @param id Id of the project to override.
581 * @return MavenSession jar override value for a particular dependency.
582 */
583 public String getMavenJarOverride( String id )
584 {
585 return (String) getVariable( MavenConstants.JAR_OVERRIDE_PROPERTY + id );
586 }
587
588 /**
589 * Get MavenSession home.
590 *
591 * @return MavenSession home.
592 */
593 public String getMavenHome()
594 {
595 return (String) getVariable( MavenConstants.MAVEN_HOME );
596 }
597
598 /**
599 * retrieve the werkz project object
600 * @param werkzProject {@link org.apache.maven.werkz.WerkzProject Werkz Project}
601 */
602 public void setWerkzProject( WerkzProject werkzProject )
603 {
604 setVariable( MavenConstants.WERKZ_PROJECT, werkzProject );
605 }
606
607 /**
608 * retrieve the werkz project object
609 * @return the {@link org.apache.maven.werkz.WerkzProject Werkz Project}
610 */
611 public WerkzProject getWerkzProject()
612 {
613 return (WerkzProject) getVariable( MavenConstants.WERKZ_PROJECT );
614 }
615
616 /**
617 * Set the ant project.
618 *
619 * @param antProject The proxy password.
620 */
621 public void setAntProject( org.apache.tools.ant.Project antProject )
622 {
623 setVariable( MavenConstants.MAVEN_ANT_PROJECT, antProject );
624 }
625
626 /**
627 * Get the ant project.
628 *
629 * @return The ant project.
630 */
631 public org.apache.tools.ant.Project getAntProject()
632 {
633 return (org.apache.tools.ant.Project) getVariable( MavenConstants.MAVEN_ANT_PROJECT );
634 }
635
636 /**
637 * Display all maven variables
638 */
639 public void display()
640 {
641 for ( Iterator i = getVariableNames(); i.hasNext(); )
642 {
643 String key = (String) i.next();
644
645 if ( key.startsWith( "maven" ) )
646 {
647 LOGGER.info( key + ": " + getVariable( key ) );
648 }
649 }
650 }
651
652 /**
653 * Get local maven home.
654 *
655 * @return location of Maven local home installation.
656 */
657 public String getMavenHomeLocal()
658 {
659 return (String) getVariable( MavenConstants.MAVEN_HOME_LOCAL );
660 }
661
662 /**
663 * Get plugins location.
664 *
665 * @return plugins location.
666 */
667 public String getPluginsDir()
668 {
669 return (String) getVariable( MavenConstants.MAVEN_PLUGINS_DIR );
670 }
671
672 /**
673 * Get user plugins location.
674 *
675 * @return user plugins location.
676 */
677 public String getUserPluginsDir()
678 {
679 return (String) getVariable( MavenConstants.MAVEN_USER_PLUGINS_DIR );
680 }
681
682 /**
683 * Get unpacked plugins location.
684 *
685 * @return unpacked plugins location.
686 */
687 public String getUnpackedPluginsDir()
688 {
689 return (String) getVariable( MavenConstants.MAVEN_UNPACKED_PLUGINS_DIR );
690 }
691
692 /**
693 * @see JellyContext#setParent(JellyContext)
694 */
695 public void setParent( JellyContext context )
696 {
697 super.setParent( context );
698 }
699
700 public void resolveRelativePaths( File basedir )
701 {
702 resolveRelativePath( basedir, MavenConstants.REPO_LOCAL );
703 resolveRelativePath( basedir, MavenConstants.MAVEN_USER_PLUGINS_DIR );
704 resolveRelativePath( basedir, MavenConstants.MAVEN_UNPACKED_PLUGINS_DIR );
705 resolveRelativePath( basedir, MavenConstants.MAVEN_PLUGINS_DIR );
706 resolveRelativePath( basedir, MavenConstants.MAVEN_HOME_LOCAL );
707 resolveRelativePath( basedir, MavenConstants.MAVEN_HOME );
708 }
709
710 private void resolveRelativePath( File basedir, String var )
711 {
712 String value = (String) getVariable( var );
713 File f = new File( value );
714
715 if ( ( f.getParentFile() != null ) && !f.getParentFile().exists() )
716 {
717 f.getParentFile().mkdirs();
718 }
719
720
721 if ( !f.isAbsolute() && !value.startsWith( "/" ) && !value.startsWith( "\\" ) )
722 {
723 f = new File( basedir, f.getPath() );
724 LOGGER.debug( "Resolving " + var + " to " + f );
725 setVariable( var, f.getAbsolutePath() );
726 }
727 }
728 }