KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > Descriptor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment;
24
25 import java.util.*;
26 import java.io.*;
27 import java.io.Serializable JavaDoc;
28 import com.sun.enterprise.util.NotificationListener;
29 import com.sun.enterprise.util.NotificationEvent;
30 import com.sun.enterprise.deployment.util.DescriptorVisitor;
31
32     /**
33     * Descriptor is the root class for all objects
34     * representing deployment information in J2EE. Descriptors
35     * notifiy listeners of state changes, and have a name, description,
36     * and icons.
37     *
38     *@author Danny Coward
39     */

40
41 public class Descriptor extends DynamicAttributesDescriptor implements Serializable JavaDoc {
42
43     /** Notification String for a general descriptor change. */
44     public static String JavaDoc DESCRIPTOR_CHANGED = "Descriptor change";
45     public static String JavaDoc NAME_CHANGED = "NameChanged";
46     public static String JavaDoc DESCRIPTION_CHANGED = "DescriptionChanged";
47     public static String JavaDoc LARGE_ICON_CHANGED = "LargeIconChanged";
48     public static String JavaDoc SMALL_ICON_CHANGED = "SmallIconChanged";
49
50     /** static flag to indicate descriptors should bounds check. */
51     private static boolean boundsChecking = true;
52     
53     /** My Vector of NotificationListener objects. */
54     protected Vector listeners = new Vector();
55
56     /** My display name indexed by language */
57     private Map displayNames = null;
58     
59     /** My descriptions indexed by language */
60     private Map descriptions = null;
61     
62     /** icons map indexed by language */
63     private Map largeIcons = null;
64     private Map smallIcons = null;
65     
66         
67     /**
68     * The default constructor. Constructs a descriptor with
69     * name, description and icons as empty Strings.
70     *
71     */

72     public Descriptor() {
73     this.listeners = new Vector();
74     }
75     
76     /**
77     * The copy constructor.
78     */

79     protected Descriptor(Descriptor other) {
80         if (other.displayNames!=null)
81     this.displayNames = new HashMap(other.displayNames);
82         if (other.descriptions!=null)
83             this.descriptions = new HashMap(other.descriptions);
84         if (other.largeIcons!=null)
85             this.largeIcons = new HashMap(other.largeIcons);
86         if (other.smallIcons!=null)
87             this.smallIcons = new HashMap(other.smallIcons);
88     }
89     
90     /**
91     * Constructs a descriptor with given
92     * name, description.
93     *
94     * @param name the name of the descriptor.
95     * @param description the name of the descriptor.
96     *
97     */

98     public Descriptor(String JavaDoc name, String JavaDoc description) {
99     this();
100     setLocalizedDisplayName(null, name);
101     setLocalizedDescription(null, description);
102     }
103     
104     /**
105     * Sets a global flag to enable or disable boudsn checking
106     * of deployment information
107     *
108     * @param b true for bounds checking on, false else.
109     *
110     */

111     public static void setBoundsChecking(boolean b) {
112     boundsChecking = b;
113     }
114     
115     /**
116     * Answers whether the object model is bounds checking.
117     *
118     * @return true for boudsn checking, false else.
119     *
120     */

121     public static boolean isBoundsChecking() {
122     return boundsChecking;
123     }
124     
125     /**
126     * Sets the name of this descriptor.
127     *
128     * @param name the new name of the descriptor.
129     *
130     */

131     public void setName(String JavaDoc name) {
132     setLocalizedDisplayName(null, name);
133     }
134     
135     /**
136     * The name of this descriptor as a String.
137     *
138     * @return the name of this descriptor
139     *
140     */

141     public String JavaDoc getName() {
142     return getLocalizedDisplayName(null);
143     }
144     
145     /**
146      * Add a localized display name for this descriptor
147      * @param the local identifier (null if using default locale)
148      * @param the localized string
149      */

150     public void setLocalizedDisplayName(String JavaDoc lang, String JavaDoc displayName) {
151         
152         if (lang==null) {
153             lang = Locale.getDefault().getLanguage();
154         }
155         if (displayNames==null) {
156             displayNames = new HashMap();
157         }
158         displayNames.put(lang, displayName);
159         changed(NAME_CHANGED);
160     }
161     
162     /**
163      * @return the localized display name for the passed language
164      * @param the language
165      */

166     public String JavaDoc getLocalizedDisplayName(String JavaDoc language) {
167         
168         if (displayNames==null) {
169             return "";
170         }
171         
172         String JavaDoc originalLanguage = language;
173         if (language==null) {
174             language=Locale.getDefault().getLanguage();
175         }
176         
177         String JavaDoc localizedName = (String JavaDoc) displayNames.get(language);
178         if (localizedName!=null) {
179             return localizedName;
180         }
181
182         // so far, no luck, it is possible that this
183
// environment property was transfered through jndi
184
// between machines with different locales, if I have
185
// at least one value, and no language was specified,
186
// let's return it.
187
if (originalLanguage==null && displayNames.size()>0) {
188             return (String JavaDoc) displayNames.values().iterator().next();
189         }
190         // all other cases return empty strings
191
return "";
192     }
193     
194     /**
195      * @return the localized display name indexed by language
196      */

197     public Map getLocalizedDisplayNames() {
198         return displayNames;
199     }
200     
201     /**
202      * sets the display name for this bundle
203      */

204     public void setDisplayName(String JavaDoc name) {
205         setName(name);
206     }
207     
208     /**
209      * @return the display name
210      */

211     public String JavaDoc getDisplayName() {
212         return getName();
213     }
214     
215     /**
216     * Sets the description text of this descriptor.
217     *
218     * @param description the new description text of the descriptor.
219     *
220     */

221     public void setDescription(String JavaDoc description) {
222         setLocalizedDescription(null, description);
223     }
224     
225     /**
226     * The description text of this descriptor as a String.
227     *
228     * @return the description text of this descriptor
229     *
230     */

231     public String JavaDoc getDescription() {
232     return getLocalizedDescription(null);
233     }
234     
235     /**
236      * Add a localized description for this descriptor
237      * @param the local identifier (null if using default locale)
238      * @param the localized string
239      */

240     public void setLocalizedDescription(String JavaDoc lang, String JavaDoc description) {
241         if (lang==null) {
242             lang = Locale.getDefault().getLanguage();
243         }
244         if (descriptions==null) {
245             descriptions = new HashMap();
246         }
247         descriptions.put(lang, description);
248         changed(DESCRIPTION_CHANGED);
249     }
250     
251     /**
252      * @return the localized description
253      * @param the local language
254      */

255     public String JavaDoc getLocalizedDescription(String JavaDoc lang) {
256         
257         if (descriptions==null)
258             return "";
259         if (lang==null) {
260             lang = Locale.getDefault().getLanguage();
261         }
262         String JavaDoc description = (String JavaDoc) descriptions.get(lang);
263         if (description==null) {
264             return "";
265         }
266         return description;
267     }
268     
269     /**
270      * @return a Map of localized description, where lang is the key
271      */

272     public Map getLocalizedDescriptions() {
273         return descriptions;
274     }
275     
276     /**
277      * Sets the large icon uri for a particular language
278      * @param the language identifier
279      * @param the large icon uri
280      */

281     public void setLocalizedLargeIconUri(String JavaDoc lang, String JavaDoc uri) {
282         if (lang==null) {
283             lang = Locale.getDefault().getLanguage();
284         }
285         if (largeIcons==null) {
286             largeIcons = new HashMap();
287         }
288         largeIcons.put(lang, uri);
289         changed(LARGE_ICON_CHANGED);
290     }
291     
292     /**
293      * @return the large icon uri for a language
294      * @param the language or null for the current locale
295      */

296     public String JavaDoc getLocalizedLargeIconUri(String JavaDoc lang) {
297         if (largeIcons==null) {
298             return null;
299         }
300         if (lang==null) {
301             lang = Locale.getDefault().getLanguage();
302         }
303         return (String JavaDoc) largeIcons.get(lang);
304     }
305     
306     /**
307      * @return a map of localized large icons uris indexed
308      * by language
309      */

310     public Map getLocalizedLargeIconUris() {
311         return largeIcons;
312     }
313     
314     /**
315      * set the localized small icon uri for the passed language
316      * @param the language
317      * @param the uri for the small icon
318      */

319     public void setLocalizedSmallIconUri(String JavaDoc lang, String JavaDoc uri) {
320         if (lang==null) {
321             lang = Locale.getDefault().getLanguage();
322         }
323         if (smallIcons==null) {
324             smallIcons = new HashMap();
325         }
326         smallIcons.put(lang, uri);
327         changed(LARGE_ICON_CHANGED);
328     }
329     
330     /**
331      * @return the small icon uri for the passed language
332      */

333     public String JavaDoc getLocalizedSmallIconUri(String JavaDoc lang) {
334         if (smallIcons==null) {
335             return null;
336         }
337         if (lang==null) {
338             lang = Locale.getDefault().getLanguage();
339         }
340         return (String JavaDoc) smallIcons.get(lang);
341     }
342     
343     /**
344      * @return the map of small icons indexed by language
345      */

346     public Map getLocalizedSmallIconUris() {
347         return smallIcons;
348     }
349     
350     /**
351     * The large icon name of this descriptor as a String.
352     *
353     * @return the large icon name of this descriptor
354     *
355     */

356     public String JavaDoc getLargeIconUri() {
357         return getLocalizedLargeIconUri(null);
358     }
359     
360     /**
361     * Sets the large icon name of this descriptor as a String.
362     *
363     * @param largeIconUri the large icon name of this descriptor
364     *
365     */

366     public void setLargeIconUri(String JavaDoc largeIconUri) {
367         setLocalizedLargeIconUri(null, largeIconUri);
368     }
369     
370     /**
371     * The small icon name of this descriptor as a String.
372     *
373     * @return the small icon name of this descriptor
374     *
375     */

376     public String JavaDoc getSmallIconUri() {
377         return getLocalizedSmallIconUri(null);
378     }
379     
380     /**
381     * Sets the small icon name of this descriptor as a String.
382     *
383     * @param smallIconUri the small icon name of this descriptor
384     *
385     */

386     public void setSmallIconUri(String JavaDoc smallIconUri) {
387         setLocalizedSmallIconUri(null, smallIconUri);
388     }
389     
390     /**
391     * Register the given notification listener for change events.
392     *
393     * @param nl the notification listener to register
394     *
395     */

396     public void addNotificationListener(NotificationListener nl) {
397     if ((nl != null) && !listeners.contains(nl)) { // avoid duplicate listeners
398
listeners.addElement(nl);
399     }
400     }
401     
402     /**
403     * Deregister the given notification listener for change events.
404     *
405     * @param nl the notification listener to deregister
406     *
407     */

408     public void removeNotificationListener(NotificationListener nl) {
409     listeners.removeElement(nl);
410     }
411
412     /**
413     * return current notification listeners
414     *
415     */

416     public List getNotificationListeners() {
417     Vector listenersClone = null;
418     synchronized (listeners) {
419         listenersClone = (Vector)this.listeners.clone();
420     }
421     return listenersClone;
422     }
423
424     /**
425     * Notify all my listeners that I have changed..
426     *
427     */

428     public void changed(String JavaDoc attribute) {
429     // fine-grain attribute currently ignored
430
// eventually, 'changed(String)' should replace 'changed()' everywhere.
431
this.changed();
432     }
433
434     /**
435     * Notify all my listeners that I have changed..
436     *
437     */

438     public void changed() {
439     String JavaDoc attribute = "";
440     NotificationEvent ne = new NotificationEvent(this, DESCRIPTOR_CHANGED, this, attribute);
441     
442     List listenersClone = this.getNotificationListeners();
443     for (Iterator e = listenersClone.iterator(); e.hasNext();) {
444         NotificationListener nl = (NotificationListener)e.next();
445         nl.notification(ne);
446     }
447     }
448     
449     /**
450     * Returns the largest substring of the given string that
451     * does not have an integer at the end.
452     *
453     */

454     private static String JavaDoc stripIntegerEndingFrom(String JavaDoc s) {
455     return recursiveStripIntegerEndingFrom(s);
456     }
457     
458     /**
459     * Returns the largest substring of the given string that
460     * does not have an integer at the end.
461     *
462     */

463     private static String JavaDoc recursiveStripIntegerEndingFrom(String JavaDoc s) {
464     if (s.length() > 1) {
465         String JavaDoc shorterByOne = s.substring(0, s.length() - 1);
466         
467         String JavaDoc lastBit = s.substring(s.length() - 1, s.length());
468         try {
469         Integer.parseInt(lastBit);
470         return recursiveStripIntegerEndingFrom(shorterByOne);
471         } catch (NumberFormatException JavaDoc nfe) {
472         return s;
473         }
474     
475     }
476     return s;
477     }
478     
479     /**
480     * Returns String based on the trial name that is guaramteed to be different
481     * from any of the strings in the vector of String names.
482     *
483     */

484     private static String JavaDoc uniquifyString(String JavaDoc trialName, Vector v, int index) {
485     for(Enumeration e = v.elements(); e.hasMoreElements();) {
486         String JavaDoc next = (String JavaDoc) e.nextElement();
487         if (next.equals(trialName)) {
488         index++;
489         return uniquifyString(stripIntegerEndingFrom(trialName) + index, v, index);
490         }
491     }
492     return trialName;
493     }
494         
495     /**
496     * Returns String based on the trial name that is guaramteed to be different
497     * from any of the strings in the vector of String names.
498     *
499     * @param trialName the suggested name
500     * @param otherNames The Vector of String objects none of which will be the same as the return
501     * @return the unique String
502     */

503     public static String JavaDoc createUniqueFilenameAmongst(String JavaDoc trialName, Vector otherNames) {
504
505     /* extract file.ext */
506     int p = trialName.lastIndexOf(".");
507     if (p < 0) {
508         return uniquifyString(trialName, otherNames, 0);
509     }
510     String JavaDoc ext = trialName.substring(p);
511     String JavaDoc file = trialName.substring(0, p);
512
513     /* get list of filenames less extension */
514     Vector nameList = new Vector();
515     for (Enumeration e = otherNames.elements(); e.hasMoreElements();) {
516         String JavaDoc name = e.nextElement().toString();
517         if (name.endsWith(ext)) {
518         nameList.add(name.substring(0, name.length() - ext.length()));
519         }
520     }
521     String JavaDoc unique = uniquifyString(file, nameList, 0);
522     return unique + ext;
523
524     }
525
526     /**
527     * Returns String based on the trial name that is guaramteed to be different
528     * from any of the strings in the vector of String names.
529     *
530     * @param trialName the suggested name
531     * @param otherNames The Vector of String objects none of which will be the same as the return
532     * @return the unique String
533     */

534     public static String JavaDoc createUniqueNameAmongst(String JavaDoc trialName, Vector otherNames) {
535     return uniquifyString(trialName, otherNames, 0);
536     }
537     
538     /**
539     * Returns String based on the trial name that is guaramteed to be different
540     * from any of the strings returnsed by the getName() call in any of the Descriptor objects in the Set supplied.
541     *
542     * @param trialName the suggested name
543     * @param descriptors The Set of Descriptor objects to whose name attribute will not be the same as the return
544     * @return the unique String
545     */

546     public static String JavaDoc createUniqueNameAmongstNamedDescriptors(String JavaDoc trialName, Set descriptors) {
547     Vector v = new Vector();
548     for (Iterator itr = descriptors.iterator(); itr.hasNext();) {
549         Descriptor next = (Descriptor) itr.next();
550         v.addElement(next.getName());
551     }
552     return createUniqueNameAmongst(trialName, v);
553     }
554     
555     /**
556      * Add a new deployment-extension for this descriptor
557      * @param deployment-extension descriptor to add
558      */

559     public void addDeploymentExtension(DeploymentExtensionDescriptor de) {
560         Vector extensions = (Vector) getExtraAttribute("deployment-extension");
561         if (extensions==null) {
562             extensions = new Vector();
563             addExtraAttribute("deployment-extension", extensions);
564         }
565         extensions.add(de);
566     }
567     
568     /**
569      * @return an iterator on the deployment-extension
570      */

571     public Iterator getDeploymentExtensions() {
572         Vector extensions = (Vector) getExtraAttribute("deployment-extension");
573         if (extensions!=null) {
574             return extensions.iterator();
575         }
576         return null;
577     }
578     
579     /**
580      * add a prefix mapping
581      */

582     public void addPrefixMapping(String JavaDoc mapping, String JavaDoc uri) {
583         Map prefixMapping = getPrefixMapping();
584         if (prefixMapping==null) {
585             prefixMapping = new java.util.HashMap JavaDoc();
586             addExtraAttribute("prefix-mapping", prefixMapping);
587         }
588         prefixMapping.put(mapping, uri);
589     }
590     
591     /**
592      * @return the map of prefix to namepace uri
593      */

594     public Map getPrefixMapping() {
595         return (Map) getExtraAttribute("prefix-mapping");
596     }
597     
598     /**
599     * A String representation of this object.
600     */

601     public void print(StringBuffer JavaDoc toStringBuffer) {
602         StringBuffer JavaDoc sb = toStringBuffer;
603         
604         if (displayNames!=null) {
605             sb.append("Display Names:");
606             displayLocalizedMap(sb, displayNames);
607         }
608         if (descriptions!=null) {
609             sb.append("\n Descriptions");
610             displayLocalizedMap(sb, descriptions);
611         }
612         if (smallIcons!=null) {
613             sb.append("\n SmallIcons");
614             displayLocalizedMap(sb, smallIcons);
615         }
616         if (largeIcons!=null) {
617             sb.append("\n LargeIcons");
618             displayLocalizedMap(sb, largeIcons);
619         }
620         Map prefix = getPrefixMapping();
621         if (prefix!=null)
622             sb.append("\n Prefix Mapping = ").append(prefix);
623         Iterator itr = getDeploymentExtensions();
624         if (itr!=null && itr.hasNext()) {
625             do {
626                 sb.append("\n Deployment Extension : ");
627                 ((Descriptor)(itr.next())).print(sb);
628             } while (itr.hasNext());
629         }
630     sb.append("\n");
631         super.print(sb);
632     }
633     
634     /**
635      * helper method to display a localized map
636      */

637     private void displayLocalizedMap(StringBuffer JavaDoc sb, Map localizedMap) {
638         for (Iterator itr = localizedMap.keySet().iterator();itr.hasNext();) {
639             String JavaDoc lang = (String JavaDoc) itr.next();
640             sb.append("\n lang[" + lang + "] = " + localizedMap.get(lang));
641         }
642     }
643         
644     
645     /**
646      * Visitor API implementation, all descriptors must be visitable
647      *
648      * @param the visitor implementation
649      */

650     public void visit(DescriptorVisitor aVisitor) {
651         aVisitor.accept(this);
652     }
653     
654     /**
655      * @return the default locale language
656      */

657     private String JavaDoc getDefaultLanguage() {
658         return Locale.getDefault().getLanguage();
659     }
660     
661     //start IASRI 4713550
662
public String JavaDoc docType = null;
663     
664     public String JavaDoc getDocType(){
665         return docType;
666     }
667     
668     public void fillDocType(InputStream in){
669         try{
670             BufferedReader inr = new BufferedReader(new InputStreamReader(in));
671             String JavaDoc s = inr.readLine();
672             while (s != null) {
673                 if (s.indexOf("DOCTYPE") > -1) {
674                     docType = s;
675                     in.close();
676                     return;
677                 }
678                 s = inr.readLine();
679             }
680         }catch(Exception JavaDoc e){
681         }
682     }
683     //end IASRI 4713550
684
}
685
Popular Tags