summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/base/FileConfigStore.java
blob: 95e3ba67497b5d0ec12cf9e4cd4cbdb5fc10a7c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.base;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Vector;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.cmsutil.util.Utils;


/**
 * FileConfigStore:
 * Extends HashConfigStore with methods to load/save from/to file for 
 * persistent storage. This is a configuration store agent who
 * reads data from a file.
 * <P>
 * Note that a LdapConfigStore can be implemented so that it reads 
 * the configuration stores from the Ldap directory.
 * <P>
 * 
 * @version $Revision$, $Date$
 * @see PropConfigStore
 */
public class FileConfigStore extends PropConfigStore implements 
        IConfigStore {

    private File mFile = null;

    /**
     * Constructs a file configuration store.
     * <P>
     *
     * @param fileName file name
     * @exception EBaseException failed to create file configuration
     */
    public FileConfigStore(String fileName) throws EBaseException {
        super(null); // top-level store without a name
        mFile = new File(fileName);
        if (!mFile.exists()) {
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_NO_CONFIG_FILE", 
                        mFile.getPath()));
        }
        load(fileName);
    }

    /**
     * Loads property file into memory.
     * <P>
     *
     * @param fileName file name
     * @exception EBaseException failed to load configuration
     */
    public void load(String fileName) throws EBaseException {
        try {
            FileInputStream fi = new FileInputStream(fileName);
            BufferedInputStream bis = new BufferedInputStream(fi);

            super.load(bis);
        } catch (IOException e) {
            throw new EBaseException("input stream error " + fileName, e);
        }
    }

    /**
     * The original config file is copied to
     * <filename>.<current_time_in_milliseconds>.
     * Commits the current properties to the configuration file.
     * <P>
     *
     * @param backup 
     */
    public void commit(boolean createBackup) throws EBaseException {
        if (createBackup) {
            File newName = new File(mFile.getPath() + "." +
                    Long.toString(System.currentTimeMillis()));

            try {
                if( Utils.isNT() ) {
                    // NT is very picky on the path
                    Utils.exec( "copy " +
                                mFile.getAbsolutePath().replace( '/', '\\' ) +
                                " " +
                                newName.getAbsolutePath().replace( '/',
                                                                   '\\' ) );
                } else {
                    // Create a copy of the original file which
                    // preserves the original file permissions.
                    Utils.exec( "cp -p " + mFile.getAbsolutePath() + " " +
                                newName.getAbsolutePath() );
                }

                // Proceed only if the backup copy was successful.
                if( !newName.exists() ) {
                    throw new EBaseException( "backup copy failed" );
                } else {
                    // Make certain that the backup file has
                    // the correct permissions.
                    if( !Utils.isNT() ) {
                        Utils.exec( "chmod 00660 " + newName.getAbsolutePath() );
                    }
                }
            } catch( EBaseException e ) {
                    throw new EBaseException( "backup copy failed" );
            }
        }

        // Overwrite the contents of the original file
        // to preserve the original file permissions.
        save( mFile.getPath() );

        try {
            // Make certain that the original file retains
            // the correct permissions.
            if( !Utils.isNT() ) {
                Utils.exec( "chmod 00660 " + mFile.getCanonicalPath() );
            }
        } catch( Exception e ) {
        }
    }

    /**
     * Saves in-memory properties to a specified file.
     * <P>
     * Note that the superclass's save is synchronized. It
     * means no properties can be altered (inserted) at
     * the saving time.
     * <P>
     *
     * @param fileName filename
     * @exception EBaseException failed to save configuration
     */
    public void save(String fileName) throws EBaseException {
        try {
            FileOutputStream fo = new FileOutputStream(fileName);
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(fo));

            printSubStore(writer, this, "");
            writer.close();
            fo.close();
        } catch (IOException e) {
            throw new EBaseException("output stream error " + fileName, e);
        }
    }

    private void printSubStore(PrintWriter writer, IConfigStore store,
        String name) throws EBaseException,
            IOException {
        // print keys
        Enumeration e0 = store.getPropertyNames();
        Vector v = new Vector();

        while (e0.hasMoreElements()) {
            v.addElement(e0.nextElement());
        }

        // sorting them lexicographically
        while (v.size() > 0) {
            String pname = (String) v.firstElement();
            int j = 0;

            for (int i = 1; i < v.size(); i++) {
                String s = (String) v.elementAt(i);

                if (pname.compareTo(s) > 0) {
                    j = i;
                    pname = (String) v.elementAt(i);
                }
            }
            v.removeElementAt(j);
            writer.println(name + pname + "=" + store.getString(pname));
        }

        // print substores
        Enumeration e1 = store.getSubStoreNames();

        while (e1.hasMoreElements()) {
            v.addElement(e1.nextElement());
        }
        while (v.size() > 0) {
            String pname = (String) v.firstElement();
            int j = 0;

            for (int i = 1; i < v.size(); i++) {
                String s = (String) v.elementAt(i);

                if (pname.compareTo(s) > 0) {
                    j = i;
                    pname = (String) v.elementAt(i);
                }
            }
            v.removeElementAt(j);
            printSubStore(writer, store.getSubStore(pname), name +
                pname + ".");
        }
    }
}