KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > NestedConfigurationDocument


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.node;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import org.sape.carbon.core.config.Configuration;
25 import org.sape.carbon.core.config.format.ConfigurationFormatException;
26
27
28 /**
29  * ConfigurationDocument implementation for configurations nested within
30  * other ConfigurationDocuments. Parents of this Node can only be
31  * AbsractConfigurationDocuments.
32  *
33  * Copyright 2002 Sapient
34  * @since carbon 1.1
35  * @author Douglas Voet, Sep 11, 2002
36  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/10/17 14:40:55 $)
37  */

38 public class NestedConfigurationDocument
39     extends AbstractConfigurationDocument {
40
41     /**
42      * This should only be called by NestedConfigurationDocumentFactory
43      *
44      * @param parent
45      * @param name
46      */

47     protected NestedConfigurationDocument(
48         AbstractConfigurationDocument parent,
49         String JavaDoc name) {
50             
51         super(
52             parent,
53             name,
54             parent.getReadOrAlterNodeLock(),
55             parent.getAddOrLoadChildLock(),
56             parent.getFormatService());
57     }
58
59     /**
60      * @see org.sape.carbon.core.config.node.ConfigurationDocument#readConfiguration()
61      */

62     public Configuration readConfiguration()
63         throws NodeIOException, ConfigurationFormatException {
64
65         if (isRemoved()) {
66             throw new NodeRemovedException(
67                 this.getClass(),
68                 this);
69         }
70
71         // note: no caching done here - the parent caches the main config
72
// and the nested config reads directly every time
73
return getConfiguration();
74     }
75
76     /**
77      * @see org.sape.carbon.core.config.node.ConfigurationDocument#writeConfiguration(Configuration)
78      */

79     public void writeConfiguration(Configuration config)
80         throws NodeIOException, ConfigurationFormatException {
81
82         if (isRemoved()) {
83             throw new NodeRemovedException(
84                 this.getClass(),
85                 this);
86         }
87
88         ConfigurationDocument parentDoc =
89             (ConfigurationDocument) getParent();
90
91         Configuration parentConfig = parentDoc.readConfiguration();
92
93         getFormatService().alterChildConfiguration(
94             parentConfig,
95             getName(),
96             config);
97
98         parentDoc.writeConfiguration(parentConfig);
99     }
100
101     /**
102      * Called by parent documents when the have changed in order for this
103      * node to notify its listeners that a change has occured
104      */

105     public void onChange() {
106         issueNodeChangedEvent();
107     }
108
109     /**
110      * This should never be called. If it is, this class was not written
111      * correctly. All reads should be done via parent's document
112      *
113      * @return never returns
114      * @throws IOException never thrown
115      * @throws UnsupportedOperationException always thrown to indicate this
116      * method is not supported.
117      */

118     protected InputStream JavaDoc openInputStream() throws IOException JavaDoc {
119         // if this class overrides super correctly,
120
// this should never get called
121
throw new UnsupportedOperationException JavaDoc(
122             "openInputStream should not be "
123             + "called on NestedConfigurationDocuments");
124     }
125
126     /**
127      * This should never be called. If it is, this class was not written
128      * correctly. All writes should be done via parent's document
129      *
130      * @return never returns
131      * @throws IOException never thrown
132      * @throws UnsupportedOperationException always thrown to indicate this
133      * method is not supported.
134      */

135     protected OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
136         // if this class overrides super correctly, this
137
// should never get called
138
throw new UnsupportedOperationException JavaDoc(
139             "openOutputStream should not be "
140                 + "called on NestedConfigurationDocuments");
141     }
142
143     /**
144      * @see org.sape.carbon.core.config.node.AbstractNode#destroyBackingData()
145      */

146     protected void destroyBackingData() throws NodeRemovalException {
147         try {
148             writeConfiguration(null);
149
150         } catch (NodeIOException nioe) {
151             throw new NodeRemovalException(
152                 this.getClass(),
153                 this,
154                 nioe);
155         } catch (ConfigurationFormatException cfe) {
156             throw new NodeRemovalException(
157                 this.getClass(),
158                 this,
159                 cfe);
160         }
161     }
162
163     /**
164      * @see org.sape.carbon.core.config.node.AbstractNode#backingDataExists()
165      */

166     protected boolean backingDataExists() {
167         try {
168             return (getConfiguration() != null);
169
170         } catch (NodeIOException e) {
171             return false;
172         } catch (ConfigurationFormatException e) {
173             return false;
174         }
175     }
176     /**
177      * @see org.sape.carbon.core.config.node.Node#isRemoved()
178      */

179     public boolean isRemoved() {
180         if (getParent().isRemoved()) {
181             // if parents are removed, nested nodes must have been removed too
182
return true;
183         } else {
184             return super.isRemoved();
185         }
186     }
187
188     /**
189      * Helper method to get the configuration for this node.
190      *
191      * @return the configuration document
192      * @throws NodeIOException indicates an error reading the node
193      * @throws ConfigurationFormatException indicates an error with the
194      * configuration document
195      */

196     private Configuration getConfiguration()
197         throws NodeIOException, ConfigurationFormatException {
198
199         Configuration parentConfig =
200             ((ConfigurationDocument) getParent()).readConfiguration();
201
202         return
203             getFormatService().getChildConfiguration(parentConfig, getName());
204     }
205 }
206
Popular Tags