From: Martin S. <sch...@us...> - 2006-02-23 22:18:32
|
Update of /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/cache In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16729/src/net/sf/magicmap/server/cache Modified Files: PositionCache.java Log Message: some cleanup Index: PositionCache.java =================================================================== RCS file: /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/cache/PositionCache.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** PositionCache.java 7 Feb 2006 16:05:37 -0000 1.10 --- PositionCache.java 23 Feb 2006 22:18:26 -0000 1.11 *************** *** 8,16 **** import java.util.Date; import java.util.Enumeration; - import java.util.HashMap; - import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; - import java.util.SortedMap; import java.util.TreeMap; --- 8,13 ---- *************** *** 19,26 **** import javax.jdo.Query; import javax.jdo.Transaction; - import javax.servlet.ServletContextEvent; - import javax.servlet.ServletContextListener; - - import com.sun.org.apache.xml.internal.utils.SuballocatedByteVector; import net.sf.magicmap.db.AccessPoint; --- 16,19 ---- *************** *** 29,33 **** import net.sf.magicmap.db.Position; import net.sf.magicmap.db.ScanResult; - import net.sf.magicmap.db.Session; import net.sf.magicmap.server.dto.AccessPointDTO; import net.sf.magicmap.server.dto.ClientDTO; --- 22,25 ---- *************** *** 35,51 **** import net.sf.magicmap.server.dto.SignalCharacterDTO; import net.sf.magicmap.server.dto.SimpleScanResultDTO; - import net.sf.magicmap.server.dto.StringReplacementDTO; import net.sf.magicmap.server.exception.MapException; - import net.sf.magicmap.server.exception.SessionException; - import net.sf.magicmap.server.facade.PositionFacade; import net.sf.magicmap.server.utils.JDOUtil; public class PositionCache extends Hashtable<String, PositionCacheEntry> { // Sigleton Hashtable ! private static PositionCache single_positionCache = null; ! public static PositionCache getPositionCache(){ ! if (single_positionCache == null) single_positionCache = new PositionCache(); return single_positionCache; } --- 27,43 ---- import net.sf.magicmap.server.dto.SignalCharacterDTO; import net.sf.magicmap.server.dto.SimpleScanResultDTO; import net.sf.magicmap.server.exception.MapException; import net.sf.magicmap.server.utils.JDOUtil; public class PositionCache extends Hashtable<String, PositionCacheEntry> { + /** + * + */ + private static final long serialVersionUID = 1L; // Sigleton Hashtable ! private static final PositionCache single_positionCache = new PositionCache(); ! public static PositionCache getPositionCache() { return single_positionCache; } *************** *** 55,60 **** } ! ! private Map checkMap(String mapName, PersistenceManager pm) throws MapException{ Map result = null; Collection results; --- 47,51 ---- } ! private Map checkMap(String mapName, PersistenceManager pm) throws MapException { Map result = null; Collection results; *************** *** 63,69 **** query.declareParameters("java.lang.String myName"); results = (Collection) query.execute(mapName); ! if (results != null && !results.isEmpty()){ result = (Map) results.iterator().next(); ! } else{ pm.currentTransaction().rollback(); throw new MapException("Karte mit dem namen:" + mapName + " existiert nicht"); --- 54,60 ---- query.declareParameters("java.lang.String myName"); results = (Collection) query.execute(mapName); ! if (results != null && !results.isEmpty()) { result = (Map) results.iterator().next(); ! } else { pm.currentTransaction().rollback(); throw new MapException("Karte mit dem namen:" + mapName + " existiert nicht"); *************** *** 72,76 **** } ! private Position checkPosition(String positionName, Map map, PersistenceManager pm){ Position result = null; Collection results; --- 63,67 ---- } ! private Position checkPosition(String positionName, Map map, PersistenceManager pm) { Position result = null; Collection results; *************** *** 80,84 **** query.declareImports("import java.lang.String; import net.sf.magicmap.db.Map;"); results = (Collection) query.execute(positionName, map); ! if (results != null && !results.isEmpty()){ result = (Position) results.iterator().next(); } --- 71,75 ---- query.declareImports("import java.lang.String; import net.sf.magicmap.db.Map;"); results = (Collection) query.execute(positionName, map); ! if (results != null && !results.isEmpty()) { result = (Position) results.iterator().next(); } *************** *** 87,92 **** } ! private Client checkClient(String mac, PersistenceManager pm) throws MapException{ ! if (mac == null || "".equals(mac)){ pm.currentTransaction().rollback(); throw new MapException("Client mac darf nicht leer sein"); --- 78,83 ---- } ! private Client checkClient(String mac, PersistenceManager pm) throws MapException { ! if (mac == null || "".equals(mac)) { pm.currentTransaction().rollback(); throw new MapException("Client mac darf nicht leer sein"); *************** *** 98,104 **** query.declareParameters("java.lang.String myMac"); results = (Collection) query.execute(mac); ! if (results != null && !results.isEmpty()){ result = (Client) results.iterator().next(); ! } else{ pm.currentTransaction().rollback(); throw new MapException("Client mit der mac-adresse:" + mac + " existiert nicht"); --- 89,95 ---- query.declareParameters("java.lang.String myMac"); results = (Collection) query.execute(mac); ! if (results != null && !results.isEmpty()) { result = (Client) results.iterator().next(); ! } else { pm.currentTransaction().rollback(); throw new MapException("Client mit der mac-adresse:" + mac + " existiert nicht"); *************** *** 108,113 **** } ! private AccessPoint findOrCreateAccessPoint(String macAP, PersistenceManager pm) throws MapException{ ! try{ AccessPoint result = null; Collection results; --- 99,104 ---- } ! private AccessPoint findOrCreateAccessPoint(String macAP, PersistenceManager pm) throws MapException { ! try { AccessPoint result = null; Collection results; *************** *** 117,128 **** query.declareImports("import java.lang.String;"); results = (Collection) query.execute(macAP); ! if (results != null && !results.isEmpty()){ result = (AccessPoint) results.iterator().next(); ! } else{ result = new AccessPoint(macAP); pm.makePersistent(result); } return result; ! } catch (Exception e){ pm.currentTransaction().rollback(); throw new MapException("", e); --- 108,119 ---- query.declareImports("import java.lang.String;"); results = (Collection) query.execute(macAP); ! if (results != null && !results.isEmpty()) { result = (AccessPoint) results.iterator().next(); ! } else { result = new AccessPoint(macAP); pm.makePersistent(result); } return result; ! } catch (Exception e) { pm.currentTransaction().rollback(); throw new MapException("", e); *************** *** 131,143 **** private void setNewSignalCharacter(PositionDTO dto, PersistenceManager pm, Client client, Position pos) ! throws MapException{ // alle Scanresults dieser Position löschen pm.deletePersistentAll(pos.getScanResults()); SimpleScanResultDTO[] dtos = dto.getCharacter().getSimpleScanResults(); ! if (dtos != null){ ! for (int i = 0; i < dtos.length; i++){ AccessPoint ap = findOrCreateAccessPoint(dtos[i].getMacAP(), pm); //@TODO think about? ap.setSsid(dtos[i].getSsid()); ! ScanResult sr = new ScanResult(client, ap, dtos[i].getSignalLevel(), dtos[i].getNoise(), new Timestamp(dto.getLastModified())); pm.makePersistent(sr); sr.setPosition(pos); --- 122,135 ---- private void setNewSignalCharacter(PositionDTO dto, PersistenceManager pm, Client client, Position pos) ! throws MapException { // alle Scanresults dieser Position löschen pm.deletePersistentAll(pos.getScanResults()); SimpleScanResultDTO[] dtos = dto.getCharacter().getSimpleScanResults(); ! if (dtos != null) { ! for (int i = 0; i < dtos.length; i++) { AccessPoint ap = findOrCreateAccessPoint(dtos[i].getMacAP(), pm); //@TODO think about? ap.setSsid(dtos[i].getSsid()); ! ScanResult sr = new ScanResult(client, ap, dtos[i].getSignalLevel(), dtos[i].getNoise(), new Timestamp(dto ! .getLastModified())); pm.makePersistent(sr); sr.setPosition(pos); *************** *** 146,154 **** } ! private void writePositionToDB(String mapName, PositionDTO position){ System.out.println("Finalize:writePositionToDB()"); PersistenceManager pm = null; Transaction tx = null; ! try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); --- 138,146 ---- } ! private void writePositionToDB(String mapName, PositionDTO position) { System.out.println("Finalize:writePositionToDB()"); PersistenceManager pm = null; Transaction tx = null; ! try { pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); *************** *** 156,160 **** Map map = checkMap(mapName, pm); Position pos = checkPosition(position.getName(), map, pm); ! if (pos == null){ pos = new Position(map, position.getPosX(), position.getPosY(), position.getName()); pm.makePersistent(pos); --- 148,152 ---- Map map = checkMap(mapName, pm); Position pos = checkPosition(position.getName(), map, pm); ! if (pos == null) { pos = new Position(map, position.getPosX(), position.getPosY(), position.getName()); pm.makePersistent(pos); *************** *** 165,184 **** pos.setFixed(position.isFixed()); ! if (position.getClient() != null){ Client client = checkClient(position.getClient().getMac(), pm); ! if (client != null){ pos.setClient(client); setNewSignalCharacter(position, pm, client, pos); } } ! if (position.getAccessPoint() != null){ AccessPoint ap = findOrCreateAccessPoint(position.getAccessPoint().getMac(), pm); pos.setAccessPoint(ap); } ! JDOUtil.commit(tx);; ! } catch (Exception e){ e.printStackTrace(); ! } finally{ ! if (tx.isActive()){ tx.rollback(); } --- 157,176 ---- pos.setFixed(position.isFixed()); ! if (position.getClient() != null) { Client client = checkClient(position.getClient().getMac(), pm); ! if (client != null) { pos.setClient(client); setNewSignalCharacter(position, pm, client, pos); } } ! if (position.getAccessPoint() != null) { AccessPoint ap = findOrCreateAccessPoint(position.getAccessPoint().getMac(), pm); pos.setAccessPoint(ap); } ! JDOUtil.commit(tx); ! } catch (Exception e) { e.printStackTrace(); ! } finally { ! if (tx.isActive()) { tx.rollback(); } *************** *** 188,211 **** } ! /** ! * @param sessionId ! * @param mapName ! * @param positionX ! * @param positionY ! * @param character ! * @param positionName ! * @param pm ! * @param tx ! * @throws SessionException ! * @throws MapException ! */ ! private void createOrUpdatePositionInternal(long sessionId, String mapName, int positionX, int positionY, ! SignalCharacterDTO character, String positionName, boolean fixed, PersistenceManager pm, Transaction tx) ! throws SessionException, MapException{} ! ! private void getDataFromDB(){ PersistenceManager pm = null; Transaction tx = null; ! try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); --- 180,187 ---- } ! private void getDataFromDB() { PersistenceManager pm = null; Transaction tx = null; ! try { pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); *************** *** 214,224 **** Query query = pm.newQuery(e); Collection mapResults = (Collection) query.execute(); ! if (mapResults != null){ Iterator it1 = mapResults.iterator(); ! while (it1.hasNext()){ Map map = (Map) it1.next(); addMapHashtable(map); // Positionsdaten für map aus der Datenbank holen ! try{ Extent extent = pm.getExtent(Position.class, true); Query posQuery = pm.newQuery(extent, "map == myMap && deleted == 0"); --- 190,200 ---- Query query = pm.newQuery(e); Collection mapResults = (Collection) query.execute(); ! if (mapResults != null) { Iterator it1 = mapResults.iterator(); ! while (it1.hasNext()) { Map map = (Map) it1.next(); addMapHashtable(map); // Positionsdaten für map aus der Datenbank holen ! try { Extent extent = pm.getExtent(Position.class, true); Query posQuery = pm.newQuery(extent, "map == myMap && deleted == 0"); *************** *** 226,236 **** posQuery.declareImports("import net.sf.magicmap.db.Map"); Collection PositionResults = (Collection) posQuery.execute(map); ! if (PositionResults != null){ Iterator it2 = PositionResults.iterator(); ! while (it2.hasNext()){ Position pos = (Position) it2.next(); PositionDTO dto = new PositionDTO(); AccessPoint ap = pos.getAccessPoint(); ! if (ap != null){ AccessPointDTO apDTO = new AccessPointDTO(); apDTO.setMac(ap.getMac()); --- 202,212 ---- posQuery.declareImports("import net.sf.magicmap.db.Map"); Collection PositionResults = (Collection) posQuery.execute(map); ! if (PositionResults != null) { Iterator it2 = PositionResults.iterator(); ! while (it2.hasNext()) { Position pos = (Position) it2.next(); PositionDTO dto = new PositionDTO(); AccessPoint ap = pos.getAccessPoint(); ! if (ap != null) { AccessPointDTO apDTO = new AccessPointDTO(); apDTO.setMac(ap.getMac()); *************** *** 240,244 **** } Client client = pos.getClient(); ! if (client != null){ ClientDTO clientDTO = new ClientDTO(); clientDTO.setMac(client.getMac()); --- 216,220 ---- } Client client = pos.getClient(); ! if (client != null) { ClientDTO clientDTO = new ClientDTO(); clientDTO.setMac(client.getMac()); *************** *** 257,267 **** // Scanresults holen Collection scanResults = pos.getScanResults(); ! if (scanResults != null){ Iterator scanIt = scanResults.iterator(); ! while (scanIt.hasNext()){ ScanResult sr = (ScanResult) scanIt.next(); Date lastSeen = null; ! if (sr.getScantime() != null){ lastSeen = new Date(sr.getScantime().getTime()); } --- 233,243 ---- // Scanresults holen Collection scanResults = pos.getScanResults(); ! if (scanResults != null) { Iterator scanIt = scanResults.iterator(); ! while (scanIt.hasNext()) { ScanResult sr = (ScanResult) scanIt.next(); Date lastSeen = null; ! if (sr.getScantime() != null) { lastSeen = new Date(sr.getScantime().getTime()); } *************** *** 277,283 **** ArrayList list; ! if (dto.getCharacter().getSimpleScanResults() == null){ list = new ArrayList(); ! } else{ list = new ArrayList(Arrays.asList(dto.getCharacter().getSimpleScanResults())); } --- 253,259 ---- ArrayList list; ! if (dto.getCharacter().getSimpleScanResults() == null) { list = new ArrayList(); ! } else { list = new ArrayList(Arrays.asList(dto.getCharacter().getSimpleScanResults())); } *************** *** 291,295 **** } } ! } catch (Exception ex){ ex.printStackTrace(); } --- 267,271 ---- } } ! } catch (Exception ex) { ex.printStackTrace(); } *************** *** 297,327 **** } JDOUtil.commit(tx); ! } finally{ JDOUtil.closePM(pm); } } ! private String getMapString(Map map){ return map.getName() + "#" + map.getImageURL() + "#" + map.getImageHeight().toString() + "#" + map.getImageWidth().toString(); } ! private void addMapHashtable(Map map){ this.put(getMapString(map), new PositionCacheEntry()); } ! private boolean existsMapHashtable(Map map){ return this.containsKey(getMapString(map)); } ! private PositionCacheEntry getMapHashtable(Map map){ return this.get(getMapString(map)); } ! public void createOrUpdatePosition(Map map, PositionDTO position){ System.out.println("enter createOrUpdatePosition()"); if (!existsMapHashtable(map)) addMapHashtable(map); ! if (getMapHashtable(map).existsPosition(position.getName())){ getMapHashtable(map).deletePosition(position.getName()); } --- 273,303 ---- } JDOUtil.commit(tx); ! } finally { JDOUtil.closePM(pm); } } ! private String getMapString(Map map) { return map.getName() + "#" + map.getImageURL() + "#" + map.getImageHeight().toString() + "#" + map.getImageWidth().toString(); } ! private void addMapHashtable(Map map) { this.put(getMapString(map), new PositionCacheEntry()); } ! private boolean existsMapHashtable(Map map) { return this.containsKey(getMapString(map)); } ! private PositionCacheEntry getMapHashtable(Map map) { return this.get(getMapString(map)); } ! public void createOrUpdatePosition(Map map, PositionDTO position) { System.out.println("enter createOrUpdatePosition()"); if (!existsMapHashtable(map)) addMapHashtable(map); ! if (getMapHashtable(map).existsPosition(position.getName())) { getMapHashtable(map).deletePosition(position.getName()); } *************** *** 329,338 **** } ! public PositionDTO[] getPositionsForMapSince(Map map, Long time){ if (!existsMapHashtable(map)) return new PositionDTO[0]; return getMapHashtable(map).getPositionsSince(time); } ! public PositionDTO getPositionForClientOnMap(Map map, String name){ if (!existsMapHashtable(map)) return null; return getMapHashtable(map).getPosition(name); --- 305,314 ---- } ! public PositionDTO[] getPositionsForMapSince(Map map, Long time) { if (!existsMapHashtable(map)) return new PositionDTO[0]; return getMapHashtable(map).getPositionsSince(time); } ! public PositionDTO getPositionForClientOnMap(Map map, String name) { if (!existsMapHashtable(map)) return null; return getMapHashtable(map).getPosition(name); *************** *** 340,376 **** } ! public void movePosition(Map map, Long id, int x, int y, boolean fixed){ ! if (existsMapHashtable(map)){ getMapHashtable(map).movePosition(id, x, y, fixed); } } ! public void removePosition(Map map, Long id){ ! if (existsMapHashtable(map)){ getMapHashtable(map).removePosition(id); } } ! public void removePosition(Map map, String name){ ! if (existsMapHashtable(map)){ getMapHashtable(map).removePosition(name); } } ! public void writeCacheDataToDB(){ System.out.println("Begin Schreiben in Datenbank"); ! Enumeration<String> mapNames = this.keys(); ! while (mapNames.hasMoreElements()){ ! String currentMap = mapNames.nextElement(); PositionCacheEntry posCache = this.get(currentMap); ! currentMap = currentMap.substring(0,currentMap.indexOf('#')); ! if(posCache != null){ PositionDTO[] positions = posCache.getPositionsSince(Long.valueOf(-1)); ! for (int i = 0; i < positions.length; ++i){ writePositionToDB(currentMap, positions[i]); } - } } - System.out.println("Schreiben in Datenbank beendet"); } --- 316,350 ---- } ! public void movePosition(Map map, Long id, int x, int y, boolean fixed) { ! if (existsMapHashtable(map)) { getMapHashtable(map).movePosition(id, x, y, fixed); } } ! public void removePosition(Map map, Long id) { ! if (existsMapHashtable(map)) { getMapHashtable(map).removePosition(id); } } ! public void removePosition(Map map, String name) { ! if (existsMapHashtable(map)) { getMapHashtable(map).removePosition(name); } } ! public void writeCacheDataToDB() { System.out.println("Begin Schreiben in Datenbank"); ! ! for (String currentMap : this.keySet()) { PositionCacheEntry posCache = this.get(currentMap); ! currentMap = currentMap.substring(0, currentMap.indexOf('#')); ! if (posCache != null) { PositionDTO[] positions = posCache.getPositionsSince(Long.valueOf(-1)); ! for (int i = 0; i < positions.length; ++i) { writePositionToDB(currentMap, positions[i]); + } } } System.out.println("Schreiben in Datenbank beendet"); } *************** *** 392,400 **** } ! protected void removePosition(Long id){ PositionDTO pos = getPosition(id); pos.setDeleted(true); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)){ ++time; } --- 366,374 ---- } ! protected void removePosition(Long id) { PositionDTO pos = getPosition(id); pos.setDeleted(true); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)) { ++time; } *************** *** 403,412 **** } ! protected void removePosition(String name){ PositionDTO pos = getPosition(name); pos.setDeleted(true); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)){ ++time; } --- 377,386 ---- } ! protected void removePosition(String name) { PositionDTO pos = getPosition(name); pos.setDeleted(true); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)) { ++time; } *************** *** 415,421 **** } ! protected void addPosition(PositionDTO position){ Long id = new Long(1); ! while (idHash.containsKey(id)){ ++id; } --- 389,395 ---- } ! protected void addPosition(PositionDTO position) { Long id = new Long(1); ! while (idHash.containsKey(id)) { ++id; } *************** *** 423,427 **** Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)){ ++time; } --- 397,401 ---- Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)) { ++time; } *************** *** 433,438 **** } ! protected void movePosition(Long id, int x, int y, boolean fixed){ ! if (!idHash.containsKey(id)){ return; } --- 407,412 ---- } ! protected void movePosition(Long id, int x, int y, boolean fixed) { ! if (!idHash.containsKey(id)) { return; } *************** *** 442,446 **** pos.setFixed(fixed); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)){ ++time; } --- 416,420 ---- pos.setFixed(fixed); Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)) { ++time; } *************** *** 449,470 **** } ! private void updateTime(Long oldTime, Long time){ timeMap.put(time, timeMap.get(oldTime)); timeMap.remove(oldTime); } ! protected boolean existsPosition(String name){ return positionHash.containsKey(name); } ! protected PositionDTO getPosition(String name){ return positionHash.get(name); } ! protected PositionDTO getPosition(Long id){ return positionHash.get(idHash.get(id)); } ! protected void deletePosition(String name){ timeMap.remove(positionHash.get(name).getLastModified()); idHash.remove(positionHash.get(name).getId()); --- 423,444 ---- } ! private void updateTime(Long oldTime, Long time) { timeMap.put(time, timeMap.get(oldTime)); timeMap.remove(oldTime); } ! protected boolean existsPosition(String name) { return positionHash.containsKey(name); } ! protected PositionDTO getPosition(String name) { return positionHash.get(name); } ! protected PositionDTO getPosition(Long id) { return positionHash.get(idHash.get(id)); } ! protected void deletePosition(String name) { timeMap.remove(positionHash.get(name).getLastModified()); idHash.remove(positionHash.get(name).getId()); *************** *** 472,476 **** } ! protected void deletePosition(Long id){ timeMap.remove(idHash.get(id)); positionHash.remove(idHash.get(id)); --- 446,450 ---- } ! protected void deletePosition(Long id) { timeMap.remove(idHash.get(id)); positionHash.remove(idHash.get(id)); *************** *** 478,482 **** } ! protected PositionDTO[] getPositionsSince(Long time){ System.out.println("enter getPositionsSince()"); Collection<String> newPositions = timeMap.tailMap(time).values(); --- 452,456 ---- } ! protected PositionDTO[] getPositionsSince(Long time) { System.out.println("enter getPositionsSince()"); Collection<String> newPositions = timeMap.tailMap(time).values(); *************** *** 486,490 **** Iterator it = newPositions.iterator(); int i = 0; ! while (it.hasNext()){ positions[i++] = getPosition((String) it.next()); } --- 460,464 ---- Iterator it = newPositions.iterator(); int i = 0; ! while (it.hasNext()) { positions[i++] = getPosition((String) it.next()); } |