public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { try { // Common XML Attributes String attrId = null; // ServiceType Attributes String attrDescription = null; // ServiceType References // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_ProcName = "startElement"; final String S_LocalName = "LocalName"; assert qName.equals("ServiceType"); CFInternetSaxLoader saxLoader = (CFInternetSaxLoader) getParser(); if (saxLoader == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } ICFInternetSchemaObj schemaObj = saxLoader.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } // Instantiate an edit buffer for the parsed information ICFInternetServiceTypeEditObj editBuff = (ICFInternetServiceTypeEditObj) schemaObj.getServiceTypeTableObj().newInstance().beginEdit(); // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory() .newUniqueIndexViolationException( getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("Description")) { if (attrDescription != null) { throw CFLib.getDefaultExceptionFactory() .newUniqueIndexViolationException( getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDescription = attrs.getValue(idxAttr); } else if (attrLocalName.equals("schemaLocation")) { // ignored } else { throw CFLib.getDefaultExceptionFactory() .newUnrecognizedAttributeException( getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values if (attrDescription == null) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException(getClass(), S_ProcName, 0, "Description"); } // Save named attributes to context CFLibXmlCoreContext curContext = getParser().getCurContext(); curContext.putNamedValue("Id", attrId); curContext.putNamedValue("Description", attrDescription); // Convert string attributes to native Java types // and apply the converted attributes to the editBuff. Integer natId; if ((attrId != null) && (attrId.length() > 0)) { natId = new Integer(Integer.parseInt(attrId)); } else { natId = null; } String natDescription = attrDescription; editBuff.setRequiredDescription(natDescription); // Get the scope/container object CFLibXmlCoreContext parentContext = curContext.getPrevContext(); Object scopeObj; if (parentContext != null) { scopeObj = parentContext.getNamedValue("Object"); } else { scopeObj = null; } CFInternetSaxLoader.LoaderBehaviourEnum loaderBehaviour = saxLoader.getServiceTypeLoaderBehaviour(); ICFInternetServiceTypeEditObj editServiceType = null; ICFInternetServiceTypeObj origServiceType = (ICFInternetServiceTypeObj) schemaObj .getServiceTypeTableObj() .readServiceTypeByUDescrIdx(editBuff.getRequiredDescription()); if (origServiceType == null) { editServiceType = editBuff; } else { switch (loaderBehaviour) { case Insert: break; case Update: editServiceType = (ICFInternetServiceTypeEditObj) origServiceType.beginEdit(); editServiceType.setRequiredDescription(editBuff.getRequiredDescription()); break; case Replace: editServiceType = (ICFInternetServiceTypeEditObj) origServiceType.beginEdit(); editServiceType.delete(); editServiceType.endEdit(); origServiceType = null; editServiceType = editBuff; break; } } if (editServiceType != null) { if (origServiceType != null) { editServiceType.update(); } else { origServiceType = (ICFInternetServiceTypeObj) editServiceType.create(); } editServiceType.endEdit(); } curContext.putNamedValue("Object", origServiceType); } catch (RuntimeException e) { throw new RuntimeException( "Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } catch (Error e) { throw new Error( "Near " + getParser().getLocationInfo() + ": Caught and rethrew " + e.getClass().getName() + " - " + e.getMessage(), e); } }
public static void main(String args[]) { final String S_ProcName = "CFInternetSaxMySqlLoaderCLI.main() "; initConsoleLog(); int numArgs = args.length; if (numArgs >= 2) { 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; } CFInternetConfigurationFile cFInternetConfig = new CFInternetConfigurationFile(); String cFInternetConfigFileName = homeDir.getPath() + File.separator + ".cfinternetmysqlrc"; cFInternetConfig.setFileName(cFInternetConfigFileName); File cFInternetConfigFile = new File(cFInternetConfigFileName); if (!cFInternetConfigFile.exists()) { cFInternetConfig.setDbServer("127.0.0.1"); cFInternetConfig.setDbPort(3306); cFInternetConfig.setDbDatabase("CFINet24"); cFInternetConfig.setDbUserName("root"); cFInternetConfig.setDbPassword("edit-me-please"); cFInternetConfig.save(); log.message( S_ProcName + "INFO: Created configuration file " + cFInternetConfigFileName + ", please edit configuration and restart."); return; } if (!cFInternetConfigFile.isFile()) { log.message( S_ProcName + "ERROR: Proposed configuration file " + cFInternetConfigFileName + " is not a file."); return; } if (!cFInternetConfigFile.canRead()) { log.message( S_ProcName + "ERROR: Permission denied attempting to read configuration file " + cFInternetConfigFileName); return; } cFInternetConfig.load(); boolean fastExit = false; CFInternetClientConfigurationFile cFDbTestClientConfig = new CFInternetClientConfigurationFile(); String cFDbTestClientConfigFileName = homeDir.getPath() + File.separator + ".cfdbtestclientrc"; cFDbTestClientConfig.setFileName(cFDbTestClientConfigFileName); File cFDbTestClientConfigFile = new File(cFDbTestClientConfigFileName); if (!cFDbTestClientConfigFile.exists()) { String cFDbTestKeyStoreFileName = homeDir.getPath() + File.separator + ".msscfjceks"; cFDbTestClientConfig.setKeyStore(cFDbTestKeyStoreFileName); 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(); cFDbTestClientConfig.setDeviceName(deviceName); cFDbTestClientConfig.save(); log.message( S_ProcName + "INFO: Created CFInternet client configuration file " + cFDbTestClientConfigFileName); fastExit = true; } if (!cFDbTestClientConfigFile.isFile()) { log.message( S_ProcName + "ERROR: Proposed client configuration file " + cFDbTestClientConfigFileName + " is not a file."); fastExit = true; } if (!cFDbTestClientConfigFile.canRead()) { log.message( S_ProcName + "ERROR: Permission denied attempting to read client configuration file " + cFDbTestClientConfigFileName); fastExit = true; } cFDbTestClientConfig.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); ICFInternetSchema cFInternetSchema = new CFInternetMySqlSchema(); cFInternetSchema.setConfigurationFile(cFInternetConfig); ICFInternetSchemaObj cFInternetSchemaObj = new CFInternetSchemaObj(); cFInternetSchemaObj.setBackingStore(cFInternetSchema); CFInternetSaxLoaderCLI cli = new CFInternetSaxMySqlLoaderCLI(); CFInternetSaxLoader loader = cli.getSaxLoader(); loader.setSchemaObj(cFInternetSchemaObj); cFInternetSchema.connect(); String url = args[1]; if (numArgs >= 5) { cli.setClusterName(args[2]); cli.setTenantName(args[3]); cli.setSecUserName(args[4]); } else { cli.setClusterName("default"); cli.setTenantName("system"); cli.setSecUserName("system"); } loader.setUseCluster(cli.getClusterObj()); loader.setUseTenant(cli.getTenantObj()); try { cFInternetSchema.beginTransaction(); cFInternetSchemaObj.setSecCluster(cli.getClusterObj()); cFInternetSchemaObj.setSecTenant(cli.getTenantObj()); cFInternetSchemaObj.setSecUser(cli.getSecUserObj()); cFInternetSchemaObj.setSecSession(cli.getSecSessionObj()); CFSecurityAuthorization auth = new CFSecurityAuthorization(); auth.setSecCluster(cFInternetSchemaObj.getSecCluster()); auth.setSecTenant(cFInternetSchemaObj.getSecTenant()); auth.setSecSession(cFInternetSchemaObj.getSecSession()); cFInternetSchemaObj.setAuthorization(auth); applyLoaderOptions(loader, args[0]); if (numArgs >= 5) { cli.evaluateRemainingArgs(args, 5); } else { cli.evaluateRemainingArgs(args, 2); } loader.parseFile(url); cFInternetSchema.commit(); cFInternetSchema.disconnect(true); } catch (Exception e) { log.message( S_ProcName + "EXCEPTION: Could not parse XML file \"" + url + "\": " + e.getMessage()); e.printStackTrace(System.out); } catch (Error e) { log.message( S_ProcName + "ERROR: Could not parse XML file \"" + url + "\": " + e.getMessage()); e.printStackTrace(System.out); } finally { if (cFInternetSchema.isConnected()) { cFInternetSchema.rollback(); cFInternetSchema.disconnect(false); } } } else { log.message( S_ProcName + "ERROR: Expected at least two argument specifying the loader options and the name of the XML file to parse. The first argument may be empty."); } }