KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > file > FileConfigurationDocument


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.file;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 import org.sape.carbon.core.config.format.ConfigurationFormatService;
28 import org.sape.carbon.core.config.node.AbstractConfigurationDocument;
29 import org.sape.carbon.core.config.node.Node;
30 import org.sape.carbon.core.config.node.NodeRemovalException;
31 import org.sape.carbon.core.exception.InvalidParameterException;
32
33 /**
34  * A node that represents a physical configuration file within a file system.
35  * Files are read and writen using a <code>ConfigurationFormatService</code>.
36  * These nodes
37  * may not have children. Read and write access to the underlying file
38  * is synchronized for thread safety.
39  *
40  * Copyright 2002 Sapient
41  * @see org.sape.carbon.core.config.node.file.FileFolder
42  * @see org.sape.carbon.core.config.format.ConfigurationFormatService
43  * @since carbon 1.0
44  * @author Mike Redd, February 2001
45  * @version $Revision: 1.16 $($Author: dvoet $ / $Date: 2003/05/05 21:21:19 $)
46  */

47 public class FileConfigurationDocument extends AbstractConfigurationDocument {
48
49     /** file where this node's configuration is persisted */
50     private File JavaDoc file;
51
52
53     /**
54      * Constructor for FileConfigurationDocument.
55      *
56      * @param parent the parent node containing this configuration.
57      * @param name name of the configuration being loaded
58      * @param formatter formatter for the document
59      * @param file file containing the configuration document
60      * @throws NullPointerException if file is null
61      */

62     public FileConfigurationDocument(
63         Node parent,
64         String JavaDoc name,
65         ConfigurationFormatService formatter,
66         File JavaDoc file) {
67
68         super(parent, name, formatter);
69
70         if (file != null) {
71             this.file = file;
72         } else {
73             throw new InvalidParameterException(
74                 this.getClass(),
75                 "The ["
76                     + getAbsoluteName()
77                     + "] FileConfigurationDocument "
78                     + "node file reference cannot be null");
79         }
80     }
81
82
83     /**
84      * @see org.sape.carbon.core.config.node.AbstractNode#destroyBackingData()
85      */

86     protected void destroyBackingData() throws NodeRemovalException {
87         if (!this.file.delete()) {
88             // If the file was not actually deleted,
89
// throw an exception
90
throw new NodeRemovalException(
91                 this.getClass(),
92                 this,
93                 "File.delete() returned false");
94         }
95     }
96
97     /**
98      * @see AbstractConfigurationDocument#openInputStream()
99      */

100     protected InputStream JavaDoc openInputStream() throws IOException JavaDoc {
101         return new FileInputStream JavaDoc(this.file);
102     }
103
104     /**
105      * @see AbstractConfigurationDocument#openOutputStream()
106      */

107     protected OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
108         return new FileOutputStream JavaDoc(this.file);
109     }
110
111     /**
112      * @see org.sape.carbon.core.config.node.AbstractConfigurationDocument#backingDataExists()
113      */

114     protected boolean backingDataExists() {
115         return (this.file.exists() && !this.file.isDirectory());
116     }
117
118 }
Popular Tags