From 2a90dbc8505e6a46296fbde2973f37a4dbad4d2f Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 11 Dec 2023 19:17:30 +0100 Subject: [PATCH] Update component Changes: * drop p-c-d * make it JSR330 * drop plexus XML mumbo jumbo --- pom.xml | 54 ++++++++---- .../velocity/DefaultVelocityComponent.java | 83 ------------------- .../plexus/velocity/VelocityComponent.java | 4 +- .../VelocityComponentConfigurator.java | 29 +++++++ .../internal/DefaultVelocityComponent.java | 73 ++++++++++++++++ .../resources/META-INF/plexus/components.xml | 52 ------------ src/site/xdoc/index.xml | 25 +----- .../DefaultVelocityComponentTest.java | 29 +++---- .../TestVelocityComponentConfigurator.java | 35 ++++++++ .../velocity/DefaultVelocityComponentTest.xml | 25 ------ 10 files changed, 195 insertions(+), 214 deletions(-) delete mode 100644 src/main/java/org/codehaus/plexus/velocity/DefaultVelocityComponent.java create mode 100644 src/main/java/org/codehaus/plexus/velocity/VelocityComponentConfigurator.java create mode 100644 src/main/java/org/codehaus/plexus/velocity/internal/DefaultVelocityComponent.java delete mode 100644 src/main/resources/META-INF/plexus/components.xml create mode 100644 src/test/java/org/codehaus/plexus/velocity/TestVelocityComponentConfigurator.java delete mode 100644 src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.xml diff --git a/pom.xml b/pom.xml index 2027568..7a26b23 100644 --- a/pom.xml +++ b/pom.xml @@ -1,3 +1,4 @@ + - 4.0.0 - plexus-components org.codehaus.plexus - 10.0 - + plexus-components + 14.2 plexus-velocity @@ -32,8 +31,8 @@ limitations under the License. scm:git:git@github.com:codehaus-plexus/plexus-velocity.git scm:git:git@github.com:codehaus-plexus/plexus-velocity.git - http://github.com/codehaus-plexus/plexus-velocity HEAD + http://github.com/codehaus-plexus/plexus-velocity github @@ -52,15 +51,34 @@ limitations under the License. - - org.codehaus.plexus - plexus-container-default - org.apache.velocity velocity-engine-core 2.3 + + + + org.slf4j + slf4j-api + ${slf4jVersion} + runtime + + + + + javax.inject + javax.inject + 1 + provided + + + org.eclipse.sisu + org.eclipse.sisu.inject + 0.9.0.M2 + provided + + org.junit.jupiter junit-jupiter @@ -69,33 +87,39 @@ limitations under the License. org.slf4j - slf4j-api + slf4j-simple ${slf4jVersion} test - org.slf4j - slf4j-simple - ${slf4jVersion} + com.google.inject + guice + 6.0.0 test + + org.eclipse.sisu + sisu-maven-plugin + org.apache.maven.plugins maven-scm-publish-plugin - ${project.reporting.outputDirectory} + ${project.reporting.outputDirectory} + scm-publish - site-deploy + publish-scm + site-deploy diff --git a/src/main/java/org/codehaus/plexus/velocity/DefaultVelocityComponent.java b/src/main/java/org/codehaus/plexus/velocity/DefaultVelocityComponent.java deleted file mode 100644 index 71921de..0000000 --- a/src/main/java/org/codehaus/plexus/velocity/DefaultVelocityComponent.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.codehaus.plexus.velocity; - -/* - * Copyright 2001-2016 Codehaus Foundation. - * - * Licensed 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. - */ - -import java.util.Properties; - -import org.apache.velocity.app.VelocityEngine; -import org.apache.velocity.runtime.RuntimeConstants; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.codehaus.plexus.logging.AbstractLogEnabled; - -/** - *

- * A simple velocity component implementation. - *

- * A typical configuration will look like this: - *
- *      <configuration>
- *        <properties>
- *          <property>
- *            <name>resource.loader</name>
- *            <value>classpath</value>
- *          </property>
- *          <property>
- *            <name>classpath.resource.loader.class</name>
- *            <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
- *          </property>
- *        </properties>
- *      </configuration>
- * 
- */ -public class DefaultVelocityComponent - extends AbstractLogEnabled - implements VelocityComponent, Initializable -{ - private VelocityEngine engine; - - private Properties properties; - - // ---------------------------------------------------------------------- - // - // ---------------------------------------------------------------------- - - public void initialize() - throws InitializationException - { - engine = new VelocityEngine(); - if ( properties != null ) - { - engine.setProperties( properties ); - } - - try - { - engine.init(); - } - catch ( Exception e ) - { - throw new InitializationException( "Cannot start the Velocity engine", e ); - } - } - - public VelocityEngine getEngine() - { - return engine; - } - -} diff --git a/src/main/java/org/codehaus/plexus/velocity/VelocityComponent.java b/src/main/java/org/codehaus/plexus/velocity/VelocityComponent.java index 9617227..5ff1a30 100644 --- a/src/main/java/org/codehaus/plexus/velocity/VelocityComponent.java +++ b/src/main/java/org/codehaus/plexus/velocity/VelocityComponent.java @@ -22,8 +22,8 @@ * The VelocityComponent API to access Apache Velocity Engine * configured through Plexus. */ -public interface VelocityComponent -{ +public interface VelocityComponent { + @Deprecated String ROLE = VelocityComponent.class.getName(); VelocityEngine getEngine(); diff --git a/src/main/java/org/codehaus/plexus/velocity/VelocityComponentConfigurator.java b/src/main/java/org/codehaus/plexus/velocity/VelocityComponentConfigurator.java new file mode 100644 index 0000000..a86d6da --- /dev/null +++ b/src/main/java/org/codehaus/plexus/velocity/VelocityComponentConfigurator.java @@ -0,0 +1,29 @@ +package org.codehaus.plexus.velocity; + +/* + * Copyright 2001-2016 Codehaus Foundation. + * + * Licensed 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. + */ + +import java.util.Properties; + +/** + * The VelocityComponent configuration that may configure {@link java.util.Properties} prior having them used to init + * Velocity Engine. This component is optional, is not needed if default are sufficient. + * + * @since TBD + */ +public interface VelocityComponentConfigurator { + void configure(Properties properties); +} diff --git a/src/main/java/org/codehaus/plexus/velocity/internal/DefaultVelocityComponent.java b/src/main/java/org/codehaus/plexus/velocity/internal/DefaultVelocityComponent.java new file mode 100644 index 0000000..4e30c28 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/velocity/internal/DefaultVelocityComponent.java @@ -0,0 +1,73 @@ +package org.codehaus.plexus.velocity.internal; + +/* + * Copyright 2001-2016 Codehaus Foundation. + * + * Licensed 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. + */ + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.util.Properties; + +import org.apache.velocity.app.VelocityEngine; +import org.codehaus.plexus.velocity.VelocityComponent; +import org.codehaus.plexus.velocity.VelocityComponentConfigurator; +import org.eclipse.sisu.Nullable; + +/** + * Default component implementation. The presence of {@link VelocityComponentConfigurator} is optional. + */ +@Singleton +@Named +public class DefaultVelocityComponent implements VelocityComponent { + private final VelocityEngine engine; + + @Inject + public DefaultVelocityComponent(@Nullable VelocityComponentConfigurator componentConfigurator) { + Properties properties = new Properties(); + + // fill in defaults + properties.setProperty("resource.loaders", "classpath,file"); + properties.setProperty( + "resource.loader.classpath.class", + "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + properties.setProperty( + "resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); + properties.setProperty("resource.loader.file.path", ""); + properties.setProperty("runtime.log.log_invalid_references", "false"); + properties.setProperty("resource.manager.log_when_found", "false"); + properties.setProperty( + "event_handler.include.class", "org.apache.velocity.app.event.implement.IncludeRelativePath"); + properties.setProperty("velocimacro.inline.replace_global", "true"); + properties.setProperty("parser.space_gobbling", "bc"); + + // customize if needed + if (componentConfigurator != null) { + componentConfigurator.configure(properties); + } + + VelocityEngine engine = new VelocityEngine(); + engine.setProperties(properties); + engine.init(); + + this.engine = engine; + } + + @Override + public VelocityEngine getEngine() { + return engine; + } +} diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml deleted file mode 100644 index f2f6bd1..0000000 --- a/src/main/resources/META-INF/plexus/components.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - org.codehaus.plexus.velocity.VelocityComponent - default - org.codehaus.plexus.velocity.DefaultVelocityComponent - - - - resource.loaders - classpath,file - - - resource.loader.classpath.class - org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader - - - resource.loader.file.class - org.apache.velocity.runtime.resource.loader.FileResourceLoader - - - resource.loader.file.path - - - - - runtime.log.log_invalid_references - false - - - resource.manager.log_when_found - false - - - event_handler.include.class - org.apache.velocity.app.event.implement.IncludeRelativePath - - - velocimacro.inline.replace_global - true - - - parser.space_gobbling - bc - - - - - - diff --git a/src/site/xdoc/index.xml b/src/site/xdoc/index.xml index 113fedf..0108fa7 100644 --- a/src/site/xdoc/index.xml +++ b/src/site/xdoc/index.xml @@ -16,7 +16,7 @@

A typical use: VelocityContext context = new VelocityContext(); -VelocityComponent velocityComponent = (VelocityComponent) lookup( VelocityComponent.ROLE ); +VelocityComponent velocityComponent = lookup( VelocityComponent.class ); Template template = velocityComponent.getEngine().getTemplate( "path to your template" ); StringWriter writer = new StringWriter(); template.merge( context, writer ); @@ -24,26 +24,9 @@ template.merge( context, writer );

Plexus Velocity Component comes with a default configuration: it is - expected to be customized to match local specific need. This is done by hand writing a - META-INF/plexus/components.xml Plexus descriptor.

-

A typical component configuration: - - - org.codehaus.plexus.velocity.VelocityComponent - org.codehaus.plexus.velocity.DefaultVelocityComponent - - - - resource.loaders - classpath - - - resource.loader.classpath.class - org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader - - - - ]]> + expected to be customized to match local specific need. This is done by creating + VelocityComponentConfigurator component implementation. Presence of this component is + optional.

See Velocity Configuration reference documentation for details on available configurations.

diff --git a/src/test/java/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.java b/src/test/java/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.java index bfd54ea..7fbcd53 100644 --- a/src/test/java/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.java +++ b/src/test/java/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.java @@ -20,45 +20,42 @@ import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; -import org.codehaus.plexus.PlexusTestCase; +import org.eclipse.sisu.launch.InjectedTest; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -public class DefaultVelocityComponentTest - extends PlexusTestCase -{ +public class DefaultVelocityComponentTest extends InjectedTest { @Test - public void testBasic() - throws Exception - { - DefaultVelocityComponent velocity; + public void testBasic() { + VelocityComponent velocity; VelocityContext context; String value; - velocity = lookup( VelocityComponent.ROLE ); + velocity = lookup(VelocityComponent.class); // test the properties - value = (String) velocity.getEngine().getProperty( "hello" ); + value = (String) velocity.getEngine().getProperty("hello"); - assertNotNull( value ); + assertNotNull(value); - assertEquals( "world", value ); + assertEquals("world", value); // test the rendering context = new VelocityContext(); - context.put( "variable", "Value from context" ); + context.put("variable", "Value from context"); - Template template = velocity.getEngine().getTemplate("org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm" ); + Template template = + velocity.getEngine().getTemplate("org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm"); StringWriter writer = new StringWriter(); - template.merge( context, writer ); + template.merge(context, writer); - assertEquals( "Static text -- Value from context -- More static text", writer.toString() ); + assertEquals("Static text -- Value from context -- More static text", writer.toString()); } } diff --git a/src/test/java/org/codehaus/plexus/velocity/TestVelocityComponentConfigurator.java b/src/test/java/org/codehaus/plexus/velocity/TestVelocityComponentConfigurator.java new file mode 100644 index 0000000..9de6cbd --- /dev/null +++ b/src/test/java/org/codehaus/plexus/velocity/TestVelocityComponentConfigurator.java @@ -0,0 +1,35 @@ +package org.codehaus.plexus.velocity; + +/* + * Copyright 2001-2016 Codehaus Foundation. + * + * Licensed 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. + */ + +import javax.inject.Named; +import javax.inject.Singleton; + +import java.util.Properties; + +@Singleton +@Named +public class TestVelocityComponentConfigurator implements VelocityComponentConfigurator { + @Override + public void configure(Properties properties) { + properties.setProperty("resource.loaders", "classpath"); + properties.setProperty( + "resource.loader.classpath.class", + "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + properties.setProperty("hello", "world"); + } +} diff --git a/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.xml b/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.xml deleted file mode 100644 index 2c8ac81..0000000 --- a/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - org.codehaus.plexus.velocity.VelocityComponent - default - org.codehaus.plexus.velocity.DefaultVelocityComponent - - - - resource.loaders - classpath - - - resource.loader.classpath.class - org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader - - - hello - world - - - - - -