KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > producer > impl > PersistentStateManagerImpl


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  *
5  * Created on 14 janv. 2004
6  */

7 package org.exoplatform.services.wsrp.producer.impl;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import net.sf.hibernate.Hibernate;
15 import net.sf.hibernate.Session;
16
17 import org.apache.commons.logging.Log;
18 import org.exoplatform.commons.utils.IOUtil;
19 import org.exoplatform.services.cache.CacheService;
20 import org.exoplatform.services.cache.ExoCache;
21 import org.exoplatform.services.database.DatabaseService;
22 import org.exoplatform.services.database.HibernateService;
23 import org.exoplatform.services.log.LogService;
24 import org.exoplatform.services.wsrp.exceptions.Faults;
25 import org.exoplatform.services.wsrp.exceptions.WSRPException;
26 import org.exoplatform.services.wsrp.producer.PersistentStateManager;
27 import org.exoplatform.services.wsrp.producer.impl.helpers.ConsumerContext;
28 import org.exoplatform.services.wsrp.type.RegistrationContext;
29 import org.exoplatform.services.wsrp.type.RegistrationData;
30
31
32 /**
33  * @author Mestrallet Benjamin
34  * benjmestrallet@users.sourceforge.net
35  */

36 public class PersistentStateManagerImpl implements PersistentStateManager {
37
38     private static String JavaDoc[] MAPPING =
39     {
40             "org/exoplatform/services/wsrp/producer/impl/StateData.hbm.xml"
41     };
42     
43     private static final String JavaDoc queryStateData =
44         "from sd in class org.exoplatform.services.wsrp.producer.impl.StateData " +
45         "where sd.id = ?";
46
47
48   //private Map mapToStoreRenderParameters;
49
private WSRPConfiguration conf;
50   private Log log;
51   private ExoCache cache;
52   private HibernateService hservice;
53
54   public PersistentStateManagerImpl(CacheService cacheService, LogService logService,
55                                     DatabaseService dbService, HibernateService hservice,
56                                     WSRPConfiguration conf) throws Exception JavaDoc {
57     this.conf = conf;
58     this.hservice = hservice;
59     hservice.addMappingFiles(MAPPING) ;
60     this.log = logService.getLog("org.exoplatform.services.wsrp");
61     this.cache = cacheService.getCacheInstance(getClass().getName());
62     //checkDatabase(dbService);
63
}
64
65   public RegistrationData getRegistrationData(RegistrationContext registrationContext)
66   throws WSRPException {
67     if (conf.isSaveRegistrationStateOnConsumer()) {
68         log.debug("Lookup registration stored on the consumer");
69         return resolveConsumerContext(registrationContext);
70     }
71     log.debug("Lookup registration data stored on the producer");
72     try {
73         StateData sD = load(registrationContext.getRegistrationHandle());
74         if (sD == null) {
75             return null;
76         }
77         return ((ConsumerContext) sD.getDataObject()).
78             getRegistationData();
79     } catch (Exception JavaDoc e) {
80         log.error("Can not extract Registration data from persistent store");
81         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
82     }
83   }
84
85   public byte[] register(String JavaDoc registrationHandle, RegistrationData data) throws WSRPException {
86     ConsumerContext cC = new ConsumerContext(registrationHandle, data);
87     if (conf.isSaveRegistrationStateOnConsumer()) {
88       log.debug("Register and send the registration state to the consumer");
89       try {
90         byte[] bytes = IOUtil.serialize(data);
91         try {
92           save(registrationHandle, "java.util.Collection", new ArrayList JavaDoc());
93         } catch (Exception JavaDoc e) {
94           log.error("Persistence error");
95           throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
96         }
97         return bytes;
98       } catch (Exception JavaDoc e) {
99         log.error("Can not serialize ConsumerContext", e);
100         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
101       }
102     }
103     log.debug("Register and save the registration state in the producer");
104     try {
105         save(registrationHandle, "org.exoplatform.services.wsrp.producer.impl.helpers.ConsumerContext", cC);
106     } catch (Exception JavaDoc e) {
107         log.error("Persistence error");
108         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
109     }
110     return null;
111   }
112
113   public void deregister(RegistrationContext registrationContext) throws WSRPException {
114     try {
115       if (!conf.isSaveRegistrationStateOnConsumer()) {
116         log.debug("Deregister the consumer (state save on producer)");
117         remove(registrationContext.getRegistrationHandle());
118       } else {
119         log.debug("Deregister the consumer (state save on consumer)");
120         remove(registrationContext.getRegistrationHandle());
121       }
122     } catch (Exception JavaDoc e) {
123       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
124     }
125   }
126
127   public boolean isRegistered(RegistrationContext registrationContext) throws WSRPException {
128     log.debug("Look up from a registration stored");
129     try {
130       StateData sD = load(registrationContext.getRegistrationHandle());
131       if (sD == null) {
132         return false;
133       }
134       if (sD.getDataObject() != null) {
135         return true;
136       }
137     } catch (Exception JavaDoc e) {
138       log.error("Can not extract Registration data from persistent store");
139       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
140     }
141     log.debug("Look up failed");
142     return false;
143   }
144
145   public boolean isConsumerConfiguredPortlet(String JavaDoc portletHandle,
146                                              RegistrationContext registrationContext)
147       throws WSRPException {
148     if (conf.isSaveRegistrationStateOnConsumer()) {
149       Collection JavaDoc c = null;
150       try {
151         StateData sD = load(registrationContext.getRegistrationHandle());
152         if (sD == null) {
153           return false;
154         }
155         c = (Collection JavaDoc) sD.getDataObject();
156       } catch (Exception JavaDoc e) {
157         log.error("Can not extract Registration data from persistent store", e);
158         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
159       }
160       if (c.contains(portletHandle)) {
161         return true;
162       }
163       return false;
164     }
165     
166     ConsumerContext consumerContext = null;
167     try {
168         StateData sD = load(registrationContext.getRegistrationHandle());
169         if (sD == null) {
170             return false;
171         }
172         consumerContext = (ConsumerContext) sD.getDataObject();
173     } catch (Exception JavaDoc e) {
174         log.error("Can not extract Registration data from persistent store");
175         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
176     }
177     return consumerContext.isPortletHandleRegistered(portletHandle);
178   }
179
180   public void addConsumerConfiguredPortletHandle(String JavaDoc portletHandle,
181                                                  RegistrationContext registrationContext)
182       throws WSRPException {
183     if (conf.isSaveRegistrationStateOnConsumer()) {
184       Collection JavaDoc c = null;
185       try {
186         StateData sD = load(registrationContext.getRegistrationHandle());
187         if (sD == null) {
188           return;
189         }
190         c = (Collection JavaDoc) sD.getDataObject();
191       } catch (Exception JavaDoc e) {
192         log.error("Can not extract Registration data from persistent store");
193         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
194       }
195       c.add(portletHandle);
196     } else {
197       ConsumerContext consumerContext = null;
198       try {
199         StateData sD = load(registrationContext.getRegistrationHandle());
200         if (sD == null) {
201           return;
202         }
203         consumerContext = (ConsumerContext) sD.getDataObject();
204       } catch (Exception JavaDoc e) {
205         log.error("Can not extract Registration data from persistent store");
206         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
207       }
208       consumerContext.addPortletHandle(portletHandle);
209     }
210   }
211
212   public void removeConsumerConfiguredPortletHandle(String JavaDoc portletHandle,
213                                                     RegistrationContext registrationContext) throws WSRPException {
214     if (conf.isSaveRegistrationStateOnConsumer()) {
215       Collection JavaDoc c = null;
216       try {
217         StateData sD = load(registrationContext.getRegistrationHandle());
218         if (sD == null) {
219           return;
220         }
221         c = (Collection JavaDoc) sD.getDataObject();
222       } catch (Exception JavaDoc e) {
223         log.error("Can not extract Registration data from persistent store");
224         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
225       }
226       c.remove(portletHandle);
227     } else {
228       ConsumerContext consumerContext = null;
229       try {
230         StateData sD = load(registrationContext.getRegistrationHandle());
231         if (sD == null) {
232           return;
233         }
234         consumerContext = (ConsumerContext) sD.getDataObject();
235       } catch (Exception JavaDoc e) {
236         log.error("Can not extract Registration data from persistent store");
237         throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
238       }
239       consumerContext.removePortletHandle(portletHandle);
240     }
241
242   }
243
244   public Map JavaDoc getNavigationalSate(String JavaDoc navigationalState) throws WSRPException {
245     try {
246       StateData sD = load(navigationalState);
247       if (sD == null) {
248         return null;
249       }
250       return (Map JavaDoc) sD.getDataObject();
251     } catch (Exception JavaDoc e) {
252       log.error("Can not extract Render Parameters Map from persistent store", e);
253       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
254     }
255   }
256
257   public void putNavigationalState(String JavaDoc ns, Map JavaDoc renderParameters) throws WSRPException {
258     try {
259       save(ns, "java.util.Map", renderParameters);
260     } catch (Exception JavaDoc e) {
261       log.error("Can not save Render Parameters Map from persistent store", e);
262       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
263     }
264
265   }
266
267   private RegistrationData resolveConsumerContext(RegistrationContext registrationContext)
268       throws WSRPException {
269     byte[] registrationState = registrationContext.getRegistrationState();
270     if (registrationState == null) {
271       throw new WSRPException(Faults.MISSING_PARAMETERS_FAULT);
272     }
273     Object JavaDoc o = null;
274     try {
275       o = IOUtil.deserialize(registrationState);
276     } catch (Exception JavaDoc e) {
277       log.error("Can not deserialize the RegistrationData object sent by the consumer");
278       throw new WSRPException(Faults.OPERATION_FAILED_FAULT, e);
279     }
280     if (o instanceof RegistrationData) {
281       return (RegistrationData) o;
282     }
283     log.error("The registration state is not of type RegistrationData");
284     throw new WSRPException(Faults.OPERATION_FAILED_FAULT);
285   }
286
287   final public void save(String JavaDoc key, String JavaDoc type, Object JavaDoc o) throws Exception JavaDoc {
288     Session session = this.hservice.openSession();
289     StateData data = load(key);
290     if (data == null) {
291         data = new StateData();
292         data.setId(key);
293         data.setDataType(type);
294         this.cache.put(key, data);
295     }
296     data.setDataObject(o);
297     session.save(data);
298     session.flush();
299   }
300
301   final public StateData load(String JavaDoc key) throws Exception JavaDoc {
302     StateData data = (StateData) this.cache.get(key);
303     if (data == null) {
304         Session session = this.hservice.openSession();
305         List JavaDoc l = session.find(queryStateData, key, Hibernate.STRING);
306         if (l.size() > 1) {
307             throw new Exception JavaDoc("Expect only one configuration but found" + l.size());
308         } else if (l.size() == 1) {
309             data = (StateData) l.get(0);
310             this.cache.put(key, data);
311         }
312     }
313     return data;
314   }
315
316   final public void remove(String JavaDoc key) throws Exception JavaDoc {
317     Session session = this.hservice.openSession();
318     StateData data = (StateData) this.cache.remove(key);
319     if (data == null) {
320         List JavaDoc l = session.find(queryStateData, key, Hibernate.STRING);
321         if (l.size() > 1) {
322             throw new Exception JavaDoc("Expect only one configuration but found" + l.size());
323         } else if (l.size() == 1) {
324             data = (StateData) l.get(0);
325             this.cache.put(key, data);
326         }
327     }
328     if (data != null) {
329         session.delete(data);
330         session.flush();
331     }
332   }
333 }
Popular Tags