Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for URLProtocol. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; /* * CFAsteriskRamURLProtocolTable in-memory RAM DbIO implementation * for URLProtocol. */ public class CFAsteriskRamURLProtocolTable implements ICFAsteriskURLProtocolTable { private ICFAsteriskSchema schema; private Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> dictByPKey = new HashMap<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>(); private Map<CFInternetURLProtocolByUNameIdxKey, CFInternetURLProtocolBuff> dictByUNameIdx = new HashMap<CFInternetURLProtocolByUNameIdxKey, CFInternetURLProtocolBuff>(); private Map<CFInternetURLProtocolByIsSecureIdxKey, Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>> dictByIsSecureIdx = new HashMap<CFInternetURLProtocolByIsSecureIdxKey, Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>>(); public CFAsteriskRamURLProtocolTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createURLProtocol(CFSecurityAuthorization Authorization, CFInternetURLProtocolBuff Buff) { final String S_ProcName = "createURLProtocol"; CFInternetURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); Buff.setRequiredURLProtocolId(pkey.getRequiredURLProtocolId()); CFInternetURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(Buff.getRequiredName()); CFInternetURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); keyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "URLProtocolUNameIdx", keyUNameIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdictIsSecureIdx; if (dictByIsSecureIdx.containsKey(keyIsSecureIdx)) { subdictIsSecureIdx = dictByIsSecureIdx.get(keyIsSecureIdx); } else { subdictIsSecureIdx = new HashMap<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>(); dictByIsSecureIdx.put(keyIsSecureIdx, subdictIsSecureIdx); } subdictIsSecureIdx.put(pkey, Buff); } public CFInternetURLProtocolBuff readDerived(CFSecurityAuthorization Authorization, CFInternetURLProtocolPKey PKey) { final String S_ProcName = "CFAsteriskRamURLProtocol.readDerived"; CFInternetURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFInternetURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetURLProtocolBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetURLProtocolPKey PKey) { final String S_ProcName = "CFAsteriskRamURLProtocol.readDerived"; CFInternetURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFInternetURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetURLProtocolBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamURLProtocol.readAllDerived"; CFInternetURLProtocolBuff[] retList = new CFInternetURLProtocolBuff[dictByPKey.values().size()]; Iterator<CFInternetURLProtocolBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetURLProtocolBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFAsteriskRamURLProtocol.readDerivedByUNameIdx"; CFInternetURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(Name); CFInternetURLProtocolBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFInternetURLProtocolBuff[] readDerivedByIsSecureIdx(CFSecurityAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAsteriskRamURLProtocol.readDerivedByIsSecureIdx"; CFInternetURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); CFInternetURLProtocolBuff[] recArray; if (dictByIsSecureIdx.containsKey(key)) { Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx .get(key); recArray = new CFInternetURLProtocolBuff[subdictIsSecureIdx.size()]; Iterator<CFInternetURLProtocolBuff> iter = subdictIsSecureIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdictIsSecureIdx = new HashMap<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>(); dictByIsSecureIdx.put(key, subdictIsSecureIdx); recArray = new CFInternetURLProtocolBuff[0]; } return (recArray); } public CFInternetURLProtocolBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAsteriskRamURLProtocol.readDerivedByIdIdx() "; CFInternetURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(URLProtocolId); CFInternetURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetURLProtocolBuff readBuff(CFSecurityAuthorization Authorization, CFInternetURLProtocolPKey PKey) { final String S_ProcName = "CFAsteriskRamURLProtocol.readBuff"; CFInternetURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFInternetURLProtocolBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetURLProtocolPKey PKey) { final String S_ProcName = "lockBuff"; CFInternetURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFInternetURLProtocolBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamURLProtocol.readAllBuff"; CFInternetURLProtocolBuff buff; ArrayList<CFInternetURLProtocolBuff> filteredList = new ArrayList<CFInternetURLProtocolBuff>(); CFInternetURLProtocolBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("UPRT")) { filteredList.add(buff); } } return (filteredList.toArray(new CFInternetURLProtocolBuff[0])); } public CFInternetURLProtocolBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAsteriskRamURLProtocol.readBuffByIdIdx() "; CFInternetURLProtocolBuff buff = readDerivedByIdIdx(Authorization, URLProtocolId); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFInternetURLProtocolBuff) buff); } else { return (null); } } public CFInternetURLProtocolBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, String Name) { final String S_ProcName = "CFAsteriskRamURLProtocol.readBuffByUNameIdx() "; CFInternetURLProtocolBuff buff = readDerivedByUNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFInternetURLProtocolBuff) buff); } else { return (null); } } public CFInternetURLProtocolBuff[] readBuffByIsSecureIdx(CFSecurityAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAsteriskRamURLProtocol.readBuffByIsSecureIdx() "; CFInternetURLProtocolBuff buff; ArrayList<CFInternetURLProtocolBuff> filteredList = new ArrayList<CFInternetURLProtocolBuff>(); CFInternetURLProtocolBuff[] buffList = readDerivedByIsSecureIdx(Authorization, IsSecure); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("UPRT")) { filteredList.add((CFInternetURLProtocolBuff) buff); } } return (filteredList.toArray(new CFInternetURLProtocolBuff[0])); } public void updateURLProtocol(CFSecurityAuthorization Authorization, CFInternetURLProtocolBuff Buff) { CFInternetURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFInternetURLProtocolBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateURLProtocol", "Existing record not found", "URLProtocol", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateURLProtocol", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFInternetURLProtocolByUNameIdxKey existingKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFInternetURLProtocolByUNameIdxKey newKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); CFInternetURLProtocolByIsSecureIdxKey existingKeyIsSecureIdx = schema.getFactoryURLProtocol() .newIsSecureIdxKey(); existingKeyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); CFInternetURLProtocolByIsSecureIdxKey newKeyIsSecureIdx = schema.getFactoryURLProtocol() .newIsSecureIdxKey(); newKeyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateURLProtocol", "URLProtocolUNameIdx", newKeyUNameIdx); } } // Validate foreign keys // Update is valid Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); subdict = dictByIsSecureIdx.get(existingKeyIsSecureIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByIsSecureIdx.containsKey(newKeyIsSecureIdx)) { subdict = dictByIsSecureIdx.get(newKeyIsSecureIdx); } else { subdict = new HashMap<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff>(); dictByIsSecureIdx.put(newKeyIsSecureIdx, subdict); } subdict.put(pkey, Buff); } public void deleteURLProtocol(CFSecurityAuthorization Authorization, CFInternetURLProtocolBuff Buff) { final String S_ProcName = "CFAsteriskRamURLProtocolTable.deleteURLProtocol() "; CFInternetURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFInternetURLProtocolBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteURLProtocol", pkey); } CFInternetURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(existing.getRequiredName()); CFInternetURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); keyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); // Validate reverse foreign keys // Delete is valid Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdict; dictByPKey.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); subdict = dictByIsSecureIdx.get(keyIsSecureIdx); subdict.remove(pkey); } public void deleteURLProtocolByIdIdx(CFSecurityAuthorization Authorization, short argURLProtocolId) { CFInternetURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(argURLProtocolId); deleteURLProtocolByIdIdx(Authorization, key); } public void deleteURLProtocolByIdIdx(CFSecurityAuthorization Authorization, CFInternetURLProtocolPKey argKey) { CFInternetURLProtocolBuff cur; LinkedList<CFInternetURLProtocolBuff> matchSet = new LinkedList<CFInternetURLProtocolBuff>(); Iterator<CFInternetURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByUNameIdx(CFSecurityAuthorization Authorization, String argName) { CFInternetURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(argName); deleteURLProtocolByUNameIdx(Authorization, key); } public void deleteURLProtocolByUNameIdx(CFSecurityAuthorization Authorization, CFInternetURLProtocolByUNameIdxKey argKey) { CFInternetURLProtocolBuff cur; LinkedList<CFInternetURLProtocolBuff> matchSet = new LinkedList<CFInternetURLProtocolBuff>(); Iterator<CFInternetURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByIsSecureIdx(CFSecurityAuthorization Authorization, boolean argIsSecure) { CFInternetURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(argIsSecure); deleteURLProtocolByIsSecureIdx(Authorization, key); } public void deleteURLProtocolByIsSecureIdx(CFSecurityAuthorization Authorization, CFInternetURLProtocolByIsSecureIdxKey argKey) { CFInternetURLProtocolBuff cur; LinkedList<CFInternetURLProtocolBuff> matchSet = new LinkedList<CFInternetURLProtocolBuff>(); Iterator<CFInternetURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public CFSecurityCursor openURLProtocolCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamURLProtocolCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openURLProtocolCursorByIsSecureIdx(CFSecurityAuthorization Authorization, boolean IsSecure) { CFSecurityCursor cursor; CFInternetURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); if (dictByIsSecureIdx.containsKey(key)) { Map<CFInternetURLProtocolPKey, CFInternetURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx .get(key); cursor = new CFAsteriskRamURLProtocolCursor(Authorization, schema, subdictIsSecureIdx.values()); } else { cursor = new CFAsteriskRamURLProtocolCursor(Authorization, schema, new ArrayList<CFInternetURLProtocolBuff>()); } return (cursor); } public void closeURLProtocolCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetURLProtocolBuff nextURLProtocolCursor(CFSecurityCursor Cursor) { CFAsteriskRamURLProtocolCursor cursor = (CFAsteriskRamURLProtocolCursor) Cursor; CFInternetURLProtocolBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetURLProtocolBuff prevURLProtocolCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFInternetURLProtocolBuff firstURLProtocolCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetURLProtocolBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFInternetURLProtocolBuff lastURLProtocolCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastURLProtocolCursor"); } public CFInternetURLProtocolBuff nthURLProtocolCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }