/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning 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.ning.metrics.collector.binder;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.ning.metrics.collector.binder.config.CollectorConfig;
import com.ning.serialization.ThriftEnvelopeSerialization;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.RawLocalFileSystem;

import java.io.IOException;
import java.net.URI;

public class FileSystemProvider implements Provider<FileSystem>
{
    private final FileSystem fileSystem;

    @Inject
    public FileSystemProvider(CollectorConfig collectorConfig) throws IOException
    {
        String hfsHost = collectorConfig.getHfsHost();
        if (!hfsHost.isEmpty()) {
            Configuration hadoopConfig = new Configuration();

            hadoopConfig.set("fs.default.name", hfsHost);
            hadoopConfig.setLong("dfs.block.size", collectorConfig.getHadoopBlockSize());
            hadoopConfig.set("hadoop.job.ugi", collectorConfig.getHadoopUgi());
            hadoopConfig.setStrings("io.serializations", ThriftEnvelopeSerialization.class.getName(), "org.apache.hadoop.io.serializer.WritableSerialization");

            fileSystem = FileSystem.get(hadoopConfig);
        }
        else {
            // Useful for testing in isolated environments
            FileSystem hadoopIsStupid = new RawLocalFileSystem();
            Configuration hadoopConfig = new Configuration();
            hadoopConfig.setStrings("io.serializations", ThriftEnvelopeSerialization.class.getName(), "org.apache.hadoop.io.serializer.WritableSerialization");
            hadoopIsStupid.initialize(URI.create("not://used"), hadoopConfig);

            fileSystem = new LocalFileSystem(hadoopIsStupid);
        }
    }

    public FileSystem get()
    {
        return fileSystem;
    }
}

@@ -937,101 +937,8 @@
 tem;
-%0Aimport org.apache.hadoop.fs.LocalFileSystem;%0Aimport org.apache.hadoop.fs.RawLocalFileSystem;
 %0A%0Aim
@@ -966,29 +966,8 @@
 ion;
-%0Aimport java.net.URI;
 %0A%0Apu
@@ -1239,46 +1239,8 @@
     
-if (!hfsHost.isEmpty()) %7B%0A            
 Conf
@@ -1285,16 +1285,180 @@
 tion();%0A
+%0A        if (hfsHost.isEmpty()) %7B%0A            // Local filesystem, for testing%0A            hadoopConfig.set(%22fs.default.name%22, %22file:///%22);%0A        %7D%0A        else %7B
 %0A       
@@ -1508,24 +1508,31 @@
 sHost);%0A    
+    %7D%0A%0A
         hado
@@ -1605,36 +1605,32 @@
 ize());%0A        
-    
 hadoopConfig.set
@@ -1677,28 +1677,24 @@
 doopUgi());%0A
-    
         hado
@@ -1838,36 +1838,32 @@
 ion%22);%0A%0A        
-    
 fileSystem = Fil
@@ -1893,532 +1893,8 @@
 g);%0A
-        %7D%0A        else %7B%0A            // Useful for testing in isolated environments%0A            FileSystem hadoopIsStupid = new RawLocalFileSystem();%0A            Configuration hadoopConfig = new Configuration();%0A            hadoopConfig.setStrings(%22io.serializations%22, ThriftEnvelopeSerialization.class.getName(), %22org.apache.hadoop.io.serializer.WritableSerialization%22);%0A            hadoopIsStupid.initialize(URI.create(%22not://used%22), hadoopConfig);%0A%0A            fileSystem = new LocalFileSystem(hadoopIsStupid);%0A        %7D%0A
     

refactor HDFS connection