Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

OpenMSPSynchroManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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 
00026 package org.openmobileis.synchro.openmsp.client;
00027 
00028 import java.io.ByteArrayOutputStream;
00029 import java.io.IOException;
00030 import java.util.zip.GZIPOutputStream;
00031 
00032 import org.openmobileis.common.context.ApplicationContextManager;
00033 import org.openmobileis.common.context.Plateform;
00034 import org.openmobileis.common.intl.IntlResourceManager;
00035 import org.openmobileis.common.user.UserNotFoundException;
00036 import org.openmobileis.common.util.PropertiesManager;
00037 import org.openmobileis.common.util.codec.GeneralCoder;
00038 import org.openmobileis.common.util.collection.Array;
00039 import org.openmobileis.common.util.exception.SynchroException;
00040 import org.openmobileis.embedded.util.ServicePropertiesManager;
00041 import org.openmobileis.synchro.client.SynchroDescriptor;
00042 import org.openmobileis.synchro.client.SynchroProcessor;
00043 import org.openmobileis.synchro.openmsp.OpenMSPException;
00044 import org.openmobileis.synchro.openmsp.client.conduit.ConduitParameter;
00045 import org.openmobileis.synchro.openmsp.client.core.ListenerOrdonancer;
00046 import org.openmobileis.synchro.openmsp.client.core.OpenMSPSyncMessageFactory;
00047 import org.openmobileis.synchro.openmsp.protocol.Message;
00048 import org.openmobileis.synchro.openmsp.protocol.MessageFactory;
00049 import org.openmobileis.synchro.openmsp.protocol.Status;
00050 import org.openmobileis.synchro.security.auth.Credential;
00051 
00059 public class OpenMSPSynchroManager implements SynchroProcessor {
00060   public static final String OPENMSPPROCESSORNAME="OpenMSPSync";
00061 
00062   private static OpenMSPSynchroManager manager;
00063   private ListenerOrdonancer ordonancer;
00064   private OpenMSPSyncMessageFactory messageFactory;
00065   private String lastSynchroLogin;
00066   private int globalSynchroStatut;
00067 
00071   private OpenMSPSynchroManager() {
00072     super();
00073     this.ordonancer = new ListenerOrdonancer();
00074     this.messageFactory = new OpenMSPSyncMessageFactory();
00075   }
00076 
00077   public static OpenMSPSynchroManager getManager() {
00078     if (manager == null)  {
00079       synchronized (OpenMSPSynchroManager.class) {
00080         if (manager == null)  {
00081           manager = new OpenMSPSynchroManager();
00082           ApplicationContextManager.getManager().addManager(manager);
00083         }
00084       }
00085     }
00086     return manager;
00087   }
00088  
00089   public String getProcessorName()  {
00090     return OpenMSPSynchroManager.OPENMSPPROCESSORNAME;   
00091   }
00092 
00093   public void notifyFailure(String listernerName) {
00094     OpenMSPSyncListener listener = this.getListenerByName(listernerName);
00095     if (listener != null) {
00096       listener.notifySynchroFailure();
00097       ordonancer.notifySynchroError(listener.getSyncName());
00098     }
00099     globalSynchroStatut = Status.STATUS_FAILED;
00100   }
00101   
00102   public OpenMSPSyncListener getListenerByName(String listenerName) {
00103     return ordonancer.getListenerByName(listenerName);
00104   }
00105   
00106   public void doSynchro(Credential cred, SynchroDescriptor descriptor) throws SynchroException {
00107     globalSynchroStatut = Status.STATUS_SYNC_IN_PROGRESS;
00108     this.lastSynchroLogin = cred.getPrincipal();
00109     //save login
00110     ServicePropertiesManager.getManager().saveProperty("OpenMSPSynchroManager", "login", this.lastSynchroLogin);
00111     this.ordonancer.beginSynchro();
00112     this.messageFactory.beginSynchro(cred, descriptor);
00113     Array listenerList = this.ordonancer.getNextListenerList();
00114     try {
00115       while (listenerList.size() != 0)  {
00116         int listsize = listenerList.size();
00117         for (int i=0; i<listsize; i++)  {
00118           OpenMSPSyncListener listener = (OpenMSPSyncListener) listenerList.get(i);
00119           listener.startSync(cred, descriptor);
00120         }
00121         while (true) {
00122           Message message = this.messageFactory.getOpenMSPMessage(listenerList);
00123           if (message.isEmpty()) {
00124             //get next listener phase.
00125             break;
00126           }
00127           String xmlString = message.encode();
00128           ConduitParameter[] parameters = new ConduitParameter[] {new ConduitParameter("openMISGroup", descriptor.getSynchroGroup())};
00129  
00130           String reqResult = descriptor.getSynchroConduit().sendRequest(parameters, new String(compressAndEncodeRequest(xmlString)), descriptor.getServerURL());
00131           // third step : Process responses
00132           Message receivedMessage = MessageFactory.getFactory().getMessage(reqResult);
00133           String target = receivedMessage.getHeader().getTarget();
00134           if (!target.equals(OpenMSPSyncMessageFactory.OpenMSPSOURCE)) {
00135             throw new OpenMSPException("Bad OpenMSP Sync Source in Server answer message. Synchro Abort.");
00136           } else {
00137             this.messageFactory.receiveOpenMSPMessage(listenerList, receivedMessage);
00138           }
00139         }
00140         for (int i=0; i<listsize; i++)  {
00141           OpenMSPSyncListener listener = (OpenMSPSyncListener) listenerList.get(i);
00142           listener.endSync();
00143         }
00144         listenerList = this.ordonancer.getNextListenerList();
00145       }
00146       globalSynchroStatut = Status.STATUS_OK;
00147     } catch (UserNotFoundException ex)  {
00148       notifyFailureToListeners(listenerList);
00149       globalSynchroStatut = Status.STATUS_UNAUTHORIZED;
00150       throw new OpenMSPException(ex);
00151     } catch (Throwable ex)  {
00152       notifyFailureToListeners(listenerList);
00153       throw new OpenMSPException(ex);
00154     } finally {
00155       this.ordonancer.endSynchro();
00156       this.messageFactory.endSynchro();      
00157     }
00158   }
00159   
00168   public void addListener(OpenMSPSyncListener listener, String[] depends) {
00169     ordonancer.addListener(listener, depends);
00170   }
00171   
00172   private void notifyFailureToListeners(Array listenerList) {
00173     for (int i=0; i<listenerList.size(); i++) {
00174       OpenMSPSyncListener listener = (OpenMSPSyncListener)listenerList.get(i);
00175       listener.notifySynchroFailure();
00176       ordonancer.notifySynchroError(listener.getSyncName());
00177     }
00178     globalSynchroStatut = Status.STATUS_FAILED;   
00179   }
00180 
00181   private byte[] compressAndEncodeRequest(String request) throws IOException {
00182     // compresse data
00183     byte[] compressedData = null;
00184     ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
00185     GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
00186     try {
00187       zipStream.write(request.getBytes());
00188     } finally  {
00189       zipStream.close();
00190     }
00191     compressedData = byteStream.toByteArray();
00192 
00193     // encode compressed data
00194     byte[] returnData = GeneralCoder.encodeBase64(compressedData);
00195     return returnData;
00196   }
00197 
00198   public String getLastSynchroLogin() {
00199     if (this.lastSynchroLogin == null){
00200       this.lastSynchroLogin = ServicePropertiesManager.getManager().getProperty("OpenMSPSynchroManager", "login");
00201 
00202     }
00203     return this.lastSynchroLogin;
00204   }
00205   
00214   public String getSynchroUserAgent(SynchroDescriptor descriptor) {
00215     String usera = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.useragent");      
00216     if (usera == null)  {
00217       Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00218       StringBuffer useragent = new StringBuffer("OpenMSPPlug-");
00219       useragent.append(plateform.getOpenMobileISMajorVersion()).append('.').append(plateform.getOpenMobileISMinorVersion()).append('-');
00220       useragent.append(plateform.getOS()).append('[').append(plateform.getOSVersion()).append("]-");
00221       String language = IntlResourceManager.getManager().getManagerLocaleString();
00222       useragent.append(language);
00223       String synctype = descriptor.getProperty("OpenMSPsynchrotype");
00224       if (synctype != null) {
00225         useragent.append('-').append(synctype);
00226       }
00227       usera = useragent.toString();
00228     }
00229     return usera;
00230   }
00231 
00232   public int getGlobalSynchroStatut() {
00233     return globalSynchroStatut;
00234   }
00235 
00236 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4