1 5 6 package org.exoplatform.services.wsrp.consumer.impl; 7 8 import org.apache.commons.logging.Log; 9 import org.exoplatform.services.database.DatabaseService; 10 import org.exoplatform.services.database.HibernateService; 11 import org.exoplatform.services.log.LogService; 12 import org.exoplatform.services.wsrp.consumer.Producer; 13 import org.exoplatform.services.wsrp.consumer.ProducerRegistry; 14 15 import net.sf.hibernate.Session; 16 import net.sf.hibernate.Hibernate; 17 18 import java.util.*; 19 import java.net.URL ; 20 21 27 public class ProducerRegistryImpl implements ProducerRegistry { 28 29 private static String [] MAPPING = 30 { 31 "org/exoplatform/services/wsrp/consumer/impl/ProducerData.hbm.xml" 32 }; 33 34 private static final String queryAllProducer = 35 "from pd in class org.exoplatform.services.wsrp.consumer.impl.ProducerData"; 36 private static final String queryProducer = 37 "from pd in class org.exoplatform.services.wsrp.consumer.impl.ProducerData " + 38 "where pd.id = ?"; 39 40 private long lastModifiedTime_; 41 private Map producers; 42 private HibernateService hservice_; 43 private Log log_; 44 45 public ProducerRegistryImpl(DatabaseService dbService, 46 HibernateService hservice, 47 LogService lservice) { 48 hservice_ = hservice; 49 log_ = lservice.getLog("org.exoplatform.services.wsrp"); 50 hservice.addMappingFiles(MAPPING) ; 51 producers = loadProducers(); 53 lastModifiedTime_ = System.currentTimeMillis(); 54 } 55 56 private Map loadProducers() { 57 Map map = new HashMap(); 58 try { 59 Collection c = loadAll(); 60 for (Iterator iterator = c.iterator(); iterator.hasNext();) { 61 ProducerData producerData = (ProducerData) iterator.next(); 62 ((ProducerImpl)producerData.getProducer()).init(); 63 map.put(producerData.getId(), producerData.getProducer()); 64 } 65 } catch (Exception e) { 66 log_.error("Error", e) ; 67 } 68 return map; 69 } 70 71 public void addProducer(Producer producer) { 72 try { 73 save(producer); 74 producers.put(producer.getID(), producer); 75 lastModifiedTime_ = System.currentTimeMillis(); 76 } catch (Exception e) { 77 e.printStackTrace(); 78 } 79 } 80 81 public Producer getProducer(String id) { 82 return (Producer) producers.get(id); 83 } 84 85 public Iterator getAllProducers() { 86 return producers.values().iterator(); 87 } 88 89 public Producer removeProducer(String id) { 90 try { 91 remove(id); 92 producers.remove(id); 93 lastModifiedTime_ = System.currentTimeMillis(); 94 Producer producer = (Producer) producers.get(id); 95 return producer; 96 } catch (Exception e) { 97 e.printStackTrace(); 98 } 99 return null; 100 } 101 102 public void removeAllProducers() throws Exception { 103 removeAll() ; 104 producers.clear(); 105 lastModifiedTime_ = System.currentTimeMillis(); 106 } 107 108 public boolean existsProducer(String id) { 109 return producers.containsKey(id); 110 } 111 112 public Producer createProducerInstance() { 113 return new ProducerImpl(); 114 } 115 116 public long getLastModifiedTime() { 117 return lastModifiedTime_; 118 } 119 120 final public void save(Producer p) throws Exception { 121 Session session = hservice_.openSession(); 122 ProducerData data = load(p.getID()); 123 if (data == null) { 124 data = new ProducerData(); 125 data.setId(p.getID()); 126 data.setProducer(p); 127 session.save(data); 128 } else { 129 data.setProducer(p); 130 session.update(data); 131 } 132 session.flush(); 133 } 134 135 final public Collection loadAll() throws Exception { 136 Session session = hservice_.openSession(); 137 return session.find(queryAllProducer); 138 } 139 140 141 final public ProducerData load(String id) throws Exception { 142 Session session = hservice_.openSession(); 143 ProducerData data = load(id, session); 144 return data; 145 } 146 147 final public ProducerData load(String id, Session session) throws Exception { 148 ProducerData data = null; 149 List l = session.find(queryProducer, id, Hibernate.STRING); 150 if (l.size() > 1) { 151 throw new Exception ("Expect only one configuration but found" + l.size()); 152 } else if (l.size() == 1) { 153 data = (ProducerData) l.get(0); 154 } 155 return data; 156 } 157 158 final public void remove(String id) throws Exception { 159 Session session = hservice_.openSession(); 160 session.delete(queryProducer, id, Hibernate.STRING); 161 session.flush(); 162 } 163 164 final public void removeAll() throws Exception { 165 Session session = hservice_.openSession(); 166 session.delete(queryAllProducer); 167 session.flush(); 168 } 169 } 170 | Popular Tags |