001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.config.core;
020:
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.PrintWriter;
024: import java.util.Enumeration;
025: import java.util.Properties;
026: import java.util.Vector;
027:
028: /**
029: * Properties Configuration
030: */
031: abstract public class PropertiesConfiguration extends FileConfiguration {
032:
033: private Properties defaultProps;
034: private Properties localProps;
035:
036: /**
037: *
038: */
039: public void readDefault() {
040: try {
041: defaultProps = new Properties();
042: defaultProps
043: .load(new FileInputStream(getFilenameDefault()));
044: } catch (Exception e) {
045: System.err.println(e.getMessage());
046: }
047: }
048:
049: /**
050: *
051: */
052: public void readLocal() {
053: try {
054: localProps = new Properties();
055: localProps.load(new FileInputStream(getFilenameLocal()));
056: } catch (Exception e) {
057: System.err.println(e.getMessage());
058: }
059: }
060:
061: /**
062: *
063: */
064: public void writeLocal() {
065: String header = "Created by org.apache.lenya.config.PropertiesConfiguration";
066:
067: try {
068: PrintWriter out = new PrintWriter(new FileOutputStream(
069: getFilenameLocal()));
070: out.println("#" + header);
071: for (int i = 0; i < params.length; i++) {
072: out.println("\n#");
073: out.println(params[i].getName() + "="
074: + params[i].getLocalValue());
075: }
076: out.close();
077: } catch (Exception e) {
078: System.err.println(e.getMessage());
079: }
080:
081: /*
082: Properties newLocalProperties = new Properties();
083: for (int i = 0; i < params.length; i++) {
084: newLocalProperties.setProperty(params[i].getName(), params[i].getLocalValue());
085: }
086:
087: try {
088: newLocalProperties.store(new FileOutputStream(getFilenameLocal()), header);
089: } catch(Exception e) {
090: System.err.println(e.getMessage());
091: }
092: */
093: }
094:
095: /**
096: *
097: */
098: public void read() {
099: readDefault();
100: readLocal();
101:
102: Vector p = new Vector();
103: Enumeration names = defaultProps.propertyNames();
104: while (names.hasMoreElements()) {
105: String name = (String) names.nextElement();
106: Parameter param = new Parameter();
107: param.setName(name);
108: param.setDefaultValue(defaultProps.getProperty(name));
109: String localValue = localProps.getProperty(name);
110: if (localValue != null) {
111: param.setLocalValue(localProps.getProperty(name));
112: } else {
113: param.setLocalValue(defaultProps.getProperty(name));
114: }
115: p.addElement(param);
116: }
117: params = new Parameter[p.size()];
118: for (int i = 0; i < params.length; i++) {
119: params[i] = (Parameter) p.elementAt(i);
120: }
121: }
122:
123: /**
124: *
125: */
126: public Parameter[] getParameters() {
127: return params;
128: }
129: }
|