SynchroTargerManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 package org.openmobileis.synchro.openmsp.server.synctarget;
00026 
00027 import java.util.HashMap;
00028 
00029 import org.openmobileis.common.context.ApplicationContextManager;
00030 import org.openmobileis.common.context.UserTerminal;
00031 import org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.synchro.openmsp.server.synctarget.proxy.GenericProxySynchroTarget;
00033 import org.openmobileis.synchro.openmsp.server.synctarget.proxy.ProxySyncroTarget;
00034 
00042 public final class SynchroTargerManager {
00043   private static SynchroTargerManager manager;
00044   private HashMap targetMap;
00045   private HashMap proxyList;
00046   private ProxySyncroTarget defaultProxy;
00047   
00048   class TargetVersion {
00049     UserTerminal terminal;
00050     String version;
00051     OpenMSPSynchroTargetListener target; 
00052   }
00053   
00054   
00055   class TargetContainer {
00056     Array TargetList;
00057     String targetName;
00058     
00059     public boolean equals(Object obj) {
00060       if ((obj != null)
00061         && (obj instanceof TargetContainer)
00062         && ((TargetContainer)obj).targetName.equals(this.targetName)
00063       ) {
00064         return true;
00065       }
00066       return false;
00067     }
00068 
00069     public int hashCode() {
00070       return this.targetName.hashCode();
00071     }
00072   }
00073   
00077   private SynchroTargerManager() {
00078     super();
00079     targetMap = new HashMap(20);
00080     proxyList = new HashMap(10);
00081   }
00082   
00083   public static SynchroTargerManager getManager() {
00084     if (manager == null)  {
00085       synchronized (SynchroTargerManager.class) {
00086         if (manager == null)  {
00087           manager = new SynchroTargerManager();
00088           ApplicationContextManager.getManager().addManager(manager);
00089         }
00090       }
00091     }
00092     return manager;
00093   }
00094   
00095   public void registerProxyTargetForTerminal(ProxySyncroTarget proxy, UserTerminal terminal)  {
00096     if(terminal == null){
00097         defaultProxy = proxy;
00098     }
00099           proxyList.put(terminal, proxy);
00100   }
00101   
00112   public void addSynchroTargetListener(OpenMSPSynchroTargetListener listener, String version, UserTerminal terminal)  {
00113     TargetContainer container = (TargetContainer)targetMap.get(listener.getTargetName());
00114     if (container == null)  {
00115       container = new TargetContainer();
00116       container.TargetList = new Array();
00117       container.targetName = listener.getTargetName();
00118       targetMap.put(listener.getTargetName(), container);
00119     }
00120     TargetVersion target = new TargetVersion();
00121     target.target = listener;
00122     target.version = version;
00123     target.terminal = terminal;
00124     container.TargetList.add(target);
00125   }
00126   
00136   public OpenMSPSynchroTargetListener getSynchroTargetListener(String listenerName, String version , UserTerminal terminal) {
00137     TargetContainer container = (TargetContainer)targetMap.get(listenerName);
00138     if ((container == null) || (container.TargetList.size() == 0))  {
00139       return null;
00140     }  
00141     OpenMSPSynchroTargetListener foundlistener = ((TargetVersion)container.TargetList.get(0)).target;
00142     int size = container.TargetList.size();
00143     for (int i=0; i<size; i++)  {
00144       TargetVersion currentTarget = (TargetVersion)container.TargetList.get(i);
00145       if ((version != null) && (terminal != null))  {
00146         if ((currentTarget.version != null) && (currentTarget.terminal != null)) {
00147           if ((currentTarget.version.equals(version)) && (currentTarget.terminal.equals(terminal))){
00148             foundlistener = currentTarget.target;
00149             break;
00150           }
00151         }else   if ((currentTarget.version != null) && (currentTarget.version.equals(version))) {
00152           foundlistener = currentTarget.target;
00153         }else   if ((currentTarget.terminal != null) && (currentTarget.terminal.equals(terminal))) {
00154           foundlistener = currentTarget.target;
00155         }
00156       } else  if (version != null)  {
00157         if ((currentTarget.version != null) && (currentTarget.version.equals(version))) {
00158           foundlistener = currentTarget.target;
00159         }
00160       } else  if (terminal != null)  {
00161         if ((currentTarget.terminal != null) && (currentTarget.terminal.equals(terminal))) {
00162           foundlistener = currentTarget.target;
00163         }
00164       } else  {
00165         if ((currentTarget.terminal == null) && (currentTarget.version == null)) {
00166           foundlistener = currentTarget.target;
00167         }
00168       }
00169     }
00170     return foundlistener;
00171   }
00172   
00173   public ProxySyncroTarget getProxySynchroTargetForTerminal(UserTerminal terminal)  {
00174     ProxySyncroTarget proxy = (ProxySyncroTarget)proxyList.get(terminal);
00175     if (proxy == null)  {
00176       proxy  = defaultProxy;
00177     }
00178     return proxy;
00179   }
00180 
00181 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1