001: /*
002: * Timer: The timer class
003: * Copyright (C) 2006-2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ServiceBrokerImpl.java
020: */
021:
022: package com.rift.coad.daemon.servicebroker;
023:
024: // local imports
025: import com.rift.coad.daemon.servicebroker.db.Service;
026: import com.rift.coad.daemon.servicebroker.db.ServicePK; //import com.rift.coad.daemon.servicebroker.db.util.HibernateUtil;
027: import com.rift.coad.hibernate.util.HibernateUtil;
028: import com.rift.coad.lib.bean.BeanRunnable;
029: import com.rift.coad.lib.configuration.ConfigurationException;
030: import com.rift.coad.lib.naming.NamingDirector;
031: import com.rift.coad.lib.naming.NamingConstants;
032: import com.rift.coad.lib.thread.ThreadStateMonitor;
033:
034: // java imports
035: import java.rmi.RemoteException;
036: import java.util.ArrayList;
037: import java.util.Hashtable;
038: import java.util.List;
039: import java.util.concurrent.ConcurrentLinkedQueue;
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043: import javax.rmi.PortableRemoteObject;
044: import javax.transaction.Status;
045: import javax.transaction.UserTransaction;
046:
047: // log4j import
048: import org.apache.log4j.Logger;
049:
050: // hibernate import
051: import org.hibernate.Session;
052:
053: /**
054: *
055: * @author Glynn Chaldecott
056: */
057: public class ServiceBrokerImpl implements ServiceBroker, BeanRunnable {
058:
059: // the class log variable
060: protected Logger log = Logger.getLogger(ServiceBrokerImpl.class
061: .getName());
062:
063: private Context ctx = null;
064:
065: private ThreadStateMonitor state = null;
066:
067: private ConcurrentLinkedQueue serviceQueue = new ConcurrentLinkedQueue();
068:
069: /** Creates a new instance of ServiceBrokerImpl */
070: public ServiceBrokerImpl() throws NamingException,
071: ServiceBrokerException {
072: ctx = new InitialContext();
073: try {
074: com.rift.coad.lib.configuration.Configuration coadConfig = com.rift.coad.lib.configuration.ConfigurationFactory
075: .getInstance()
076: .getConfig(
077: com.rift.coad.daemon.servicebroker.ServiceBrokerImpl.class);
078: state = new ThreadStateMonitor(coadConfig
079: .getLong("sleep_time"));
080: } catch (ConfigurationException ex) {
081: ex.printStackTrace();
082: }
083: }
084:
085: /**
086: *
087: * @param JNDI This is the JNDI of the daemon that a service will be
088: * linked to.
089: * @param services This is a list of the services that a daemon can perform.
090: */
091: public void registerService(String JNDI, List services)
092: throws RemoteException, ServiceBrokerException {
093: boolean ownTransaction = false;
094:
095: try {
096: if (!NamingDirector.getInstance().isPrimary()) {
097: sendToParent(JNDI, services);
098: }
099: Session session = HibernateUtil
100: .getInstance(
101: com.rift.coad.daemon.servicebroker.ServiceBrokerImpl.class)
102: .getSession();
103: for (int i = 0; i < services.size(); i++) {
104: String temp = (String) services.get(i);
105: List list = session.createSQLQuery(
106: "SELECT * FROM Service WHERE" + " jndi = '"
107: + JNDI + "' AND service = '" + temp
108: + "'").list();
109: if (list.size() == 0) {
110: ServicePK servicePK = new ServicePK(JNDI, temp);
111: Service service = new Service(servicePK, 0);
112: session.save(service);
113: }
114: }
115: } catch (Exception ex) {
116: log.error("Failed to register service :" + ex.getMessage(),
117: ex);
118: throw new ServiceBrokerException(
119: "Failed to register service :" + ex.getMessage());
120: }
121: }
122:
123: private void sendToParent(String JNDI, List services) {
124: try {
125: Object obj = ctx.lookup(NamingDirector.getInstance()
126: .getPrimaryJNDIUrl()
127: + "/ServiceBroker");
128: com.rift.coad.daemon.servicebroker.ServiceBroker beanInterface = (com.rift.coad.daemon.servicebroker.ServiceBroker) PortableRemoteObject
129: .narrow(
130: obj,
131: com.rift.coad.daemon.servicebroker.ServiceBroker.class);
132: beanInterface.registerService(NamingDirector.getInstance()
133: .getJNDIBase()
134: + "/" + JNDI, services);
135: } catch (Exception ex) {
136: log.error("Failed to pass service to parent:"
137: + ex.getMessage(), ex);
138: List temp = new ArrayList();
139: temp.add(JNDI);
140: temp.add(services);
141: serviceQueue.offer(temp);
142: }
143: }
144:
145: /**
146: *
147: * @param services This is a list of the services you wish to access.
148: * @return This method will return a String containing the JNDI of the
149: * Daemon linked to the service.
150: */
151: public String getServiceProvider(List services)
152: throws RemoteException, ServiceBrokerException {
153: boolean ownTransaction = false;
154: String returnValue = "";
155: try {
156:
157: Session session = HibernateUtil
158: .getInstance(
159: com.rift.coad.daemon.servicebroker.ServiceBrokerImpl.class)
160: .getSession();
161:
162: String query = "SELECT DISTINCT jndi,service,counter FROM Service"
163: + " WHERE ";
164: String orVal = "";
165:
166: for (int i = 0; i < services.size(); i++) {
167: query += orVal + "service = '"
168: + (String) services.get(i) + "' ";
169: orVal = "or ";
170: }
171:
172: List list = session.createSQLQuery(
173: query + "ORDER BY counter ASC").list();
174:
175: Object[] row = (Object[]) list.get(0);
176: String JNDI = (String) row[0];
177: String service = (String) row[1];
178: int counter = Integer.parseInt(row[2].toString());
179: counter++;
180:
181: int update = session.createSQLQuery(
182: "UPDATE Service SET counter = " + counter
183: + " WHERE jndi = '" + JNDI
184: + "' AND service = '" + service + "'")
185: .executeUpdate();
186:
187: returnValue = JNDI;
188:
189: } catch (Exception ex) {
190: log.error("Failed to retrieve single service :"
191: + ex.getMessage(), ex);
192: throw new ServiceBrokerException(
193: "Failed to retrieve single " + "service :"
194: + ex.getMessage());
195: }
196: return returnValue;
197: }
198:
199: /**
200: *
201: * @param services This is a List of the services' JNDI's you wish to
202: * retrieve from the database.
203: * @return This method returns the JNDI for multiple daemons.
204: */
205: public List getServiceProviders(List services)
206: throws RemoteException, ServiceBrokerException {
207: boolean ownTransaction = false;
208: List returnValue = null;
209: try {
210:
211: Session session = HibernateUtil
212: .getInstance(
213: com.rift.coad.daemon.servicebroker.ServiceBrokerImpl.class)
214: .getSession();
215:
216: String query = "SELECT DISTINCT jndi FROM Service WHERE ";
217: String orVal = "";
218:
219: for (int i = 0; i < services.size(); i++) {
220: query += orVal + "service = '"
221: + (String) services.get(i) + "' ";
222: orVal = "or ";
223: }
224:
225: returnValue = session.createSQLQuery(query).list();
226:
227: } catch (Exception ex) {
228: log.error("Failed to retrieve multiple services :"
229: + ex.getMessage(), ex);
230: throw new ServiceBrokerException(
231: "Failed to retrieve multiple" + " services :"
232: + ex.getMessage());
233: }
234: return returnValue;
235: }
236:
237: /**
238: *
239: *
240: * @param JNDI This is the JNDI of the service you want to delete.
241: * @param services This is a List containing the services linked to the
242: * daemon you wish to delete.
243: */
244: public void removeServiceProviders(String JNDI, List services)
245: throws RemoteException, ServiceBrokerException {
246: UserTransaction ut = null;
247: boolean ownTransaction = false;
248: try {
249:
250: Session session = HibernateUtil
251: .getInstance(
252: com.rift.coad.daemon.servicebroker.ServiceBrokerImpl.class)
253: .getSession();
254:
255: for (int i = 0; i < services.size(); i++) {
256: String temp = (String) services.get(i);
257: session
258: .createQuery(
259: "DELETE FROM Service as ser WHERE "
260: + "ser.comp_id.service = ? AND ser.comp_id.jndi = ?")
261: .setString(0, temp).setString(1, JNDI)
262: .executeUpdate();
263: }
264:
265: } catch (Exception ex) {
266: log.error("Failed to delete the service :"
267: + ex.getMessage(), ex);
268: throw new ServiceBrokerException(
269: "Failed to delete the service :" + ex.getMessage());
270: }
271: }
272:
273: /**
274: * This method is run to register services with the parent.
275: */
276: public void process() {
277: while (!state.isTerminated()) {
278: List temp = (List) serviceQueue.poll();
279: if (temp != null) {
280: String JNDI = (String) temp.get(0);
281: List services = (List) temp.get(1);
282: try {
283: sendToParent(JNDI, services);
284: } catch (Exception ex) {
285: log.error("There was an error registering to the "
286: + "parent:" + ex.getMessage(), ex);
287: }
288: }
289: state.monitor();
290: }
291: }
292:
293: public void terminate() {
294: state.terminate(true);
295: }
296:
297: }
|