Java tutorial
// Description: Java 8 Swing Client-Server GUI persisting via HTTP Web Client for CFAsterisk. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * 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 3 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, see <http://www.gnu.org/licenses/>. * */ package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskSwingHttpCLI; import org.apache.log4j.*; import java.io.File; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*; import javax.swing.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgClient.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskSwing.*; public class CFAsteriskSwingHttpCLI { private static ICFLibMessageLog log = new CFLibMessageLogWrapper(); protected ICFAsteriskSchemaObj schema = null; protected ICFAsteriskSwingSchema swingSchema = null; protected JFrame desktop = null; protected CFAsteriskXMsgClientHttpSchema httpClientSchema = null; // Constructors public CFAsteriskSwingHttpCLI() { } // Accessors // CFAsteriskXMsgClientHttpSchema accessors are needed to complete the wiring of the direct request invoker instance // that has been bound by the main() to a PostgreSQL persistance implementation. public CFAsteriskXMsgClientHttpSchema getXMsgClientHttpSchema() { return (httpClientSchema); } public void setXMsgClientHttpSchema(CFAsteriskXMsgClientHttpSchema invoker) { httpClientSchema = invoker; } public ICFAsteriskSchemaObj getSchema() { return (schema); } public void setSchema(ICFAsteriskSchemaObj argSchema) { schema = argSchema; } public ICFAsteriskSwingSchema getSwingSchema() { if (swingSchema == null) { swingSchema = new CFAsteriskSwingSchema(); } return (swingSchema); } public void setSwingSchema(ICFAsteriskSwingSchema argSchema) { if (argSchema == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), "setSwingSchema", 1, "argSchema"); } swingSchema = argSchema; } public JFrame getDesktop() { if (desktop == null) { ICFAsteriskSwingSchema swing = getSwingSchema(); desktop = swing.newMainJFrame(); } return (desktop); } // main() entry point public static void main(String args[]) { final String S_ProcName = "CFAsteriskSwingHttpCLI.main() "; initConsoleLog(); boolean fastExit = false; String homeDirName = System.getProperty("HOME"); if (homeDirName == null) { homeDirName = System.getProperty("user.home"); if (homeDirName == null) { log.message(S_ProcName + "ERROR: Home directory not set"); return; } } File homeDir = new File(homeDirName); if (!homeDir.exists()) { log.message(S_ProcName + "ERROR: Home directory \"" + homeDirName + "\" does not exist"); return; } if (!homeDir.isDirectory()) { log.message(S_ProcName + "ERROR: Home directory \"" + homeDirName + "\" is not a directory"); return; } CFAsteriskClientConfigurationFile cFAsteriskClientConfig = new CFAsteriskClientConfigurationFile(); String cFAsteriskClientConfigFileName = homeDir.getPath() + File.separator + ".cfasteriskclientrc"; cFAsteriskClientConfig.setFileName(cFAsteriskClientConfigFileName); File cFAsteriskClientConfigFile = new File(cFAsteriskClientConfigFileName); if (!cFAsteriskClientConfigFile.exists()) { String cFAsteriskKeyStoreFileName = homeDir.getPath() + File.separator + ".msscfjceks"; cFAsteriskClientConfig.setKeyStore(cFAsteriskKeyStoreFileName); InetAddress localHost; try { localHost = InetAddress.getLocalHost(); } catch (UnknownHostException e) { localHost = null; } if (localHost == null) { log.message(S_ProcName + "ERROR: LocalHost is null"); return; } String hostName = localHost.getHostName(); if ((hostName == null) || (hostName.length() <= 0)) { log.message("ERROR: LocalHost.HostName is null or empty"); return; } String userName = System.getProperty("user.name"); if ((userName == null) || (userName.length() <= 0)) { log.message("ERROR: user.name is null or empty"); return; } String deviceName = hostName.replaceAll("[^\\w]", "_").toLowerCase() + "-" + userName.replaceAll("[^\\w]", "_").toLowerCase(); cFAsteriskClientConfig.setDeviceName(deviceName); cFAsteriskClientConfig.save(); log.message(S_ProcName + "INFO: Created CFAsterisk client configuration file " + cFAsteriskClientConfigFileName); fastExit = true; } if (!cFAsteriskClientConfigFile.isFile()) { log.message(S_ProcName + "ERROR: Proposed client configuration file " + cFAsteriskClientConfigFileName + " is not a file."); fastExit = true; } if (!cFAsteriskClientConfigFile.canRead()) { log.message(S_ProcName + "ERROR: Permission denied attempting to read client configuration file " + cFAsteriskClientConfigFileName); fastExit = true; } cFAsteriskClientConfig.load(); if (fastExit) { return; } // Configure logging Properties sysProps = System.getProperties(); sysProps.setProperty("log4j.rootCategory", "WARN"); sysProps.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger"); Logger httpLogger = Logger.getLogger("org.apache.http"); httpLogger.setLevel(Level.WARN); // The Invoker and it's implementation CFAsteriskXMsgClientHttpSchema invoker = new CFAsteriskXMsgClientHttpSchema(); // And now for the client side cache implementation that invokes it ICFAsteriskSchemaObj clientSchemaObj = new CFAsteriskSchemaObj() { public void logout() { CFAsteriskXMsgClientHttpSchema invoker = (CFAsteriskXMsgClientHttpSchema) getBackingStore(); try { invoker.logout(getAuthorization()); } catch (RuntimeException e) { } setAuthorization(null); } }; clientSchemaObj.setBackingStore(invoker); // And stitch the response handler to reference our client instance invoker.setResponseHandlerSchemaObj(clientSchemaObj); // And now we can stitch together the CLI to the SAX loader code CFAsteriskSwingHttpCLI cli = new CFAsteriskSwingHttpCLI(); cli.setXMsgClientHttpSchema(invoker); cli.setSchema(clientSchemaObj); ICFAsteriskSwingSchema swing = cli.getSwingSchema(); swing.setClientConfigurationFile(cFAsteriskClientConfig); swing.setSchema(clientSchemaObj); swing.setClusterName("system"); swing.setTenantName("system"); swing.setSecUserName("system"); JFrame jframe = cli.getDesktop(); jframe.setVisible(true); jframe.toFront(); } // Initialize the console log protected static void initConsoleLog() { Layout layout = new PatternLayout("%d{ISO8601}" // Start with a timestamp + " %-5p" // Include the severity + " %C.%M" // pkg.class.method() + " %F[%L]" // File[lineNumber] + ": %m\n"); // Message text BasicConfigurator.configure(new ConsoleAppender(layout, "System.out")); } }