/**
 *
 * Copyright (c) 2006-2016, Speedment, Inc. All Rights Reserved.
 *
 * 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.
 */
package com.speedment.maven;

import com.speedment.generator.internal.component.CodeGenerationComponentImpl;
import com.speedment.runtime.Speedment;
import com.speedment.runtime.SpeedmentBuilder;
import com.speedment.runtime.component.Component;
import com.speedment.runtime.internal.runtime.DefaultApplicationBuilder;
import com.speedment.tool.internal.component.UserInterfaceComponentImpl;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 *
 * @author Emil Forslund
 */
abstract class AbstractSpeedmentMojo extends AbstractMojo {
    
    private final SpeedmentBuilder<?, ?> builder;

    protected abstract File configLocation();
    protected abstract Class<? extends Component>[] components();
    protected abstract String launchMessage();
    protected abstract void execute(Speedment speedment) 
        throws MojoExecutionException, MojoFailureException;
    
    protected AbstractSpeedmentMojo() {
        builder = createBuilder();
    }

    @Override
    public final void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info(launchMessage());    
        execute(builder.build());
    }
    
    protected final boolean hasConfigFile() {
        if (configLocation() == null) {
            final String err = "The specified .json-file is null.";
            getLog().error(err);
            return false;
        } else if (!configLocation().exists()) {
            final String err = "The specified .json-file '" + configLocation().getAbsolutePath() + "' does not exist.";
            getLog().error(err);
            return false;
        } else if (!configLocation().canRead()) {
            final String err = "The specified .json-file '" + configLocation().getAbsolutePath() + "' is not readable.";
            getLog().error(err);
            return false;
        } else return true;
    }
    
    private SpeedmentBuilder<?, ?> createBuilder() {
        final SpeedmentBuilder<?, ?> result;
        
        if (hasConfigFile()) {
            result = new DefaultApplicationBuilder(configLocation());
        } else {
            result = new DefaultApplicationBuilder();
        }
        
        result.with(CodeGenerationComponentImpl.class);
        result.with(UserInterfaceComponentImpl.class);
        
        final Class<? extends Component>[] components = components();
        if (components != null) {
            for (final Class<? extends Component> component : components()) {
                result.with(component);
            }
        }
        
        return result;
    }
}
@@ -1012,24 +1012,113 @@
 ponentImpl;%0A
+import static com.speedment.tool.internal.util.ConfigFileHelper.DEFAULT_CONFIG_LOCATION;%0A
 import java.
@@ -1377,24 +1377,106 @@
 Mojo %7B%0A    %0A
+    private final static File DEFAULT_CONFIG = new File(DEFAULT_CONFIG_LOCATION);%0A
     private 
@@ -2126,28 +2126,130 @@
     
-if (configLocation()
+return hasConfigFile(configLocation());%0A    %7D%0A    %0A    protected final boolean hasConfigFile(File file) %7B%0A        if (file
  == 
@@ -2403,32 +2403,20 @@
 se if (!
-configLocation()
+file
 .exists(
@@ -2482,32 +2482,20 @@
 le '%22 + 
-configLocation()
+file
 .getAbso
@@ -2607,32 +2607,20 @@
 se if (!
-configLocation()
+file
 .canRead
@@ -2687,32 +2687,20 @@
 le '%22 + 
-configLocation()
+file
 .getAbso
@@ -3036,24 +3036,144 @@
 ocation());%0A
+        %7D else if (hasConfigFile(DEFAULT_CONFIG)) %7B%0A            result = new DefaultApplicationBuilder(DEFAULT_CONFIG);%0A
         %7D el

Fix bug with reloading from json file