mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-05 03:40:56 +01:00
improve ScmWebPlugin api
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class HgPlugin extends ScmWebPluginAdapter
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String SCRIPT = "/sonia/scm/hg.config.js";
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public InputStream getScript()
|
||||
{
|
||||
return HgPlugin.class.getResourceAsStream(SCRIPT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class HgWebPlugin implements ScmWebPlugin
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String SCRIPT = "/sonia/scm/hg.config.js";
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void contextDestroyed(ScmWebPluginContext context)
|
||||
{
|
||||
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void contextInitialized(ScmWebPluginContext context)
|
||||
{
|
||||
context.addScriptResource(new ClasspathWebResource(SCRIPT));
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
sonia.scm.HgPlugin
|
||||
@@ -0,0 +1 @@
|
||||
sonia.scm.web.HgWebPlugin
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
import com.google.inject.Module;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ScmWebPluginAdapter implements ScmWebPlugin {
|
||||
|
||||
@Override
|
||||
public Module[] getModules()
|
||||
{
|
||||
return new Module[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getScript()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,30 +5,28 @@
|
||||
|
||||
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.List;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ScmWebPluginContext
|
||||
public class ClasspathWebResource implements WebResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param contentPath
|
||||
*/
|
||||
public ScmWebPluginContext()
|
||||
public ClasspathWebResource(String contentPath)
|
||||
{
|
||||
plugins = ServiceUtil.getServices(ScmWebPlugin.class);
|
||||
this.contentPath = contentPath;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -39,13 +37,14 @@ public class ScmWebPluginContext
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<ScmWebPlugin> getPlugins()
|
||||
@Override
|
||||
public InputStream getContent()
|
||||
{
|
||||
return plugins;
|
||||
return ClasspathWebResource.class.getResourceAsStream(contentPath);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private List<ScmWebPlugin> plugins;
|
||||
private String contentPath;
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
|
||||
|
||||
package sonia.scm;
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
@@ -22,19 +22,7 @@ import java.io.InputStream;
|
||||
public interface ScmWebPlugin
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Module[] getModules();
|
||||
public void contextInitialized( ScmWebPluginContext context );
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public InputStream getScript();
|
||||
public void contextDestroyed( ScmWebPluginContext context );
|
||||
}
|
||||
102
scm-web-api/src/main/java/sonia/scm/web/ScmWebPluginContext.java
Normal file
102
scm-web-api/src/main/java/sonia/scm/web/ScmWebPluginContext.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.Module;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class ScmWebPluginContext
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param module
|
||||
*/
|
||||
public void addInjectModule(Module module)
|
||||
{
|
||||
injectModules.add(module);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param resource
|
||||
*/
|
||||
public void addScriptResource(WebResource resource)
|
||||
{
|
||||
scriptResources.add(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param module
|
||||
*/
|
||||
public void removeInjectModule(Module module)
|
||||
{
|
||||
injectModules.remove(module);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param resource
|
||||
*/
|
||||
public void removeScriptResource(WebResource resource)
|
||||
{
|
||||
scriptResources.remove(resource);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<Module> getInjectModules()
|
||||
{
|
||||
return injectModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<WebResource> getScriptResources()
|
||||
{
|
||||
return scriptResources;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Set<WebResource> scriptResources = new HashSet<WebResource>();
|
||||
|
||||
/** Field description */
|
||||
private Set<Module> injectModules = new HashSet<Module>();
|
||||
}
|
||||
31
scm-web-api/src/main/java/sonia/scm/web/WebResource.java
Normal file
31
scm-web-api/src/main/java/sonia/scm/web/WebResource.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public interface WebResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public InputStream getContent() throws IOException;
|
||||
}
|
||||
@@ -14,14 +14,19 @@ import com.google.inject.Injector;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.servlet.GuiceServletContextListener;
|
||||
|
||||
import sonia.scm.util.ServiceUtil;
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.web.ScmWebPlugin;
|
||||
import sonia.scm.web.ScmWebPluginContext;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -29,18 +34,47 @@ import java.util.List;
|
||||
public class ContextListener extends GuiceServletContextListener
|
||||
{
|
||||
|
||||
/*
|
||||
* @Override
|
||||
* public void contextInitialized(ServletContextEvent servletContextEvent)
|
||||
* {
|
||||
* Logger logger = Logger.getLogger("");
|
||||
* logger.setLevel(Level.ALL);
|
||||
* ConsoleHandler handler = new ConsoleHandler();
|
||||
* handler.setLevel(Level.ALL);
|
||||
* logger.addHandler( handler );
|
||||
* super.contextInitialized(servletContextEvent);
|
||||
* }
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param servletContextEvent
|
||||
*/
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent)
|
||||
{
|
||||
List<ScmWebPlugin> plugins = ServiceUtil.getServices(ScmWebPlugin.class);
|
||||
|
||||
for (ScmWebPlugin plugin : plugins)
|
||||
{
|
||||
plugin.contextDestroyed(webPluginContext);
|
||||
}
|
||||
|
||||
super.contextDestroyed(servletContextEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param servletContextEvent
|
||||
*/
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent)
|
||||
{
|
||||
webPluginContext = new ScmWebPluginContext();
|
||||
|
||||
List<ScmWebPlugin> plugins = ServiceUtil.getServices(ScmWebPlugin.class);
|
||||
|
||||
for (ScmWebPlugin plugin : plugins)
|
||||
{
|
||||
plugin.contextInitialized(webPluginContext);
|
||||
}
|
||||
|
||||
super.contextInitialized(servletContextEvent);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -51,25 +85,22 @@ public class ContextListener extends GuiceServletContextListener
|
||||
@Override
|
||||
protected Injector getInjector()
|
||||
{
|
||||
ScmWebPluginContext webPluginContext = new ScmWebPluginContext();
|
||||
List<ScmWebPlugin> plugins = webPluginContext.getPlugins();
|
||||
List<Module> modules = new ArrayList<Module>();
|
||||
|
||||
modules.add(new ScmServletModule(webPluginContext));
|
||||
|
||||
if (Util.isNotEmpty(plugins))
|
||||
{
|
||||
for (ScmWebPlugin plugin : plugins)
|
||||
{
|
||||
Module[] moduleArray = plugin.getModules();
|
||||
Collection<Module> pluginModules = webPluginContext.getInjectModules();
|
||||
|
||||
if (Util.isNotEmpty(moduleArray))
|
||||
{
|
||||
modules.addAll(Arrays.asList(moduleArray));
|
||||
}
|
||||
}
|
||||
if (Util.isNotEmpty(pluginModules))
|
||||
{
|
||||
modules.addAll(pluginModules);
|
||||
}
|
||||
|
||||
return Guice.createInjector(modules);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private ScmWebPluginContext webPluginContext;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import sonia.scm.plugin.ScriptResourceServlet;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.security.Authenticator;
|
||||
import sonia.scm.security.DemoAuthenticator;
|
||||
import sonia.scm.web.ScmWebPluginContext;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@ package sonia.scm.plugin;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import sonia.scm.ScmWebPlugin;
|
||||
import sonia.scm.ScmWebPluginContext;
|
||||
import sonia.scm.util.Util;
|
||||
import sonia.scm.web.ScmWebPlugin;
|
||||
import sonia.scm.web.ScmWebPluginContext;
|
||||
import sonia.scm.web.WebResource;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -23,6 +24,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -60,13 +62,14 @@ public class ScriptResourceServlet extends AbstractResourceServlet
|
||||
"function sayPluginHello(){ alert('Plugin Hello !'); }".concat(
|
||||
System.getProperty("line.separator")).getBytes());
|
||||
|
||||
List<ScmWebPlugin> plugins = webPluginContext.getPlugins();
|
||||
Collection<WebResource> scriptResources =
|
||||
webPluginContext.getScriptResources();
|
||||
|
||||
if (Util.isNotEmpty(plugins))
|
||||
if (Util.isNotEmpty(scriptResources))
|
||||
{
|
||||
for (ScmWebPlugin plugin : plugins)
|
||||
for (WebResource scriptResource : scriptResources)
|
||||
{
|
||||
appendResource(stream, plugin);
|
||||
appendResource(stream, scriptResource);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,15 +95,15 @@ public class ScriptResourceServlet extends AbstractResourceServlet
|
||||
*
|
||||
*
|
||||
* @param stream
|
||||
* @param plugin
|
||||
* @param script
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws ServletException
|
||||
*/
|
||||
private void appendResource(OutputStream stream, ScmWebPlugin plugin)
|
||||
private void appendResource(OutputStream stream, WebResource script)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
InputStream input = plugin.getScript();
|
||||
InputStream input = script.getContent();
|
||||
|
||||
if (input != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user