📄 syncadapter.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.server.engine;import java.io.*;import java.lang.reflect.Method;import java.util.logging.Logger;import java.util.logging.Level;import java.beans.XMLDecoder;import java.net.URL;import java.net.MalformedURLException;import javax.naming.*;import org.kxml.wap.SyncMLWriter;import org.kxml.parser.XmlParser;import org.kxml.parser.ParseEvent;import org.kxml.Xml;import sync4j.framework.core.*;import sync4j.framework.server.SyncResponse;import sync4j.framework.logging.Sync4jLogger;import sync4j.framework.engine.SyncEngineFactory;import sync4j.framework.config.Configuration;import sync4j.framework.config.ConfigClassLoader;import sync4j.framework.config.ConfigurationException;import sync4j.framework.server.session.SessionHandler;import sync4j.server.session.SimpleSessionHandler;import sync4j.framework.protocol.ProtocolException;import sync4j.framework.core.Sync4jException;import sync4j.framework.core.RepresentationException;import sync4j.framework.server.session.*;import sync4j.framework.server.error.*;import sync4j.framework.tools.WBXMLTools;import sync4j.framework.tools.beans.*;import sync4j.framework.engine.pipeline.PipelineManager;import sync4j.framework.engine.pipeline.MessageProcessingContext;import sync4j.server.SyncMLCanonizer;import org.jibx.runtime.*;import org.jibx.runtime.impl.*;/** * This class handles a synchronization request. * <p> * This server accepts synchronization requests addressed to the hostname * indicated by the configuration property pointed by {CONFIG_SERVER_URI} (see * Sync4j.properties). * <p> * SyncAdapter uses an environment property: * <ul> * <li><i>{ENV_ENGINE_FACTORY_NAME}</i> points to the bean to be used as a * factory for the synchronization engine</li> * </ul> * * LOG NAME: sync4j.server * * @author Luigia Fassina @ Funambol * * @version $Id: SyncAdapter.java,v 1.9 2004/05/28 16:12:19 luigiafassina Exp $ * */public class SyncAdapter { // ------------------------------------------------------- Private constants protected static final String ENV_ENGINE_FACTORY_NAME = "java:comp/env/syncengine/factory/bean"; protected static final String ENV_SERVER_CONFIG_URI = "java:comp/env/server/config_uri"; protected static final String ENV_SERVER_CONFIG_PATH = "java:comp/env/server/config_path"; protected static final String CONFIG_SYNCML_CANONIZER = "sync4j/server/SyncMLCanonizer.xml"; protected static final String CONFIG_SERVER_URI = "server.uri"; protected static final String CONFIG_ENGINE_PIPELINE = "engine.pipeline"; // ------------------------------------------------------------ Private data protected transient Logger log = null; protected SyncEngineFactory syncEngineFactory = null; protected SimpleSessionHandler sessionHandler = null; protected Configuration config = null; protected String sessionId = null; protected PipelineManager pipelineManager = null; protected MessageProcessingContext mpc = null; protected SyncML messageInput = null; protected SyncMLCanonizer syncMLCanonizer = null; // ------------------------------------------------------------ Constructors public SyncAdapter() { super(); log = Sync4jLogger.getLogger("server"); try { loadConfiguration(); getSyncEngineFactory(); } catch (Sync4jException e) { log.throwing("SyncAdapter", "constructor", e); new Sync4jException( "Error " + e.getClass().getName() + " creating the SyncAdapter: " + e.getMessage() ).printStackTrace(); } sessionHandler = new SimpleSessionHandler(); sessionHandler.setSyncEngineFactory(syncEngineFactory); pipelineManager = (PipelineManager)config.getBeanInstance(CONFIG_ENGINE_PIPELINE); try { syncMLCanonizer = (SyncMLCanonizer)BeanFactory.getBeanInstance(config.getClassLoader(), CONFIG_SYNCML_CANONIZER); } catch (Exception e){ log.throwing("SyncAdapter", "constructor", e); new Sync4jException( "Error " + e.getClass().getName() + " creating the syncMLCanonizer: " + e.getMessage() ).printStackTrace(); } mpc = new MessageProcessingContext(); } public void endSync() { int currentState = sessionHandler.getCurrentState(); switch (currentState) { case SimpleSessionHandler.STATE_END: sessionHandler.commit(); break; case SimpleSessionHandler.STATE_ERROR: sessionHandler.abort(StatusCode.PROCESSING_ERROR); break; default: sessionHandler.abort(StatusCode.SESSION_EXPIRED); break; } log = null; } /** * process the incoming sync message * * @param msg must be non-null * @param mimeType may be null. * */ public SyncResponse processMessage( final byte[] msg, final String mimeType) throws ServerException { Sync4jResponse response = null; String inMessage = null; SyncML outMessage = null; if (msg == null) { String err = "msg is null!"; log.severe(err); throw new ServerException(err); } if (mimeType == null) { String err = "mimeType is null!"; log.severe(err); throw new ServerException(err); } if (log.isLoggable(Level.FINE)) { log.fine("mimeType: " + mimeType); } try { if (Constants.MIMETYPE_SYNCML_WBXML.equals(mimeType)) { // convert WBXML to XML, and then pass to processXMLMessage // todo: change this so that it mirrors processXMLMessage // instead of converting to XML then calling processXMLMessage if (log.isLoggable(Level.FINEST)) { log.finest("Convert message from wbxml to xml"); } inMessage = WBXMLTools.wbxmlToXml(msg); } else if (Constants.MIMETYPE_SYNCML_XML.equals(mimeType)) { inMessage = new String(msg); } else { // // its an unsupported MIME type // String err = "Unknown mime type: " + mimeType; log.severe(err); throw new NotImplementedException(err); } } catch (Sync4jException e) { String err = "Error processing message: " + e.getMessage(); log.severe(err); throw new ServerException(err); } try { if (log.isLoggable(Level.INFO)) { log.info("Message for creating SyncML object\n" + inMessage); } IBindingFactory f = BindingDirectory.getFactory(SyncML.class); IUnmarshallingContext c = f.createUnmarshallingContext(); Object syncML = c.unmarshalDocument( new ByteArrayInputStream(inMessage.getBytes()), null ); messageInput = (SyncML)syncML; } catch(org.jibx.runtime.JiBXException e) { e.printStackTrace(); throw new ServerException(e); } try { if (log.isLoggable(Level.FINE)) { log.fine("Calling input pipeline"); } pipelineManager.preProcessMessage(mpc, messageInput); if (log.isLoggable(Level.FINE)) { log.fine("Calling process message"); } outMessage = processXMLMessage(inMessage); final String resultMimeType = mimeType; if (log.isLoggable(Level.FINE)) { log.fine("Calling output pipeline"); } pipelineManager.postProcessMessage(mpc, outMessage);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -