diff --git a/api/src/main/java/org/gatein/pc/api/spi/PortalContext.java b/api/src/main/java/org/gatein/pc/api/spi/PortalContext.java index c61e382c..4f32daed 100755 --- a/api/src/main/java/org/gatein/pc/api/spi/PortalContext.java +++ b/api/src/main/java/org/gatein/pc/api/spi/PortalContext.java @@ -22,7 +22,6 @@ ******************************************************************************/ package org.gatein.pc.api.spi; -import org.gatein.common.util.Version; import org.gatein.pc.api.WindowState; import java.util.Map; @@ -36,7 +35,6 @@ */ public interface PortalContext { - public static final Version VERSION = new Version("GateIn Portlet Container", 2, 4, 1, new Version.Qualifier(Version.Qualifier.Prefix.CR, Version.Qualifier.Suffix.SUFFIX_2), "Community"); /** * Return info about the portal. Must conform to javax.portlet.PortalContext.getPortalInfo(). @@ -66,17 +64,4 @@ public interface PortalContext */ Map getProperties(); - Version.Format PORTLET_SPEC_FORMAT = new Version.Format() - { - public String toString(Version version) - { - StringBuffer buffer = new StringBuffer(version.getName()); - buffer.append("/") - .append(version.getMajor()).append('.') - .append(version.getMinor()).append('.') - .append(version.getPatch()).append('-') - .append(version.getQualifier()); - return buffer.toString(); - } - }; } diff --git a/portlet/src/main/java/org/gatein/pc/portlet/Version.java b/portlet/src/main/java/org/gatein/pc/portlet/Version.java new file mode 100644 index 00000000..27a47e07 --- /dev/null +++ b/portlet/src/main/java/org/gatein/pc/portlet/Version.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2012 eXo Platform SAS. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.gatein.pc.portlet; + +import java.io.InputStream; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + *

Determine version information from the artifact metadata. When the version cannot be determined, the + * version 0.0.0-GA is used.

+ * + * + *

This class only uses classes provided by the Java Platform in order to be used without any further dependency. + * Information is logged through {@link java.util.logging.Logger} on purpose instead of any other logger.

+ * + *

This class provides a main class that can be executed to check the version manually (hence the reason + * it does not depend on any other class than the Java Platform):

+ * + * java -cp target/pc-portlet-2.4.1.CR03-SNAPSHOT.jar org.gatein.pc.portlet.Version + * + * @author Julien Viet + */ +public class Version +{ + + /** Major and minor extracted. */ + private static final Pattern MAJOR_MINOR_PATTERN = Pattern.compile("^(\\d)+\\.(\\d)+"); + + /** . */ + public static final String VALUE; + + /** . */ + public static final int MAJOR; + + /** . */ + public static final int MINOR; + + static + { + String value = "0.0.0-GA"; + int major = 0; + int minor = 0; + try + { + Properties props = new Properties(); + InputStream in = Version.class.getResourceAsStream("/META-INF/maven/org.gatein.pc/pc-portlet/pom.properties"); + if (in != null) + { + props.load(in); + String version = props.getProperty("version"); + if (version != null) + { + version = version.trim(); + if (version.length() > 0) + { + value = version.trim(); + Matcher matcher = MAJOR_MINOR_PATTERN.matcher(value); + if (matcher.find()) + { + major = Integer.parseInt(matcher.group(1)); + minor = Integer.parseInt(matcher.group(2)); + } + } + } + } + } + catch (Exception e) + { + Logger.getLogger(Version.class.getName()).log(Level.WARNING, "Could not load version from maven", e); + } + VALUE = value; + MAJOR = major; + MINOR = minor; + } + + public static void main(String[] args) + { + System.out.println("version=" + VALUE); + System.out.println("major=" + MAJOR); + System.out.println("minor=" + MINOR); + } +} diff --git a/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/PortletContextImpl.java b/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/PortletContextImpl.java index bec502e3..9245be14 100755 --- a/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/PortletContextImpl.java +++ b/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/PortletContextImpl.java @@ -22,9 +22,9 @@ ******************************************************************************/ package org.gatein.pc.portlet.impl.jsr168.api; +import org.gatein.pc.portlet.Version; import org.gatein.pc.portlet.impl.info.ContainerPortletApplicationInfo; import org.gatein.pc.api.info.RuntimeOptionInfo; -import org.gatein.pc.api.spi.PortalContext; import javax.portlet.PortletContext; import javax.portlet.PortletRequestDispatcher; @@ -50,10 +50,6 @@ public class PortletContextImpl implements PortletContext /** . */ private ContainerPortletApplicationInfo info; - private static final String VERSION = PortalContext.VERSION.getName().replace(" ", "") + "/" - + PortalContext.VERSION.getMajor() + "." + PortalContext.VERSION.getMinor() + "." - + PortalContext.VERSION.getQualifier(); - public PortletContextImpl(ServletContext servletContext) { this.servletContext = servletContext; @@ -61,7 +57,7 @@ public PortletContextImpl(ServletContext servletContext) public String getServerInfo() { - return VERSION; + return "GateInPC/" + Version.VALUE; } public PortletRequestDispatcher getRequestDispatcher(String path) @@ -105,12 +101,12 @@ public InputStream getResourceAsStream(String s) public int getMajorVersion() { - return 2; + return Version.MAJOR; } public int getMinorVersion() { - return 0; + return Version.MINOR; } public String getMimeType(String s) diff --git a/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/AbstractPortalContext.java b/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/AbstractPortalContext.java index bdeb1e1b..48ff484c 100644 --- a/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/AbstractPortalContext.java +++ b/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/AbstractPortalContext.java @@ -26,6 +26,7 @@ import org.gatein.pc.api.Mode; import org.gatein.pc.api.WindowState; import org.gatein.pc.api.spi.PortalContext; +import org.gatein.pc.portlet.Version; import java.util.Collections; import java.util.Map; @@ -38,6 +39,11 @@ public class AbstractPortalContext implements PortalContext { + /** + * The default info returned by the Portal. + * . */ + public static final String DEFAULT_INFO = "GateIn/" + Version.VALUE; + /** . */ private static final Map EMPTY_STRING_TO_STRING_MAP = Collections.emptyMap(); @@ -87,7 +93,7 @@ public AbstractPortalContext() public String getInfo() { - return PortalContext.PORTLET_SPEC_FORMAT.toString(VERSION); + return DEFAULT_INFO; } public Set getWindowStates() diff --git a/test/core/src/main/java/org/gatein/pc/test/unit/TestConstants.java b/test/core/src/main/java/org/gatein/pc/test/unit/TestConstants.java deleted file mode 100755 index a2f90a4b..00000000 --- a/test/core/src/main/java/org/gatein/pc/test/unit/TestConstants.java +++ /dev/null @@ -1,43 +0,0 @@ -/****************************************************************************** - * JBoss, a division of Red Hat * - * Copyright 2006, Red Hat Middleware, LLC, and individual * - * contributors as indicated by the @authors tag. See the * - * copyright.txt in the distribution for a full listing of * - * individual contributors. * - * * - * This is free software; you can redistribute it and/or modify it * - * under the terms of the GNU Lesser General Public License as * - * published by the Free Software Foundation; either version 2.1 of * - * the License, or (at your option) any later version. * - * * - * This software 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 * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this software; if not, write to the Free * - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. * - ******************************************************************************/ -package org.gatein.pc.test.unit; - -/** - * Constants for test asserts - * - * @author Boleslaw Dawidowicz - * @version $Revision: 5510 $ - */ -public final class TestConstants -{ - - /** String from PortletContext.getServerInfo(). */ - public static final String SERVER_INVO = "JBossPortal/1.0"; - - /** int for PortletContext.getMinorVersion(). */ - public static final int MINOR_VERSION = 0; - - /** int for PortletContext.getMajorVersion(). */ - public static final int MAJOR_VERSION = 2; - -} diff --git a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portalcontext/PortalInfo.java b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portalcontext/PortalInfo.java index fcb67b71..feeb71cf 100644 --- a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portalcontext/PortalInfo.java +++ b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portalcontext/PortalInfo.java @@ -22,7 +22,6 @@ ******************************************************************************/ package org.gatein.pc.test.portlet.jsr168.api.portalcontext; -import org.gatein.pc.api.spi.PortalContext; import org.gatein.pc.test.unit.web.UTP1; import org.gatein.pc.test.unit.Assertion; import org.gatein.pc.test.unit.PortletTestCase; @@ -36,7 +35,10 @@ import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; -import static org.gatein.pc.test.unit.Assert.assertTrue; +import java.util.regex.Pattern; + +import static org.gatein.pc.test.unit.Assert.assertNotNull; +import static org.gatein.pc.test.unit.Assert.fail; /** * @author Julien Viet @@ -45,6 +47,10 @@ @TestCase({Assertion.API286_PORTAL_CONTEXT_2}) public class PortalInfo { + + /** . */ + Pattern PATTERN = Pattern.compile("^[^/]+/[^/]+$"); + public PortalInfo(PortletTestCase seq) { seq.bindAction(0, UTP1.RENDER_JOIN_POINT, new PortletRenderTestAction() @@ -52,16 +58,11 @@ public PortalInfo(PortletTestCase seq) protected Response run(Portlet portlet, RenderRequest request, RenderResponse response, PortletTestContext context) { String info = request.getPortalContext().getPortalInfo(); - - assertTrue(info.length() > 0); - - String[] components = info.split("/"); - assertTrue(components.length == 2); - assertTrue(PortalContext.VERSION.getName().equals(components[0])); - String version = PortalContext.VERSION.getMajor() + "." + PortalContext.VERSION.getMinor() + "." - + PortalContext.VERSION.getPatch() + "-" + PortalContext.VERSION.getQualifier(); - assertTrue(version.equals(components[1])); - + assertNotNull(info); + if (!PATTERN.matcher(info).matches()) + { + fail("Bad portal context info " + info); + } return new EndTestResponse(); } }); diff --git a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/ServerInfo.java b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/ServerInfo.java index 8f521601..34e52684 100644 --- a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/ServerInfo.java +++ b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/ServerInfo.java @@ -47,10 +47,7 @@ public class ServerInfo { /** . */ - private static final String VERSION_REGEX = "GateInPortletContainer/[0-9]+\\.[0-9]+(|\\.CR[0-9]{2})(|\\.SNAPSHOT)(|\\.ALPHA[0-9]{2})(|\\.BETA[0-9]{2})(|\\.SP[0-9]{2})(|\\.CP[0-9]{2})(|\\.GA)"; - - /** . */ - private static final Pattern VERSION_PATTERN = Pattern.compile(VERSION_REGEX, Pattern.CASE_INSENSITIVE); + private static final Pattern VERSION_PATTERN = Pattern.compile("GateInPC/[^/]+"); public ServerInfo(PortletTestCase seq) { diff --git a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/Version.java b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/Version.java index 6f2b5009..049f5a24 100644 --- a/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/Version.java +++ b/test/core/src/test/java/org/gatein/pc/test/portlet/jsr168/api/portletcontext/Version.java @@ -25,12 +25,10 @@ import org.gatein.pc.test.unit.web.UTP1; import org.gatein.pc.test.unit.PortletTestCase; import org.gatein.pc.test.unit.PortletTestContext; -import org.gatein.pc.test.unit.TestConstants; import org.gatein.pc.test.unit.Assertion; import org.gatein.pc.test.unit.actions.PortletRenderTestAction; import org.gatein.pc.test.unit.annotations.TestCase; import org.gatein.pc.test.unit.web.AbstractUniversalTestPortlet; -import static org.gatein.pc.test.unit.Assert.assertEquals; import org.gatein.pc.test.unit.protocol.response.Response; import org.gatein.pc.test.unit.protocol.response.EndTestResponse; @@ -55,9 +53,9 @@ public Version(PortletTestCase seq) protected Response run(Portlet portlet, RenderRequest request, RenderResponse response, PortletTestContext context) { AbstractUniversalTestPortlet aport = (AbstractUniversalTestPortlet)portlet; - - assertEquals(TestConstants.MINOR_VERSION, aport.getPortletContext().getMinorVersion()); - assertEquals(TestConstants.MAJOR_VERSION, aport.getPortletContext().getMajorVersion()); + // Just test we can retrieve any value without an error + aport.getPortletContext().getMinorVersion(); + aport.getPortletContext().getMajorVersion(); return new EndTestResponse(); } });