KickJava   Java API By Example, From Geeks To Geeks.

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


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.transport.http.server;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import javax.servlet.http.*;
25 import javax.naming.*;
26 import javax.ejb.CreateException JavaDoc;
27 import javax.ejb.RemoveException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30
31 import sync4j.framework.transport.http.SyncHolder;
32 import sync4j.framework.server.SyncResponse;
33 import sync4j.server.syncbean.SyncLocal;
34 import sync4j.server.syncbean.SyncHomeLocal;
35
36 import sync4j.framework.core.Sync4jException;
37 import sync4j.framework.server.error.ServerException;
38 import sync4j.framework.server.error.ServerFailureException;
39 import sync4j.framework.server.error.NotImplementedException;
40 import sync4j.framework.protocol.ProtocolException;
41
42
43 import javax.security.auth.login.LoginContext JavaDoc;
44 import javax.security.auth.login.LoginException JavaDoc;
45
46 /**
47  * Implementes a <i>SyncHolder</i> wrapping a local EJB.
48  *
49  * @author Stefano Fornari @ Funambol
50  * @version $Id: LocalEJBSyncHolder.java,v 1.15 2005/03/02 20:57:40 harrie Exp $
51  */

52 public class LocalEJBSyncHolder implements SyncHolder {
53
54     // --------------------------------------------------------------- Constants
55

56     public static final String JavaDoc SYNCEJB_HOME_JNDI_NAME =
57         "java:comp/env/ejb/LocalSyncBean";
58
59     // ------------------------------------------------------------ Private data
60

61     private SyncLocal sync = null;
62     private String JavaDoc sessionId = null;
63     private SyncHomeLocal home = null;
64     private long creationTimestamp ;
65
66     // ------------------------------------------------------------ Constructors
67

68     public LocalEJBSyncHolder() throws NamingException {
69         InitialContext ctx = new InitialContext();
70         home = (SyncHomeLocal)PortableRemoteObject.narrow(
71             ctx.lookup(SYNCEJB_HOME_JNDI_NAME),
72             SyncHomeLocal.class
73         );
74         creationTimestamp = System.currentTimeMillis();
75     }
76
77     // ---------------------------------------------------------- Public methods
78

79     /** Processes an incoming XML message.
80      *
81      * @param requestData the SyncML request as an array of bytes
82      * @param parameters SyncML request parameters
83      * @param headers SyncML request headers
84      *
85      * @return the SyncML response as a <i>ISyncResponse</i> object
86      *
87      * @throws ServerException in case of a server error
88      *
89      */

90     public SyncResponse processXMLMessage(final byte[] requestData,
91                                           final Map JavaDoc parameters ,
92                                           final Map JavaDoc headers )
93     throws ServerException {
94         return sync.processXMLMessage(requestData, parameters, headers);
95     }
96     
97     /** Processes an incoming WBXML message.
98      *
99      * @param requestData the SyncML request as an array of bytes
100      * @param parameters SyncML request parameters
101      * @param headers SyncML request headers
102      *
103      * @return the SyncML response as a <i>ISyncResponse</i> object
104      *
105      * @throws ServerException in case of a server error
106      *
107      */

108     public SyncResponse processWBXMLMessage(final byte[] requestData,
109                                             final Map JavaDoc parameters ,
110                                             final Map JavaDoc headers )
111     throws ServerException {
112         return sync.processWBXMLMessage(requestData, parameters, headers);
113     }
114
115     public void setSessionId(String JavaDoc sessionId) throws Sync4jException {
116         try {
117             sync = home.create(sessionId);
118         } catch (CreateException JavaDoc e) {
119             throw new ServerFailureException(e);
120         }
121
122         this.sessionId = sessionId;
123     }
124
125     public String JavaDoc getSessionId() {
126         return sessionId;
127     }
128
129     /** Called when the SyncHolder is not required any more. It gives the holder
130      * an opportunity to release and clean up resources.
131      *
132      * @throws java.lang.Exception in case of error. The real exception is stored
133      * in the cause.
134      *
135      */

136     public void close() throws Exception JavaDoc {
137         try {
138             sync.remove();
139         } catch (RemoveException JavaDoc e) {
140             throw new Exception JavaDoc("Error in closing the SyncHolder", e);
141         } catch (EJBException JavaDoc e) {
142             throw new Exception JavaDoc("Error in closing the SyncHolder", e);
143         }
144     }
145
146     /**
147      * Returns the creation timestamp (in milliseconds since midnight, January
148      * 1, 1970 UTC).
149      *
150      * @see sync4j.framework.transport.http.SyncHolder
151      */

152     public long getCreationTimestamp() {
153         return creationTimestamp;
154     }
155 }
156
Popular Tags