improve utils

This commit is contained in:
Sebastian Sdorra
2010-09-28 20:19:18 +02:00
parent af7b7b8e4d
commit 6fec6196a0
19 changed files with 483 additions and 126 deletions

View File

@@ -16,6 +16,7 @@ import sonia.scm.io.INIConfiguration;
import sonia.scm.io.INIConfigurationReader;
import sonia.scm.io.INIConfigurationWriter;
import sonia.scm.io.INISection;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
@@ -115,7 +116,7 @@ public class HgRepositoryHandler extends AbstractRepositoryHandler<HgConfig>
if (new File(directory, ".hg").exists())
{
Util.delete(directory);
IOUtil.delete(directory);
}
else
{

View File

@@ -0,0 +1,74 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Singleton;
import sonia.scm.web.cgi.AbstractCGIServlet;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author Sebastian Sdorra
*/
@Singleton
public class HgCGIServlet extends AbstractCGIServlet
{
/** Field description */
private static final long serialVersionUID = -3492811300905099810L;
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws ServletException
*/
@Override
public void init() throws ServletException
{
command = HgUtil.getCGI();
super.init();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param req
*
* @return
*
* @throws IOException
* @throws ServletException
*/
@Override
protected File getCommand(HttpServletRequest req)
throws ServletException, IOException
{
return command;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private File command;
}

View File

@@ -9,18 +9,9 @@ package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Scopes;
import com.google.inject.servlet.ServletModule;
import sonia.scm.web.filter.BasicAuthenticationFilter;
import sonia.scm.web.cgi.CGIServlet;
//~--- JDK imports ------------------------------------------------------------
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
/**
*
@@ -29,19 +20,6 @@ import javax.servlet.ServletContext;
public class HgServletModule extends ServletModule
{
/**
* Constructs ...
*
*
* @param servletContext
*/
HgServletModule(ServletContext servletContext)
{
cgiPath = HgUtil.getCGI().getAbsolutePath();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
@@ -50,12 +28,7 @@ public class HgServletModule extends ServletModule
protected void configureServlets()
{
filter("/hg/*").through(BasicAuthenticationFilter.class);
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("command", cgiPath);
bind(CGIServlet.class).in(Scopes.SINGLETON);
serve("/hg/*").with(CGIServlet.class, initParams);
serve("/hg/*").with(HgCGIServlet.class);
}
//~--- fields ---------------------------------------------------------------

View File

@@ -15,7 +15,7 @@ import sonia.scm.io.INISection;
import sonia.scm.io.RegexResourceProcessor;
import sonia.scm.io.ResourceProcessor;
import sonia.scm.repository.HgConfig;
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -102,8 +102,8 @@ public class HgWebConfigWriter
}
finally
{
Util.close(input);
Util.close(output);
IOUtil.close(input);
IOUtil.close(output);
}
}

View File

@@ -42,6 +42,6 @@ public class HgWebPlugin implements ScmWebPlugin
public void contextInitialized(ScmWebPluginContext context)
{
context.addScriptResource(new ClasspathWebResource(SCRIPT));
context.addInjectModule(new HgServletModule( context.getServletContext() ));
context.addInjectModule(new HgServletModule());
}
}

View File

@@ -28,6 +28,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXB;
import sonia.scm.util.IOUtil;
/**
*
@@ -114,8 +115,8 @@ public class SvnRepositoryHandler extends AbstractRepositoryHandler<SvnConfig>
if (directory.exists() && repositoryFile.exists())
{
Util.delete(directory);
Util.delete(repositoryFile);
IOUtil.delete(directory);
IOUtil.delete(repositoryFile);
}
else
{

View File

@@ -9,7 +9,7 @@ package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -96,7 +96,7 @@ public class INIConfigurationReader extends AbstractReader<INIConfiguration>
}
finally
{
Util.close(input);
IOUtil.close(input);
}
return configuration;

View File

@@ -9,7 +9,7 @@ package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -52,7 +52,7 @@ public class INIConfigurationWriter extends AbstractWriter<INIConfiguration>
}
finally
{
Util.close(writer);
IOUtil.close(writer);
}
}
}

View File

@@ -9,7 +9,7 @@ package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -66,8 +66,8 @@ public class RegexResourceProcessor extends AbstractResourceProcessor
}
finally
{
Util.close(reader);
Util.close(writer);
IOUtil.close(reader);
IOUtil.close(writer);
}
}

View File

@@ -9,7 +9,7 @@ package sonia.scm.io;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -150,7 +150,7 @@ public class SimpleCommand implements Command
}
finally
{
Util.close(input);
IOUtil.close(input);
}
return result;

View File

@@ -28,6 +28,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXB;
import sonia.scm.util.IOUtil;
/**
*
@@ -115,8 +116,8 @@ public abstract class AbstractSimpleRepositoryHandler<T extends SimpleRepository
if (directory.exists() && repositoryFile.exists())
{
Util.delete(directory);
Util.delete(repositoryFile);
IOUtil.delete(directory);
IOUtil.delete(repositoryFile);
}
else
{

View File

@@ -0,0 +1,192 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.util;
//~--- JDK imports ------------------------------------------------------------
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Sebastian Sdorra
*/
public class IOUtil
{
/** Field description */
private static final Logger logger = Logger.getLogger(IOUtil.class.getName());
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param closeable
*/
public static void close(Closeable closeable)
{
if (closeable != null)
{
try
{
closeable.close();
}
catch (IOException ex)
{
logger.log(Level.SEVERE, null, ex);
}
}
}
/**
* Method description
*
*
* @param reader
* @param writer
*
* @throws IOException
*/
public static void copy(Reader reader, Writer writer) throws IOException
{
char[] buffer = new char[0xFFFF];
for (int len; (len = reader.read(buffer)) != -1; )
{
writer.write(buffer, 0, len);
}
}
/**
* Method description
*
*
* @param in
* @param out
*
* @throws IOException
*/
public static void copy(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[0xFFFF];
for (int len; (len = in.read(buffer)) != -1; )
{
out.write(buffer, 0, len);
}
}
/**
* Method description
*
*
* @param reader
* @param writer
*/
public static void copyThread(Reader reader, Writer writer)
{
new Thread(new IOCopyThread(reader, writer)).start();
}
/**
* Method description
*
*
* @param file
*
* @throws IOException
*/
public static void delete(File file) throws IOException
{
if (file.isDirectory())
{
File[] children = file.listFiles();
if (children != null)
{
for (File child : children)
{
delete(child);
}
}
}
if (!file.delete())
{
throw new IOException("could not delete file ".concat(file.getPath()));
}
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 10/09/28
* @author Enter your name here...
*/
private static class IOCopyThread implements Runnable
{
/**
* Constructs ...
*
*
* @param reader
* @param writer
*/
public IOCopyThread(Reader reader, Writer writer)
{
this.reader = reader;
this.writer = writer;
}
//~--- methods ------------------------------------------------------------
/**
* Method description
*
*/
@Override
public void run()
{
try
{
copy(reader, writer);
}
catch (IOException ex)
{
logger.log(Level.SEVERE, null, ex);
}
finally
{
close(reader);
close(writer);
}
}
//~--- fields -------------------------------------------------------------
/** Field description */
private Reader reader;
/** Field description */
private Writer writer;
}
}

View File

@@ -14,6 +14,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -39,75 +41,6 @@ public class Util
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param closeable
*/
public static void close(Closeable closeable)
{
if (closeable != null)
{
try
{
closeable.close();
}
catch (IOException ex)
{
logger.log(Level.SEVERE, null, ex);
}
}
}
/**
* Method description
*
*
* @param in
* @param out
*
* @throws IOException
*/
public static void copy(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[0xFFFF];
for (int len; (len = in.read(buffer)) != -1; )
{
out.write(buffer, 0, len);
}
}
/**
* Method description
*
*
* @param file
*
* @throws IOException
*/
public static void delete(File file) throws IOException
{
if (file.isDirectory())
{
File[] children = file.listFiles();
if (children != null)
{
for (File child : children)
{
delete(child);
}
}
}
if (!file.delete())
{
throw new IOException("could not delete file ".concat(file.getPath()));
}
}
/**
* Method description
*

View File

@@ -0,0 +1,148 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.web.cgi;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractCGIServlet extends HttpServlet
{
/** Field description */
private static final long serialVersionUID = -8638099037069714140L;
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param req
*
* @return
*
* @throws IOException
* @throws ServletException
*/
protected abstract File getCommand(HttpServletRequest req)
throws ServletException, IOException;
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @throws ServletException
*/
@Override
public void init() throws ServletException
{
cgiRunner = new CGIRunner(getServletContext(), createEnvironment(),
getCmdPrefix(), isExitStateIgnored());
}
/**
* Method description
*
*
* @return
*/
protected EnvList createEnvironment()
{
EnvList env = new EnvList();
Enumeration e = getInitParameterNames();
while (e.hasMoreElements())
{
String n = (String) e.nextElement();
if ((n != null) && n.startsWith("ENV_"))
{
env.set(n.substring(4), getInitParameter(n));
}
}
if (!env.containsKey("SystemRoot"))
{
String os = System.getProperty("os.name");
if ((os != null) && (os.toLowerCase().indexOf("windows") != -1))
{
env.set("SystemRoot", "C:\\WINDOWS");
}
}
return env;
}
/**
* Method description
*
*
* @param req
* @param resp
*
* @throws IOException
* @throws ServletException
*/
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
cgiRunner.exec(getCommand(req), req.getPathInfo(), req, resp);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected String getCmdPrefix()
{
return null;
}
;
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
protected boolean isExitStateIgnored()
{
return false;
}
;
//~--- fields ---------------------------------------------------------------
/** Field description */
private CGIRunner cgiRunner;
}

View File

@@ -9,7 +9,7 @@ package sonia.scm.web.cgi;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -164,6 +164,11 @@ public class CGIRunner
execCmd = cmdPrefix + " " + execCmd;
}
if (logger.isLoggable(Level.FINE))
{
logger.fine("execute cgi: ".concat(execCmd));
}
Process p = (dir == null)
? Runtime.getRuntime().exec(execCmd, env.getEnvArray())
: Runtime.getRuntime().exec(execCmd, env.getEnvArray(), dir);
@@ -193,6 +198,11 @@ public class CGIRunner
{
logger.log(Level.FINEST, null, e);
}
finally
{
IOUtil.close(inFromReq);
IOUtil.close(outToCgi);
}
}
}).start();
@@ -244,7 +254,7 @@ public class CGIRunner
// copy cgi content to response stream...
os = res.getOutputStream();
Util.copy(inFromCgi, os);
IOUtil.copy(inFromCgi, os);
p.waitFor();
if (!ignoreExitState)
@@ -280,7 +290,7 @@ public class CGIRunner
{
if (os != null)
{
Util.close(os);
IOUtil.close(os);
}
os = null;

View File

@@ -9,6 +9,8 @@ package sonia.scm.web.cgi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Singleton;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
@@ -46,6 +48,7 @@ import javax.servlet.http.HttpServletResponse;
* @author Sebastian Sdorra
*
*/
@Singleton
public class CGIServlet extends HttpServlet
{

View File

@@ -24,6 +24,10 @@ import sonia.scm.web.ScmWebPluginContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
@@ -62,6 +66,7 @@ public class ContextListener extends GuiceServletContextListener
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
enableDebugLogging();
webPluginContext =
new ScmWebPluginContext(servletContextEvent.getServletContext());
@@ -100,6 +105,24 @@ public class ContextListener extends GuiceServletContextListener
return Guice.createInjector(modules);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
private void enableDebugLogging()
{
Logger logger = LogManager.getLogManager().getLogger("");
logger.setLevel(Level.FINEST);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.FINEST);
logger.addHandler(handler);
}
//~--- fields ---------------------------------------------------------------
/** Field description */

View File

@@ -9,7 +9,7 @@ package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.util.Util;
import sonia.scm.util.IOUtil;
//~--- JDK imports ------------------------------------------------------------
@@ -82,7 +82,7 @@ public abstract class AbstractResourceServlet extends HttpServlet
}
finally
{
Util.close(output);
IOUtil.close(output);
}
this.content = output.toByteArray();
@@ -164,7 +164,7 @@ public abstract class AbstractResourceServlet extends HttpServlet
}
finally
{
Util.close(output);
IOUtil.close(output);
}
}

View File

@@ -12,20 +12,18 @@ package sonia.scm.plugin;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.util.IOUtil;
import sonia.scm.util.Util;
import sonia.scm.web.ScmWebPlugin;
import sonia.scm.web.ScmWebPluginContext;
import sonia.scm.web.WebResource;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import javax.servlet.ServletException;
@@ -109,11 +107,11 @@ public class ScriptResourceServlet extends AbstractResourceServlet
{
try
{
Util.copy(input, stream);
IOUtil.copy(input, stream);
}
finally
{
Util.close(input);
IOUtil.close(input);
}
}
}