edu.american.student.stonewall.util.Deployer.java Source code

Java tutorial

Introduction

Here is the source code for edu.american.student.stonewall.util.Deployer.java

Source

/**
 * Copyright 2013 Cameron Cook<br>
 * <br>
 * Licensed under the Apache License, Version 2.0 (the "License");<br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at<br>
 * <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br>
 * <br>
 * Unless required by applicable law or agreed to in writing, software<br>
 * distributed under the License is distributed on an "AS IS" BASIS,<br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br>
 * See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 */
package edu.american.student.stonewall.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.reflections.Reflections;

import edu.american.student.stonewall.display.html.framework.ValidationException;
import edu.american.student.stonewall.pages.Index;
import edu.american.student.stonewall.pages.Page;

public class Deployer {
    public static void deploy(List<String> packages, File deployDirectory) throws DeployException {
        Class<? extends Index> indexPage = null;
        List<Class<? extends Page>> otherPages = new ArrayList<Class<? extends Page>>();
        for (String pkg : packages) {
            Reflections reflections = new Reflections(pkg);
            Set<Class<? extends Index>> indexClasses = reflections.getSubTypesOf(Index.class);
            if (indexClasses.size() > 1) {
                throw new DeployException("More than 1 class found that extends Index");
            }
            if (indexClasses.size() == 1) {
                if (indexPage != null) {
                    throw new DeployException("More than 1 class found that extends Index");
                } else {
                    indexPage = indexClasses.iterator().next();
                }
            }
            Set<Class<? extends Page>> pageClasses = reflections.getSubTypesOf(Page.class);
            for (Class<? extends Page> page : pageClasses) {
                otherPages.add(page);
            }
        }
        try {
            deployMe(indexPage, otherPages, deployDirectory);
        } catch (Exception e) {
            throw new DeployException("Unable to deploy ", e);
        }
    }

    public static void deployMe(Class<? extends Index> indexPage, List<Class<? extends Page>> otherPages,
            File deployDirectory) throws DeployException {
        if (!deployDirectory.exists()) {
            deployDirectory.mkdirs();
        }
        // save resources
        File resourcesDir = new File("src/main/resources/" + ("res" + File.separator));
        try {
            saveResources(resourcesDir, deployDirectory);
        } catch (IOException e) {
            throw new DeployException("", e);
        }
        // save the index page as index.html
        Index page;
        try {
            page = (Index) indexPage.newInstance();
            save(HTMLFormatter.format(page.makePage()), "index.html", deployDirectory);
        } catch (InstantiationException e) {
            throw new DeployException("", e);
        } catch (IllegalAccessException e) {
            throw new DeployException("", e);
        } catch (IOException e) {
            throw new DeployException("", e);
        } catch (ValidationException e) {
            throw new DeployException("", e);
        }
        for (Class<? extends Page> p : otherPages) {
            Page pg;
            try {
                pg = p.newInstance();
                save(HTMLFormatter.format(pg.makePage()), pg.getClass().getSimpleName() + ".html",
                        new File(deployDirectory + File.separator + getPath(p)));
            } catch (InstantiationException e) {
                throw new DeployException("", e);
            } catch (IllegalAccessException e) {
                throw new DeployException("", e);
            } catch (IOException e) {
                throw new DeployException("", e);
            } catch (ValidationException e) {
                throw new DeployException("", e);
            }
        }
    }

    private static void saveResources(File dir, File deployDir) throws IOException {
        walkin(dir, new File(deployDir.getAbsolutePath() + File.separator + dir.getName()));
    }

    private static void walkin(File dir, File deployDir) throws IOException {
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {
                    File f = new File(deployDir.getAbsolutePath() + File.separator + listFile[i].getName());
                    f.mkdir();
                    walkin(listFile[i], f);
                } else {
                    System.out.println("found resource:" + listFile[i].getAbsolutePath());
                    saveResource(listFile[i], deployDir);
                }
            }
        }
    }

    private static void saveResource(File file, File deployDir) throws IOException {
        FileUtils.copyFile(file, new File(deployDir + File.separator + file.getName()));
    }

    private static String getPath(Class<? extends Page> p) {
        Package pkg = p.getPackage();
        System.out.println(pkg.toString());
        return pkg.toString().replace("root", "").replace(".", File.separator).replace("package", "").trim();
    }

    private static void save(String source, String name, File destination) throws IOException {
        if (!destination.exists()) {
            destination.mkdirs();
        }
        // Create file
        FileWriter fstream = new FileWriter(destination.getAbsolutePath() + File.separator + name);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(source);
        // Close the output stream
        out.close();
    }
}