/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-08 The eXist Project
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */

package org.exist.management.impl;
import java.io.File;
import java.io.FilenameFilter;
import javax.management.*;
import org.exist.storage.BrokerPool;
import org.exist.storage.journal.Journal;
import org.exist.util.Configuration;

/**
 * Class DiskUsage
 * 
 * @author wessels
 */
public class DiskUsage implements DiskUsageMBean {
    
    private BrokerPool pool;
    private Configuration config;

    
    public DiskUsage(BrokerPool pool){
        this.pool=pool;
        config = pool.getConfiguration();
    }

    public long getDataDirectoryFreeDiskSpace() {
        //return (new File( getJournalDirectory() ).getUsableSpace());
        return -(1L);
    }

    public String getDataDirectory() {
        return (String) config.getProperty(BrokerPool.PROPERTY_DATA_DIR);
    }

    public String getJournalDirectory() {
        return (String) config.getProperty(Journal.PROPERTY_RECOVERY_JOURNAL_DIR);
    }

    public long getJournalDirectoryFreeSpace() {
        //return new File(getJournalDirectory()).getUsableSpace();
        return -(1L);
    }

    public long getDataDirectoryTotalSpace() {
        //return (new File( getJournalDirectory() ).getTotalSpace());
        return -(1L);
    }

    public long getJournalDirectoryTotalSpace() {
         //return new File(getJournalDirectory()).getTotalSpace();
        return -(1L);
    }
    
    
    
}

class DbxFilenameFiler implements FilenameFilter {

    public boolean accept(File directory, String name) {
        if(name.endsWith(".dbx")){
            return true;
        } else {
            return false;
        }
    }
    
}

class JournalFilenameFiler implements FilenameFilter {

    public boolean accept(File directory, String name) {
        if(name.endsWith(".log")){
            return true;
        } else {
            return false;
        }
    }
    
}



@@ -974,23 +974,30 @@
 java
-x.management.*;
+.lang.reflect.Method;%0A
 %0Aimp
@@ -1394,37 +1394,38 @@
 ();%0A    %7D%0A%0A    p
-ublic
+rivate
  long getDataDir
@@ -1421,110 +1421,397 @@
  get
-DataDirectoryFreeDiskSpace() %7B%0A        //return (new File( getJournalDirectory() ).getUsableSp
+Space(File dir, String method) %7B%0A        try %7B%0A            Class cls = dir.getClass();%0A            Method m = cls.getMethod(method, new Class%5B0%5D);%0A            Long a = (Long) m.invoke(dir, new Object%5B0%5D);%0A            return a;%0A        %7D catch (NoSuchMethodException ex)%7B%0A            // method not %0A            %0A        %7D catch (Exception ex) %7B%0A            ex.printStackTr
 ace()
-)
 ;%0A
+        %7D%0A%0A
     
@@ -1814,36 +1814,33 @@
         return -
-(1L)
+1
 ;%0A    %7D%0A%0A    pub
@@ -2095,39 +2095,36 @@
  public long get
-Journal
+Data
 DirectoryFreeSpa
@@ -2116,20 +2116,21 @@
 irectory
-Free
+Total
 Space() 
@@ -2135,32 +2135,34 @@
 ) %7B%0A        
-//return
+File dir =
  new File(ge
@@ -2154,39 +2154,36 @@
 r = new File(get
-Journal
+Data
 Directory()).get
@@ -2182,46 +2182,208 @@
 y())
-.getUsableSpace();%0A        return -(1L
+;%0A        return getSpace(dir, %22getTotalSpace%22);%0A    %7D%0A%0A    public long getDataDirectoryFreeDiskSpace() %7B%0A        File dir = new File(getDataDirectory());%0A        return getSpace(dir, %22getUsableSpace%22
 );%0A 
@@ -2399,36 +2399,39 @@
  public long get
-Data
+Journal
 DirectoryTotalSp
@@ -2450,18 +2450,19 @@
     
-//return (
+File dir = 
 new 
@@ -2466,17 +2466,16 @@
 ew File(
- 
 getJourn
@@ -2491,48 +2491,55 @@
 ry()
- ).getTotalSpace());%0A        return -(1L
+);%0A        return getSpace(dir, %22getTotalSpace%22
 );%0A 
@@ -2571,37 +2571,36 @@
 JournalDirectory
-Total
+Free
 Space() %7B%0A      
@@ -2605,17 +2605,18 @@
     
- //return
+File dir =
  new
@@ -2647,24 +2647,8 @@
 y())
-.getTotalSpace()
 ;%0A  
@@ -2664,12 +2664,38 @@
 urn 
--(1L
+getSpace(dir, %22getUsableSpace%22
 );%0A 
@@ -2695,24 +2695,25 @@
 ce%22);%0A    %7D%0A
+%0A
     %0A    %0A  

check using reflection for Java6 extensions