KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > property > PropertyValidation


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.property.PropertyValidation
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.property;
23
24 import org.apache.derby.iapi.reference.Attribute;
25 import org.apache.derby.iapi.reference.Property;
26 import org.apache.derby.iapi.reference.SQLState;
27
28 import org.apache.derby.iapi.services.property.PropertyUtil;
29 import org.apache.derby.iapi.error.StandardException;
30 import org.apache.derby.iapi.services.daemon.Serviceable;
31 import org.apache.derby.iapi.services.property.PropertySetCallback;
32 import org.apache.derby.iapi.store.access.TransactionController;
33 import org.apache.derby.iapi.store.access.conglomerate.TransactionManager;
34 import java.io.Serializable JavaDoc;
35 import java.util.Dictionary JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 public class PropertyValidation implements PropertyFactory
41 {
42     private Vector JavaDoc notifyOnSet;
43
44     /* Constructors for This class: */
45     public PropertyValidation()
46     {
47
48     }
49
50     public Serializable JavaDoc doValidateApplyAndMap(TransactionController tc,
51                                              String JavaDoc key, Serializable JavaDoc value,
52                                              Dictionary JavaDoc d, boolean dbOnlyProperty)
53          throws StandardException
54     {
55         Serializable JavaDoc mappedValue = null;
56         if (notifyOnSet != null) {
57             synchronized (this) {
58
59                 for (int i = 0; i < notifyOnSet.size() ; i++) {
60                     PropertySetCallback psc = (PropertySetCallback) notifyOnSet.elementAt(i);
61                     if (!psc.validate(key, value, d))
62                         continue;
63
64                     // if this property should not be used then
65
// don't call apply. This depends on where
66
// the old value comes from
67
// SET_IN_JVM - property will not be used
68
// SET_IN_DATABASE - propery will be used
69
// SET_IN_APPLICATION - will become SET_IN_DATABASE
70
// NOT_SET - will become SET_IN_DATABASE
71

72                     if (!dbOnlyProperty && key.startsWith("derby.")) {
73                         if (PropertyUtil.whereSet(key, d) == PropertyUtil.SET_IN_JVM)
74                             continue;
75                     }
76
77                     Serviceable s;
78                     if ((s = psc.apply(key,value,d)) != null)
79                         ((TransactionManager) tc).addPostCommitWork(s);
80                     if (mappedValue == null)
81                         mappedValue = psc.map(key, value, d);
82                 }
83             }
84         }
85         return mappedValue;
86     }
87     /**
88       Call the property set callbacks to map a proposed property value
89       to a value to save.
90       <P>
91       The caller must run this in a block synchronized on this
92       to serialize validations with changes to the set of
93       property callbacks
94       */

95     public Serializable JavaDoc doMap(String JavaDoc key,
96                              Serializable JavaDoc value,
97                              Dictionary JavaDoc set)
98          throws StandardException
99     {
100         Serializable JavaDoc mappedValue = null;
101         if (notifyOnSet != null) {
102             for (int i = 0; i < notifyOnSet.size() && mappedValue == null; i++) {
103                 PropertySetCallback psc = (PropertySetCallback) notifyOnSet.elementAt(i);
104                 mappedValue = psc.map(key, value, set);
105             }
106         }
107
108         if (mappedValue == null)
109             return value;
110         else
111             return mappedValue;
112     }
113
114     public void validateSingleProperty(String JavaDoc key,
115                           Serializable JavaDoc value,
116                           Dictionary JavaDoc set)
117          throws StandardException
118     {
119         // RESOLVE: log device cannot be changed on the fly right now
120
if (key.equals(Attribute.LOG_DEVICE))
121         {
122             throw StandardException.newException(
123                     SQLState.RAWSTORE_CANNOT_CHANGE_LOGDEVICE);
124         }
125
126         if (notifyOnSet != null) {
127             for (int i = 0; i < notifyOnSet.size(); i++) {
128                 PropertySetCallback psc = (PropertySetCallback) notifyOnSet.elementAt(i);
129                 psc.validate(key, value, set);
130             }
131         }
132     }
133
134     public synchronized void addPropertySetNotification(PropertySetCallback who){
135
136         if (notifyOnSet == null)
137             notifyOnSet = new Vector JavaDoc(1,1);
138         notifyOnSet.addElement(who);
139
140     }
141
142     public synchronized void verifyPropertySet(Properties JavaDoc p,Properties JavaDoc ignore)
143          throws StandardException
144     {
145         for (Enumeration JavaDoc e = p.propertyNames(); e.hasMoreElements();)
146         {
147             String JavaDoc pn = (String JavaDoc)e.nextElement();
148             //
149
//Ignore the ones we are told to ignore.
150
if (ignore.getProperty(pn) != null) continue;
151             Serializable JavaDoc pv = p.getProperty(pn);
152             validateSingleProperty(pn,pv,p);
153         }
154     }
155 }
156
157
158
159
Popular Tags