From a3c48da9bb8971497d414f86e352123d95b9c3da Mon Sep 17 00:00:00 2001 From: lresende Date: Fri, 20 Nov 2009 23:53:35 +0000 Subject: Moving 2.x trunk git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@882795 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/tomcat/foo/TuscanyTomcatNode.java | 74 +++++ .../java/org/apache/tuscany/sca/war/Installer.java | 318 +++++++++++++++++++++ .../apache/tuscany/sca/war/InstallerServlet.java | 70 +++++ 3 files changed, 462 insertions(+) create mode 100644 sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/tomcat/foo/TuscanyTomcatNode.java create mode 100644 sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/Installer.java create mode 100644 sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java (limited to 'sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany') diff --git a/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/tomcat/foo/TuscanyTomcatNode.java b/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/tomcat/foo/TuscanyTomcatNode.java new file mode 100644 index 0000000000..972aafd8b6 --- /dev/null +++ b/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/tomcat/foo/TuscanyTomcatNode.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.tomcat.foo; + +import java.io.File; +import java.net.MalformedURLException; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.war.Installer; + +public class TuscanyTomcatNode implements ServletContextListener { + + protected Node tomcatNode; + + public void contextInitialized(ServletContextEvent arg0) { + if (!Installer.isTuscanyHookRunning()) { + return; + } + + // TODO: this relys on the location of webapp folder, find way to get actual catalina base + File tomcatBase = new File(arg0.getServletContext().getRealPath("/")).getParentFile().getParentFile(); + + File contributionDir = new File(tomcatBase, "sca-contributions"); + if (!contributionDir.exists()) { + return; + } + File[] contributionFiles = contributionDir.listFiles(); + if (contributionFiles.length < 1) { + return; + } + + Contribution[] nodeContributions = new Contribution[contributionFiles.length]; + for (int i = 0; i\r\n" + + ""; + + private void updateServerXml(File serverXmlFile) { + String serverXML = readAll(serverXmlFile); + if (!serverXML.contains(tuscanyListener)) { + String newServerXml = replace(serverXML, "", ">" + tuscanyListener); + backup(serverXmlFile); + writeAll(serverXmlFile, newServerXml); + } + + } + + private void removeServerXml(File serverXmlFile) { + String serverXML = readAll(serverXmlFile); + if (serverXML.contains(tuscanyListener)) { + String newServerXml = replace(serverXML, "" + tuscanyListener, ">"); + writeAll(serverXmlFile, newServerXml); + } + + } + + private String replace(String inputText, String begin, String newBegin, String end, String newEnd) { + BeginEndTokenHandler tokenHandler = new BeginEndTokenHandler(newBegin, newEnd); + + ByteArrayInputStream in = new ByteArrayInputStream(inputText.getBytes()); + + InputStream replacementStream = new DelimitedTokenReplacementInputStream(in, begin, end, tokenHandler, true); + String newServerXml = readAll(replacementStream); + close(replacementStream); + return newServerXml; + } + + private boolean backup(File source) { + File backupFile = new File(source.getParent(), source.getName() + ".b4Tuscany"); + if (!backupFile.exists()) { + copyFile(source, backupFile); + } + return true; + } + + private String readAll(File file) { + FileInputStream in = null; + try { + in = new FileInputStream(file); + String text = readAll(in); + return text; + } catch (Exception e) { + return null; + } finally { + close(in); + } + } + + private String readAll(InputStream in) { + try { + // SwizzleStream block read methods are broken so read byte at a time + StringBuilder sb = new StringBuilder(); + int i = in.read(); + while (i != -1) { + sb.append((char)i); + i = in.read(); + } + return sb.toString(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private void copyFile(File source, File destination) { + InputStream in = null; + OutputStream out = null; + try { + in = new FileInputStream(source); + out = new FileOutputStream(destination); + writeAll(in, out); + } catch (IOException e) { + throw new IllegalStateException(e); + } finally { + close(in); + close(out); + } + } + + private boolean writeAll(File file, String text) { + FileOutputStream fileOutputStream = null; + try { + fileOutputStream = new FileOutputStream(file); + writeAll(new ByteArrayInputStream(text.getBytes()), fileOutputStream); + return true; + } catch (Exception e) { + return false; + } finally { + close(fileOutputStream); + } + } + + private void writeAll(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[4096]; + int count; + while ((count = in.read(buffer)) > 0) { + out.write(buffer, 0, count); + } + out.flush(); + } + + private void close(Closeable thing) { + if (thing != null) { + try { + thing.close(); + } catch (Exception ignored) { + } + } + } + + private class BeginEndTokenHandler extends StringTokenHandler { + private final String begin; + private final String end; + + public BeginEndTokenHandler(String begin, String end) { + this.begin = begin; + this.end = end; + } + + public String handleToken(String token) throws IOException { + String result = begin + token + end; + return result; + } + } + +} diff --git a/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java b/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java new file mode 100644 index 0000000000..a7e5c7e3d7 --- /dev/null +++ b/sca-java-2.x/trunk/distribution/tomcat/tomcat-servlet/src/main/java/org/apache/tuscany/sca/war/InstallerServlet.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.war; + +import java.io.File; +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class InstallerServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + private transient ServletConfig servletConfig; + private transient Installer installer; + + public void init(ServletConfig servletConfig) throws ServletException { + this.servletConfig = servletConfig; + String path = servletConfig.getServletContext().getRealPath("/"); + File tuscanyWarDir = null; + if (path != null) { + tuscanyWarDir = new File(path); + } + File tomcatBase = new File(System.getProperty("catalina.base")); + installer = new Installer(tuscanyWarDir, tomcatBase); + } + + protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + doIt(httpServletRequest, httpServletResponse); + } + + protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + doIt(httpServletRequest, httpServletResponse); + } + + protected void doIt(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + + if ("Install".equalsIgnoreCase(req.getParameter("action"))) { + String singleton = req.getParameter("singleton"); + installer.install(singleton!=null && singleton.equalsIgnoreCase("true")); + } else if ("Uninstall".equalsIgnoreCase(req.getParameter("action"))) { + installer.uninstall(); + } + + req.setAttribute("installer", installer); + RequestDispatcher rd = servletConfig.getServletContext().getRequestDispatcher("/installer.jsp"); + rd.forward(req,res); + } + +} -- cgit v1.2.3