diff --git a/resources/WebContent/page/ConfigPage.tmpl b/resources/WebContent/page/ConfigPage.tmpl index 182a040..aab240f 100755 --- a/resources/WebContent/page/ConfigPage.tmpl +++ b/resources/WebContent/page/ConfigPage.tmpl @@ -57,14 +57,14 @@ - + + \ No newline at end of file diff --git a/resources/WebContent/page/index.tmpl b/resources/WebContent/page/index.tmpl index d9cbc60..29d479b 100755 --- a/resources/WebContent/page/index.tmpl +++ b/resources/WebContent/page/index.tmpl @@ -108,7 +108,9 @@ diff --git a/src/wa/server/WAContext.java b/src/wa/server/WAContext.java index f1d6684..99961a9 100755 --- a/src/wa/server/WAContext.java +++ b/src/wa/server/WAContext.java @@ -37,6 +37,8 @@ import zutil.ui.Navigation; import java.io.File; import java.nio.file.NoSuchFileException; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -47,6 +49,8 @@ import java.util.logging.Logger; * Created by Ziver on 2015-04-06. */ public class WAContext { + public static final String CONFIG_DB_VERSION = "db_version"; + private static final Logger logger = LogUtil.getLogger(); private static Navigation rootNav; private static DBConnection db; @@ -93,12 +97,8 @@ public class WAContext { throw new NoSuchFileException("Deafult DB missing: "+defDbFile.getAbsolutePath()); db = new DBConnection(DBConnection.DBMS.SQLite, dbFile.getAbsolutePath()); DBConnection defaultDB = new DBConnection(DBConnection.DBMS.SQLite, defDbFile.getAbsolutePath()); - int defaultDBVersion = Integer.parseInt(defaultDB.exec( - "SELECT value FROM "+WAConstants.DB_TABLE_PREFIX+"_config WHERE key=='db_version'", - new SimpleSQLResult())); - int dbVersion = Integer.parseInt(db.exec( - "SELECT value FROM "+WAConstants.DB_TABLE_PREFIX+"_config WHERE key=='db_version'", - new SimpleSQLResult())); + int defaultDBVersion = Integer.parseInt(getConfig(defaultDB, CONFIG_DB_VERSION)); + int dbVersion = Integer.parseInt(getConfig(db, CONFIG_DB_VERSION)); logger.info("DB version: "+ dbVersion); if(defaultDBVersion > dbVersion ) { logger.info("Starting DB upgrade..."); @@ -113,6 +113,7 @@ public class WAContext { handler.addIgnoredTable("sqlite_sequence"); //sqlite internal handler.setTargetDB(db); handler.upgrade(); + setConfig(CONFIG_DB_VERSION, ""+defaultDBVersion); } // Setup fields @@ -147,4 +148,18 @@ public class WAContext { public static PluginManager getPluginManager() { return pluginManager; } + + + + public static String getConfig(DBConnection db, String key) throws SQLException { + PreparedStatement stmt = db.getPreparedStatement("SELECT value FROM "+WAConstants.DB_TABLE_PREFIX+"_config WHERE key==?"); + stmt.setObject(1, key); + return db.exec(stmt, new SimpleSQLResult()); + } + public static void setConfig(String key, String value) throws SQLException { + PreparedStatement stmt = db.getPreparedStatement("REPLACE INTO wa_config (key, value) VALUES (?, ?)"); + stmt.setObject(1, key); + stmt.setObject(2, value); + DBConnection.exec(stmt); + } } diff --git a/src/wa/server/page/ConfigPage.java b/src/wa/server/page/ConfigPage.java index c827580..2e40e9f 100755 --- a/src/wa/server/page/ConfigPage.java +++ b/src/wa/server/page/ConfigPage.java @@ -23,7 +23,7 @@ package wa.server.page; import wa.server.WAContext; -import wa.server.plugin.WAConfiguration; +import wa.server.plugin.WAConfigObject; import zutil.db.DBConnection; import zutil.io.file.FileUtil; import zutil.log.LogUtil; @@ -49,15 +49,15 @@ public class ConfigPage extends WAPage { private static final String TEMPLATE = "WebContent/page/ConfigPage.tmpl"; - private Class configClass; + private Class configClass; private String name; - public ConfigPage(Class configClass) { + public ConfigPage(Class configClass) { super(getPageName(configClass)); this.configClass = configClass; - WAConfiguration.WAConfig params = configClass.getAnnotation(WAConfiguration.WAConfig.class); + WAConfigObject.WAConfig params = configClass.getAnnotation(WAConfigObject.WAConfig.class); WAContext.getRootNav() .createSubNav(WAServicePage.NAVIGATION_NAME) .createSubNav(params.serviceName()) @@ -65,8 +65,8 @@ public class ConfigPage extends WAPage { this.name = params.name(); } - private static String getPageName(Class configClass){ - WAConfiguration.WAConfig params = configClass.getAnnotation(WAConfiguration.WAConfig.class); + private static String getPageName(Class configClass){ + WAConfigObject.WAConfig params = configClass.getAnnotation(WAConfigObject.WAConfig.class); return WAServicePage.PAGE_NAME +"/"+ params.servicePage() +"/"+ params.pageName(); } @@ -80,29 +80,29 @@ public class ConfigPage extends WAPage { Map request) throws Exception { try { DBConnection db = WAContext.getDB(); - List objList = getAllConfigs(); + List objList = WAConfigObject.getAllConfigObjs(db, configClass); // Actions if (request.containsKey("action")) { int id = -1; if (request.containsKey("pageName")) id = Integer.parseInt(request.get("pageName")); - WAConfiguration target = getConfig(id); + WAConfigObject target = WAConfigObject.getConfig(db, configClass, id); switch (request.get("action")) { case "create": - target = newConfig(); + target = configClass.newInstance(); case "modify": if (target != null) { - new Configurator(target) + new Configurator(target) .setValues(request).applyConfiguration(); target.saveConfiguration(); - saveConfig(target); + target.save(db); } break; case "delete": if (target != null) { target.deleteConfiguration(); - deleteConfig(id); + target.delete(db); } break; } @@ -121,27 +121,11 @@ public class ConfigPage extends WAPage { } - private static WAConfiguration newConfig(){ - return null; - } - private static WAConfiguration getConfig(int id){ - return null; - } - private static List getAllConfigs(){ - return null; - } - private static WAConfiguration saveConfig(WAConfiguration target){ - return null; - } - private static WAConfiguration deleteConfig(int id){ - return null; - } - public static void initialize() { - for (Iterator> it = WAContext.getPluginManager().getClassIterator(WAConfiguration.class); it.hasNext(); ){ - Class clazz = it.next(); + for (Iterator> it = WAContext.getPluginManager().getClassIterator(WAConfigObject.class); it.hasNext(); ){ + Class clazz = it.next(); WAContext.registerWaPage(new ConfigPage(clazz)); } } diff --git a/src/wa/server/plugin/WAConfigObject.java b/src/wa/server/plugin/WAConfigObject.java new file mode 100755 index 0000000..5a4ac62 --- /dev/null +++ b/src/wa/server/plugin/WAConfigObject.java @@ -0,0 +1,82 @@ +package wa.server.plugin; + + +import zutil.db.DBConnection; +import zutil.db.bean.DBBean; +import zutil.db.bean.DBBeanSQLResultHandler; +import zutil.db.handler.SimpleSQLResult; +import zutil.parser.json.JSONParser; +import zutil.parser.json.JSONWriter; +import zutil.ui.Configurator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +/** + * Created by Ziver on 2016-07-27. + */ +@DBBean.DBTable(WAConfigObject.DB_TABLE) +public abstract class WAConfigObject extends DBBean{ + protected static final String DB_TABLE = "wa_config_obj"; + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface WAConfig{ + String pageName(); + String name(); + String servicePage(); // TODO: this is horrible design! + String serviceName(); // TODO: this is horrible design! + } + + + private String className; + private String objConfig; + + + public WAConfigObject(){ + className = this.getClass().getName(); + } + + + public static WAConfigObject getConfig(DBConnection db, Class clazz, int id) throws SQLException, ClassNotFoundException { + PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM "+DB_TABLE+" WHERE id == ?" ); + stmt.setInt(1, id); + WAConfigObject obj = DBConnection.exec(stmt, DBBeanSQLResultHandler.create(clazz, db) ); + obj.configureObj(); + return obj; + } + public static List getAllConfigObjs(DBConnection db, Class clazz) throws SQLException { + List list = + (List) db.exec("SELECT * FROM "+DB_TABLE, DBBeanSQLResultHandler.createList(clazz, db) ); + for (WAConfigObject obj : list) + obj.configureObj(); + return list; + } + + + private void configureObj(){ + new Configurator(this).setValues(JSONParser.read(objConfig)).applyConfiguration(); + } + + public void save(DBConnection db) throws SQLException { + objConfig = JSONWriter.toString( + new Configurator(this).getValuesAsNode()); + super.save(db); + } + + + /** + * Configure or reconfigure service with current configuration data + */ + public abstract void saveConfiguration() throws Exception; + + /** + * Remove the configuration + */ + public abstract void deleteConfiguration() throws Exception; +} diff --git a/src/wa/server/plugin/WAConfiguration.java b/src/wa/server/plugin/WAConfiguration.java deleted file mode 100755 index e4f7ffa..0000000 --- a/src/wa/server/plugin/WAConfiguration.java +++ /dev/null @@ -1,33 +0,0 @@ -package wa.server.plugin; - - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by Ziver on 2016-07-27. - */ -public abstract class WAConfiguration{ - - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.TYPE) - public @interface WAConfig{ - String pageName(); - String name(); - String servicePage(); // TODO: this is horrible design! - String serviceName(); // TODO: this is horrible design! - } - - - /** - * Configure or reconfigure service with current configuration data - */ - public abstract void saveConfiguration() throws Exception; - - /** - * Remove the configuration - */ - public abstract void deleteConfiguration() throws Exception; -} diff --git a/src/wa/server/plugin/apache/ApacheAbstractConfig.java b/src/wa/server/plugin/apache/ApacheAbstractConfig.java index 29ed046..eda6653 100755 --- a/src/wa/server/plugin/apache/ApacheAbstractConfig.java +++ b/src/wa/server/plugin/apache/ApacheAbstractConfig.java @@ -1,15 +1,14 @@ package wa.server.plugin.apache; import wa.server.WAConstants; -import wa.server.plugin.WAConfiguration; -import zutil.db.bean.DBBean.DBTable; +import wa.server.plugin.WAConfigObject; import zutil.ui.Configurator; import java.io.IOException; import java.io.PrintStream; -public abstract class ApacheAbstractConfig extends WAConfiguration { +public abstract class ApacheAbstractConfig extends WAConfigObject { private static final String CONFIG_NAME = "vhost"; private static final String NAVIGATION_NAME = "Apache Virtual Host"; diff --git a/src/wa/server/plugin/apache/ApacheConfigProxy.java b/src/wa/server/plugin/apache/ApacheConfigProxy.java index 4c6d722..5364b3d 100755 --- a/src/wa/server/plugin/apache/ApacheConfigProxy.java +++ b/src/wa/server/plugin/apache/ApacheConfigProxy.java @@ -1,12 +1,12 @@ package wa.server.plugin.apache; -import wa.server.plugin.WAConfiguration; +import wa.server.plugin.WAConfigObject; import zutil.ui.Configurator; import java.io.PrintStream; -@WAConfiguration.WAConfig(servicePage=ApacheService.SERVICE_NAME, serviceName=ApacheService.NAVIGATION_NAME, pageName ="proxy", name="Proxy") +@WAConfigObject.WAConfig(servicePage=ApacheService.SERVICE_NAME, serviceName=ApacheService.NAVIGATION_NAME, pageName ="proxy", name="Proxy") public class ApacheConfigProxy extends ApacheAbstractConfig { @Configurator.Configurable("Proxy Target") diff --git a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java index b07a0bd..f0338f1 100755 --- a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java +++ b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java @@ -1,12 +1,12 @@ package wa.server.plugin.apache; -import wa.server.plugin.WAConfiguration; +import wa.server.plugin.WAConfigObject; import zutil.ui.Configurator; import java.io.PrintStream; -@WAConfiguration.WAConfig(servicePage=ApacheService.SERVICE_NAME, serviceName=ApacheService.NAVIGATION_NAME, pageName ="vhost", name="VirtualHost") +@WAConfigObject.WAConfig(servicePage=ApacheService.SERVICE_NAME, serviceName=ApacheService.NAVIGATION_NAME, pageName ="vhost", name="VirtualHost") public class ApacheConfigVirtualHost extends ApacheAbstractConfig { @Configurator.Configurable("Document Root") diff --git a/src/wa/server/plugin/apache/plugin.json b/src/wa/server/plugin/apache/plugin.json index 800cddc..a319238 100755 --- a/src/wa/server/plugin/apache/plugin.json +++ b/src/wa/server/plugin/apache/plugin.json @@ -4,7 +4,7 @@ "interfaces": [ {"wa.server.page.WAPage": "wa.server.plugin.apache.ApacheService"}, {"wa.server.plugin.WAServiceStatus": "wa.server.plugin.apache.ApacheStatus"}, - {"wa.server.plugin.WAConfiguration": "wa.server.plugin.apache.ApacheConfigProxy"}, - {"wa.server.plugin.WAConfiguration": "wa.server.plugin.apache.ApacheConfigVirtualHost"} + {"wa.server.plugin.WAConfigObject": "wa.server.plugin.apache.ApacheConfigProxy"}, + {"wa.server.plugin.WAConfigObject": "wa.server.plugin.apache.ApacheConfigVirtualHost"} ] } \ No newline at end of file diff --git a/webadmin.db b/webadmin.db index 88bdde7..1f6212f 100755 Binary files a/webadmin.db and b/webadmin.db differ diff --git a/webadmin_default.db b/webadmin_default.db index 88bdde7..f3cc013 100755 Binary files a/webadmin_default.db and b/webadmin_default.db differ