1 package org.apache.maven.werkz.jelly;
2
3 /* ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 */
20
21 /*
22 $Id: AttainGoalTag.java 517014 2007-03-11 21:15:50Z ltheussl $
23
24 Copyright 2002 (C) The Werken Company. All Rights Reserved.
25
26 Redistribution and use of this software and associated documentation
27 ("Software"), with or without modification, are permitted provided
28 that the following conditions are met:
29
30 1. Redistributions of source code must retain copyright
31 statements and notices. Redistributions must also contain a
32 copy of this document.
33
34 2. Redistributions in binary form must reproduce the
35 above copyright notice, this list of conditions and the
36 following disclaimer in the documentation and/or other
37 materials provided with the distribution.
38
39 3. The name "werkz" must not be used to endorse or promote
40 products derived from this Software without prior written
41 permission of The Werken Company. For written permission,
42 please contact bob@werken.com.
43
44 4. Products derived from this Software may not be called "werkz"
45 nor may "werkz" appear in their names without prior written
46 permission of The Werken Company. "werkz" is a registered
47 trademark of The Werken Company.
48
49 5. Due credit should be given to "the werkz project"
50 ( http://werkz.werken.com/ ).
51
52 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
53 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
54 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
55 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
56 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
61 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
63 OF THE POSSIBILITY OF SUCH DAMAGE.
64
65 */
66
67 import org.apache.commons.jelly.JellyTagException;
68 import org.apache.commons.jelly.XMLOutput;
69 import org.apache.maven.werkz.NoActionDefinitionException;
70 import org.apache.maven.werkz.NoSuchGoalException;
71 import org.apache.maven.werkz.Session;
72 import org.apache.maven.werkz.UnattainableGoalException;
73 import org.apache.maven.werkz.WerkzProject;
74
75 /**
76 * Attains one or more goals.
77 *
78 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
79 * @version $Revision: 1.4 $
80 */
81 public class AttainGoalTag
82 extends WerkzTagSupport
83 {
84 /** The goal name. */
85 private String name;
86
87 /** The session. */
88 private Session session;
89
90 /** Construct.
91 */
92 public AttainGoalTag()
93 {
94 // intentionally left blank
95 }
96
97 /** Set the <code>Session</code> to use.
98 *
99 * @param session The session.
100 */
101 public void setSession( Session session )
102 {
103 this.session = session;
104 }
105
106 /** Retrieve the <code>Session</code> to use, if set.
107 *
108 * @return The session or null.
109 */
110 public Session getSession()
111 {
112 return this.session;
113 }
114
115 // Tag interface
116 //-------------------------------------------------------------------------
117
118 /**
119 * Evaluate the body to register all the various goals and pre/post conditions
120 * then run all the current targets
121 */
122 public void doTag( final XMLOutput output )
123 throws JellyTagException
124 {
125 AttainTag attainTag = (AttainTag) findAncestorWithClass( AttainTag.class );
126
127 Session session = null;
128
129 if ( this.session != null )
130 {
131 session = this.session;
132 }
133 else if ( attainTag == null )
134 {
135 session = new JellySession( output );
136 }
137 else
138 {
139 session = attainTag.getSession();
140 }
141
142 WerkzProject project = getProject();
143
144 if ( project == null )
145 {
146 throw new JellyTagException( "No Project available" );
147 }
148
149 invokeBody( output );
150
151 try
152 {
153 project.attainGoal( getName(), session );
154 }
155 catch ( UnattainableGoalException e )
156 {
157 Throwable root = e.getRootCause();
158
159 if ( root != null )
160 {
161 if ( root instanceof JellyTagException )
162 {
163 throw (JellyTagException) root;
164 }
165 if ( root instanceof UnattainableGoalException )
166 {
167 throw new JellyTagException( e );
168 }
169 }
170 else
171 {
172 e.fillInStackTrace();
173 throw new JellyTagException( e );
174 }
175 }
176 catch ( NoActionDefinitionException e )
177 {
178 throw new JellyTagException( e );
179 }
180 catch ( NoSuchGoalException e )
181 {
182 throw new JellyTagException( e );
183 }
184 }
185
186 // Properties
187 //-------------------------------------------------------------------------
188
189 public void setName( String name )
190 {
191 this.name = name;
192 }
193
194 public String getName()
195 {
196 return this.name;
197 }
198 }