KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > transport > http > server > LocalSyncHolder


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.transport.http.server;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import java.util.logging.Level JavaDoc;
25
26 import sync4j.framework.transport.http.SyncHolder;
27 import sync4j.framework.server.SyncResponse;
28 import sync4j.framework.core.Sync4jException;
29 import sync4j.framework.config.ConfigClassLoader;
30 import sync4j.framework.config.ConfigurationException;
31 import sync4j.framework.logging.Sync4jLogger;
32 import sync4j.framework.logging.Sync4jLoggerName;
33 import sync4j.framework.server.error.ServerException;
34 import sync4j.framework.server.error.ServerFailureException;
35 import sync4j.framework.server.error.NotImplementedException;
36 import sync4j.framework.protocol.ProtocolException;
37
38 import sync4j.server.config.Configuration;
39 import sync4j.server.engine.SyncAdapter;
40
41 /**
42  * Implements a <i>SyncHolder</i> that instance a SyncAdapter to handle a
43  * synchronization request.
44  *
45  * @author Luigia Fassina @ Funambol
46  * @version $Id: LocalSyncHolder.java,v 1.11 2005/03/02 20:57:40 harrie Exp $
47  */

48 public class LocalSyncHolder implements SyncHolder {
49     
50     // --------------------------------------------------------------- Constants
51

52     // ------------------------------------------------------------ Private data
53

54     private transient Logger JavaDoc log = Sync4jLogger.getLogger(Sync4jLoggerName.HANDLER);
55     
56     private SyncAdapter syncAdapter = null;
57     private long creationTimestamp ;
58     private String JavaDoc sessionId ;
59     
60     // ------------------------------------------------------------ Constructors
61
public LocalSyncHolder() throws ServerException {
62         try {
63             syncAdapter = new SyncAdapter(loadConfiguration());
64         } catch (ConfigurationException e) {
65             
66         }
67         creationTimestamp = System.currentTimeMillis();
68     }
69     
70     // ---------------------------------------------------------- Public methods
71

72    /** Processes an incoming XML message.
73      *
74      * @param requestData the SyncML request as an array of bytes
75      * @param parameters SyncML request parameters
76      * @param headers SyncML request headers
77      *
78      * @return the SyncML response as a <i>ISyncResponse</i> object
79      *
80      * @throws ServerException in case of a server error
81      *
82      */

83     public SyncResponse processXMLMessage(final byte[] requestData,
84                                           final Map JavaDoc parameters ,
85                                           final Map JavaDoc headers )
86     throws ServerException {
87         return syncAdapter.processXMLMessage(requestData, parameters, headers);
88     }
89     
90     /** Processes an incoming WBXML message.
91      *
92      * @param requestData the SyncML request as an array of bytes
93      * @param parameters SyncML request parameters
94      * @param headers SyncML request headers
95      *
96      * @return the SyncML response as a <i>ISyncResponse</i> object
97      *
98      * @throws ServerException in case of a server error
99      *
100      */

101     public SyncResponse processWBXMLMessage(final byte[] requestData,
102                                             final Map JavaDoc parameters ,
103                                             final Map JavaDoc headers )
104                                            
105     throws ServerException {
106         return syncAdapter.processWBXMLMessage(requestData, parameters, headers);
107     }
108     
109     public void setSessionId(String JavaDoc sessionId) throws Sync4jException {
110         this.sessionId = sessionId;
111         syncAdapter.beginSync(sessionId);
112     }
113     
114     public String JavaDoc getSessionId() {
115         return this.sessionId;
116     }
117     
118     /** Called when the SyncHolder is not required any more. It gives the holder
119      * an opportunity to release and clean up resources.
120      *
121      * @throws java.lang.Exception in case of error. The real exception is stored
122      * in the cause.
123      *
124      */

125     public void close() throws Exception JavaDoc {
126         syncAdapter.endSync();
127     }
128     
129     /**
130      * Returns the creation timestamp (in milliseconds since midnight, January
131      * 1, 1970 UTC).
132      *
133      * @see sync4j.framework.transport.http.SyncHolder
134      */

135     public long getCreationTimestamp() {
136         return creationTimestamp;
137     }
138     
139     /**
140      * Loads the configuration for the server.
141      *
142      * @throws ConfigurationException in case of errors.
143      */

144     private Configuration loadConfiguration()
145     throws ConfigurationException {
146
147         Configuration config = Configuration.getConfiguration();
148         
149         //
150
// If config is null, being Configuration a singleton, only an exception
151
// could have been happend.
152
//
153
if (config == null) {
154             throw new ConfigurationException("Error loading configuration (see the log for details)!");
155         }
156         
157         return config;
158     }
159 }
160
Popular Tags