[Yanel-commits] rev 25689 -
public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources
simon at wyona.com
simon at wyona.com
Mon Jul 2 12:11:56 CEST 2007
Author: simon
Date: 2007-07-02 12:11:55 +0200 (Mon, 02 Jul 2007)
New Revision: 25689
Added:
public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/TomcatContextHandler.java
Log:
TomcatContextHandler adde with helper methodes to create, list, move, delete webapps and contexts
Added: public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/TomcatContextHandler.java
===================================================================
--- public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/TomcatContextHandler.java (rev 0)
+++ public/yanel/trunk/src/realms/welcome-admin/yanel/resources/update-webapp/src/java/org/wyona/yanel/impl/resources/TomcatContextHandler.java 2007-07-02 10:11:55 UTC (rev 25689)
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2006 Wyona
+ */
+
+package org.wyona.yanel.impl.resources;
+
+import org.apache.log4j.Category;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import javax.servlet.http.HttpServletRequest;
+
+
+
+public class TomcatContextHandler {
+ private static Category log = Category.getInstance(TomcatContextHandler.class);
+ private String webappsDirectoryPath;
+ private File webappsDirectory;
+ private String contextConfPath;
+ private File contextConfDirectory;
+
+ public TomcatContextHandler(HttpServletRequest request) throws Exception {
+ this.webappsDirectoryPath = request.getSession().getServletContext().getRealPath(".") + File.separator + ".." + File.separator;
+ this.webappsDirectory = new File(webappsDirectoryPath);
+ this.contextConfPath = webappsDirectoryPath + ".." + File.separator + "conf" + File.separator + "Catalina" + File.separator + "localhost" + File.separator;
+ this.contextConfDirectory = new File(contextConfPath);
+ }
+
+ public String[] getWebappNames() {
+ String[] webapps = new String[this.webappsDirectory.listFiles().length];
+ for (int i = 0; i < this.webappsDirectory.listFiles().length; i++) {
+ webapps[i] = this.webappsDirectory.listFiles()[i].getName();
+ }
+ return webapps;
+ }
+
+ public String[] getContextNames() {
+ String[] contexts = new String[this.contextConfDirectory.listFiles().length];
+ for (int i = 0; i < this.contextConfDirectory.listFiles().length; i++) {
+ contexts[i] = this.contextConfDirectory.listFiles()[i].getName().replaceAll(".xml", "");
+ if (contexts[i].equals("ROOT")) {
+ contexts[i] = "/";
+ }
+ }
+ return contexts;
+ }
+
+ public Map getContextAndWebapp() throws Exception {
+ Map contextAndWebapps = new HashMap();
+ for (int i = 0; i < this.contextConfDirectory.listFiles().length; i++) {
+ String context = this.contextConfDirectory.listFiles()[i].getName().replaceAll(".xml", "");;
+ if (context.equals("ROOT")) {
+ context = "/";
+ }
+ String webapp = getWebappOfContext(context);
+ contextAndWebapps.put(context, webapp);
+ }
+ return contextAndWebapps;
+ }
+
+ public String getWebappOfContext (String context) throws FileNotFoundException, IOException {
+ File file = new File( contextConfPath + context);
+ String line = "";
+ String webapp = "";
+
+ FileInputStream fis = new FileInputStream(file);
+ BufferedInputStream bis = new BufferedInputStream(fis);
+ DataInputStream dis = new DataInputStream(bis);
+ while (dis.available() != 0) {
+ line = line + dis.readLine();
+ }
+ fis.close();
+ bis.close();
+ dis.close();
+
+ line = line.replaceAll("[ ]+", " ");
+ line = line.replaceAll("\"/>", "");
+ webapp = line.split(File.separator)[line.split(File.separator).length];
+
+ return webapp;
+ }
+
+ public void setContext (String context, String webapp) throws Exception, IOException {
+ if (context.equals("/")) {
+ context = "ROOT";
+ }
+ if (!context.equals(webapp) && new File(contextConfPath + context + ".xml").exists()){
+ log.debug("Its prohibited to modify a context if context name and webapp name are the same.");
+ throw new Exception("Its prohibited to modify a context if context name and webapp name are the same.");
+ }
+ String contextEntry = "<Context docBase=\"${catalina.home}/yanel-webapps/" + webapp + "\"/>";
+ BufferedWriter out = new BufferedWriter(new FileWriter(contextConfPath + context + ".xml"));
+ out.write(contextEntry);
+ out.close();
+ }
+
+ public void setWebappAsRoot(String webapp) throws Exception {
+ try {
+ setContext ("ROOT", webapp);
+ } catch (Exception e) {
+ log.error("Setting of webapp (" + webapp + ") as root failed.");
+ throw new Exception("Setting of webapp (" + webapp + ") as root failed.");
+ }
+ }
+
+ public void removeWebapp (String webapp, String context) throws Exception {
+ if (context.equals("/") || context.equals("ROOT")) {
+ log.error("Deletion of root context prohibited");
+ throw new Exception("Deletion of root context prohibited. Use setWebappAsRoot(String webapp) instead");
+ }
+ if (!getWebappOfContext(context).equals(webapp)) {
+ log.error("This context (" + context + ") does not point to this webapp (" + webapp + ")");
+ throw new Exception("This context (" + context + ") does not point to this webapp (" + webapp + ")");
+ }
+ boolean success = (new File(contextConfPath + context)).delete();
+ if (!success) {
+ log.error("Deletion of contex file not successful!");
+ throw new Exception("Deletion of contex file (" + contextConfPath + context + ") not successful!");
+ }
+ try {
+ FileUtils.deleteDirectory(new File(webappsDirectoryPath + webapp));
+ } catch (Exception e) {
+ log.error("Deletion of webapp not successful!");
+ throw new Exception("Deletion of webapp (" + webappsDirectoryPath + webapp + ") not successful!" + e);
+ }
+ }
+}
\ No newline at end of file
More information about the Yanel-commits
mailing list