KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > SuffixOrderHelper


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.jboss.logging.Logger;
31
32 /**
33  * SuffixOrderHelper.
34  *
35  * This class wraps the SuffixOrder and EnhandedSuffixes attributes
36  * of MainDeployer.
37  *
38  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
39  * @version $Revision: 57108 $
40  */

41 public final class SuffixOrderHelper
42 {
43    // Constants -----------------------------------------------------
44

45    /**
46     * Default EnhancedSuffixes
47     *
48     * Those values are indicative - we just know they'll work with
49     * the compiled order of subdeployers like the aop or ejb3,
50     * but any order can be set using the EnhancedSuffixes
51     * attribute and/or the individual subdeployer's relative order.
52     *
53     * The commented out entries indicate those are dynamically
54     * added by their respective subdeployers when they register.
55     */

56    public static final String JavaDoc[] DEFAULT_ENHANCED_SUFFIXES = {};
57    /*
58     Moved this list to org.jboss.deployment.MainDeployer-xmbean.xml
59     so there are no hardcoded defaults.
60    {
61        //"050:.deployer",
62        //"050:-deployer.xml",
63        //"100:.aop",
64        //"100:-aop.xml",
65        //"150:.sar",
66        //"150:-service.xml",
67        //"200:.beans",
68          "250:.rar",
69          "300:-ds.xml",
70        //"350:.har",
71          "400:.jar", // ejb .jar
72        //"450:.ejb3",
73        //"450:.par",
74          "500:.war", // don't comment out this!
75          "600:.wsr",
76          "650:.ear",
77        //"700:.jar", // plain .jar
78        //"750:.zip",
79          "800:.bsh",
80          "900:.last" // the JARDeployer really handles those?
81    };
82    */

83    
84    /** A default relative order just before 900:.last */
85    public static final int DEFAULT_RELATIVE_ORDER = 850;
86    
87    /** The Logger */
88    public static final Logger log = Logger.getLogger(SuffixOrderHelper.class);
89    
90    // Private Data --------------------------------------------------
91

92    /** Wrapped DeploymentSorter that stores the value for SuffixOrder attribute */
93    private final DeploymentSorter sorter;
94    
95    /** The actual value of EnhancedSuffixes attribute */
96    private String JavaDoc[] enhancedSuffixes;
97    
98    /** List of sorted EnhancedSuffix instances */
99    private List JavaDoc suffixes;
100    
101    /** Set of static String suffixes that cannot be overriden/removed */
102    private Set JavaDoc staticSuffixes;
103    
104    // Constructor ---------------------------------------------------
105

106    public SuffixOrderHelper(DeploymentSorter sorter)
107    {
108       this.sorter = sorter;
109       this.suffixes = new ArrayList JavaDoc();
110       this.staticSuffixes = new HashSet JavaDoc();
111    }
112
113    // Accessors -----------------------------------------------------
114

115    /**
116     * Getter only for the SuffixOrder as known by the MainDeployer and the Scanners
117     *
118     * The value is updated during init() with suffixes that remain constant.
119     * After that suffixes are added/removed using the corresponding methods.
120     *
121     * @return the SuffixOrder string array
122     */

123    public String JavaDoc[] getSuffixOrder()
124    {
125       return sorter.getSuffixOrder();
126    }
127    
128    /**
129     * Getter for the EnhancedSuffixes attribute
130     *
131     * @return the EnhancedSuffixes string array
132     */

133    public String JavaDoc[] getEnhancedSuffixes()
134    {
135       return enhancedSuffixes;
136    }
137    
138    /**
139     * Setter for the EnhancedSuffixes attribute
140     *
141     * @param enhancedSuffixes the EnhancedSuffixes string array
142     */

143    public void setEnhancedSuffixes(String JavaDoc[] enhancedSuffixes)
144    {
145       this.enhancedSuffixes = enhancedSuffixes;
146    }
147    
148    /**
149     * Initialise the SuffixOrder from EnhancedSuffixes.
150     *
151     * If no enchangedSuffixes is specified, DEFAULT_ENHANCED_SUFFIXES
152     * will be used. Individual entries may contain an additional order
153     * element of the form [order:]suffix, e.g. 100:.sar
154     *
155     * The suffixes specified during init, will remain constant,
156     * i.e. they can't be overriden or removed.
157     */

158    public void initialize()
159    {
160       // if enhancedSuffixes not provided, use the default
161
if (enhancedSuffixes == null)
162       {
163          enhancedSuffixes = DEFAULT_ENHANCED_SUFFIXES;
164       }
165
166       // reset, just in case we are called more than once
167
suffixes.clear();
168       staticSuffixes.clear();
169       
170       // add all enhanced suffixes; mark them as static, too.
171
for (int i = 0; i < enhancedSuffixes.length; i++)
172       {
173          EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]);
174          addSuffix(es);
175          
176          // mark all initial entries as static!
177
staticSuffixes.add(es.suffix);
178       }
179       
180       // set the resulting SuffixOrder
181
sorter.setSuffixOrder(produceSuffixOrder());
182    }
183
184    /**
185     * Add the specified enhanced suffixes in the correct
186     * position(s) and regenerate the SuffixOrder, if needed.
187     *
188     * A suffix that exists already and is marked as static
189     * will be skipped. Otherwise, duplicate entries are allowed.
190     */

191    public void addEnhancedSuffixes(String JavaDoc [] enhancedSuffixes)
192    {
193       if (enhancedSuffixes != null)
194       {
195          // remember the initial size of the list
196
int size = suffixes.size();
197          
198          // add all enhanced suffixes
199
for (int i = 0; i < enhancedSuffixes.length; i++)
200          {
201             EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]);
202             addSuffix(es);
203          }
204          if (suffixes.size() > size)
205          {
206             // suffixes were added, recreate the resulting SuffixOrder
207
sorter.setSuffixOrder(produceSuffixOrder());
208          }
209       }
210    }
211    
212    /**
213     * Insert the specified suffixes in the correct position
214     * and regenerate the SuffixOrder array, if needed.
215     *
216     * A suffix that exists already and is marked as static
217     * will be skipped. Otherwise, duplicate entries are allowed.
218     */

219    public void addSuffixes(String JavaDoc[] suffixes, int relativeOrder)
220    {
221       if (suffixes != null)
222       {
223          // remember the initial size of the list
224
int size = this.suffixes.size();
225          
226          for (int i = 0; i < suffixes.length; i++)
227          {
228             addSuffix(new EnhancedSuffix(suffixes[i], relativeOrder));
229          }
230          
231          if (this.suffixes.size() > size)
232          {
233             // suffixes were added, recreate the resulting SuffixOrder
234
sorter.setSuffixOrder(produceSuffixOrder());
235          }
236       }
237    }
238    
239    /**
240     * Remove the enhanced suffixes if they are not marked as static
241     * and regenerate the SuffixOrder, if needed.
242     */

243    public void removeEnhancedSuffixes(String JavaDoc[] enhancedSuffixes)
244    {
245       if (enhancedSuffixes != null)
246       {
247          // remember the initial size of the list
248
int size = suffixes.size();
249          
250          for (int i = 0; i < enhancedSuffixes.length; i++)
251          {
252             EnhancedSuffix es = new EnhancedSuffix(enhancedSuffixes[i]);
253
254             // if this is a static suffix, don't remove
255
if (staticSuffixes.contains(es.suffix))
256             {
257                continue;
258             }
259             else
260             {
261                // remove if exists
262
suffixes.remove(es);
263             }
264          }
265          
266          if (this.suffixes.size() < size)
267          {
268             // entries removed, recreate the resulting SuffixOrder
269
sorter.setSuffixOrder(produceSuffixOrder());
270          }
271       }
272    }
273    
274    /**
275     * Remove the specified suffixes if they are not marked as static
276     * and regenerate the SuffixOrder, if needed.
277     */

278    public void removeSuffixes(String JavaDoc[] suffixes, int relativeOrder)
279    {
280       if (suffixes != null)
281       {
282          // remember the initial size of the list
283
int size = this.suffixes.size();
284
285          for (int i = 0; i < suffixes.length; i++)
286          {
287             // if this is a static suffix, don't remove
288
if (staticSuffixes.contains(suffixes[i]))
289             {
290                continue;
291             }
292             else
293             {
294                // remove if exists
295
this.suffixes.remove(new EnhancedSuffix(suffixes[i], relativeOrder));
296             }
297          }
298          
299          if (this.suffixes.size() < size)
300          {
301             // entries removed, recreate the resulting SuffixOrder
302
sorter.setSuffixOrder(produceSuffixOrder());
303          }
304       }
305    }
306    
307    // Private -------------------------------------------------------
308

309    /**
310     * Produce the SuffixOrder from the sorted suffixes ArrayList
311     */

312    private String JavaDoc[] produceSuffixOrder()
313    {
314       String JavaDoc[] suffixOrder = new String JavaDoc[suffixes.size()];
315       
316       for (int i = 0; i < suffixes.size(); i++)
317       {
318          suffixOrder[i] = ((EnhancedSuffix)suffixes.get(i)).suffix;
319       }
320       return suffixOrder;
321    }
322
323    /**
324     * Add an EnhancedSuffix at the correct position in the sorted List.
325     *
326     * Sorting is based on EnhancedSuffix.order. A new entry with an equal
327     * order value to an existing entry is placed AFTER the existing entry.
328     *
329     * If EnhancedSuffix.suffix exists in the staticSuffixes Set the entry
330     * is NOT added. Otherwise, they EnhancedSuffix will be added, even
331     * if it is a duplicate of an existing one.
332     *
333     * @param enhancedsuffix the enhanced suffix
334     */

335    private void addSuffix(EnhancedSuffix enhancedSuffix)
336    {
337       // if this is a static suffix, don't add it
338
if (staticSuffixes.contains(enhancedSuffix.suffix))
339       {
340          log.debug("Static suffix exists; ignoring request for adding enhanced suffix: " + enhancedSuffix);
341       }
342       else
343       {
344          int size = suffixes.size();
345          
346          // if List empty, just add the suffix
347
if (size == 0)
348          {
349             suffixes.add(enhancedSuffix);
350          }
351          else
352          {
353             // insertion sort starting from the last element
354
for (int i = size - 1; i > -1; i--)
355             {
356                EnhancedSuffix entry = (EnhancedSuffix)suffixes.get(i);
357                if (enhancedSuffix.order >= entry.order)
358                {
359                   // add the suffix AFTER the entry and stop
360
suffixes.add(i + 1, enhancedSuffix);
361                   break;
362                }
363                else if (i == 0)
364                {
365                   // reached the beginning so add the suffix right there
366
suffixes.add(0, enhancedSuffix);
367                }
368             }
369          }
370       }
371    }
372    
373    /**
374     * Inner class that encapsulates an enhanceSuffix
375     * consisting of suffix + order
376     */

377    public final static class EnhancedSuffix
378    {
379       /** The suffix, e.g. .sar */
380       public String JavaDoc suffix;
381       
382       /** The order, by convention a 3 digit number, e.g. 100 */
383       public int order;
384       
385       /**
386        * Simple CTOR
387        */

388       public EnhancedSuffix(String JavaDoc suffix, int order)
389       {
390          this.suffix = suffix;
391          this.order = order;
392       }
393       
394       /**
395        * CTOR that parses an enhancedSuffix string of the form: [order:]suffix
396        * If the optional 'order' is missing, use DEFAULT_RELATIVE_ORDER
397        */

398       public EnhancedSuffix(String JavaDoc enhancedSuffix) throws IllegalArgumentException JavaDoc
399       {
400          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(enhancedSuffix, ":");
401          int tokens = tokenizer.countTokens();
402          
403          switch (tokens)
404          {
405             case 1:
406                this.order = DEFAULT_RELATIVE_ORDER;
407                this.suffix = enhancedSuffix;
408                break;
409              
410             case 2:
411                this.order = Integer.parseInt(tokenizer.nextToken());
412                this.suffix = tokenizer.nextToken();
413                break;
414                
415             default:
416                throw new IllegalArgumentException JavaDoc("Cannot parse enhancedSuffix: " + enhancedSuffix);
417          }
418       }
419
420       /**
421        * Override equals to allow EnhancedSuffix to be searchable
422        * using ArrayList.indexOf()/ArrayList.lastIndexOf()
423        *
424        * Base equality on both suffix and order
425        */

426       public boolean equals(Object JavaDoc other)
427       {
428          if (other == this)
429             return true;
430
431          if (!(other instanceof EnhancedSuffix))
432             return false;
433
434          EnhancedSuffix that = (EnhancedSuffix)other;
435             
436          // suffix shouldn't be null
437
return this.suffix.equals(that.suffix) && this.order == that.order;
438       }
439       
440       /**
441        * Use both fields
442        */

443       public int hashCode()
444       {
445          int result = 17;
446          result = 37 * result + suffix.hashCode();
447          result = 37 * result + order;
448          return result;
449       }
450       
451       /**
452        * Pretty print
453        */

454       public String JavaDoc toString()
455       {
456          return order + ":" + suffix;
457       }
458    }
459 }
460
Popular Tags