Source Code Cross Referenced for WorkThread.java in  » ESB » open-esb » com » sun » jbi » binding » file » framework » 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 » ESB » open esb » com.sun.jbi.binding.file.framework 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BEGIN_HEADER - DO NOT EDIT
003:         *
004:         * The contents of this file are subject to the terms
005:         * of the Common Development and Distribution License
006:         * (the "License").  You may not use this file except
007:         * in compliance with the License.
008:         *
009:         * You can obtain a copy of the license at
010:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011:         * See the License for the specific language governing
012:         * permissions and limitations under the License.
013:         *
014:         * When distributing Covered Code, include this CDDL
015:         * HEADER in each file and include the License file at
016:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017:         * If applicable add the following below this CDDL HEADER,
018:         * with the fields enclosed by brackets "[]" replaced with
019:         * your own identifying information: Portions Copyright
020:         * [year] [name of copyright owner]
021:         */
022:
023:        /*
024:         * @(#)WorkThread.java
025:         * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026:         *
027:         * END_HEADER - DO NOT EDIT
028:         */
029:        package com.sun.jbi.binding.file.framework;
030:
031:        import com.sun.jbi.binding.file.FileBindingContext;
032:        import com.sun.jbi.binding.file.FileBindingResources;
033:        import com.sun.jbi.binding.file.util.StringTranslator;
034:
035:        import java.util.logging.Logger;
036:
037:        /**
038:         * This class executes the command in its thread. The class is always a part of
039:         * a threadpool and cannot exist on its own.
040:         *
041:         * @author Sun Microsystems, Inc.
042:         */
043:        class WorkThread implements  Runnable, FileBindingResources {
044:            /**
045:             * Default Thread sleep time in milliseconds.
046:             */
047:            private static final int DEFAULT_THREAD_SLEEP_TIME = 100;
048:
049:            /**
050:             * Thread sleep time in milliseconds.
051:             */
052:            private static int sThreadSleeptime = DEFAULT_THREAD_SLEEP_TIME;
053:
054:            /**
055:             * Container for holding the command to be executed in this thread.
056:             */
057:            private Command mCurrentCommand;
058:
059:            /**
060:             * Internal handle to the logger instance.
061:             */
062:            private Logger mLog;
063:
064:            /**
065:             * Helper class for i18n.
066:             */
067:            private StringTranslator mTranslator;
068:
069:            /**
070:             * Internal handle to the thread in which this runnable object is running.
071:             */
072:            private Thread mThread;
073:
074:            /**
075:             * Internal handle to WorkThreadPool.
076:             */
077:            private WorkThreadPool mThreadPool;
078:
079:            /**
080:             * A flag which indicates whether the thread should continue processing or
081:             * not.
082:             */
083:            private boolean mContinue;
084:
085:            /**
086:             * Creates a new instance of WorkThread.
087:             *
088:             * @param workThreadPool - worker thread pool parent
089:             */
090:            WorkThread(WorkThreadPool workThreadPool) {
091:                mThreadPool = workThreadPool;
092:                mLog = FileBindingContext.getInstance().getLogger();
093:                mTranslator = new StringTranslator();
094:            }
095:
096:            /**
097:             * Sets the log file.
098:             *
099:             * @param logFile log file.
100:             */
101:            public void setLogger(String logFile) {
102:                mLog = mLog.getLogger(logFile);
103:            }
104:
105:            /**
106:             * Gets the thread name
107:             *
108:             * @return thread name.
109:             */
110:            public String getName() {
111:                return mThread.getName();
112:            }
113:
114:            /**
115:             * Sets the sleep time for the thread.
116:             *
117:             * @param time in millieseconds
118:             */
119:            public void setSleepTime(int time) {
120:                sThreadSleeptime = time;
121:            }
122:
123:            /**
124:             * The method polls the container for a new work when the thread is ready
125:             * to do work. This is indicated by the method doWork(). If there is new
126:             * work, it processes the command in its thread. Once the command has been
127:             * processed, it clears its work container and notifies the thread pool
128:             * that it is now free. The method will clean up the thread and shut
129:             * itself down when its state is set to "STOP".
130:             */
131:            public void run() {
132:                mThread = Thread.currentThread();
133:                mLog.info(mTranslator.getString(FBC_RUNNING_THREAD, mThread
134:                        .getName()));
135:                mContinue = true;
136:
137:                while (mContinue) {
138:                    if (isWorkAssigned()) {
139:                        try {
140:                            processCommand();
141:                        } catch (Throwable th) {
142:                            mLog.severe(mTranslator
143:                                    .getString(FBC_THREADS_COMMAND_FAILED));
144:                            th.printStackTrace();
145:                        }
146:
147:                        clearCommand();
148:                        mThreadPool.releaseThread(this );
149:                    }
150:
151:                    try {
152:                        Thread.sleep(sThreadSleeptime);
153:                    } catch (InterruptedException interruptException) {
154:                        mLog.fine(mTranslator.getString(
155:                                FBC_THREADS_INTERUPPTED, mThread.getName()));
156:                    }
157:                }
158:
159:                try {
160:                    mLog.fine(mTranslator.getString(FBC_THREADS_STOPPED,
161:                            mThread.getName()));
162:                } catch (Exception e) {
163:                    ;
164:                }
165:            }
166:
167:            /**
168:             * Assigns the command to this thread.
169:             *
170:             * @param command - command instance.
171:             */
172:            synchronized void setCommand(Command command) {
173:                if (mCurrentCommand == null) {
174:                    mCurrentCommand = command;
175:                }
176:            }
177:
178:            /**
179:             * Gets the command associated with this thread.
180:             *
181:             * @return the command associated with the thread
182:             */
183:            synchronized Command getCommand() {
184:                return mCurrentCommand;
185:            }
186:
187:            /**
188:             * Returns a boolean indicating if work has been assigned to this thread.
189:             *
190:             * @return true if work has been allocated to this thread; otherwise false.
191:             */
192:            synchronized boolean isWorkAssigned() {
193:                return (mCurrentCommand != null);
194:            }
195:
196:            /**
197:             * Clears the command associated with the thread.
198:             */
199:            void clearCommand() {
200:                mCurrentCommand = null;
201:            }
202:
203:            /**
204:             * Processes the command in this thread.
205:             */
206:            void processCommand() {
207:                mCurrentCommand.execute();
208:            }
209:
210:            /**
211:             * Stops the worker thread.
212:             *
213:             * @throws IllegalStateException
214:             */
215:            void stop() {
216:                if (mContinue) {
217:                    mLog.fine(mTranslator.getString(FBC_THREADS_SHUTDOWN,
218:                            getName()));
219:                    mContinue = false;
220:
221:                    try {
222:                        mThread.interrupt();
223:                    } catch (SecurityException securityException) {
224:                        mLog.warning(mTranslator.getString(
225:                                FBC_THREADS_CANNOT_INTERRUPT, securityException
226:                                        .toString()));
227:                    }
228:
229:                    try {
230:                        mThread.join();
231:                    } catch (InterruptedException exp) {
232:                        ;
233:                    }
234:
235:                    mThread = null;
236:                } else {
237:                    throw new IllegalStateException("Thread is not running");
238:                }
239:            }
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.