Java tutorial
/* * Leaves Framework * Copyright (C) 2015 Hugo Miard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package com.hmiard.leaves.framework; import com.hmiard.leaves.framework.IO.Builder; import com.hmiard.leaves.framework.IO.Files.BranchParser; import com.hmiard.leaves.framework.IO.Files.FongiParser; import com.hmiard.leaves.framework.IO.Files.LeafWriter; import com.hmiard.leaves.framework.IO.Files.RootReader; import com.hmiard.leaves.framework.Objects.Application.LeavesConfiguration; import com.hmiard.leaves.framework.Objects.Hibernate.DBConfiguration; import com.hmiard.leaves.framework.Utils.Persistence.HibernateUtils; import com.hmiard.leaves.internal.Application.Controls.Branches.InternalBranch; import com.hmiard.leaves.internal.Application.Leaves.INTERNAL_IndexLeaf; import com.hmiard.leaves.webserver.Http.Exceptions.ReservedRouteException; import com.hmiard.leaves.webserver.Leaves; import com.hmiard.leaves.webserver.Server.Routing.BranchRoute; import com.hmiard.leaves.webserver.Server.Routing.LRoute; import com.hmiard.leaves.webserver.Utils.FileUtils; import com.hmiard.leaves.webserver.Utils.LeavesUtils; import org.hibernate.Session; import org.joda.time.DateTime; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.HashMap; import java.util.Properties; import java.util.jar.JarFile; /** * Encapsulates a basic Leaves application. * * Upon construction, the application will construct/update the leaves under the registered * leaves directory, according to the html files present in the registered roots directory. */ public class LeavesApplication extends Leaves { public static final String CURRENT_VERSION = "0.7.1"; public static String DEFAULT_APPLICATION_DIRECTORY = "Application"; private Session ORM = null; private RootReader rootReader; private LeafWriter leafWriter; private BranchParser branchParser; private FongiParser fongiParser; private DBConfiguration dbConfiguration; /** * Creating an app with the default directory architecture and a database. * Default constructor. * * @param conf The application configuration * @param dbconf The database access configuration * @param routes Optionnal additional routes */ public LeavesApplication(String[] args, LeavesConfiguration conf, DBConfiguration dbconf, LRoute... routes) { this(args, conf, dbconf, true, routes); } public LeavesApplication(String[] args, LeavesConfiguration conf, DBConfiguration dbconf, boolean doRun, LRoute... routes) { super(conf); this.dbConfiguration = dbconf; this.launch(args, doRun, routes); } /** * Creating an app that does not need any database connectivity. * * @param conf The application configuration * @param routes Optionnal additional routes */ public LeavesApplication(String[] args, LeavesConfiguration conf, LRoute... routes) { this(args, conf, null, true, routes); } public LeavesApplication(String[] args, LeavesConfiguration conf, boolean doRun, LRoute... routes) { this(args, conf, null, doRun, routes); } /** * Starting the application. */ private void launch(String[] args, boolean doRun, LRoute... routes) { if (!isInProduction()) Builder.buildMissingDirectories(this); this.rootReader = new RootReader(this); this.leafWriter = new LeafWriter(this); this.branchParser = new BranchParser(this); this.fongiParser = new FongiParser(this); // Common tasks. try { if (args[0] != null) { switch (args[0]) { case "clean": Builder.clean(this); System.exit(0); case "generate": if (args[1] == null || args[1].equals("leaves")) Builder.generateLeaves(this); else System.out.println("Unknown command : generate " + args[1]); System.exit(0); break; default: System.out.println("Unknown command : " + args[0]); } } } catch (ArrayIndexOutOfBoundsException ignored) { } if (this.hasDb()) { Builder.createHibernateConfFile(this.dbConfiguration, this.getCONF_DIRECTORY(), this.branchParser.gatherEntities(), this); ORM = HibernateUtils.getSessionFactory().openSession(); } this.leafWriter.additionalLeaves(); try { this.router.registerInternalRoutes(this); this.router.registerRoutes(routes); } catch (ReservedRouteException e) { e.printStackTrace(); } this.rootReader.parseRoots(); this.branchParser.parseBranches(); this.fongiParser.parseFongi(); if (!isInProduction()) LeavesUtils.updateLeavesListConfiguration(this.configuration, this.objectModel, this.resourceHolder); // Finally, launch the server. if (doRun) this.run(); } /** * Utility debug. * * @param something String to debug. */ public static void say(String something) { DateTime date = new DateTime(); System.out.println(date.toString() + " > Leaves Application : " + something + Leaves.NLSP); } public static void logo() { // Loadin' fancy, because we can. System.out.println("*******************************************" + Leaves.NLSP + "" + " .-." + Leaves.NLSP + "" + " _(/_ \\ L E A V E S F R A M E W O R K" + Leaves.NLSP + "" + " (\\ / Current version : v" + CURRENT_VERSION + " " + Leaves.NLSP + "" + " `-`" + Leaves.NLSP + "" + "*******************************************" + Leaves.NLSP); } /** * Checking if the application is using a db connectivity. * * @return boolean */ public boolean hasDb() { return (this.dbConfiguration != null); } public Class<?> getAPP() { return configuration.APP; } public Boolean isInProduction() { return configuration.PRODUCTION_MODE; } public Boolean devModeActivated() { return configuration.DEV_MODE; } public Session getORM() { return ORM; } public File getLEAVES_DIRECTORY() { return configuration.LEAVES_DIRECTORY; } public File getROOTS_DIRECTORY() { return configuration.ROOTS_DIRECTORY; } public File getCONTROLS_DIRECTORY() { return configuration.CONTROLS_DIRECTORY; } public File getBRANCHES_DIRECTORY() { return configuration.BRANCHES_DIRECTORY; } public File getBUDS_DIRECTORY() { return configuration.BUDS_DIRECTORY; } public File getFONGI_DIRECTORY() { return configuration.FONGI_DIRECTORY; } public File getENTITIES_DIRECTORY() { return configuration.ENTITIES_DIRECTORY; } public File getCONF_DIRECTORY() { return configuration.CONF_DIRECTORY; } public File getRESOURCES_DIRECTORY() { return configuration.RESOURCES_DIRECTORY; } public File getWEB_DIRECTORY() { return configuration.WEB_DIRECTORY; } public RootReader getRootReader() { return rootReader; } public LeafWriter getLeafWriter() { return leafWriter; } public BranchParser getBranchParser() { return branchParser; } public FongiParser getFongiParser() { return fongiParser; } public DBConfiguration getDbConfiguration() { return dbConfiguration; } }