Revision: 101 http://mvn-infix.svn.sourceforge.net/mvn-infix/?rev=101&view=rev Author: bindul Date: 2010-12-21 09:19:39 +0000 (Tue, 21 Dec 2010)
Log Message: ----------- Implemented .htaccess generation Modified Paths: -------------- infix-parent/trunk/pom.xml plugins/sfnet-mvnrepo-plugin/trunk/src/main/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenAction.java shared/infix-plugins-common/trunk/infix-velocity-utils/pom.xml shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/AbstractVelocityExecutor.java Added Paths: ----------- plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/ plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/htaccess.vm plugins/sfnet-mvnrepo-plugin/trunk/src/test/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenActionTest.java shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ParsedTextFile.java shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/PathFormatTool.java shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml Modified: infix-parent/trunk/pom.xml =================================================================== --- infix-parent/trunk/pom.xml 2010-12-21 03:26:06 UTC (rev 100) +++ infix-parent/trunk/pom.xml 2010-12-21 09:19:39 UTC (rev 101) @@ -542,11 +542,18 @@ <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> - <version>1.7-beta1</version> + <version>1.7</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> + <groupId>org.apache.velocity</groupId> + <artifactId>velocity-tools</artifactId> + <version>2.0</version> + <type>jar</type> + <scope>compile</scope> + </dependency> + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> <version>2.0.5</version> Modified: plugins/sfnet-mvnrepo-plugin/trunk/src/main/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenAction.java =================================================================== --- plugins/sfnet-mvnrepo-plugin/trunk/src/main/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenAction.java 2010-12-21 03:26:06 UTC (rev 100) +++ plugins/sfnet-mvnrepo-plugin/trunk/src/main/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenAction.java 2010-12-21 09:19:39 UTC (rev 101) @@ -20,6 +20,12 @@ */ package com.mindtree.techworks.infix.plugins.sfnetmvnrepo.actions; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + import org.codehaus.plexus.component.annotations.Component; import com.mindtree.techworks.infix.plugins.sfnetmvnrepo.SfNetMvnMojoInfo; @@ -35,14 +41,20 @@ */ @Component ( role = SfNetMvnRepoAction.class, hint = "repo-handler" ) public class RepoHandlerGenAction extends AbstractVelocityExecutor implements SfNetMvnRepoAction { + + /** + * The htaccess template + */ + private static final String TEMPLATE_HTACCESS = "/velocity/htaccess.vm"; + private MojoInfo mojoInfo; + /* (non-Javadoc) * @see com.mindtree.techworks.infix.pluginscommon.velocity.AbstractVelocityExecutor#getMojoInfo() */ @Override protected MojoInfo getMojoInfo () { - // TODO Auto-generated method stub - return null; + return mojoInfo; } /* (non-Javadoc) @@ -50,8 +62,48 @@ */ @Override public void execute (SfNetMvnMojoInfo mojoInfo) throws MapGenerationException { + + this.mojoInfo = mojoInfo; + + // Everything should be after the project's url + String projectUrlStr = mojoInfo.getProject().getUrl(); + String redirectBase = null; + try { + URL projectUrl = new URL (projectUrlStr); + redirectBase = projectUrl.getPath(); + if (redirectBase.endsWith("/") && redirectBase.length() > 0) { + redirectBase = redirectBase.substring(0, redirectBase.length() - 1); + } + if (!redirectBase.startsWith("/")) { + redirectBase = "/" + redirectBase; + } + } catch (MalformedURLException e) { + mojoInfo.getLog().warn("Invalid project URL! [" + projectUrlStr + "]", e); + throw new MapGenerationException("Invalid project URL [" + projectUrlStr + "]", e); + } + + createHtAccess(redirectBase); + // TODO Auto-generated method stub } + /** + * Creates the htaccess file + * @throws MapGenerationException For velocity exceptions + */ + private void createHtAccess (String redirectBase) throws MapGenerationException { + File htAccessOutput = new File (mojoInfo.getWorkDirectory(), ".htaccess"); + + Map<String, Object> contextInfo = new HashMap<String, Object>(2); + contextInfo.put("rewriteBase", redirectBase); + contextInfo.put("mojoInfo", mojoInfo); + try { + processVelocityTemplate(TEMPLATE_HTACCESS, contextInfo, htAccessOutput, true); + } catch (Exception e) { + mojoInfo.getLog().error("Error generating .htaccess script", e); + throw new MapGenerationException("Error generating .htaccess script", e); + } + } + } Added: plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/htaccess.vm =================================================================== --- plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/htaccess.vm (rev 0) +++ plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/htaccess.vm 2010-12-21 09:19:39 UTC (rev 101) @@ -0,0 +1,32 @@ +#* + * + * Copyright (c) 2010 MindTree Ltd. + * + * This file is part of Infix Maven Plugins. + * + * Infix Maven Plugins is free software: you can redistribute + * it and/or modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * Infix Maven Plugins is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Infix Maven Plugins. If not, see <http://www.gnu.org/licenses/>. + * + * $Id$ + *# + +# Maven Repository .htaccess file generated by MindTree Infix Sf.net Maven Repo Plugin +RewriteEngine On + +## Set the rewrite base from the input +RewriteBase $rewriteBase + +## Generate the following line once for every +#foreach($repo in ${mojoInfo.RepositoryConfigs}) +RewriteRule ^$pathFormat.trimSlashes($repo.id)/(.*) repo-handler.php?repo=${repo.id}&relpath=${esc.dollar}1&compPath=${esc.dollar}0 +#end \ No newline at end of file Property changes on: plugins/sfnet-mvnrepo-plugin/trunk/src/main/resources/velocity/htaccess.vm ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: plugins/sfnet-mvnrepo-plugin/trunk/src/test/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenActionTest.java =================================================================== --- plugins/sfnet-mvnrepo-plugin/trunk/src/test/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenActionTest.java (rev 0) +++ plugins/sfnet-mvnrepo-plugin/trunk/src/test/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenActionTest.java 2010-12-21 09:19:39 UTC (rev 101) @@ -0,0 +1,112 @@ +/* + * $HeadURL$ + * + * Copyright (c) 2010 MindTree Ltd. + * + * This file is part of Infix Maven Plugins + * + * Infix Maven Plugins is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Infix Maven Plugins is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * Infix Maven Plugins. If not, see <http://www.gnu.org/licenses/>. + */ +package com.mindtree.techworks.infix.plugins.sfnetmvnrepo.actions; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import com.mindtree.techworks.infix.plugins.sfnetmvnrepo.SfNetMvnMojoInfoTCImpl; +import com.mindtree.techworks.infix.plugins.sfnetmvnrepo.mapper.MapGenerationException; +import com.mindtree.techworks.infix.plugins.sfnetmvnrepo.model.repoconfig.RepositoryConfig; +import com.mindtree.techworks.infix.pluginscommon.mojo.MojoInfo; +import com.mindtree.techworks.infix.pluginscommon.test.PlexusTestCase; +import com.mindtree.techworks.infix.pluginscommon.test.text.ParsedTextFile; + + +/** + * @author Bindul Bhowmik + * @version $Revision$ $Date$ + * + */ +public class RepoHandlerGenActionTest extends PlexusTestCase { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testRepoHandlerGen () { + try { + SfNetMvnRepoAction action = lookup(SfNetMvnRepoAction.class, "repo-handler"); + SfNetMvnMojoInfoTCImpl mojoInfo = new SfNetMvnMojoInfoTCImpl(); + setProjectBasedir(mojoInfo); + + mojoInfo.getProject().setUrl("http://example.com/testproj/"); + mojoInfo.setWorkDirectory(tempFolder.getRoot()); + + // Setup two simple repo maps to merge + RepositoryConfig repoConfig = new RepositoryConfig(); + repoConfig.setBaseUrl("http://example.com/testproj/mvnrepo/"); + repoConfig.setId("mvnrepo"); + repoConfig.setName("Example Merge 1"); + mojoInfo.addRepositoryConfig(repoConfig); + + repoConfig = new RepositoryConfig(); + repoConfig.setBaseUrl("http://example.com/testproj/mvnrepo2/"); + repoConfig.setId("mvnrepo2"); + repoConfig.setName("Maven Repository Project"); + mojoInfo.addRepositoryConfig(repoConfig); + + action.execute(mojoInfo); + + // Check .htaccess + File htaccess = new File(tempFolder.getRoot(), ".htaccess"); + assertTrue(htaccess.exists()); + + ParsedTextFile parsedHtaccess = new ParsedTextFile(htaccess, ParsedTextFile.TRIM_LINES + | ParsedTextFile.SKIP_EMPTY_LINES); + assertTrue(parsedHtaccess.hasLine("RewriteEngine On", true)); + assertTrue(parsedHtaccess.hasLine("RewriteBase /testproj", true)); + assertEquals(2, parsedHtaccess.countLines("RewriteRule.*", true)); + assertTrue(parsedHtaccess.hasLine("RewriteRule ^mvnrepo2/(.*) repo-handler.php?repo=mvnrepo2&relpath=$1&compPath=$0", false)); + + } catch (ComponentLookupException e) { + getLog().warn("Component lookup exception", e); + fail("Component lookup exception"); + } catch (MapGenerationException e) { + getLog().warn("Map generation exception", e); + fail("Unable to generate output"); + } catch (IOException e) { + getLog().warn("Unable to read file", e); + fail("Unable to read file"); + } + + } + + protected void setProjectBasedir(MojoInfo mojoInfo) { + MavenProject mvnProj = mojoInfo.getProject(); + String baseDirPath = System.getProperty( "basedir" ); + if (null == baseDirPath) { + baseDirPath = new File ("").getAbsolutePath(); + } + + File projFilePath = new File (baseDirPath, "pom.xml"); + + mvnProj.setFile(projFilePath); + } +} Property changes on: plugins/sfnet-mvnrepo-plugin/trunk/src/test/java/com/mindtree/techworks/infix/plugins/sfnetmvnrepo/actions/RepoHandlerGenActionTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ParsedTextFile.java =================================================================== --- shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ParsedTextFile.java (rev 0) +++ shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ParsedTextFile.java 2010-12-21 09:19:39 UTC (rev 101) @@ -0,0 +1,115 @@ +/* + * $HeadURL$ + * + * Copyright (c) 2010 MindTree Ltd. + * + * This file is part of Infix Maven Plugins + * + * Infix Maven Plugins is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Infix Maven Plugins is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * Infix Maven Plugins. If not, see <http://www.gnu.org/licenses/>. + */ +package com.mindtree.techworks.infix.pluginscommon.test.text; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +/** + * Parses and holds a text file, can be used to compare and test parts of the + * file + * + * @author Bindul Bhowmik + * @version $Revision$ $Date$ + */ +public class ParsedTextFile { + + /** + * Option to trim lines when reading the text + */ + public static final int TRIM_LINES = 1; + + /** + * Option to skip empty lines while parsing + */ + public static final int SKIP_EMPTY_LINES = 2; + + /** + * The parsed lines + */ + private final String[] text; + + public ParsedTextFile (File file, int options) throws IOException { + this (new FileReader(file), options); + } + + public ParsedTextFile (Reader reader, int options) throws IOException { + boolean trimLines = ((options & TRIM_LINES) == TRIM_LINES); + boolean skipEmpty = ((options & SKIP_EMPTY_LINES) == SKIP_EMPTY_LINES); + + BufferedReader bufReader = new BufferedReader(reader); + List<String> lines = new ArrayList<String>(); + + String lineRead = null; + while (null != (lineRead = bufReader.readLine())) { + if (trimLines) { + lineRead = lineRead.trim(); + } + if (skipEmpty && lineRead.trim().length() == 0) { + continue; + } + lines.add(lineRead); + } + + text = lines.toArray(new String[lines.size()]); + } + + public int countLines (String needle, boolean isRegex) { + + int counter = 0; + if (isRegex) { + Pattern pattern = Pattern.compile(needle); + Matcher matcher = null; + + for (String line : text) { + if (null == matcher) { + matcher = pattern.matcher(line); + } else { + matcher.reset(line); + } + + if (matcher.matches()) { + counter ++; + } + } + } else { + for (String line : text) { + if (line.equals(needle)) { + counter ++; + } + } + } + + return counter; + } + + public boolean hasLine (String needle, boolean isRegex) { + return (countLines(needle, isRegex) >= 1); + } +} Property changes on: shared/infix-plugins-common/trunk/infix-test-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/test/text/ParsedTextFile.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Modified: shared/infix-plugins-common/trunk/infix-velocity-utils/pom.xml =================================================================== --- shared/infix-plugins-common/trunk/infix-velocity-utils/pom.xml 2010-12-21 03:26:06 UTC (rev 100) +++ shared/infix-plugins-common/trunk/infix-velocity-utils/pom.xml 2010-12-21 09:19:39 UTC (rev 101) @@ -48,6 +48,10 @@ <artifactId>velocity</artifactId> </dependency> <dependency> + <groupId>org.apache.velocity</groupId> + <artifactId>velocity-tools</artifactId> + </dependency> + <dependency> <groupId>${project.groupId}</groupId> <artifactId>infix-mojo-utils</artifactId> <version>${project.version}</version> Modified: shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/AbstractVelocityExecutor.java =================================================================== --- shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/AbstractVelocityExecutor.java 2010-12-21 03:26:06 UTC (rev 100) +++ shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/AbstractVelocityExecutor.java 2010-12-21 09:19:39 UTC (rev 101) @@ -33,6 +33,7 @@ import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; import org.apache.velocity.runtime.resource.loader.FileResourceLoader; +import org.apache.velocity.tools.ToolManager; import com.mindtree.techworks.infix.pluginscommon.mojo.MojoInfo; @@ -69,7 +70,10 @@ engine.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH, pathBuilder.toString()); engine.addProperty("file.resource.loader.class", FileResourceLoader.class.getName()); engine.init(); - VelocityContext context = new VelocityContext(contextInfo); + + ToolManager toolManager = new ToolManager(true, true); + toolManager.configure("/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml"); + VelocityContext context = new VelocityContext(contextInfo, toolManager.createContext()); Template template = null; Added: shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/PathFormatTool.java =================================================================== --- shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/PathFormatTool.java (rev 0) +++ shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/PathFormatTool.java 2010-12-21 09:19:39 UTC (rev 101) @@ -0,0 +1,46 @@ +/* + * $HeadURL$ + * + * Copyright (c) 2010 MindTree Ltd. + * + * This file is part of Infix Maven Plugins + * + * Infix Maven Plugins is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Infix Maven Plugins is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * Infix Maven Plugins. If not, see <http://www.gnu.org/licenses/>. + */ +package com.mindtree.techworks.infix.pluginscommon.velocity.tools; + +import org.apache.velocity.tools.config.DefaultKey; + + +/** + * @author Bindul Bhowmik + * @version $Revision$ $Date$ + * + */ +...@defaultkey("pathFormat") +public class PathFormatTool { + + public static String trimSlashes (String untrimmed) { + String trimmed = untrimmed; + if (null != trimmed) { + if (trimmed.endsWith("/") && trimmed.length() > 0) { + trimmed = trimmed.substring(0, trimmed.length() - 1); + } + if (trimmed.startsWith("/")) { + trimmed = trimmed.substring(1); + } + } + return trimmed; + } +} Property changes on: shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/PathFormatTool.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml =================================================================== --- shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml (rev 0) +++ shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml 2010-12-21 09:19:39 UTC (rev 101) @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + | + | Copyright (c) 2010 MindTree Ltd. + | + | This file is part of Infix. + | + | Infix is free software: you can redistribute it + | and/or modify it under the terms of the GNU General Public License as + | published by the Free Software Foundation, either version 3 of the License, + | or (at your option) any later version. + | + | Infix is distributed in the hope that it will be + | useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + | Public License for more details. + | + | You should have received a copy of the GNU General Public License along with + | Infix. If not, see <http://www.gnu.org/licenses/>. + | +--> +<tools> + <toolbox scope="application"> + <tool class="com.mindtree.techworks.infix.pluginscommon.velocity.tools.PathFormatTool"/> + </toolbox> +</tools> Property changes on: shared/infix-plugins-common/trunk/infix-velocity-utils/src/main/java/com/mindtree/techworks/infix/pluginscommon/velocity/tools/tools.xml ___________________________________________________________________ Added: svn:mime-type + text/xml Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Lotusphere 2011 Register now for Lotusphere 2011 and learn how to connect the dots, take your collaborative environment to the next level, and enter the era of Social Business. http://p.sf.net/sfu/lotusphere-d2d _______________________________________________ mvn-Infix-commits mailing list mvn-Infix-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mvn-infix-commits