2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
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  package org.apache.cactus.eclipse.webapp.internal;
22  
23  import java.io.File;
24  import java.util.StringTokenizer;
25  import java.util.Vector;
26  
27  import org.apache.cactus.eclipse.webapp.internal.ui.WebappPlugin;
28  import org.eclipse.core.resources.IProject;
29  import org.eclipse.core.runtime.CoreException;
30  import org.eclipse.core.runtime.IPath;
31  import org.eclipse.core.runtime.Path;
32  import org.eclipse.core.runtime.QualifiedName;
33  import org.eclipse.jdt.core.IClasspathEntry;
34  import org.eclipse.jdt.core.IJavaProject;
35  import org.eclipse.jdt.core.JavaCore;
36  import org.eclipse.jdt.core.JavaModelException;
37  
38  /**
39   * Represents a web application for a given project.
40   * It knows how to load its values from project properties and
41   * how to persist them.
42   * 
43   * @version $Id: Webapp.java 238816 2004-02-29 16:36:46Z vmassol $
44   */
45  public class Webapp
46  {
47      /**
48       * Delimiter for classpaths entries in the String that will be used
49       * for persisting the webapp settings. 
50       */
51      private static final String CLASSPATH_DELIMITER = ";";
52  
53      /**
54       * Qualified name of the output war property. Used for persistence of 
55       * project properties. 
56       */
57      private static final QualifiedName OUTPUT_QN =
58          new QualifiedName(WebappPlugin.getPluginId(), "output");
59  
60      /**
61       * Qualified name of the webapp directory property. Used for persistence 
62       * of project properties. 
63       */
64      private static final QualifiedName DIR_QN =
65          new QualifiedName(WebappPlugin.getPluginId(), "dir");
66  
67      /**
68       * Qualified name of the classpath property. Used for persistence of 
69       * project properties. 
70       */
71      private static final QualifiedName CLASSPATH_QN =
72          new QualifiedName(WebappPlugin.getPluginId(), "webappClasspath");
73  
74      /**
75       * Default path for the generated war 
76       */
77      private static final String DEFAULT_OUTPUT =
78          System.getProperty("java.io.tmpdir") + "webapp.war";
79  
80      /**
81       * Default directory of where the webapp is located. 
82       */
83      private static final String DEFAULT_DIR =
84          "src" + File.separator + "webapp";
85              
86      /**
87       * Full path to the webapp War.
88       */
89      private String output;
90  
91      /**
92       * Directory of the webapp relative to the user's project. 
93       */
94      private String dir;
95  
96      /**
97       * Paths to the webapp libraries
98       */
99      private IClasspathEntry[] classpath;
100 
101     /**
102      * The current project to which this webapp refers.  
103      */
104     private IJavaProject javaProject;
105 
106     /**
107      * @param theJavaProject the project this webapp is linked to
108      */
109     public Webapp(final IJavaProject theJavaProject)
110     {
111         this.javaProject = theJavaProject;
112     }
113 
114     /**
115      * Initialize the web app properties with default values or stored values
116      * if they exist. 
117      * @return boolean true if we loaded the default values
118      */
119     public final boolean init()
120     {
121         return loadValues();
122     }
123 
124     /**
125      * Loads this webapp from the project properties. If the persistent
126      * properties cannot be loaded or if a value is not set, we load the 
127      * default values.
128      * 
129      * @return true if the default values were loaded or false if the 
130      *         persistent ones were loaded
131      */
132     public final boolean loadValues()
133     {
134         boolean isDefaultValues;
135 
136         try
137         {
138             loadPersistentValues();
139             isDefaultValues = false;
140         }
141         catch (CoreException ce)
142         {
143             loadDefaultValues();
144             isDefaultValues = true;
145         }
146 
147         if (output == null
148             || dir == null
149             || classpath == null)
150         {
151             loadDefaultValues();
152             isDefaultValues = true;
153         }
154 
155         return isDefaultValues;
156     }
157 
158     /**
159      * Loads the persistent properties for this webapp.
160      * @throws CoreException if we fail to load a persistent property
161      */
162     public final void loadPersistentValues() throws CoreException
163     {
164         IProject theProject = javaProject.getProject();
165 
166         this.output = theProject.getPersistentProperty(OUTPUT_QN);
167         this.dir = theProject.getPersistentProperty(DIR_QN);
168         this.classpath = toClasspathEntryArray(
169             theProject.getPersistentProperty(CLASSPATH_QN));
170     }
171 
172     /**
173      *  Loads the default values of a webapp.
174      */
175     public final void loadDefaultValues()
176     {
177         this.output = DEFAULT_OUTPUT;
178         this.dir = DEFAULT_DIR;
179 
180         try
181         {
182             this.classpath = javaProject.getRawClasspath();
183         }
184         catch (JavaModelException e)
185         {
186             this.classpath = new IClasspathEntry[0];
187         }
188     }
189 
190     /**
191      * Saves this webapp in the project's properties
192      * @throws CoreException if an error occurs while saving 
193      */
194     public final void persist() throws CoreException
195     {
196         IProject project = javaProject.getProject();
197         project.setPersistentProperty(OUTPUT_QN, output);
198         project.setPersistentProperty(DIR_QN, dir);
199         project.setPersistentProperty(CLASSPATH_QN, toString(classpath));
200     }
201 
202     /**
203      * Converts a String classpath to an array of library classpath entries.
204      * @param theClasspathEntriesString string of delimiter-separated 
205      *        classpaths
206      * @return an array of library entries
207      */
208     private IClasspathEntry[] toClasspathEntryArray(
209         final String theClasspathEntriesString)
210     {
211         if (theClasspathEntriesString == null)
212         {
213             return null;
214         }
215 
216         Vector result = new Vector();
217 
218         StringTokenizer cpTokenizer =
219             new StringTokenizer(theClasspathEntriesString, 
220             CLASSPATH_DELIMITER);
221             
222         while (cpTokenizer.hasMoreElements())
223         {
224             String element = cpTokenizer.nextToken();
225             try
226             {
227                 IClasspathEntry newEntry =
228                     JavaCore.newLibraryEntry(new Path(element), null, null);
229                 result.add(newEntry);
230             }
231             catch (Exception e)
232             {
233                 // Do not add the entry
234             }
235         }
236 
237         return (IClasspathEntry[]) result.toArray(
238             new IClasspathEntry[result.size()]);
239     }
240 
241     /**
242      * Converts an array of library classpath entries to a String 
243      * @param theClasspathEntries an array of library entries
244      * @return String string of delimiter-separated classpaths
245      */
246     private String toString(final IClasspathEntry[] theClasspathEntries)
247     {
248         StringBuffer result = new StringBuffer();
249         for (int i = 0; i < theClasspathEntries.length; i++)
250         {
251             IClasspathEntry current = theClasspathEntries[i];
252             result.append(current.getPath());
253             result.append(CLASSPATH_DELIMITER);
254         }
255         return result.toString();
256     }
257 
258     /**
259      * Sets the classpath.
260      * @param theClasspath The classpath to set
261      */
262     public final void setClasspath(final IClasspathEntry[] theClasspath)
263     {
264         this.classpath = theClasspath;
265     }
266 
267     /**
268      * Sets the dir.
269      * @param theDir The dir to set
270      */
271     public final void setDir(final String theDir)
272     {
273         this.dir = theDir;
274     }
275 
276     /**
277      * Sets the output.
278      * @param theOutput The output to set
279      */
280     public final void setOutput(final String theOutput)
281     {
282         this.output = theOutput;
283     }
284 
285     /**
286      * @return IClasspathEntry[] the array of jar entries for this webapp
287      */
288     public final IClasspathEntry[] getClasspath()
289     {
290         return this.classpath;
291     }
292 
293     /**
294      * @return String directory of this webapp source files
295      * relative to the project path 
296      */
297     public final String getDir()
298     {
299         return this.dir;
300     }
301     
302     /**
303      * @return the absolute directory to this webapp source files
304      */
305     public final File getAbsoluteDir()
306     {
307         File result = null;
308 
309         if (this.dir != null)
310         {
311             IPath projectPath = javaProject.getProject().getLocation();
312             result = projectPath.append(this.dir).toFile();
313         } 
314         return result; 
315     }
316 
317     /**
318      * @return String location of the generated war
319      */
320     public final String getOutput()
321     {
322         return this.output;
323     }
324 }