1 21 22 package com.rift.coad.daemon.servicebroker; 23 24 import com.rift.coad.daemon.servicebroker.db.Service; 26 import com.rift.coad.daemon.servicebroker.db.ServicePK; 27 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 import java.rmi.RemoteException ; 37 import java.util.ArrayList ; 38 import java.util.Hashtable ; 39 import java.util.List ; 40 import java.util.concurrent.ConcurrentLinkedQueue ; 41 import javax.naming.Context ; 42 import javax.naming.InitialContext ; 43 import javax.naming.NamingException ; 44 import javax.rmi.PortableRemoteObject ; 45 import javax.transaction.Status ; 46 import javax.transaction.UserTransaction ; 47 48 import org.apache.log4j.Logger; 50 51 import org.hibernate.Session; 53 54 58 public class ServiceBrokerImpl implements ServiceBroker, BeanRunnable { 59 60 protected Logger log = 62 Logger.getLogger(ServiceBrokerImpl.class.getName()); 63 64 private Context ctx = null; 65 66 private ThreadStateMonitor state = null; 67 68 private ConcurrentLinkedQueue serviceQueue = new ConcurrentLinkedQueue (); 69 70 71 public ServiceBrokerImpl() throws NamingException , ServiceBrokerException { 72 ctx = new InitialContext (); 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 90 public void registerService(String JNDI, List services) throws 91 RemoteException , 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 temp = (String ) services.get(i); 104 List 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 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 JNDI, List services) { 122 try { 123 Object 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 ex) { 134 log.error("Failed to pass service to parent:" 135 + ex.getMessage(),ex); 136 List temp = new ArrayList (); 137 temp.add(JNDI); 138 temp.add(services); 139 serviceQueue.offer(temp); 140 } 141 } 142 143 149 public String getServiceProvider(List services) throws 150 RemoteException , ServiceBrokerException { 151 boolean ownTransaction = false; 152 String returnValue = ""; 153 try { 154 155 Session session = HibernateUtil 156 .getInstance(com.rift.coad.daemon.servicebroker. 157 ServiceBrokerImpl.class) 158 .getSession(); 159 160 String query = "SELECT DISTINCT jndi,service,counter FROM Service" + 161 " WHERE "; 162 String orVal = ""; 163 164 for (int i = 0; i < services.size(); i ++) { 165 query += orVal + "service = '" + (String ) services.get(i) + "' "; 166 orVal = "or "; 167 } 168 169 List list = session.createSQLQuery(query + "ORDER BY counter ASC") 170 .list(); 171 172 Object [] row = (Object []) list.get(0); 173 String JNDI = (String ) row[0]; 174 String service = (String ) 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 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 200 public List getServiceProviders(List services) throws 201 RemoteException , ServiceBrokerException { 202 boolean ownTransaction = false; 203 List returnValue = null; 204 try { 205 206 Session session = HibernateUtil 207 .getInstance(com.rift.coad.daemon.servicebroker. 208 ServiceBrokerImpl.class) 209 .getSession(); 210 211 String query = "SELECT DISTINCT jndi FROM Service WHERE "; 212 String orVal = ""; 213 214 for (int i = 0; i < services.size(); i ++) { 215 query += orVal + "service = '" + (String ) services.get(i) + "' "; 216 orVal = "or "; 217 } 218 219 returnValue = session.createSQLQuery(query).list(); 220 221 222 } catch (Exception 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 238 public void removeServiceProviders(String JNDI, List services) throws 239 RemoteException , ServiceBrokerException { 240 UserTransaction 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 temp = (String ) 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 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 269 public void process() { 270 while (!state.isTerminated()) { 271 List temp = (List ) serviceQueue.poll(); 272 if (temp != null) { 273 String JNDI = (String ) temp.get(0); 274 List services = (List ) temp.get(1); 275 try { 276 sendToParent(JNDI, services); 277 } catch (Exception 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 |