com.stratuscom.harvester.PropertiesFileReader.java Source code

Java tutorial

Introduction

Here is the source code for com.stratuscom.harvester.PropertiesFileReader.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stratuscom.harvester;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileType;

/**
 *
 * @author trasukg
 */
public class PropertiesFileReader {

    private static final Logger log = Logger.getLogger(PropertiesFileReader.class.getName(),
            MessageNames.BUNDLE_NAME);

    @Injected(style = InjectionStyle.BY_TYPE)
    Context context = null;
    @Injected
    FileUtility fileUtility = null;

    @Init
    public void initialize() {
        try {
            FileObject[] childFiles = fileUtility.getProfileDirectory().getChildren();
            for (FileObject fo : childFiles) {
                if (fo.getName().getBaseName().endsWith(Strings.DOT_PROPERTIES) && fo.getType() == FileType.FILE) {
                    readPropertiesFile(fo);
                }
            }
        } catch (Exception ex) {
            throw new LocalizedRuntimeException(ex, MessageNames.BUNDLE_NAME, MessageNames.FAILED_READ_PROPERTIES);
        }
    }

    private void readPropertiesFile(FileObject fo) throws FileSystemException, IOException {
        String name = fo.getName().getBaseName();
        Properties props = getProperties(fo);
        context.put(name, props);
        log.log(Level.FINE, MessageNames.READ_PROPERTIES_FILE, name);
        if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, MessageNames.READ_PROPERTIES, Utils.format(props));
        }
    }

    public Properties getProperties(FileObject fo) throws FileSystemException, IOException {
        InputStream is = fo.getContent().getInputStream();
        Properties props = new Properties();
        props.load(is);
        return props;
    }

}