KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > consumer > impl > ProducerRegistryImpl


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

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 JavaDoc;
20
21 /**
22  * @author Mestrallet Benjamin
23  * benjmestrallet@users.sourceforge.net
24  * Date: 2 févr. 2004
25  * Time: 23:04:48
26  */

27 public class ProducerRegistryImpl implements ProducerRegistry {
28
29   private static String JavaDoc[] MAPPING =
30     {
31       "org/exoplatform/services/wsrp/consumer/impl/ProducerData.hbm.xml"
32     };
33
34   private static final String JavaDoc queryAllProducer =
35       "from pd in class org.exoplatform.services.wsrp.consumer.impl.ProducerData";
36   private static final String JavaDoc 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     //checkDatabase(dbService);
52
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 JavaDoc 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 JavaDoc e) {
77       e.printStackTrace();
78     }
79   }
80
81   public Producer getProducer(String JavaDoc 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 JavaDoc 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 JavaDoc e) {
97       e.printStackTrace();
98     }
99     return null;
100   }
101
102   public void removeAllProducers() throws Exception JavaDoc {
103     removeAll() ;
104     producers.clear();
105     lastModifiedTime_ = System.currentTimeMillis();
106   }
107
108   public boolean existsProducer(String JavaDoc 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 JavaDoc {
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 JavaDoc {
136     Session session = hservice_.openSession();
137     return session.find(queryAllProducer);
138   }
139
140
141   final public ProducerData load(String JavaDoc id) throws Exception JavaDoc {
142     Session session = hservice_.openSession();
143     ProducerData data = load(id, session);
144     return data;
145   }
146
147   final public ProducerData load(String JavaDoc id, Session session) throws Exception JavaDoc {
148     ProducerData data = null;
149     List l = session.find(queryProducer, id, Hibernate.STRING);
150     if (l.size() > 1) {
151       throw new Exception JavaDoc("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 JavaDoc id) throws Exception JavaDoc {
159     Session session = hservice_.openSession();
160     session.delete(queryProducer, id, Hibernate.STRING);
161     session.flush();
162   }
163
164   final public void removeAll() throws Exception JavaDoc {
165     Session session = hservice_.openSession();
166     session.delete(queryAllProducer);
167     session.flush();
168   }
169 }
170
Popular Tags