KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > ConfigBean


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.config;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.text.CharacterIterator JavaDoc;
29 import java.text.StringCharacterIterator JavaDoc;
30
31 import org.netbeans.modules.schema2beans.BaseBean;
32
33 //FIXME: TO BE REMOVED
34
import org.netbeans.modules.schema2beans.Version;
35 import org.netbeans.modules.schema2beans.BaseProperty;
36 import java.util.Properties JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Hashtable JavaDoc;
40
41 import java.util.logging.Logger JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import com.sun.enterprise.config.pluggable.ConfigBeanInterceptor;
44 import com.sun.enterprise.config.pluggable.ConfigBeansSettings;
45 import com.sun.enterprise.config.pluggable.EnvironmentFactory;
46 import com.sun.enterprise.config.util.LoggerHelper;
47
48 import com.sun.enterprise.config.impl.ConfigContextImpl;
49 import org.w3c.dom.DocumentType JavaDoc;
50
51 /**
52  * ConfigBean is the root class for all objects (beans)
53  * representing Config information in server.xml
54  *
55  *@author Sridatta Viswanath
56  */

57
58 public abstract class ConfigBean extends ConfigBeanBase implements Serializable JavaDoc {
59     
60     /**
61      * is pluggable for adding additional functionality
62      * FIXME: to be initialized
63      */

64     transient private ConfigBeanInterceptor _interceptor = null;
65     transient private ConfigBeansSettings _cbSettings = null;
66     transient protected ConfigContext ctx = null;
67     
68     transient private Hashtable JavaDoc transientProperties = null;
69
70     private static final String JavaDoc UNINITIALIZED_ATTRIBUTE_VALUE = "DONOTKNOW";
71     private static final long UNINITIALIZED_LAST_MODIFIED_VALUE = -1;
72     
73     /**
74      * lastModifed keeps track of the timestamp when it was
75      * modified last. This is not user visible and is
76      * automatically updated and maintained. User gets
77      * a StaleWriteConfigException if timestamp is different
78      * while updating from config change List. Changes to
79      * this feature are in updateFromConfig* methods
80      *
81      * There is no meaning for lastModified in a deserialized
82      * ConfigBean. So, it is transient. However, we do copy it
83      * during the clone operation.
84      * -1 is uninitialized value.
85      */

86     transient private long thisLastModified = UNINITIALIZED_LAST_MODIFIED_VALUE;
87     transient private long globalLastModified = UNINITIALIZED_LAST_MODIFIED_VALUE;
88     
89     private String JavaDoc xpath;
90     
91     /**
92      * Get transient property value
93      *
94      * returns value for transientProperty or null if it did not have one
95      **/

96     public Object JavaDoc getTransientProperty(String JavaDoc name)
97     {
98         if(name==null || transientProperties==null)
99             return null;
100         return transientProperties.get(name);
101     }
102     /**
103      * Set value for named transientProperty
104      * if value==null then the property will be removed
105      * returns the previous value of the specified key,
106      * or null if it did not have one
107      **/

108     public Object JavaDoc setTransientProperty(String JavaDoc name, Object JavaDoc value)
109     {
110         if(name==null)
111             return null;
112         if(value==null)
113         {
114             if(transientProperties==null)
115                 return null;
116             value = transientProperties.get(name);
117             if(value!=null)
118                 transientProperties.remove(name);
119             return value;
120         }
121         if(transientProperties==null)
122             transientProperties=new Hashtable JavaDoc();
123         return transientProperties.put(name, value);
124     }
125     
126     public void setXPath(String JavaDoc xpath) {
127         this.xpath = (null != xpath ? xpath.trim() : xpath);
128     }
129     
130     public String JavaDoc getXPath() {
131         return (null != xpath ? xpath.trim() : xpath);
132     }
133         /**
134          * Indicate if the receiver can have siblings (i.e. there can be
135          * more than one of this kind of child under the current parent).
136          * @return true iff the receiver can have siblings
137          */

138     public boolean canHaveSiblings(){
139         return beanProp() != null && beanProp().isIndexed();
140     }
141     
142     
143    public ConfigContext getConfigContext() {
144         return ctx;
145     }
146     
147     public void setConfigContext(ConfigContext ctx) {
148         //FIXME make it package specific
149
this.ctx = ctx;
150     }
151
152     //FIXME: reimplement??
153
public void cleanup() {
154     this.ctx = null;
155     this._interceptor = null;
156         setInvalidState();
157     }
158
159     public ConfigBean() {
160     super(null, new org.netbeans.modules.schema2beans.Version(4, 0, 0));
161         setThisLastModified();
162         //_interceptor = getInterceptor();
163
_cbSettings = getConfigBeansSettings();
164     }
165     
166     /*
167      * FIXME: TO BE REMOVED
168      */

169     
170     public ConfigBean(Vector JavaDoc comps, Version version) {
171             super(comps, version);
172             setThisLastModified();
173             //_interceptor = getInterceptor();
174
_cbSettings = getConfigBeansSettings();
175     }
176    
177      
178             
179     /**
180      * This method is used to convert a string value to boolean.
181      * @return true if the value is one of true, on, yes, 1. Note
182      * that the values are case sensitive. If it is not one of these
183      * values, then, it returns false. A finest message is printed if
184      * the value is null or a info message if the values are
185      * wrong cases for valid true values.
186      */

187     public static boolean toBoolean(final String JavaDoc value){
188         final String JavaDoc v = (null != value ? value.trim() : value);
189         return null != v && (v.equals("true")
190                              || v.equals("yes")
191                              || v.equals("on")
192                              || v.equals("1"));
193     }
194
195     
196     public void setAttributeValue(String JavaDoc name, String JavaDoc value) {
197         try {
198             setAttributeValue(name, value, true);
199         } catch(StaleWriteConfigException swce) {
200             String JavaDoc url = (this.ctx==null)?" ":ctx.getUrl();
201             LoggerHelper.finest("Detected external changes to config file \"" +
202                         url +
203                         "\". Ignoring the condition and proceeding");
204         }
205     }
206     
207     /**
208      * @deprecated. will be made private soon.
209      */

210      public void setAttributeValue(String JavaDoc name, String JavaDoc value , boolean overwrite)
211             throws StaleWriteConfigException {
212          final String JavaDoc nm = (null != name ? name.trim() : name);
213          final String JavaDoc vl = (null != value ? value.trim() : value);
214          
215              //FIXME: make this private.
216

217              // Do nothing if the value hasn't actually changed
218
if (getAttributeValueSafe(nm) != null && getAttributeValueSafe(nm).equals(vl)){
219              return;
220          }
221          
222              
223          if(!overwrite && this.ctx != null && this.ctx.isFileChangedExternally()) {
224              throw new StaleWriteConfigException
225                 ("ConfigBean: cannot change since FileChangedExternally");
226          }
227          
228          String JavaDoc oldValue = UNINITIALIZED_ATTRIBUTE_VALUE;
229          //special case for description
230
if(_cbSettings.isSpecialElement(nm)) {
231              oldValue = preSetAttributeValueSpecial(nm);
232              super.setValue(nm, vl);
233              postSetAttributeValueSpecial(nm, vl, oldValue);
234          } else {
235             oldValue = preSetAttributeValue(nm, vl);
236             super.setAttributeValue(nm, vl);
237             postSetAttributeValue(nm, vl, oldValue);
238          }
239      }
240
241      /**
242       * to get the child element generically
243       */

244      public ConfigBean[] getChildBeansByName(String JavaDoc childBeanName) {
245          validateState();
246
247          if(childBeanName == null) return null;
248          childBeanName = childBeanName.trim();
249          
250          childBeanName = _cbSettings.mapElementName(childBeanName);
251          
252          ConfigBean[] ret = null;
253          try {
254              ret = (ConfigBean[]) getValues(childBeanName);
255          } catch(Exception JavaDoc e) {
256              // is a single valued element
257
ret = getChildBeanByName(childBeanName);
258          }
259          return ret;
260      }
261      
262      /**
263       * get All child beans
264       */

265      public ConfigBean[] getAllChildBeans() {
266          ArrayList JavaDoc cbRet = new ArrayList JavaDoc();
267          String JavaDoc[] childNames = getChildBeanNames();
268          if(childNames == null || childNames.length == 0) return null;
269          
270          for(int i = 0;i<childNames.length;i++) {
271              ConfigBean[] cb = getChildBeansByName(childNames[i]);
272              if (cb == null) continue;
273              for(int k=0;k<cb.length;k++) {
274                 cbRet.add(cb[k]);
275              }
276          }
277          return toConfigBeanArray(cbRet);
278      }
279      
280      /**
281       * getAll child bean names
282       */

283      private String JavaDoc[] getChildBeanNames() {
284          BaseProperty[] bp = super.listProperties();
285          if(bp == null) return null;
286          String JavaDoc[] s = new String JavaDoc[bp.length];
287          for(int i = 0;i< bp.length;i++) {
288                 s[i] = bp[i].getDtdName();
289          }
290          return s;
291      }
292      
293      public int removeChild(ConfigBean child) throws ConfigException {
294          //FIXME: remove???
295
return removeChild(child, true);
296      }
297      
298      /**
299       * generically remove child
300       * returns the index of removed child
301       */

302      public int removeChild(ConfigBean child, boolean overwrite) throws ConfigException {
303          //FIXME: to be removed??
304
if(!overwrite && this.ctx != null && this.ctx.isFileChangedExternally()) {
305              throw new StaleWriteConfigException
306                 ("ConfigBean: cannot change since FileChangedExternally");
307          }
308          
309          if(child == null)
310              throw new ConfigException("Cannot remove null child");
311          
312          return this.removeValue(child.name(), child);
313      }
314      
315      
316      public int addValue(String JavaDoc name, Object JavaDoc value) {
317          int i=0;
318          try {
319             i= addValue(name, value, true);
320          } catch(StaleWriteConfigException swce) {
321              //dont throw it!!
322
}
323          return i;
324      }
325      
326      /**
327       * throws StaleWriteConfigException for the case of checking timestamps
328       * throws exception if overwrite is false and file changed
329       * externally
330       */

331      public int addValue(String JavaDoc name, Object JavaDoc value, boolean overwrite)
332             throws StaleWriteConfigException {
333          if (null != name){
334              name = name.trim();
335          }
336          
337          
338          if(!overwrite && this.ctx != null && this.ctx.isFileChangedExternally()) {
339              throw new StaleWriteConfigException
340                 ("ConfigBean: cannot change since FileChangedExternally");
341          }
342
343          preAddValue(name, value);
344          int i = super.addValue(name, value);
345          postAddValue(name, value);
346          return i;
347      }
348      
349      public int removeValue(String JavaDoc name, Object JavaDoc value) {
350          if (null != name){
351              name = name.trim();
352          }
353          
354          int i=0;
355          try {
356             i= removeValue(name, value, true);
357          } catch (StaleWriteConfigException swce) {
358              //dont throw it!!
359
}
360          return i;
361      }
362       public int removeValue(String JavaDoc name, Object JavaDoc value, boolean overwrite)
363                                             throws StaleWriteConfigException {
364           if (null != name) {
365               name = name.trim();
366           }
367           
368          if(!overwrite && this.ctx != null && this.ctx.isFileChangedExternally()) {
369              throw new StaleWriteConfigException
370                 ("ConfigBean: cannot change since FileChangedExternally");
371          }
372          
373          preRemoveValue(name, value);
374          int i = super.removeValue(name, value);
375          postRemoveValue(name, value);
376          return i;
377       }
378      
379       public Object JavaDoc getValue(String JavaDoc name)
380       {
381           if (null != name) {
382               name = name.trim();
383           }
384           
385           Object JavaDoc res = super.getValue(name);
386           res = postGetValue(name, res);
387           return res;
388       }
389       
390       public Object JavaDoc getValue(String JavaDoc name, int index)
391       {
392           if (null != name) {
393               name = name.trim();
394           }
395           
396          Object JavaDoc res = super.getValue(name, index);
397          res = postGetValue(name, res);
398          return res;
399       }
400       public Object JavaDoc getValueById(String JavaDoc name, int id)
401       {
402           if (null != name) {
403               name = name.trim();
404           }
405           
406          Object JavaDoc res = super.getValueById(name, id);
407          res = postGetValue(name, res);
408          return res;
409       }
410       public Object JavaDoc[] getValues(String JavaDoc name)
411       {
412           if (null != name) {
413               name = name.trim();
414           }
415           
416          Object JavaDoc[] res = super.getValues(name);
417          res = postGetValues(name, res);
418          return res;
419       }
420       
421
422       public void setValue(String JavaDoc name, Object JavaDoc value) {
423           if (null != name) {
424               name = name.trim();
425           }
426           
427           try {
428             setValue(name, value, true);
429           } catch(StaleWriteConfigException swce) {
430                 // dont throw it!!
431
}
432       }
433       
434       public void setValue(String JavaDoc name, Object JavaDoc value, boolean overwrite) throws StaleWriteConfigException {
435           if (null != name) {
436               name = name.trim();
437           }
438           
439          
440          if(!overwrite && this.ctx != null && this.ctx.isFileChangedExternally()) {
441              throw new StaleWriteConfigException
442                 ("ConfigBean: cannot change since FileChangedExternally");
443          }
444          Object JavaDoc oldValue = super.getValue(name);
445              
446          preSetValue(name, value);
447          super.setValue(name, value);
448          if(oldValue!=null && oldValue!=value && oldValue instanceof ConfigBean)
449             postRemoveValue(name, oldValue);
450          postSetValue(name, value);
451       }
452       
453       /**
454        */

455       //FIXME: to take care of overwrite and config change event
456
public void setValue(String JavaDoc name, Object JavaDoc[] value) {
457           if (null != name) {
458               name = name.trim();
459           }
460           
461          preSetArrayValue(name, value);
462          super.setValue(name, value);
463          postSetArrayValue(name, value);
464          
465          
466           
467       }
468       
469       /**
470        * generic method to get default value from dtd. will be over ridden in beans
471        * note that it is also static which can cause some confusion
472        * @deprecated use getDefaultAttributeValue
473        */

474       public static String JavaDoc getDefaultAttributeValueFromDtd(String JavaDoc attr) {
475           if (null != attr) {
476               attr = attr.trim();
477           }
478           
479           return getDefaultAttributeValue(attr);
480       }
481   
482       /**
483        * generic method to get default value from dtd. will be over ridden in beans
484        * note that it is also static which can cause some confusion
485        */

486       public static String JavaDoc getDefaultAttributeValue(String JavaDoc attr) {
487           return null;
488       }
489       
490       /**
491        * get the xpath representation for this element
492        * returns something like abc[@name='value'] or abc
493        * depending on the type of the bean
494        */

495       protected String JavaDoc getRelativeXPath() {
496           return null;
497       }
498       
499       /**
500        * get absolute xpath given the parent xpath
501        *
502        */

503       public String JavaDoc getAbsoluteXPath(String JavaDoc parentXpath) {
504           if(xpath!=null)
505               return this.xpath.trim();
506           if(parentXpath==null)
507               return null;
508           String JavaDoc rel = getRelativeXPath();
509           if(rel == null) return null;
510           return parentXpath.trim() + "/" + getRelativeXPath().trim();
511       }
512      
513         /**
514          * All attribute values containing strings of form ${system-property}
515          * are modified such that all occurrences of ${system-property} are
516          * replaced by the corresponding value of the specifed system
517          * property. If you wish to fetch the un-mapped value you must invoke
518          * getRawAttributeValue instead.
519          **/

520         public synchronized String JavaDoc getAttributeValue(String JavaDoc name) {
521           if (null != name) {
522               name = name.trim();
523           }
524           
525             String JavaDoc res = getRawAttributeValue(name);
526             return postGetAttributeValue(name, res);
527         }
528         
529         /**
530          * We must support a clone() operation in which attribute values
531          * are not expanded. In other words the clone must have all system property
532          * references kept in the form ${xxxx} without expanding xxxx. The clone
533          * operation is synchronized with getAttributeValue so that an invocation of
534          * get AttributeValue while a clone is in progress will not result in a
535          * raw attribute value.
536          *
537          * We also clone the last modified timestamp.
538          */

539         public synchronized Object JavaDoc clone() {
540             final Object JavaDoc orig = preClone();
541             final ConfigBean result = (ConfigBean) super.clone();
542             if (this == parent()){
543                 result.setDoctype(this.getPublicId(), this.getSystemId());
544             }
545             postClone(orig, result);
546             result.setGlobalLastModified(getThisLastModified());
547             return result;
548         }
549
550         // We keep a copy of the public/system ids because the copies
551
// in the graph manager are read only. We also try to ensure
552
// that the ones in the graph manager take precedence - after
553
// all, the graph manager might be changed underneath us and
554
// we wouldn't know!
555

556         // This mechanism is really only for root objects (i.e the
557
// Domain bean). Hence we're not exposing it any further than
558
// necessary - its not been designed nor tested with
559
// inheritance and/or wider use n mind, hence its private.
560

561     private String JavaDoc publicId;
562     private String JavaDoc systemId;
563
564     private void setDoctype(final String JavaDoc pid, final String JavaDoc sid){
565         publicId = (null != pid ? pid.trim() : pid);
566         systemId = (null != sid ? sid.trim() : sid);
567         if (graphManager() != null){
568             graphManager().setDoctype(pid,sid);
569         }
570     }
571
572     private String JavaDoc getPublicId() {
573         if (getDoctype() != null){
574             publicId = getDoctype().getPublicId();
575         }
576         return publicId.trim();
577     }
578     private String JavaDoc getSystemId() {
579         if (getDoctype() != null){
580             systemId = getDoctype().getSystemId();
581         }
582         return systemId.trim();
583     }
584
585     private DocumentType JavaDoc getDoctype(){
586         return (graphManager().getXmlDocument() != null
587                 ? graphManager().getXmlDocument().getDoctype()
588                 : (DocumentType JavaDoc) null);
589     }
590     
591                 
592        public String JavaDoc getRawAttributeValue(String JavaDoc name) {
593            if (null != name) {
594                name = name.trim();
595            }
596
597                //FIXME: description fix later for config change event.
598
if(_cbSettings.isSpecialElement(name)) {
599                 final String JavaDoc v = (String JavaDoc)super.getValue(name);
600                 return (null != v ? v.trim() : v);
601             }
602             preRawGetAttributeValue(name);
603             String JavaDoc s = super.getAttributeValue(name);
604             postRawGetAttributeValue(name, s);
605           return (null != s ? s.trim() : s);
606        }
607
608        /**
609         * overriding BaseBean.java to maskout user names and passwords
610         */

611        public void dumpAttributes(String JavaDoc name, int index, StringBuffer JavaDoc str,
612                                                             String JavaDoc indent) {
613           if (null != name) {
614               name = name.trim();
615           }
616           
617          String JavaDoc[] names = this.getAttributeNames(name);
618
619          for (int i=0; i<names.length; i++) {
620              String JavaDoc v = null;
621              //To mask out Password and UserName
622
if(names[i].indexOf("Password") != -1 || names[i].indexOf("UserName") != -1) {
623                  v = "*****";
624              } else {
625                 v = this.getAttributeValue(name, index, names[i]);
626              }
627              
628             if (v != null) {
629                 str.append(indent + "\t attr: "); // NOI18N
630
str.append(names[i]);
631                 str.append("="); // NOI18N
632
str.append(v);
633             }
634          }
635        }
636      
637        
638     // FIXME: TO BE REMOVED
639
public void changed() {
640     }
641
642   //ROB: config changes - added to fix compilation errors in subclasses
643
/**
644      *
645      */

646     public boolean isEnabled() {
647         //FIXME: to be removed
648
return true;
649     }
650
651
652     //ROB: config changes - added to fix compilation errors in subclasses
653
/**
654      *
655      */

656     public void setEnabled(boolean enabled) {
657                 //FIXME: to be removed
658
//do nothing
659
}
660
661 // Temporary FIX for monitoring enabled. will be changed to use new monitoring service element later.
662
public boolean isMonitoringEnabled() {
663                 //FIXME: to be removed
664
return false;
665     }
666
667     //FIXME: move camelize into another class and make it the base class
668
// and make the gen classes extend from that class.
669

670     /**
671      * camelize will convert the lower case into upper case in the following
672      * format. Eg: user-group ==> UserGroup
673      *
674      * Convert a DTD name into a bean name:
675      *
676      * Any - or _ character is removed. The letter following - and _
677      * is changed to be upper-case.
678      * If the user mixes upper-case and lower-case, the case is not
679      * changed.
680      * If the Word is entirely in upper-case, the word is changed to
681      * lower-case (except the characters following - and _)
682      * The first letter is always upper-case.
683      */

684     //FIXME: TO BE REMOVED. Making it private.
685
public static String JavaDoc camelize(String JavaDoc name)
686     {
687         if (null != name) {
688             name = name.trim();
689         }
690           
691
692         CharacterIterator JavaDoc ci;
693         StringBuffer JavaDoc n = new StringBuffer JavaDoc();
694         boolean up = true;
695         boolean keepCase = false;
696         char c;
697         
698         ci = new StringCharacterIterator JavaDoc(name);
699         c = ci.first();
700         
701         // If everything is uppercase, we'll lowercase the name.
702
while (c != CharacterIterator.DONE)
703         {
704             if (Character.isLowerCase(c))
705             {
706                 keepCase = true;
707                 break;
708             }
709             c = ci.next();
710         }
711         
712         c = ci.first();
713         while (c != CharacterIterator.DONE)
714         {
715             if (c == '-' || c == '_')
716                 up = true;
717             else
718             {
719                 if (up)
720                     c = Character.toUpperCase(c);
721                 else
722                     if (!keepCase)
723                         c = Character.toLowerCase(c);
724                 n.append(c);
725                 up = false;
726             }
727             c = ci.next();
728         }
729         return n.toString();
730     }
731     
732     /**
733      * should be called from the constructor and
734      * all the changeable methods
735      * Sets the lastModified to current time.
736      */

737     private void setThisLastModified() {
738         thisLastModified = System.currentTimeMillis();
739     }
740     
741     /**
742      * should be called ONLY from clone
743      * sets the lastmodified to timestamp
744      */

745     private void setGlobalLastModified(long timestamp) {
746         globalLastModified = timestamp;
747     }
748     
749     /**
750      * is package scope since it is called from configcontext
751      * FIXME: temporarily making it public for testing
752      */

753     public long getGlobalLastModified() {
754         return globalLastModified;
755     }
756     
757     public long getThisLastModified() {
758         return thisLastModified;
759     }
760     
761     public synchronized void setInterceptor(ConfigBeanInterceptor cbi) {
762         this._interceptor = cbi;
763     }
764
765     public synchronized ConfigBeanInterceptor getInterceptor() {
766         if (null != _interceptor) {
767             return _interceptor;
768         }
769         ConfigBeanInterceptor cbi = null;
770         //get interceptor of ctx.
771
if (null != ctx) {
772             /**
773              * Should have used the ConfigContext interface. Too late and
774              * risky to change the interface.
775              */

776             cbi = ((ConfigContextImpl)ctx).getConfigBeanInterceptor();
777         } else {
778             //get interceptor of parent.
779
ConfigBean parent = (ConfigBean)parent();
780             /*
781                 Hack :- BaseBean.parent() returns 'this' if this is root.
782                 Added the check (this != parent) to avoid recursion.
783             */

784             if ((null != parent) && (this != parent)) {
785                 cbi = parent.getInterceptor();
786             }
787         }
788         /*
789         if (null == cbi) {
790             cbi = EnvironmentFactory.getEnvironmentFactory().
791                 getConfigEnvironment().getConfigBeanInterceptor();
792         }*/

793         //_interceptor = cbi;
794
return cbi;
795     }
796     
797     private ConfigBeansSettings getConfigBeansSettings() {
798         return EnvironmentFactory.
799                 getEnvironmentFactory().
800                 getConfigEnvironment().
801                 getConfigBeansSettings();
802     }
803     
804     private Object JavaDoc postGetValue(String JavaDoc name, Object JavaDoc res) {
805         addXPathToChild(res);
806         return (getInterceptor()==null)
807                 ?res:getInterceptor().postGetValue(this, name, res);
808     }
809     private Object JavaDoc[] postGetValues(String JavaDoc name, Object JavaDoc[] res) {
810         addXPathToChild(res);
811         return (getInterceptor()==null)
812                 ?res:getInterceptor().postGetValues(name, res);
813     }
814     private String JavaDoc postGetAttributeValue(String JavaDoc name, String JavaDoc res) {
815         String JavaDoc s = (getInterceptor()==null)
816                 ?res:getInterceptor().postGetAttributeValue(name, res);
817     return (null != s ? s.trim() : s);
818     }
819     private Object JavaDoc preClone() {
820         if(getInterceptor() != null)
821             return getInterceptor().preClone();
822         return null;
823     }
824     
825     private void postClone(Object JavaDoc o, Object JavaDoc result) {
826         final ConfigBeanInterceptor cbi = getInterceptor();
827         if(cbi != null) {
828             cbi.postClone(o);
829         }
830     }
831     
832     /** remove this if getattribute value is always safe
833      */

834     private String JavaDoc getAttributeValueSafe(String JavaDoc name) {
835         try {
836             return this.getAttributeValue(name);
837         } catch(Throwable JavaDoc t){
838            //ignore exceptions. all kind.
839
}
840         return null;
841     }
842     
843     private void addToConfigChangeList(String JavaDoc xpath,
844                                         String JavaDoc tag,
845                                         String JavaDoc oldValue,
846                                         String JavaDoc newValue) {
847         ConfigChange cChange = null;
848         if(ctx != null) {
849             cChange = this.ctx.addToConfigChangeList(
850                                         (null != xpath ? xpath.trim() : xpath),
851                                         (null != tag ? tag.trim() : tag),
852                                         (null != oldValue ? oldValue.trim() : oldValue),
853                                         (null != newValue ? newValue.trim() : newValue));
854             if(cChange!=null) //can be null for "description" change
855
cChange.setGlobalLastModified(getGlobalLastModified());
856         }
857     }
858     
859     private String JavaDoc preSetAttributeValue(String JavaDoc name, String JavaDoc value) {
860         preConfigChange(name, value, ConfigContextEvent.PRE_UPDATE_CHANGE, "UPDATE",
861                         (null != this.name() ? this.name().trim() : this.name()));
862         
863         return this.getAttributeValueSafe(name);
864     }
865     
866     private void preAddValue(String JavaDoc name, Object JavaDoc value) {
867         preConfigChange(name, value, ConfigContextEvent.PRE_ADD_CHANGE, "ADD");
868     }
869     
870      private void preRemoveValue(String JavaDoc name, Object JavaDoc value) {
871          preConfigChange(name, value, ConfigContextEvent.PRE_DELETE_CHANGE, "DELETE");
872     }
873      
874      private void preSetValue(String JavaDoc name, Object JavaDoc value) {
875           preConfigChange(name, value, ConfigContextEvent.PRE_SET_CHANGE, "SET");
876      }
877
878     private void preConfigChange(String JavaDoc name, Object JavaDoc value, String JavaDoc type, String JavaDoc operation){
879         preConfigChange(name, value, type,operation, null);
880     }
881     
882     private void preConfigChange(String JavaDoc name, Object JavaDoc value, String JavaDoc type, String JavaDoc operation, String JavaDoc beanName) {
883         validateState();
884         
885         if(ctx !=null) {
886              ConfigContextEvent ccce = new ConfigContextEvent(ctx, type,name,value,operation, beanName);
887              ccce.setClassObject(this);
888              ctx.preChange(ccce);
889          }
890     }
891     
892     private void postSetAttributeValue(String JavaDoc name, String JavaDoc value, String JavaDoc oldValue) {
893         //reset the lastmodified timestamp
894
setThisLastModified();
895             
896             ConfigChange cChange = null;
897             
898           if(ctx != null) {
899                 cChange = this.ctx.addToConfigChangeList(this.xpath, name, oldValue, value);
900                 if(cChange != null) cChange.setGlobalLastModified(this.getGlobalLastModified());
901                 
902                 // <addition> srini@sun.com server.xml verifier
903
//this.ctx.postChange(ConfigContextEvent.POST_UPDATE_CHANGE);
904
ConfigContextEvent ccce = new ConfigContextEvent(ctx, ConfigContextEvent.POST_UPDATE_CHANGE,name,value,"UPDATE");
905                 this.ctx.postChange(ccce);
906                 // </addition> server.xml verifier
907
}
908     }
909     
910     private void postAddValue(String JavaDoc name, Object JavaDoc value) {
911         
912         //reset the lastmodified timestamp
913
setThisLastModified();
914          
915         //notification
916
if(ctx != null) {
917          if(value instanceof ConfigBean) {
918              try {
919                  //FIXME: clone?
920
ConfigChange cChange =
921                     this.ctx.addToConfigChangeList(
922                         this.xpath,
923                         ((ConfigBean)value).getAbsoluteXPath(this.xpath),
924                         name,
925                         ((ConfigBean)this.ctx.getRootConfigBean().clone()));
926                 if(cChange != null) cChange.setGlobalLastModified(this.getGlobalLastModified());
927              } catch(Exception JavaDoc ce) {
928                  ce.printStackTrace();
929              }
930             
931             ((ConfigBean)value).setConfigContext(this.ctx);
932             ((ConfigBean)value).setXPath(((ConfigBean)value).getAbsoluteXPath(this.xpath));
933          }
934             
935             ConfigContextEvent ccce = new ConfigContextEvent(ctx, ConfigContextEvent.POST_ADD_CHANGE,name,value,"ADD");
936             this.ctx.postChange(ccce);
937             
938          } else {
939              //_logger.log(Level.INFO,"config.change_not_registered");
940
}
941     }
942     
943     private void postSetValue(String JavaDoc name, Object JavaDoc value) {
944          //reset the lastmodified timestamp
945
setThisLastModified();
946          
947          if(ctx != null) {
948             // this.ctx.addToConfigChangeList(this.xpath, name, value, null);
949
//FIXME: remove later
950
if(value instanceof ConfigBean) {
951                 //FIXME: clone?
952
ConfigChange cChange = this.ctx.addToConfigChangeList(this.xpath, name, ((ConfigBean)value).clone(), null);
953                 if(cChange != null) cChange.setGlobalLastModified(this.getGlobalLastModified());
954                 
955                 ((ConfigBean)value).setConfigContext(this.ctx); //incase of description
956
((ConfigBean)value).setXPath(((ConfigBean)value).getAbsoluteXPath(this.xpath));
957                 // <addition> srini@sun.com
958
//ctx.postChange(ConfigContextEvent.POST_SET_CHANGE);
959
ConfigContextEvent ccce = new ConfigContextEvent(ctx,ConfigContextEvent.POST_SET_CHANGE,name,value,"SET");
960                 ctx.postChange(ccce);
961                 // </addition> server.xml verifier
962

963             } else {
964                 ConfigChange cChange = this.ctx.addToConfigChangeList(this.xpath, name, value, null);
965                 if(cChange != null) cChange.setGlobalLastModified(this.getGlobalLastModified());
966             }
967          } else {
968              //_logger.log(Level.INFO,"config.change_not_registered");
969
}
970     }
971     
972     private void postRemoveValue(String JavaDoc name, Object JavaDoc value) {
973         //reset the lastmodified timestamp
974
setThisLastModified();
975           
976           //notification
977
if(value instanceof ConfigBean){
978          if(ctx != null) {
979             ConfigChange cChange = this.ctx.addToConfigChangeList(((ConfigBean)value).getXPath());
980             if(cChange != null) cChange.setGlobalLastModified(this.getGlobalLastModified());
981             
982             // <addition> srini@sun.com server.xml verifier
983
//ctx.postChange(ConfigContextEvent.POST_DELETE_CHANGE);
984
ConfigContextEvent ccce = new ConfigContextEvent(ctx, ConfigContextEvent.POST_DELETE_CHANGE,name,value,"DELETE");
985             ctx.postChange(ccce);
986             // </addition> server.xml verifier
987

988          } else {
989              //_logger.log(Level.INFO,"config.change_not_registered");
990
}
991          }
992     }
993     private String JavaDoc preSetAttributeValueSpecial(String JavaDoc name) {
994         return getAttributeValueSafe(name);
995     }
996
997     private void postSetAttributeValueSpecial(String JavaDoc name,
998                             String JavaDoc value, String JavaDoc oldValue) {
999         //reset the lastmodified timestamp
1000
setThisLastModified();
1001             
1002        addToConfigChangeList(this.xpath,
1003                                     name,
1004                                     oldValue,
1005                                     value);
1006    }
1007
1008     private ConfigBean[] toConfigBeanArray(ArrayList JavaDoc cbRet) {
1009          ConfigBean[] ret = new ConfigBean[cbRet.size()];
1010                for(int j=0;j<cbRet.size();j++) {
1011                    ret[j] = (ConfigBean) cbRet.get(j);
1012                }
1013         return ret;
1014     }
1015     
1016     private ConfigBean[] getChildBeanByName(String JavaDoc childBeanName) {
1017         ConfigBean[] ret = null;
1018          try {
1019                ConfigBean cb = (ConfigBean) getValue(childBeanName);
1020                if(cb!=null)
1021                {
1022                    ret = new ConfigBean[1];
1023                    ret[0] = cb;
1024                }
1025             } catch (Exception JavaDoc c) {}
1026          return ret;
1027     }
1028      private void addXPathToChild(Object JavaDoc obj)
1029      {
1030          if(obj!=null && obj instanceof ConfigBean)
1031          {
1032            //set XPath for child bean
1033
ConfigBean cb = (ConfigBean)obj;
1034            if(cb.xpath==null && this.xpath!=null)
1035               cb.setXPath(cb.getAbsoluteXPath(this.xpath));
1036          }
1037      }
1038      
1039      private void addXPathToChild(Object JavaDoc[] obj)
1040      {
1041          if(obj==null)
1042              return;
1043          for(int i=0; i<obj.length; i++)
1044          {
1045              addXPathToChild(obj[i]);
1046          }
1047      }
1048      private void preRawGetAttributeValue(String JavaDoc name) {
1049          validateState();
1050          
1051          if(ctx !=null) {
1052             ConfigContextEvent ccce = new ConfigContextEvent(ctx, ConfigContextEvent.PRE_ACCESS);
1053             ccce.setClassObject(this);
1054             ctx.preChange(ccce);
1055         }
1056      }
1057      
1058      private void postRawGetAttributeValue(String JavaDoc name, String JavaDoc s) {
1059          if(ctx !=null) {
1060             ConfigContextEvent ccce = new ConfigContextEvent(ctx, ConfigContextEvent.POST_ACCESS);
1061             ctx.postChange(ccce);
1062         }
1063      }
1064      
1065      private void preSetArrayValue(String JavaDoc name, Object JavaDoc[] value) {
1066          validateState();
1067          
1068           if(ctx !=null) {
1069             ConfigContextEvent ccce =
1070                new ConfigContextEvent(ctx,
1071                                    ConfigContextEvent.PRE_SET_CHANGE,
1072                                    name,
1073                                    value,
1074                                    "SET");
1075             ccce.setClassObject(this);
1076             ccce.setBeanName(this.name());
1077             ctx.preChange(ccce);
1078         }
1079      }
1080      
1081      private void postSetArrayValue(String JavaDoc name, Object JavaDoc[] value) {
1082      
1083          //reset the lastmodified timestamp
1084
setThisLastModified();
1085          if(ctx != null) {
1086              ConfigChange cChange =
1087                this.ctx.addToConfigChangeList(this.xpath, name, null, value);
1088              if(cChange != null) {
1089                    cChange.setGlobalLastModified(this.getGlobalLastModified());
1090              }
1091              
1092              ConfigContextEvent ccce =
1093                new ConfigContextEvent(ctx,
1094                                    ConfigContextEvent.POST_SET_CHANGE,
1095                                    name,
1096                                    value,
1097                                    "SET");
1098             //ccce.setClassObject(this); //why FIXME??
1099
ccce.setBeanName(this.name());
1100             try {
1101                ctx.postChange(ccce);
1102             } catch(Exception JavaDoc e) {
1103                 //catch for now. may remove later.
1104
//e.printStackTrace(); //FIXME
1105
}
1106          } else {
1107              //_logger.log(Level.INFO,"config.change_not_registered");
1108
}
1109      }
1110      
1111      /**
1112       * state of the config beans
1113       */

1114      transient private String JavaDoc _state = VALID_STATE;
1115      
1116      /*
1117       * possible states
1118       */

1119      private static final String JavaDoc VALID_STATE = "valid_state";
1120      private static final String JavaDoc INVALID_STATE = "invalid_state";
1121      
1122      /**
1123       * is used to validate state before any operation to the config bean.
1124       * If the state is not valid, a runtime exception will be thrown
1125       */

1126      private void validateState() {
1127          if(!VALID_STATE.equals(_state)) {
1128              throw new ConfigRuntimeException("Config API Usage Error: State of ConfigBean is INVALID. No operations are permitted");
1129          }
1130      }
1131      
1132      private void setInvalidState() {
1133          _state = INVALID_STATE;
1134      }
1135}
1136
Popular Tags