KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > webapps > fichotheque > InitServlet


1 package org.jahia.webapps.fichotheque;
2
3
4 import javax.servlet.http.HttpServlet JavaDoc;
5 import java.io.*;
6 import java.sql.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10
11 import org.jdom.input.*;
12 import org.jdom.*;
13
14 import java.security.Security JavaDoc;
15 import java.net.URLEncoder JavaDoc;
16 import java.net.URLDecoder JavaDoc;
17
18 import java.util.*;
19 import org.apache.log4j.PropertyConfigurator;
20 import org.apache.log4j.xml.DOMConfigurator;
21
22 import org.jdom.output.*;
23 import org.jahia.tools.Tools;
24
25 public class InitServlet extends HttpServlet JavaDoc {
26
27     static private String JavaDoc servletDiskPath = null;
28
29     private static org.apache.log4j.Logger logger =
30             org.apache.log4j.Logger.getLogger(InitServlet.class);
31
32     private String JavaDoc dbURL;
33
34     public void init() {
35
36       String JavaDoc xmlFile = getInitParameter("log4j-xml-init-file");
37       String JavaDoc realXmlFile = getServletContext().getRealPath(xmlFile);
38
39         // if the log4j-init-file is not set, then no point in trying
40
if (xmlFile != null) {
41             DOMConfigurator.configure(realXmlFile);
42         }
43
44         logger.debug("Log4j configured, initializing the rest of the application...");
45
46         // the following line MUST be done before any call to the URL class,
47
// actually it should probably be done on the startup command line for
48
// the system properties.
49
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
50         // System.setProperty("https.proxyHost", "www.yourproxyhost.com");
51
// System.setProperty("https.proxyPort", "8080");
52

53         String JavaDoc webInfPath = getServletContext().getRealPath("WEB-INF");
54
55         servletDiskPath = this.getServletContext().getRealPath(File.separator);
56         File repositoryConfigFile = new File(servletDiskPath + "WEB-INF" + File.separator + "classes" + File.separator + "repository.xml");
57         if (repositoryConfigFile.exists()) {
58             SAXBuilder builder = new SAXBuilder();
59             try {
60                 Document document = builder.build(repositoryConfigFile);
61                 if (document != null) {
62                     Element rootElement = document.getRootElement();
63                     Element connElement = rootElement.getChild("jdbc-connection-descriptor");
64                     if (connElement != null) {
65                         logger.debug("Found <jdbc-connection-descriptor> tag.");
66                         String JavaDoc dbAlias = connElement.getAttributeValue("dbalias");
67                         logger.debug("Read dbalias=[" + dbAlias + "]");
68                         if (dbAlias.startsWith("$context/")) {
69                             String JavaDoc withoutMarker = dbAlias.substring("$context/".length());
70                             // we must translate the path separators to something
71
// acceptable for the system.
72
withoutMarker = Tools.replace(withoutMarker, "/", File.separator);
73                             logger.debug("Setting OJB dbalis to [" + servletDiskPath + withoutMarker + "]");
74                             connElement.setAttribute("dbalias", servletDiskPath + withoutMarker);
75                         }
76                     }
77                     XMLOutputter serializer = new XMLOutputter();
78                     FileOutputStream repositoryOutput = new FileOutputStream(repositoryConfigFile);
79                     serializer.output(document, repositoryOutput);
80                 }
81             } catch (org.jdom.JDOMException jdome) {
82                 logger.error("Error loading repository configuration file " + repositoryConfigFile.toString(), jdome);
83             } catch (java.io.FileNotFoundException JavaDoc fnfe) {
84                 logger.error("Error modifying repository config file " + repositoryConfigFile.toString(), fnfe);
85             } catch (java.io.IOException JavaDoc ioe) {
86                 logger.error("Error modifying repository config file " + repositoryConfigFile.toString(), ioe);
87             }
88         }
89
90     }
91
92     public void service(HttpServletRequest request,
93                         HttpServletResponse response)
94             throws ServletException, IOException {
95     }
96
97 }
Popular Tags