Source Code Cross Referenced for DerbyControl.java in  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » common » util » 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 » cbesb 1.2 » com.bostechcorp.cbesb.common.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ChainBuilder ESB
003:         *          Visual Enterprise Integration
004:         * 
005:         * Copyright (C) 2006 Bostech Corporation
006:         * 
007:         * This program is free software; you can redistribute it and/or modify it 
008:         * under the terms of the GNU General Public License as published by the 
009:         * Free Software Foundation; either version 2 of the License, or (at your option) 
010:         * any later version.
011:         *
012:         * This program is distributed in the hope that it will be useful, 
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
014:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
015:         * for more details.
016:         * 
017:         * You should have received a copy of the GNU General Public License along with 
018:         * this program; if not, write to the Free Software Foundation, Inc., 
019:         * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020:         *
021:         *
022:         * $Id: DerbyControl.java 11775 2008-01-31 07:48:00Z lzheng $
023:         */
024:
025:        package com.bostechcorp.cbesb.common.util;
026:
027:        import java.io.File;
028:        import java.io.FileOutputStream;
029:        import java.io.FilenameFilter;
030:        import java.io.IOException;
031:        import java.io.InputStream;
032:        import java.io.OutputStream;
033:        import java.util.Properties;
034:
035:        import org.apache.commons.logging.Log;
036:        import org.apache.commons.logging.LogFactory;
037:
038:        public class DerbyControl extends Thread {
039:            private static boolean isRunning = false;
040:            private static DerbyControl runner;
041:            private String host;
042:            private String port;
043:            private String serverRunDirectory;
044:            private String rootDir;
045:            private static Log log = LogFactory.getLog(DerbyControl.class);
046:
047:            private Process serverProcess;
048:            private StopServerOnShutdown shutdownHook;
049:
050:            private DerbyControl() {
051:                super ();
052:                shutdownHook = new StopServerOnShutdown();
053:                Runtime.getRuntime().addShutdownHook(shutdownHook);
054:            }
055:
056:            public void finalize() {
057:                if (shutdownHook != null)
058:                    Runtime.getRuntime().removeShutdownHook(shutdownHook);
059:            }
060:
061:            private static final String PROPERTY_FILE = "derbyserver.properties";
062:            private static final String SERVER_CLASS = "org.apache.derby.drda.NetworkServerControl";
063:
064:            private static void init(String rootDir) throws IOException {
065:                Properties dbprops = new Properties();
066:                runner = new DerbyControl();
067:                runner.rootDir = rootDir;
068:                dbprops.load(RuntimeClassLoader.getInputStream(PROPERTY_FILE));
069:                runner.host = dbprops.getProperty("host");
070:                runner.port = dbprops.getProperty("port");
071:                runner.serverRunDirectory = dbprops.getProperty("directory");
072:                log.info("path:" + runner.makeClasspath());
073:            }
074:
075:            public static void startServer(String rootDir) {
076:                try {
077:                    if (isRunning) {
078:                        // The server may be shutting down from a stop call so give it some time;
079:                        Thread.sleep(5000);
080:                    }
081:                    if (isRunning)
082:                        return;
083:
084:                    // create a new runner since we cant restart a thread after it terminates
085:                    //	String rootDir = runner.rootDir;
086:                    DerbyControl.init(rootDir);
087:                    runner.start();
088:                } catch (Exception e) {
089:                    ErrorUtil.printError("\n\n\nerror starting Derby server: ",
090:                            e);
091:                }
092:            }
093:
094:            public static void stopServer() {
095:                if (!isRunning)
096:                    return;
097:                runner.serverProcess.destroy();
098:                synchronized (runner.serverProcess) {
099:                    runner.serverProcess.notify();
100:                }
101:
102:            }
103:
104:            private class ServerLog implements  Runnable {
105:                private Log log;
106:                private InputStream is;
107:                private OutputStream os;
108:                private Thread runner;
109:                private boolean isRunLog = false;
110:
111:                protected ServerLog(Log log, InputStream is, OutputStream os) {
112:                    this .os = os;
113:                    this .log = log;
114:                    this .is = is;
115:                    isRunLog = true;
116:                    runner = new Thread(this );
117:                    runner.start();
118:                }
119:
120:                public void run() {
121:                    while (isRunLog) {
122:                        byte[] inbytes = new byte[100];
123:                        try {
124:                            int bytes = is.read(inbytes);
125:                            if (bytes > 0) {
126:                                os.write(inbytes, 0, bytes);
127:                            } else
128:                                break;
129:                        } catch (Exception e) {
130:                            if (!(e instanceof  IOException)) {
131:                                ErrorUtil
132:                                        .printError(
133:                                                "\n\n\nerror logging database server output:",
134:                                                e);
135:                            }
136:                        }
137:                    }
138:                    //	log.info("ServerLog stopped");
139:                }
140:
141:                public void stop() {
142:                    isRunLog = false;
143:                }
144:            }
145:
146:            private static class DerbyJarFilter implements  FilenameFilter {
147:                public boolean accept(File dir, String name) {
148:                    return name.endsWith(".jar") && name.contains("derby");
149:                }
150:            }
151:
152:            private String makeClasspath() {
153:                File rootDirFile = new File(rootDir);
154:                File[] derbyJars = rootDirFile.listFiles(new DerbyJarFilter());
155:                String result = "";
156:                for (int i = 0; i < derbyJars.length; i++)
157:                    result = result.concat(((i > 0) ? File.pathSeparator : "")
158:                            + derbyJars[i].getAbsolutePath());
159:
160:                //log.info("class path: " + result);
161:                return result;
162:            }
163:
164:            public void run() {
165:                try {
166:                    String command = "java -cp " + makeClasspath() + " "
167:                            + SERVER_CLASS + " start -h " + runner.host
168:                            + " -p " + runner.port;
169:                    File workingDir = new File(runner.serverRunDirectory);
170:                    Runtime rt = Runtime.getRuntime();
171:                    FileOutputStream os = new FileOutputStream(new File(
172:                            workingDir, "derbyserver.log"));
173:                    log.info("\n\n\nstarting derby server");
174:                    log.info("	working dir=" + workingDir);
175:                    runner.serverProcess = rt.exec(command, null, workingDir);
176:                    isRunning = true;
177:                    ServerLog ol = new ServerLog(log, runner.serverProcess
178:                            .getInputStream(), os);
179:                    ServerLog el = new ServerLog(log, runner.serverProcess
180:                            .getErrorStream(), os);
181:                    runner.serverProcess.waitFor();
182:                    isRunning = false;
183:                    log.info("\n\n\nderby server exiting:");
184:                    ol.stop();
185:                    el.stop();
186:
187:                } catch (Exception e) {
188:                    ErrorUtil.printError("\n\n\nerror starting derby server: ",
189:                            e);
190:                }
191:            }
192:
193:            public class StopServerOnShutdown extends Thread {
194:                public void run() {
195:                    log.info("In Shutdown hook, derby server exiting:");
196:                    if (isRunning) {
197:                        runner.serverProcess.destroy();
198:                        synchronized (runner.serverProcess) {
199:                            runner.serverProcess.notify();
200:                        }
201:
202:                    }
203:                }
204:            }
205:
206:            static public void main(String[] args) {
207:
208:                //DerbyControl.init(EsbPathHelper.getCbesbHomeDir()+"/db/");
209:                DerbyControl.startServer(EsbPathHelper.getCbesbHomeDir()
210:                        + "/db/");
211:
212:            }
213:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.