From: Florian L. <fle...@us...> - 2005-11-30 17:57:25
|
Update of /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/cache In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15597/src/net/sf/magicmap/server/cache Modified Files: PositionCache.java Log Message: DB integration in die ServerCaches: Daten werden bei initialisierung von der DB in die Caches geladen Index: PositionCache.java =================================================================== RCS file: /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/cache/PositionCache.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PositionCache.java 18 Nov 2005 14:41:59 -0000 1.6 --- PositionCache.java 30 Nov 2005 17:57:13 -0000 1.7 *************** *** 1,5 **** --- 1,11 ---- + package net.sf.magicmap.server.cache; + import java.sql.Timestamp; + import java.util.ArrayList; + import java.util.Arrays; import java.util.Collection; + import java.util.Date; + import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; *************** *** 9,203 **** import java.util.TreeMap; import net.sf.magicmap.db.Map; import net.sf.magicmap.server.dto.PositionDTO; import net.sf.magicmap.server.facade.PositionFacade; ! 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; ! } - private PositionCache (){ - //TODO: hohle daten aus der Datenbank - } - - 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).removePosition(position.getName()); - } - getMapHashtable(map).addPosition(position); - } - - 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); - - } - - 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); - } - } - } class PositionCacheEntry { - private Hashtable<String,PositionDTO> positionHash; - private TreeMap<Long ,String> timeMap; - private Hashtable<Long, String> idHash; - - protected PositionCacheEntry (){ - positionHash = new Hashtable<String,PositionDTO>(60); - timeMap = new TreeMap<Long ,String>(); - idHash = new Hashtable<Long, String>(60); - } - - protected void removePosition(Long id) { - PositionDTO pos = getPosition(id); - pos.setDeleted(true); - Long time = System.currentTimeMillis(); - while(timeMap.containsKey(time)){ - ++time; - } - updateTime(pos.getLastModified(), time); - pos.setLastModified(time); - } ! protected void removePosition(String name) { ! PositionDTO pos = getPosition(name); ! pos.setDeleted(true); ! Long time = System.currentTimeMillis(); ! while(timeMap.containsKey(time)){ ! ++time; ! } ! updateTime(pos.getLastModified(), time); ! pos.setLastModified(time); ! } ! protected void addPosition(PositionDTO position){ ! Long id = new Long(1); ! while(idHash.containsKey(id)){ ! ++id; ! } ! position.setId(id); ! ! Long time = System.currentTimeMillis(); ! while(timeMap.containsKey(time)){ ! ++time; ! } ! position.setLastModified(time); ! ! timeMap.put(position.getLastModified(),position.getName()); ! idHash.put(id,position.getName()); ! positionHash.put(position.getName(),position); ! } ! ! protected void movePosition(Long id, int x, int y, boolean fixed){ ! if(!idHash.containsKey(id)){ ! return; ! } ! PositionDTO pos = getPosition(id); ! pos.setPosX(x); ! pos.setPosY(y); ! pos.setFixed(fixed); ! Long time = System.currentTimeMillis(); ! while(timeMap.containsKey(time)){ ! ++time; ! } ! updateTime(pos.getLastModified(), time); ! pos.setLastModified(time); ! } ! ! 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()); ! positionHash.remove(name); ! } ! ! protected void deletePosition(Long id){ ! timeMap.remove(idHash.get(id)); ! positionHash.remove(idHash.get(id)); ! idHash.remove(id); ! } ! ! protected PositionDTO[] getPositionsSince(Long time){ ! System.out.println("enter getPositionsSince()"); ! Collection<String> newPositions = timeMap.tailMap(time).values(); ! System.out.println("Daten in timeMap: " + timeMap.toString()); ! System.out.println("Daten in tailMap: " + newPositions.toString()); ! PositionDTO [] positions = new PositionDTO[newPositions.size()]; ! Iterator it = newPositions.iterator(); ! int i = 0; ! while(it.hasNext()){ ! positions[i++] = getPosition((String)it.next()); ! } ! return positions; ! } ! } --- 15,482 ---- import java.util.TreeMap; + import javax.jdo.Extent; + import javax.jdo.PersistenceManager; + import javax.jdo.Query; + import javax.jdo.Transaction; + + import net.sf.magicmap.db.AccessPoint; + import net.sf.magicmap.db.Client; import net.sf.magicmap.db.Map; + 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; import net.sf.magicmap.server.dto.PositionDTO; + 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; ! } ! ! private PositionCache() { ! getDataFromDB(); ! } ! ! protected void finalize() throws Throwable{ ! Enumeration<String> mapNames = this.keys(); ! while (mapNames.hasMoreElements()){ ! String currentMap = mapNames.nextElement(); ! PositionCacheEntry posCache = this.get(currentMap); ! PositionDTO[] positions = posCache.getPositionsSince(Long.valueOf(-1)); ! for (int i = 0; i < positions.length; ++i){ ! writePositionToDB(currentMap, positions[i]); ! } ! } ! super.finalize(); ! } ! ! private Map checkMap(String mapName, PersistenceManager pm) throws MapException{ ! Map result = null; ! Collection results; ! Extent e = pm.getExtent(Map.class, true); ! Query query = pm.newQuery(e, "name == myName"); ! 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"); ! } ! return result; ! } ! ! private Position checkPosition(String positionName, Map map, PersistenceManager pm){ ! Position result = null; ! Collection results; ! Extent e = pm.getExtent(Position.class, true); ! Query query = pm.newQuery(e, "name == myName && map == myMap"); ! query.declareParameters("java.lang.String myName, net.sf.magicmap.db.Map myMap"); ! 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(); ! } ! return result; ! ! } ! ! 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"); ! } ! Client result = null; ! Collection results; ! Extent e = pm.getExtent(Client.class, true); ! Query query = pm.newQuery(e, "mac == myMac"); ! 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"); ! } ! return result; ! ! } ! ! private AccessPoint findOrCreateAccessPoint(String macAP, PersistenceManager pm) throws MapException{ ! try{ ! AccessPoint result = null; ! Collection results; ! Extent e = pm.getExtent(AccessPoint.class, true); ! Query query = pm.newQuery(e, "mac == myMac"); ! query.declareParameters("java.lang.String myMac"); ! 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); ! } ! } ! ! 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); ! } ! } ! } ! ! private void writePositionToDB(String mapName, PositionDTO position){ ! PersistenceManager pm = null; ! Transaction tx = null; ! try{ ! pm = JDOUtil.pmfactory.getPersistenceManager(); ! tx = pm.currentTransaction(); ! tx.begin(); ! 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); ! } ! pos.setDeleted(position.isDeleted()); ! pos.setPosX(position.getPosX()); ! pos.setPosY(position.getPosY()); ! 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(); ! } ! JDOUtil.closePM(pm); ! } ! ! } ! ! /** ! * @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(); ! tx.begin(); ! Extent e = pm.getExtent(Map.class, true); ! 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"); ! posQuery.declareParameters("net.sf.magicmap.db.Map myMap"); ! 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()); ! apDTO.setId(new Long(ap.getId())); ! apDTO.setSsid(ap.getSsid()); ! dto.setAccessPoint(apDTO); ! } ! Client client = pos.getClient(); ! if (client != null){ ! ClientDTO clientDTO = new ClientDTO(); ! clientDTO.setMac(client.getMac()); ! clientDTO.setId(new Long(client.getId())); ! clientDTO.setName(client.getName()); ! dto.setClient(clientDTO); ! } ! dto.setId(new Long(pos.getId())); ! dto.setDeleted(pos.isDeleted()); ! dto.setLastModified(new Long(pos.getLastModified())); ! dto.setPosX(new Integer(pos.getPosX())); ! dto.setPosY(new Integer(pos.getPosY())); ! dto.setFixed(pos.isFixed()); ! dto.setName(pos.getName()); ! dto.setCharacter(new SignalCharacterDTO()); ! // 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()); ! } ! ! SimpleScanResultDTO ssr = new SimpleScanResultDTO(); ! ! // ssr.setLastSeen(lastSeen); //TODO: ! // last seen benutzen ! ssr.setMacAP(sr.getAccessPoint().getMac()); ! ssr.setNoise(sr.getNoiseLevel()); ! ssr.setSignalLevel(sr.getSignalLevel()); ! ssr.setSsid(sr.getAccessPoint().getSsid()); ! ! ArrayList list; ! if (dto.getCharacter().getSimpleScanResults() == null){ ! list = new ArrayList(); ! } else{ ! list = new ArrayList(Arrays.asList(dto.getCharacter().getSimpleScanResults())); ! } ! ! list.add(ssr); ! dto.getCharacter().setSimpleScanResults( ! (SimpleScanResultDTO[]) list.toArray(new SimpleScanResultDTO[0])); ! } ! } ! getMapHashtable(map).addPosition(dto); ! } ! } ! } catch (Exception ex){ ! ex.printStackTrace(); ! } ! } ! } ! 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).removePosition(position.getName()); ! } ! getMapHashtable(map).addPosition(position); ! } ! ! 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); ! ! } ! ! 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); ! } ! } } class PositionCacheEntry { ! private Hashtable<String, PositionDTO> positionHash; ! private TreeMap<Long, String> timeMap; ! private Hashtable<Long, String> idHash; ! protected PositionCacheEntry() { ! positionHash = new Hashtable<String, PositionDTO>(60); ! timeMap = new TreeMap<Long, String>(); ! idHash = new Hashtable<Long, String>(60); ! } ! ! protected void removePosition(Long id){ ! PositionDTO pos = getPosition(id); ! pos.setDeleted(true); ! Long time = System.currentTimeMillis(); ! while (timeMap.containsKey(time)){ ! ++time; ! } ! updateTime(pos.getLastModified(), time); ! pos.setLastModified(time); ! } + protected void removePosition(String name){ + + PositionDTO pos = getPosition(name); + pos.setDeleted(true); + Long time = System.currentTimeMillis(); + while (timeMap.containsKey(time)){ + ++time; + } + updateTime(pos.getLastModified(), time); + pos.setLastModified(time); + } + + protected void addPosition(PositionDTO position){ + Long id = new Long(1); + while (idHash.containsKey(id)){ + ++id; + } + position.setId(id); + + Long time = System.currentTimeMillis(); + while (timeMap.containsKey(time)){ + ++time; + } + position.setLastModified(time); + + timeMap.put(position.getLastModified(), position.getName()); + idHash.put(id, position.getName()); + positionHash.put(position.getName(), position); + } + + protected void movePosition(Long id, int x, int y, boolean fixed){ + if (!idHash.containsKey(id)){ + return; + } + PositionDTO pos = getPosition(id); + pos.setPosX(x); + pos.setPosY(y); + pos.setFixed(fixed); + Long time = System.currentTimeMillis(); + while (timeMap.containsKey(time)){ + ++time; + } + updateTime(pos.getLastModified(), time); + pos.setLastModified(time); + } + + 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()); + positionHash.remove(name); + } + + protected void deletePosition(Long id){ + timeMap.remove(idHash.get(id)); + positionHash.remove(idHash.get(id)); + idHash.remove(id); + } + + protected PositionDTO[] getPositionsSince(Long time){ + System.out.println("enter getPositionsSince()"); + Collection<String> newPositions = timeMap.tailMap(time).values(); + System.out.println("Daten in timeMap: " + timeMap.toString()); + System.out.println("Daten in tailMap: " + newPositions.toString()); + PositionDTO[] positions = new PositionDTO[newPositions.size()]; + Iterator it = newPositions.iterator(); + int i = 0; + while (it.hasNext()){ + positions[i++] = getPosition((String) it.next()); + } + return positions; + } + } |