Source Code Cross Referenced for LiquibaseServletListener.java in  » Database-Client » LiquiBase » liquibase » servlet » 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 » Database Client » LiquiBase » liquibase.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package liquibase.servlet;
002:
003:        import liquibase.log.LogFactory;
004:        import liquibase.*;
005:        import liquibase.util.NetUtil;
006:        import liquibase.database.DatabaseFactory;
007:
008:        import javax.servlet.ServletContextListener;
009:        import javax.servlet.ServletContextEvent;
010:        import javax.naming.Context;
011:        import javax.naming.InitialContext;
012:        import javax.sql.DataSource;
013:        import java.util.logging.Handler;
014:        import java.util.logging.LogRecord;
015:        import java.net.InetAddress;
016:        import java.net.UnknownHostException;
017:        import java.sql.Connection;
018:
019:        /**
020:         * Servlet listener than can be added to web.xml to allow LiquiBase to run on every application server startup.
021:         * Using this listener allows users to know that they always have the most up to date database, although it will
022:         * slow down application server startup slightly.
023:         * See the <a href="http://www.liquibase.org/manual/latest/servlet_listener_migrator.html">LiquiBase documentation</a> for
024:         * more information.
025:         */
026:        public class LiquibaseServletListener implements  ServletContextListener {
027:
028:            private String changeLogFile;
029:            private String dataSource;
030:            private String contexts;
031:
032:            public String getChangeLogFile() {
033:                return changeLogFile;
034:            }
035:
036:            public void setContexts(String ctxt) {
037:                contexts = ctxt;
038:            }
039:
040:            public String getContexts() {
041:                return contexts;
042:            }
043:
044:            public void setChangeLogFile(String changeLogFile) {
045:                this .changeLogFile = changeLogFile;
046:            }
047:
048:            public String getDataSource() {
049:                return dataSource;
050:            }
051:
052:            public void setDataSource(String dataSource) {
053:                this .dataSource = dataSource;
054:            }
055:
056:            public void contextInitialized(
057:                    ServletContextEvent servletContextEvent) {
058:                LogFactory.getLogger().addHandler(new Handler() {
059:                    public synchronized void publish(LogRecord record) {
060:                        LiquibaseStatusServlet.logMessage(record);
061:                    }
062:
063:                    public void flush() {
064:                    }
065:
066:                    public void close() throws SecurityException {
067:                    }
068:                });
069:                String hostName;
070:                try {
071:                    hostName = NetUtil.getLocalHost().getHostName();
072:                } catch (Exception e) {
073:                    servletContextEvent.getServletContext().log(
074:                            "Cannot find hostname: " + e.getMessage());
075:                    return;
076:                }
077:
078:                String shouldRunProperty = System
079:                        .getProperty(Liquibase.SHOULD_RUN_SYSTEM_PROPERTY);
080:                if (shouldRunProperty != null
081:                        && !Boolean.valueOf(shouldRunProperty)) {
082:                    LogFactory.getLogger().info(
083:                            "LiquiBase did not run on " + hostName
084:                                    + " because '"
085:                                    + Liquibase.SHOULD_RUN_SYSTEM_PROPERTY
086:                                    + "' system property was set to false");
087:                    return;
088:                }
089:
090:                String machineIncludes = servletContextEvent
091:                        .getServletContext().getInitParameter(
092:                                "LIQUIBASE_HOST_INCLUDES");
093:                String machineExcludes = servletContextEvent
094:                        .getServletContext().getInitParameter(
095:                                "LIQUIBASE_HOST_EXCLUDES");
096:                String failOnError = servletContextEvent.getServletContext()
097:                        .getInitParameter("LIQUIBASE_FAIL_ON_ERROR");
098:
099:                boolean shouldRun = false;
100:                if (machineIncludes == null && machineExcludes == null) {
101:                    shouldRun = true;
102:                } else if (machineIncludes != null) {
103:                    for (String machine : machineIncludes.split(",")) {
104:                        machine = machine.trim();
105:                        if (hostName.equalsIgnoreCase(machine)) {
106:                            shouldRun = true;
107:                        }
108:                    }
109:                } else if (machineExcludes != null) {
110:                    shouldRun = true;
111:                    for (String machine : machineExcludes.split(",")) {
112:                        machine = machine.trim();
113:                        if (hostName.equalsIgnoreCase(machine)) {
114:                            shouldRun = false;
115:                        }
116:                    }
117:                }
118:
119:                if (!shouldRun) {
120:                    servletContextEvent
121:                            .getServletContext()
122:                            .log(
123:                                    "LiquibaseServletListener did not run due to LIQUIBASE_HOST_INCLUDES and/or LIQUIBASE_HOST_EXCLUDES");
124:                    return;
125:                }
126:
127:                setDataSource(servletContextEvent.getServletContext()
128:                        .getInitParameter("LIQUIBASE_DATA_SOURCE"));
129:                setChangeLogFile(servletContextEvent.getServletContext()
130:                        .getInitParameter("LIQUIBASE_CHANGELOG"));
131:                setContexts(servletContextEvent.getServletContext()
132:                        .getInitParameter("LIQUIBASE_CONTEXTS"));
133:                if (getChangeLogFile() == null) {
134:                    throw new RuntimeException(
135:                            "Cannot run LiquiBase, LIQUIBASE_CHANGELOG is not set");
136:                }
137:                if (getDataSource() == null) {
138:                    throw new RuntimeException(
139:                            "Cannot run LiquiBase, LIQUIBASE_DATA_SOURCE is not set");
140:                }
141:
142:                try {
143:                    Context ic = null;
144:                    Connection connection = null;
145:                    try {
146:                        ic = new InitialContext();
147:                        DataSource dataSource = (DataSource) ic
148:                                .lookup(this .dataSource);
149:
150:                        connection = dataSource.getConnection();
151:
152:                        FileOpener clFO = new ClassLoaderFileOpener();
153:                        FileOpener fsFO = new FileSystemFileOpener();
154:
155:                        Liquibase liquibase = new Liquibase(getChangeLogFile(),
156:                                new CompositeFileOpener(clFO, fsFO),
157:                                DatabaseFactory.getInstance()
158:                                        .findCorrectDatabaseImplementation(
159:                                                connection));
160:                        liquibase.update(getContexts());
161:                    } finally {
162:                        if (ic != null) {
163:                            ic.close();
164:                        }
165:                        if (connection != null) {
166:                            connection.close();
167:                            connection.close();
168:                        }
169:                    }
170:
171:                } catch (Exception e) {
172:                    if (!"false".equals(failOnError)) {
173:                        throw new RuntimeException(e);
174:                    }
175:                }
176:            }
177:
178:            public void contextDestroyed(ServletContextEvent servletContextEvent) {
179:            }
180:
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.