KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > servicebroker > ServiceBrokerImpl


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ServiceBrokerImpl.java
20  */

21
22 package com.rift.coad.daemon.servicebroker;
23
24 // local imports
25
import com.rift.coad.daemon.servicebroker.db.Service;
26 import com.rift.coad.daemon.servicebroker.db.ServicePK;
27 //import com.rift.coad.daemon.servicebroker.db.util.HibernateUtil;
28
import com.rift.coad.hibernate.util.HibernateUtil;
29 import com.rift.coad.lib.bean.BeanRunnable;
30 import com.rift.coad.lib.configuration.ConfigurationException;
31 import com.rift.coad.lib.naming.NamingDirector;
32 import com.rift.coad.lib.naming.NamingConstants;
33 import com.rift.coad.lib.thread.ThreadStateMonitor;
34
35 // java imports
36
import java.rmi.RemoteException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
41 import javax.naming.Context JavaDoc;
42 import javax.naming.InitialContext JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44 import javax.rmi.PortableRemoteObject JavaDoc;
45 import javax.transaction.Status JavaDoc;
46 import javax.transaction.UserTransaction JavaDoc;
47
48 // log4j import
49
import org.apache.log4j.Logger;
50
51 // hibernate import
52
import org.hibernate.Session;
53
54 /**
55  *
56  * @author Glynn Chaldecott
57  */

58 public class ServiceBrokerImpl implements ServiceBroker, BeanRunnable {
59     
60     // the class log variable
61
protected Logger log =
62             Logger.getLogger(ServiceBrokerImpl.class.getName());
63     
64     private Context JavaDoc ctx = null;
65     
66     private ThreadStateMonitor state = null;
67     
68     private ConcurrentLinkedQueue JavaDoc serviceQueue = new ConcurrentLinkedQueue JavaDoc();
69     
70     /** Creates a new instance of ServiceBrokerImpl */
71     public ServiceBrokerImpl() throws NamingException JavaDoc, ServiceBrokerException {
72         ctx = new InitialContext JavaDoc();
73         try {
74             com.rift.coad.lib.configuration.Configuration coadConfig =
75                         com.rift.coad.lib.configuration.ConfigurationFactory.
76                         getInstance().getConfig(com.rift.coad.daemon.servicebroker.
77
                        ServiceBrokerImpl.class);
78             state = new ThreadStateMonitor(coadConfig.getLong("sleep_time"));
79         } catch (ConfigurationException ex) {
80             ex.printStackTrace();
81         }
82     }
83     
84     /**
85      *
86      * @param JNDI This is the JNDI of the daemon that a service will be
87      * linked to.
88      * @param services This is a list of the services that a daemon can perform.
89      */

90     public void registerService(String JavaDoc JNDI, List JavaDoc services) throws
91             RemoteException JavaDoc, ServiceBrokerException {
92         boolean ownTransaction = false;
93         
94         try {
95             if (!NamingDirector.getInstance().isPrimary()) {
96                 sendToParent(JNDI, services);
97             }
98             Session session = HibernateUtil
99                         .getInstance(com.rift.coad.daemon.servicebroker.
100
                                ServiceBrokerImpl.class)
101                         .getSession();
102             for (int i = 0; i < services.size(); i ++) {
103                 String JavaDoc temp = (String JavaDoc) services.get(i);
104                 List JavaDoc list = session.createSQLQuery("SELECT * FROM Service WHERE"
105                         + " jndi = '" + JNDI + "' AND service = '"
106                         + temp + "'").list();
107                 if (list.size() == 0) {
108                     ServicePK servicePK = new ServicePK(JNDI,temp);
109                     Service service = new Service(servicePK,0);
110                     session.save(service);
111                 }
112             }
113         } catch (Exception JavaDoc ex) {
114             log.error("Failed to register service :"
115                     + ex.getMessage(),ex);
116             throw new ServiceBrokerException("Failed to register service :"
117                     + ex.getMessage());
118         }
119     }
120     
121     private void sendToParent(String JavaDoc JNDI, List JavaDoc services) {
122         try {
123             Object JavaDoc obj = ctx.lookup(NamingDirector.getInstance().
124                     getPrimaryJNDIUrl() + "/ServiceBroker");
125             com.rift.coad.daemon.servicebroker.ServiceBroker
126                     beanInterface = (com.rift.coad.daemon.servicebroker
127
                    .ServiceBroker)
128                     PortableRemoteObject.narrow(obj,com.rift.coad.
129
                    daemon.servicebroker.ServiceBroker.class);
130             beanInterface.registerService(
131                     NamingDirector.getInstance().getJNDIBase() + "/" + JNDI,
132                     services);
133         } catch(Exception JavaDoc ex) {
134             log.error("Failed to pass service to parent:"
135                     + ex.getMessage(),ex);
136             List JavaDoc temp = new ArrayList JavaDoc();
137             temp.add(JNDI);
138             temp.add(services);
139             serviceQueue.offer(temp);
140         }
141     }
142     
143     /**
144      *
145      * @param services This is a list of the services you wish to access.
146      * @return This method will return a String containing the JNDI of the
147      * Daemon linked to the service.
148      */

149     public String JavaDoc getServiceProvider(List JavaDoc services) throws
150             RemoteException JavaDoc, ServiceBrokerException {
151         boolean ownTransaction = false;
152         String JavaDoc returnValue = "";
153         try {
154             
155             Session session = HibernateUtil
156                         .getInstance(com.rift.coad.daemon.servicebroker.
157
                                ServiceBrokerImpl.class)
158                         .getSession();
159             
160             String JavaDoc query = "SELECT DISTINCT jndi,service,counter FROM Service" +
161                     " WHERE ";
162             String JavaDoc orVal = "";
163             
164             for (int i = 0; i < services.size(); i ++) {
165                 query += orVal + "service = '" + (String JavaDoc) services.get(i) + "' ";
166                 orVal = "or ";
167             }
168             
169             List JavaDoc list = session.createSQLQuery(query + "ORDER BY counter ASC")
170             .list();
171             
172             Object JavaDoc[] row = (Object JavaDoc[]) list.get(0);
173             String JavaDoc JNDI = (String JavaDoc) row[0];
174             String JavaDoc service = (String JavaDoc) row[1];
175             int counter = Integer.parseInt(row[2].toString());
176             counter ++;
177             
178             int update = session.createSQLQuery("UPDATE Service SET counter = "
179                     + counter + " WHERE jndi = '" + JNDI + "' AND service = '"
180                     + service + "'").executeUpdate();
181             
182             returnValue = JNDI;
183             
184             
185         } catch (Exception JavaDoc ex) {
186             log.error("Failed to retrieve single service :"
187                     + ex.getMessage(),ex);
188             throw new ServiceBrokerException("Failed to retrieve single " +
189                     "service :" + ex.getMessage());
190         }
191         return returnValue;
192     }
193     
194     /**
195      *
196      * @param services This is a List of the services' JNDI's you wish to
197      * retrieve from the database.
198      * @return This method returns the JNDI for multiple daemons.
199      */

200     public List JavaDoc getServiceProviders(List JavaDoc services) throws
201             RemoteException JavaDoc, ServiceBrokerException {
202         boolean ownTransaction = false;
203         List JavaDoc returnValue = null;
204         try {
205             
206             Session session = HibernateUtil
207                         .getInstance(com.rift.coad.daemon.servicebroker.
208
                                ServiceBrokerImpl.class)
209                         .getSession();
210             
211             String JavaDoc query = "SELECT DISTINCT jndi FROM Service WHERE ";
212             String JavaDoc orVal = "";
213             
214             for (int i = 0; i < services.size(); i ++) {
215                 query += orVal + "service = '" + (String JavaDoc) services.get(i) + "' ";
216                 orVal = "or ";
217             }
218             
219             returnValue = session.createSQLQuery(query).list();
220             
221             
222         } catch (Exception JavaDoc ex) {
223             log.error("Failed to retrieve multiple services :"
224                     + ex.getMessage(),ex);
225             throw new ServiceBrokerException("Failed to retrieve multiple" +
226                     " services :" + ex.getMessage());
227         }
228         return returnValue;
229     }
230     
231     /**
232      *
233      *
234      * @param JNDI This is the JNDI of the service you want to delete.
235      * @param services This is a List containing the services linked to the
236      * daemon you wish to delete.
237      */

238     public void removeServiceProviders(String JavaDoc JNDI, List JavaDoc services) throws
239             RemoteException JavaDoc, ServiceBrokerException {
240         UserTransaction JavaDoc ut = null;
241         boolean ownTransaction = false;
242         try {
243             
244             Session session = HibernateUtil
245                         .getInstance(com.rift.coad.daemon.servicebroker.
246
                                ServiceBrokerImpl.class)
247                         .getSession();
248             
249             for (int i = 0; i < services.size(); i ++) {
250                 String JavaDoc temp = (String JavaDoc) services.get(i);
251                 session.createQuery("DELETE FROM Service as ser WHERE " +
252                         "ser.comp_id.service = ? AND ser.comp_id.jndi = ?")
253                         .setString(0,temp).setString(1,JNDI).executeUpdate();
254             }
255             
256             
257         } catch (Exception JavaDoc ex) {
258             log.error("Failed to delete the service :"
259                     + ex.getMessage(),ex);
260             throw new ServiceBrokerException("Failed to delete the service :"
261                     + ex.getMessage());
262         }
263     }
264     
265     
266     /**
267      * This method is run to register services with the parent.
268      */

269     public void process() {
270         while (!state.isTerminated()) {
271             List JavaDoc temp = (List JavaDoc) serviceQueue.poll();
272             if (temp != null) {
273                 String JavaDoc JNDI = (String JavaDoc) temp.get(0);
274                 List JavaDoc services = (List JavaDoc) temp.get(1);
275                 try {
276                     sendToParent(JNDI, services);
277                 } catch (Exception JavaDoc ex) {
278                     log.error("There was an error registering to the " +
279                             "parent:" + ex.getMessage(),ex);
280                 }
281             }
282             state.monitor();
283         }
284     }
285
286     public void terminate() {
287         state.terminate(true);
288     }
289     
290     
291 }
292
Popular Tags