KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > PropertyHelper


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
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import com.sun.enterprise.config.ConfigBean;
30 import com.sun.enterprise.config.serverbeans.ElementProperty;
31
32 /**
33  * helper class with convenience util methods
34  * for property changes validation
35 */

36
37 /* Class for attribute type Integer */
38
39 public abstract class PropertyHelper {
40     
41     static final String JavaDoc ELEMENT_PROPERTY = "ElementProperty";
42     static final String JavaDoc PROP_NAME_ATTRNAME = "name";
43     static final String JavaDoc PROP_VALUE_ATTRNAME = "value";
44
45     /***********************************************************
46      *
47      * this method can be called from a custom validator
48      * during validatePropertyChanges
49     ************************************************************/

50     static public Map JavaDoc getPropertiesMap(ConfigBean parent) {
51         HashMap JavaDoc map = new HashMap JavaDoc();
52         if(parent!=null)
53         {
54             ElementProperty[] props =
55                     (ElementProperty[])parent.getValues(ELEMENT_PROPERTY);
56             if(props!=null)
57             {
58                 for(int i=0; i<props.length; i++)
59                 {
60                     map.put(props[i].getName(), props[i].getValue());
61                 }
62             }
63         }
64         return map;
65     }
66    
67     /********************************************************
68      *
69      * returns true is named property will be changed as result of change event
70      *
71      ********************************************************/

72     static public boolean isPropertyChanged(ValidationContext propValCtx, String JavaDoc name) {
73         ElementProperty prop = (ElementProperty)propValCtx.getTargetBean();
74         if(!prop.getName().equals(name))
75         {
76             //check if name of property changing to checked one
77
if(!propValCtx.isUPDATE() ||
78                !PROP_NAME_ATTRNAME.equals(propValCtx.name) ||
79                !name.equals(propValCtx.value))
80             {
81                 //not related changes
82
return false;
83             }
84         }
85         
86         // first get current properties as map
87
Map JavaDoc map = getPropertiesMap(propValCtx.getParentBean());
88         // save "old" value for tested property
89
String JavaDoc oldValue = (String JavaDoc)map.get(name);
90         // "apply" change over existing props
91
addChangesToPropertiesMap(propValCtx, map);
92         // get "new" value for tested property
93
String JavaDoc newValue = (String JavaDoc)map.get(name);
94         // now - compare
95
if((oldValue==null && newValue==null) ||
96            (oldValue!=null && oldValue.equals(newValue)) )
97         {
98             //we are trying to set prop to already existing value
99
return false;
100         }
101         return true;
102     }
103     
104     /********************************************************
105      * returns map of element properties if changes would be accepted
106      *
107      *********************************************************/

108     static public Map JavaDoc getFuturePropertiesMap(ValidationContext propValCtx) {
109         Map JavaDoc map = getPropertiesMap(propValCtx.getParentBean());
110         return addChangesToPropertiesMap(propValCtx, map);
111     }
112
113     /********************************************************
114      *********************************************************/

115     static private Map JavaDoc addChangesToPropertiesMap(ValidationContext propValCtx, Map JavaDoc map) {
116         if(propValCtx.isADD() || propValCtx.isSET())
117         {
118             ElementProperty prop = (ElementProperty)propValCtx.getTargetBean();
119             map.put(prop.getName(), prop.getValue());
120         }
121         else if(propValCtx.isUPDATE())
122         {
123             ElementProperty prop = (ElementProperty)propValCtx.getTargetBean();
124             if(PROP_NAME_ATTRNAME.equals(propValCtx.name))
125             {
126                 // when name of property is changing
127
map.remove(prop.getName());
128                 map.put(propValCtx.value, prop.getValue());
129             }
130             else
131             {
132                 // when value of property is changing
133
map.put(prop.getName(), propValCtx.value);
134             }
135         }
136         return map;
137     }
138     
139 }
140
Popular Tags