KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > syncbean > SyncBean


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
19 package sync4j.server.syncbean;
20
21 import javax.ejb.*;
22 import javax.naming.*;
23
24 import javax.rmi.PortableRemoteObject JavaDoc;
25
26 import java.io.*;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 import sync4j.framework.core.*;
32 import sync4j.framework.server.SyncResponse;
33 import sync4j.framework.logging.Sync4jLogger;
34 import sync4j.framework.config.ConfigClassLoader;
35 import sync4j.framework.config.ConfigurationException;
36 import sync4j.framework.protocol.ProtocolException;
37
38 import sync4j.framework.core.Sync4jException;
39 import sync4j.framework.core.RepresentationException;
40 import sync4j.framework.server.session.SessionExpiredException;
41 import sync4j.framework.server.error.*;
42
43 import sync4j.server.config.Configuration;
44 import sync4j.server.engine.SyncAdapter;
45 import sync4j.server.session.SyncSessionHandler;
46
47 import sync4j.server.admin.*;
48 import sync4j.server.admin.ejb.*;
49
50 /**
51  * This is the session enterprise java bean that handles a synchronization
52  * request. It is designed to be a stateful session bean so that the state
53  * and the session management are on charge of the application server.
54  *
55  * LOG NAME: sync4j.server
56  *
57  * @author Stefano Fornari @ Funambol
58  * @author Luigia Fassina @ Funambol
59  *
60  * @version $Id: SyncBean.java,v 1.61 2005/03/02 20:57:39 harrie Exp $
61  *
62  */

63 public class SyncBean
64 implements javax.ejb.SessionBean JavaDoc {
65
66     // ------------------------------------------------------- Private constants
67

68     private static final String JavaDoc ENV_ADMIN_NAME
69         = "java:comp/env/ejb/LocalAdminBean";
70
71     // ------------------------------------------------------------ Private data
72

73     private transient Logger JavaDoc log = null;
74
75     private SessionContext sessionContext = null;
76
77     private SyncSessionHandler sessionHandler = null;
78
79     private Configuration config = null;
80
81     private String JavaDoc sessionId = null;
82
83     private SyncAdapter syncAdapter = null;
84
85     // ------------------------------------------------------------- EJB methods
86

87     public void ejbCreate(String JavaDoc sessionId) throws CreateException {
88
89         log = Sync4jLogger.getLogger("server");
90
91         loadAdminBean();
92
93         try {
94             syncAdapter = new SyncAdapter(config);
95             syncAdapter.beginSync(sessionId);
96         } catch (Exception JavaDoc e) {
97             log.throwing("SyncBean", "ejbCreate", e);
98             throw new CreateException( "Error "
99                                      + e.getClass().getName()
100                                      + " creating the SyncBean: "
101                                      + e.getMessage()
102                                      );
103         }
104         this.sessionId = sessionId;
105     }
106
107     /**
108      * Creates an instance of AdminBean
109      */

110     private void loadAdminBean() throws CreateException {
111
112         try {
113
114             InitialContext ctx = new InitialContext();
115
116             Object JavaDoc obj = ctx.lookup(ENV_ADMIN_NAME);
117             AdminHomeLocal home =
118                 (AdminHomeLocal)PortableRemoteObject.narrow(obj, AdminHomeLocal.class);
119             AdminLocal admin = home.create();
120
121             config = admin.getConfig();
122             
123         } catch (NamingException e) {
124             e.printStackTrace();
125             log.throwing("SyncBean", "loadAdminBean", e);
126             throw new CreateException( "Error "
127                                      + e.getClass().getName()
128                                      + " creating the AdminBean: "
129                                      + e.getMessage()
130                                      );
131         } catch (Exception JavaDoc e) {
132             e.printStackTrace();
133             log.throwing("SyncBean", "loadAdminBean", e);
134             throw new CreateException( "Error "
135                                      + e.getClass().getName()
136                                      + " creating the AdminBean: "
137                                      + e.getMessage()
138                                      );
139         }
140     }
141
142
143     /**
144      * When the session terminates this object is removed. This can happen
145      * because the synchronization process has been completed or because the
146      * session has expired. If the session completes its job, the synchro-
147      * nization can be committed.
148      *
149      */

150     public void ejbRemove() {
151         syncAdapter.endSync();
152     }
153
154     public void ejbActivate() {
155         log = Sync4jLogger.getLogger();
156     }
157
158     public void ejbPassivate() {
159         log = null;
160     }
161
162     public void setSessionContext (SessionContext sessionContext)
163     throws EJBException {
164         this.sessionContext = sessionContext;
165     }
166
167     /** Processes an incoming XML message.
168      *
169      * @param msg the SyncML request as an array of bytes
170      * @param parameters SyncML request parameters
171      * @param headers SyncML request headers
172      *
173      * @return the SyncML response as a <i>ISyncResponse</i> object
174      *
175      * @throws ServerException in case of a server error
176      *
177      */

178     public SyncResponse processXMLMessage(final byte[] msg ,
179                                           final Map JavaDoc parameters ,
180                                           final Map JavaDoc headers )
181     throws ServerException {
182         return (SyncResponse) syncAdapter.processXMLMessage(msg, parameters, headers);
183     }
184     
185     /** Processes an incoming WBXML message.
186      *
187      * @param msg the SyncML request (WBXML encoded) as an array of bytes
188      * @param parameters SyncML request parameters
189      * @param headers SyncML request headers
190      *
191      * @return the SyncML response as a <i>ISyncResponse</i> object
192      *
193      * @throws ServerException in case of a server error
194      *
195      */

196     public SyncResponse processWBXMLMessage(final byte[] msg ,
197                                             final Map JavaDoc parameters ,
198                                             final Map JavaDoc headers )
199     throws ServerException {
200         return (SyncResponse) syncAdapter.processWBXMLMessage(msg, parameters, headers);
201     }
202
203     /**
204      * Used to process a status information as needed by the client object (i.e.
205      * errors or success).
206      *
207      * @param statusCode the status code
208      * @param info additional informational message
209      *
210      * @see sync4j.framework.core.StatusCode for valid status codes.
211      */

212     public SyncResponse processStatusCode(int statusCode, String JavaDoc info){
213         return syncAdapter.processStatusCode(statusCode, info);
214     }
215 }
216
Popular Tags