package org.jtheque.core.impl;

/*
 * Copyright JTheque (Baptiste Wicht)
 *
 * Licensed 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.
 */

import org.jtheque.core.ApplicationListener;
import org.jtheque.core.Core;
import org.jtheque.core.FoldersContainer;
import org.jtheque.core.application.Application;
import org.jtheque.utils.annotations.ThreadSafe;
import org.jtheque.utils.io.FileUtils;

import java.io.File;

/**
 * Give access to the folders of the application. Pay attention that if the application has not been launched,
 * the folders are all {@code null}. 
 *
 * @author Baptiste Wicht
 */
@ThreadSafe
public final class Folders implements FoldersContainer, ApplicationListener {
    private File applicationFolder;
    private File modulesFolder;
    private File logsFolder;

    /**
     * Construct a new Folders.
     *
     * @param core The core.
     */
    public Folders(Core core) {
        super();

        core.addApplicationListener(this);
    }

    @Override
    public File getApplicationFolder() {
        return applicationFolder;
    }

    @Override
    public File getLogsFolder() {
        return logsFolder;
    }

    @Override
    public File getModulesFolder() {
        return modulesFolder;
    }

    @Override
    public void applicationLaunched(Application application) {
        synchronized (this){
            applicationFolder = new File(application.getFolderPath());
            FileUtils.createIfNotExists(applicationFolder);

            logsFolder = new File(applicationFolder, "logs");
            FileUtils.createIfNotExists(logsFolder);

            modulesFolder = new File(applicationFolder, "modules");
            FileUtils.createIfNotExists(modulesFolder);
        }
    }
}
@@ -1221,24 +1221,33 @@
     private 
+volatile 
 File applica
@@ -1267,24 +1267,33 @@
     private 
+volatile 
 File modules
@@ -1313,16 +1313,25 @@
 private 
+volatile 
 File log
@@ -1606,182 +1606,601 @@
     
-return applicationFolder;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public File getLogsFolder() %7B%0D%0A        return logsFolder;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public File getModulesFolder() %7B
+if (applicationFolder == null) %7B%0D%0A            throw new IllegalStateException(%22The application has not been launched. %22);%0D%0A        %7D%0D%0A%0D%0A        return applicationFolder;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public File getLogsFolder() %7B%0D%0A        if (logsFolder == null) %7B%0D%0A            throw new IllegalStateException(%22The application has not been launched. %22);%0D%0A        %7D%0D%0A%0D%0A        return logsFolder;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public File getModulesFolder() %7B%0D%0A        if(modulesFolder == null)%7B%0D%0A            throw new IllegalStateException(%22The application has not been launched. %22);%0D%0A        %7D%0D%0A
 %0D%0A  
@@ -2320,42 +2320,8 @@
  %7B%0D%0A
-        synchronized (this)%7B%0D%0A    
     
@@ -2384,36 +2384,32 @@
 th());%0D%0A        
- 
-   
 FileUtils.create
@@ -2435,36 +2435,32 @@
 tionFolder);%0D%0A%0D%0A
-    
         logsFold
@@ -2502,36 +2502,32 @@
 ogs%22);%0D%0A        
- 
-   
 FileUtils.create
@@ -2550,28 +2550,24 @@
 Folder);%0D%0A%0D%0A
-    
         modu
@@ -2623,28 +2623,24 @@
 );%0D%0A        
-    
 FileUtils.cr
@@ -2672,27 +2672,16 @@
 lder);%0D%0A
-        %7D%0D%0A
     %7D%0D%0A%7D

Correct synchronization issues