Source Code Cross Referenced for Background.java in  » Net » GNetWatch » net » fenyo » gnetwatch » activities » 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 » GNetWatch » net.fenyo.gnetwatch.activities 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * GNetWatch
003:         * Copyright 2006, 2007 Alexandre Fenyo
004:         * gnetwatch@fenyo.net
005:         *
006:         * This file is part of GNetWatch.
007:         *
008:         * GNetWatch is free software; you can redistribute it and/or modify
009:         * it under the terms of the GNU General Public License as published by
010:         * the Free Software Foundation; either version 2 of the License, or
011:         * (at your option) any later version.
012:         *
013:         * GNetWatch is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         * GNU General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public License
019:         * along with GNetWatch; if not, write to the Free Software
020:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
021:         */
022:
023:        package net.fenyo.gnetwatch.activities;
024:
025:        import net.fenyo.gnetwatch.*;
026:        import net.fenyo.gnetwatch.GUI.VisualElement;
027:        import net.fenyo.gnetwatch.actions.Action;
028:        import net.fenyo.gnetwatch.targets.Target;
029:
030:        import java.util.*;
031:        import java.io.*;
032:
033:        import org.apache.commons.logging.Log;
034:        import org.apache.commons.logging.LogFactory;
035:
036:        // every queues must be created at startup, no addition after.
037:
038:        /**
039:         * This class creates queues at startup and manages them through a background thread:
040:         * long active actions in a queue are interrupted after a timeout.
041:         * @author Alexandre Fenyo
042:         * @version $Id: Background.java,v 1.24 2007/03/12 05:04:15 fenyo Exp $
043:         */
044:
045:        public class Background implements  Runnable {
046:            private static Log log = LogFactory.getLog(Background.class);
047:
048:            private final Config config;
049:
050:            private Thread interrupt_thread = null;
051:
052:            // gérer des queues (loops) qui sont chacune associée à un thread particulier,
053:            // le nombre de loops est dynamique
054:            // many threads access to queues
055:            private final Map<String, Queue> queues = Collections
056:                    .synchronizedMap(new HashMap<String, Queue>());
057:
058:            /**
059:             * Constructor.
060:             * main thread.
061:             * @param config configuration.
062:             */
063:            public Background(final Config config) {
064:                this .config = config;
065:
066:                // create default queues
067:                //    queues.put("standard", new Queue("standard", config));
068:                //    queues.put("debug", new DebugQueue("debug", config));
069:                for (int cnt = 1; cnt <= new Integer(config
070:                        .getProperty("queues.count.icmp")); cnt++)
071:                    queues.put("icmp-" + cnt, new PingQueue("icmp-" + cnt,
072:                            config));
073:                for (int cnt = 1; cnt <= new Integer(config
074:                        .getProperty("queues.count.snmp")); cnt++)
075:                    queues.put("snmp-" + cnt, new SNMPQueue("snmp-" + cnt,
076:                            config));
077:                for (int cnt = 1; cnt <= new Integer(config
078:                        .getProperty("queues.count.flood")); cnt++)
079:                    queues.put("flood-" + cnt, new FloodQueue("flood-" + cnt,
080:                            config));
081:                for (int cnt = 1; cnt <= new Integer(config
082:                        .getProperty("queues.count.http")); cnt++)
083:                    queues.put("http-" + cnt, new HTTPQueue("http-" + cnt,
084:                            config));
085:                for (int cnt = 1; cnt <= new Integer(config
086:                        .getProperty("queues.count.nmap")); cnt++)
087:                    queues.put("nmap-" + cnt, new NmapQueue("nmap-" + cnt,
088:                            config));
089:                // never add nor remove queues after this constructor returns,
090:                // since we may iterate on queues in run() 
091:            }
092:
093:            /**
094:             * Terminates background threads.
095:             * @param none.
096:             * @return void.
097:             * @throws InterruptedException exception.
098:             */
099:            // main thread
100:            public void end() throws InterruptedException {
101:                // terminate each background queue thread
102:                for (final Queue queue : queues.values())
103:                    queue.end();
104:
105:                // terminate the background interrupt thread
106:                interrupt_thread.interrupt();
107:                interrupt_thread.join();
108:            }
109:
110:            /**
111:             * Returns the list of background queues.
112:             * @param none.
113:             * @return Map<String, Queue> list of queues.
114:             */
115:            // main & GUI thread
116:            public Map<String, Queue> getQueues() {
117:                return queues;
118:            }
119:
120:            /**
121:             * Adds an action to a running queue.
122:             * @param action action to add.
123:             * @return void.
124:             * @throws GeneralException queue does not exist.
125:             */
126:            // GUI thread
127:            public void addActionQueue(final Action action)
128:                    throws GeneralException {
129:                if (!queues.containsKey(action.getQueueName() + "-1"))
130:                    throw new GeneralException("queue does not exist");
131:
132:                int smallest_queue = 1;
133:                int smallest_size = queues.get(action.getQueueName() + "-1")
134:                        .size();
135:                for (int cnt = 1; cnt <= new Integer(config
136:                        .getProperty("queues.count." + action.getQueueName())); cnt++) {
137:                    if (queues.get(action.getQueueName() + "-" + cnt).size() < smallest_size)
138:                        smallest_queue = cnt;
139:                }
140:                queues.get(action.getQueueName() + "-" + smallest_queue)
141:                        .addAction(action);
142:            }
143:
144:            /**
145:             * Removes an action from a running queue.
146:             * @param action action to remove.
147:             * @return void.
148:             * @throws GeneralException queue does not exist.
149:             */
150:            // GUI thread
151:            public void removeActionQueue(final Action action)
152:                    throws GeneralException {
153:                if (!queues.containsKey(action.getQueueName() + "-1"))
154:                    throw new GeneralException("queue does not exist");
155:
156:                for (int cnt = 1; cnt <= new Integer(config
157:                        .getProperty("queues.count." + action.getQueueName())); cnt++)
158:                    queues.get(action.getQueueName() + "-" + cnt).removeAction(
159:                            action);
160:            }
161:
162:            /**
163:             * Starts the background thread.
164:             * @param none.
165:             * @return void.
166:             */
167:            // main thread
168:            public void createBackgroundThread() {
169:                interrupt_thread = new Thread(this , "Interrupt Thread");
170:                interrupt_thread.start();
171:            }
172:
173:            /**
174:             * Interrupts long actions.
175:             * @param none.
176:             * @return void.
177:             */
178:            // Background thread
179:            public void run() {
180:                while (!config.isEnd())
181:                    try {
182:                        Thread.sleep(1000);
183:                        for (final Queue queue : queues.values())
184:                            if (queue.isExhausted()) {
185:                                queue.interrupt(Action.InterruptCause.timeout);
186:                            }
187:                    } catch (final InterruptedException ex) {
188:                        // this thread is interrupted when the application is terminated
189:                    } catch (final IOException ex) {
190:                        log.warn("Exception", ex);
191:                    }
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.