KickJava   Java API By Example, From Geeks To Geeks.

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


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

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