jvanzyl 2002/07/05 14:14:33
Modified: src/java/org/apache/maven/app Maven.java
Added: src/java/org/apache/maven/app PluginManager.java
Log:
o First pass at separating all the plugin related processing to PluginManager.
Will now integrate what I can of Dion's code, and create a descriptor
describing the JARs required by a plugin and use the new feature in Forehead
added by bob so that modify specified classloaders after Forehead has
initially started. Looking good so far.
Revision Changes Path
1.45 +10 -155 jakarta-turbine-maven/src/java/org/apache/maven/app/Maven.java
Index: Maven.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/app/Maven.java,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -r1.44 -r1.45
--- Maven.java 5 Jul 2002 15:02:25 -0000 1.44
+++ Maven.java 5 Jul 2002 21:14:33 -0000 1.45
@@ -138,12 +138,6 @@
/** Initialization jellyscript name. */
public static final String DRIVER_SCRIPT_NAME = "driver.jelly";
- /** Plug-in main script name. */
- public static final String PLUGIN_SCRIPT_NAME = "plugin.jelly";
-
- /** Plug-in default properties name. */
- public static final String PLUGIN_PROPERTIES_NAME = "plugin.properties";
-
// ------------------------------------------------------------
// Class members
// ------------------------------------------------------------
@@ -164,9 +158,6 @@
/** ${maven.home}/bin/ directory. */
private File mavenBin;
- /** ${maven.home}/bin/plugins directory. */
- private File pluginsDir;
-
/** Project directory. */
private File dir;
@@ -206,6 +197,9 @@
/** Saved original console System.err. */
private PrintStream consoleErr;
+ /** Plugin Manager */
+ private PluginManager pluginManager;
+
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
@@ -216,6 +210,7 @@
{
this.properties = new Properties();
this.werkzProject = new com.werken.werkz.Project();
+ this.pluginManager = new PluginManager();
}
// ------------------------------------------------------------
@@ -330,9 +325,6 @@
this.mavenBin = new File(mavenHome,
"bin");
-
- this.pluginsDir = new File(mavenHome,
- "plugins");
}
public File getDriverFile()
@@ -341,74 +333,6 @@
DRIVER_SCRIPT_NAME );
}
- /** Retrieve the directory containing all plugins.
- *
- * @return The directory containing all plugins.
- */
- public File getPluginsDir()
- {
- return this.pluginsDir;
- }
-
- /** Retrieve the directory for the specified
- * plugin.
- *
- * @param pluginName The name of the plugin.
- *
- * @return The directory for the plugin, or
- * <code>null</code> if no such plugin.
- */
- public File getPluginDir(String pluginName)
- {
- return new File(getPluginsDir(),
- pluginName);
- }
-
- /** Retrieve the plugin's entry-point jelly script.
- *
- * @param pluginName The name of the plugin.
- *
- * @return The entry-point script for the plugin, or
- * <code>null</code> if no such plugin.
- */
- public File getPluginScript(String pluginName)
- {
- return new File(getPluginDir(pluginName),
- PLUGIN_SCRIPT_NAME);
- }
-
-
- /** Retrieve the plugin's default properties.
- *
- * @param pluginName The name of the plugin.
- *
- * @return The default properties file for the plugin, or
- * <code>null</code> if no such plugin.
- *
- * @throws IOException If an IO error occurs while attempting
- * to read the plugin's properties.
- */
- public Properties getPluginProperties(String pluginName) throws IOException
- {
- File propsFile = new File(getPluginDir(pluginName),
- PLUGIN_PROPERTIES_NAME);
-
- if (!propsFile.exists())
- {
- return null;
- }
-
- Properties props = new Properties();
-
- FileInputStream in = new FileInputStream(propsFile);
-
- props.load(in);
-
- in.close();
-
- return props;
- }
-
/** Retrieve the ${maven.home} directory.
*
* @return The ${maven.home} directory.
@@ -700,82 +624,13 @@
*/
private void initializePlugins() throws Exception
{
- loadPlugin("core");
-
- File pluginsDir = getPluginsDir();
-
- File[] pluginDirs = pluginsDir.listFiles();
-
- for (int i = 0; i < pluginDirs.length; ++i)
- {
- if (pluginDirs[i].isDirectory()
- &&
- ! pluginDirs[i].getName().equals("core"))
- {
- loadPlugin(pluginDirs[i].getName());
- }
- }
- }
-
- /** Load the specified plugin.
- *
- * @param name The name of the plugin to load.
- *
- * @throws Exception If an error occurs while initializing
- * the plugin.
- */
- private void loadPlugin(String name) throws Exception
- {
- File pluginScript = getPluginScript(name);
-
- if (!pluginScript.exists())
- {
- /*
- output.write("Unable to load plugin script: "
- + pluginScript
- + "\n");
- */
- return;
- }
-
- loadPluginProperties(name);
-
- // Setup the classloader that adds the plugin's own
- // directory/jar to the classloader available to
- // jelly. Don't forget to set it back to how it
- // was. Leave nothing but footprints, take nothing
- // but photographs.
-
- ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
-
- ClassLoader cl = new URLClassLoader(new URL[]{getPluginDir(name).toURL()},
-
getAntProject().getClass().getClassLoader());
-
- // Thread.currentThread().setContextClassLoader(cl);
-
- JellyUtils.runScript(pluginScript,
- getPluginDir(name).toURL(),
- getJellyContext(),
- getXMLOutput());
-
- // Thread.currentThread().setContextClassLoader(oldCl);
-
- }
-
- /** Load the properties for the specified plugin.
- *
- * @param name The name of the plugin to load.
- *
- * @throws IOException If an error occurs while initializing
- * the plugin's properties.
- */
- private void loadPluginProperties(String name) throws IOException
- {
- Properties props = getPluginProperties(name);
-
- integrateProps( props );
+ pluginManager.setMavenHome( getMavenHome() );
+ pluginManager.setAntProject( getAntProject() );
+ pluginManager.setJellyContext( getJellyContext() );
+ pluginManager.setXMLOutput( getXMLOutput() );
+ pluginManager.initializePlugins();
}
-
+
/** Run maven.
*
* @throws Exception If an error occurs.
1.1
jakarta-turbine-maven/src/java/org/apache/maven/app/PluginManager.java
Index: PluginManager.java
===================================================================
package org.apache.maven.app;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.expression.CompositeExpression;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.CompositeExpression;
import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
import org.apache.commons.grant.GrantProject;
/** Plugin manager for Maven.
*
* <p>
* The <code>PluginManager</code> deals with all aspects of a plugins
* lifecycle.
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
*/
public class PluginManager
{
/** ${maven.home}/bin/plugins directory. */
private File pluginsDir;
/** Plug-in main script name. */
public static final String PLUGIN_SCRIPT_NAME = "plugin.jelly";
/** Plug-in default properties name. */
public static final String PLUGIN_PROPERTIES_NAME = "plugin.properties";
/** Jelly's output. */
private XMLOutput output;
/** Jelly conext. */
private MavenJellyContext jellyContext;
/** The Ant project. */
private GrantProject antProject;
/** ${maven.home}/ directory. */
private File mavenHome;
/** Initialize all plugins.
*
* @throws Exception If an error occurs while initializing
* any plugin.
*/
public void initializePlugins()
throws Exception
{
setPluginsDir( new File( getMavenHome(), "plugins" ) );
loadPlugin("core");
File pluginsDir = getPluginsDir();
File[] pluginDirs = pluginsDir.listFiles();
for (int i = 0; i < pluginDirs.length; ++i)
{
if (pluginDirs[i].isDirectory()
&&
! pluginDirs[i].getName().equals("core"))
{
loadPlugin(pluginDirs[i].getName());
}
}
}
/** Load the specified plugin.
*
* @param name The name of the plugin to load.
*
* @throws Exception If an error occurs while initializing
* the plugin.
*/
private void loadPlugin(String name) throws Exception
{
File pluginScript = getPluginScript(name);
if (!pluginScript.exists())
{
/*
output.write("Unable to load plugin script: "
+ pluginScript
+ "\n");
*/
return;
}
loadPluginProperties(name);
// Setup the classloader that adds the plugin's own
// directory/jar to the classloader available to
// jelly. Don't forget to set it back to how it
// was. Leave nothing but footprints, take nothing
// but photographs.
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
ClassLoader cl = new URLClassLoader(new URL[]{getPluginDir(name).toURL()},
getAntProject().getClass().getClassLoader());
// Thread.currentThread().setContextClassLoader(cl);
JellyUtils.runScript(pluginScript,
getPluginDir(name).toURL(),
getJellyContext(),
getXMLOutput());
// Thread.currentThread().setContextClassLoader(oldCl);
}
/** Load the properties for the specified plugin.
*
* @param name The name of the plugin to load.
*
* @throws IOException If an error occurs while initializing
* the plugin's properties.
*/
private void loadPluginProperties(String name) throws IOException
{
Properties props = getPluginProperties(name);
integrateProps( props );
}
/** Retrieve the directory for the specified
* plugin.
*
* @param pluginName The name of the plugin.
*
* @return The directory for the plugin, or
* <code>null</code> if no such plugin.
*/
public File getPluginDir(String pluginName)
{
return new File(getPluginsDir(),
pluginName);
}
/** Retrieve the plugin's entry-point jelly script.
*
* @param pluginName The name of the plugin.
*
* @return The entry-point script for the plugin, or
* <code>null</code> if no such plugin.
*/
public File getPluginScript(String pluginName)
{
return new File(getPluginDir(pluginName),
PLUGIN_SCRIPT_NAME);
}
/** Retrieve the plugin's default properties.
*
* @param pluginName The name of the plugin.
*
* @return The default properties file for the plugin, or
* <code>null</code> if no such plugin.
*
* @throws IOException If an IO error occurs while attempting
* to read the plugin's properties.
*/
public Properties getPluginProperties(String pluginName) throws IOException
{
File propsFile = new File(getPluginDir(pluginName),
PLUGIN_PROPERTIES_NAME);
if (!propsFile.exists())
{
return null;
}
Properties props = new Properties();
FileInputStream in = new FileInputStream(propsFile);
props.load(in);
in.close();
return props;
}
private void integrateProps(Properties props)
{
if (props == null)
{
return;
}
JellyContext context = getJellyContext();
Enumeration propNames = props.propertyNames();
String eachName = null;
String propText = null;
Object propVal = null;
JexlExpressionFactory factory = new JexlExpressionFactory();
while (propNames.hasMoreElements())
{
eachName = (String) propNames.nextElement();
if (context.getVariable(eachName) == null)
{
propText = props.getProperty(eachName);
try
{
Expression expr = CompositeExpression.parse(propText,
factory);
if (expr != null)
{
propVal = expr;
}
else
{
propVal = propText;
}
context.setVariable(eachName,
propVal);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
/** Set Maven home.
*
*/
public void setMavenHome( File mavenHome )
{
this.mavenHome = mavenHome;
}
/** Retrieve Maven home.
*
* @return The ant project.
*/
public File getMavenHome()
{
return mavenHome;
}
/** Set Ant project.
*
*/
public void setAntProject( GrantProject antProject )
{
this.antProject = antProject;
}
/** Retrieve the Ant project.
*
* @return The ant project.
*/
public GrantProject getAntProject()
{
return this.antProject;
}
/** Set Jelly context.
*
*/
public void setJellyContext( MavenJellyContext jellyContext )
{
this.jellyContext = jellyContext;
}
/** Retrieve the Jelly context.
*
* @return The Jelly context.
*/
public MavenJellyContext getJellyContext()
{
return this.jellyContext;
}
public void setXMLOutput( XMLOutput output )
{
this.output = output;
}
/** Retrieve the XML execution output sink.
*
* @return The output sink.
*/
public XMLOutput getXMLOutput()
{
return this.output;
}
public void setPluginsDir( File pluginsDir )
{
this.pluginsDir = pluginsDir;
}
/** Retrieve the directory containing all plugins.
*
* @return The directory containing all plugins.
*/
public File getPluginsDir()
{
return this.pluginsDir;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>