KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.rmi.RemoteException JavaDoc;
25
26 import javax.servlet.http.*;
27 import javax.naming.*;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.RemoveException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.Handle JavaDoc;
32 import javax.security.auth.login.LoginContext JavaDoc;
33 import javax.security.auth.login.LoginException JavaDoc;
34
35 import sync4j.server.syncbean.SyncRemote;
36 import sync4j.server.syncbean.SyncHomeRemote;
37
38 import sync4j.framework.transport.http.SyncHolder;
39 import sync4j.framework.server.SyncResponse;
40
41 import sync4j.framework.core.Sync4jException;
42 import sync4j.framework.server.error.ServerException;
43 import sync4j.framework.server.error.ServerFailureException;
44 import sync4j.framework.server.error.NotImplementedException;
45 import sync4j.framework.protocol.ProtocolException;
46
47 /**
48  * Implementes a <i>SyncHolder</i> wrapping a remote EJB.
49  * <p>
50  * This holder must be used in a clustered environment.
51  *
52  * @author Luigia Fassina
53  * @author Stefano Fornari
54  *
55  * @version $Id: RemoteEJBSyncHolder.java,v 1.9 2005/03/02 20:57:40 harrie Exp $
56  */

57 public class RemoteEJBSyncHolder
58 implements SyncHolder, Serializable JavaDoc {
59
60     // --------------------------------------------------------------- Constants
61

62     public static final String JavaDoc SYNCEJB_HOME_JNDI_NAME = "java:comp/env/ejb/RemoteSyncBean";
63
64     public static final String JavaDoc CONTEXT_FACTORY
65         = "org.jnp.interfaces.NamingContextFactory";
66     public static final String JavaDoc CONTEXT_URL_PREFIXES
67         = "org.jboss.naming:org.jnp.interfaces";
68
69     // ------------------------------------------------------------ Private data
70

71     private transient SyncRemote syncBean = null ;
72     private transient boolean newHolder = false;
73     private Handle JavaDoc syncHandle ;
74     private String JavaDoc sessionId ;
75     private long creationTimestamp ;
76
77     private String JavaDoc jndiAddress;
78
79     // ------------------------------------------------------------ Constructors
80

81     /**
82      * For serialization purposes
83      */

84     protected RemoteEJBSyncHolder() {}
85
86     /**
87      * Creates a new SyncServerEJBSyncHolder setting the <i>newHolder</i>
88      * properties to </i>newHolderFlag</i>. Please note that when the default
89      * constructore is used (i.e.during deserialization), the default value
90      * <i>false</i> is taken. This is wanted, because if the object is
91      * deserialized, it is not new by construction. The same is true for
92      * <i>creationTimestamp</i> but in this case no default value is provided
93      * as it is not transient (the serialized value must be taken).
94      *
95      * @param newHolderFlag is this holder a new one?
96      */

97     public RemoteEJBSyncHolder(boolean newHolderFlag) {
98         newHolder = newHolderFlag;
99         if (newHolder) {
100             creationTimestamp = System.currentTimeMillis();
101         }
102     }
103
104     // ---------------------------------------------------------- Public methods
105

106     /** Processes an incoming XML message.
107      *
108      * @param requestData the SyncML request as an array of bytes
109      * @param parameters SyncML request parameters
110      * @param headers SyncML request headers
111      *
112      * @return the SyncML response as a <i>ISyncResponse</i> object
113      *
114      * @throws ServerException in case of a server error
115      *
116      */

117     public SyncResponse processXMLMessage(final byte[] requestData,
118                                           final Map JavaDoc parameters ,
119                                           final Map JavaDoc headers )
120     throws ServerException {
121        try {
122             return getSyncServerBeanInstance()
123                    .processXMLMessage(requestData, parameters, headers);
124         } catch (Exception JavaDoc e) {
125             throw new ServerException(e);
126         }
127     }
128
129     /** Processes an incoming WBXML message.
130      *
131      * @param requestData the SyncML request as an array of bytes
132      * @param parameters SyncML request parameters
133      * @param headers SyncML request headers
134      *
135      * @return the SyncML response as a <i>ISyncResponse</i> object
136      *
137      * @throws ServerException in case of a server error
138      *
139      */

140     public SyncResponse processWBXMLMessage(final byte[] requestData,
141                                             final Map JavaDoc parameters ,
142                                             final Map JavaDoc headers )
143     throws ServerException {
144         try {
145             return getSyncServerBeanInstance()
146                    .processWBXMLMessage(requestData, parameters, headers);
147         } catch (Exception JavaDoc e) {
148             throw new ServerException(e);
149         }
150     }
151     
152     public void setSessionId(String JavaDoc sessionId) {
153         this.sessionId = sessionId;
154     }
155
156     public String JavaDoc getSessionId() {
157         return sessionId;
158     }
159
160     public void setJndiAddress(String JavaDoc jndiAddress){
161         this.jndiAddress = jndiAddress;
162     }
163
164     public String JavaDoc getJndiAddress() {
165         return jndiAddress;
166     }
167
168     /** Called when the SyncHolder is not required any more. It gives the holder
169      * an opportunity to release and clean resources.
170      *
171      * @throws java.lang.Exception in case of error. The real exception is stored
172      * in the cause.
173      *
174      */

175     public void close() throws Exception JavaDoc {
176         try {
177             if (syncBean != null) {
178                 syncBean.remove();
179             }
180         } catch (Exception JavaDoc e) {
181             throw new Exception JavaDoc("Error in closing the SyncHolder", e);
182         }
183     }
184
185     /**
186      * Returns the creation timestamp
187      *
188      * @return the creation timestamp
189      */

190     public long getCreationTimestamp() {
191         return creationTimestamp;
192     }
193
194     /**
195      * Returns the newHolder flag
196      *
197      * @return the newHolder flag
198      */

199     public boolean isNew() {
200         return newHolder;
201     }
202
203     // --------------------------------------------------------- Private methods
204

205     /**
206      * Returns the SyncBean instance to use for remote calls. Note that field
207      * <i>syncBean</i> can be null if the bean has never been used or this
208      * instance comes from a deserialization process.<br>
209      * If <i>syncBean</i> is null then if <i>syncHandle</i> is not null, it is
210      * used to get the EJB instance. If also </i>syncHandle</i> is null, the
211      * bean is created.
212      *
213      * @return the SyncServerBean EJB instance
214      *
215      * @throws ServerFailureException in case of error
216      */

217     private SyncRemote getSyncServerBeanInstance()
218     throws ServerFailureException {
219         if (syncBean != null) {
220             return syncBean;
221         }
222
223         if (syncHandle != null) {
224             try {
225                 return syncBean = (SyncRemote)syncHandle.getEJBObject();
226             } catch (RemoteException JavaDoc e) {
227                 throw new ServerFailureException(e);
228             }
229         }
230
231         //
232
// Never used before.... create a new EJB instance from scratch
233
// NOTE: session Id cannot be empty
234
//
235
if ((sessionId == null) || (sessionId.length() == 0)) {
236             throw new ServerFailureException("No session id is associated to this handler");
237         }
238
239         SyncHomeRemote home;
240         InitialContext ctx;
241
242          try {
243              Hashtable JavaDoc env = new Hashtable JavaDoc();
244
245              env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY );
246              env.put(Context.URL_PKG_PREFIXES, CONTEXT_URL_PREFIXES);
247              env.put(Context.PROVIDER_URL, jndiAddress );
248
249              ctx = new InitialContext(env);
250              home = (SyncHomeRemote)ctx.lookup(SYNCEJB_HOME_JNDI_NAME);;
251              syncBean = home.create(sessionId);
252              syncHandle = syncBean.getHandle();
253         } catch (Exception JavaDoc e) {
254             throw new ServerFailureException(e);
255         }
256
257         return syncBean;
258     }
259
260 }
261
Popular Tags