Source Code Cross Referenced for ServiceBrokerImpl.java in  » Net » Coadunation_1.0.1 » com » rift » coad » daemon » servicebroker » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Coadunation_1.0.1 » com.rift.coad.daemon.servicebroker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.