You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by wo...@apache.org on 2009/10/09 12:34:34 UTC
svn commit: r823509 - in /portals/pluto/trunk/pluto-container/src:
main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java
test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java
Author: woonsan
Date: Fri Oct 9 10:34:34 2009
New Revision: 823509
URL: http://svn.apache.org/viewvc?rev=823509&view=rev
Log:
PLUTO-580: Allowing portlet descriptors which do not have namespace uris but have version attributes.
Modified:
portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java
portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java
Modified: portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java?rev=823509&r1=823508&r2=823509&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java (original)
+++ portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletAppDescriptorServiceImpl.java Fri Oct 9 10:34:34 2009
@@ -31,6 +31,11 @@
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
@@ -142,28 +147,70 @@
*/
@SuppressWarnings("unchecked")
- public PortletApplicationDefinition read(String name, String contextPath, InputStream in) throws IOException {
+ public PortletApplicationDefinition read(String name, String contextPath, InputStream in) throws IOException
+ {
JAXBElement app = null;
- try {
- JAXBContext jc = JAXBContext.newInstance(
- "org.apache.pluto.container.om.portlet10.impl" + ":" +
- "org.apache.pluto.container.om.portlet.impl", PortletAppDescriptorServiceImpl.class.getClassLoader());
-
+
+ try
+ {
+ ClassLoader containerClassLoader = PortletAppDescriptorServiceImpl.class.getClassLoader();
+ JAXBContext jc = JAXBContext.newInstance("org.apache.pluto.container.om.portlet10.impl" + ":" +
+ "org.apache.pluto.container.om.portlet.impl",
+ containerClassLoader);
+
+ XMLInputFactory inputFactory = getXMLInputFactory(containerClassLoader);
+ XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
+ StreamReaderDelegate delegatingStreamReader = new StreamReaderDelegate(streamReader)
+ {
+ private String adjustedNamespaceURI = null;
+
+ @Override
+ public int next() throws XMLStreamException
+ {
+ int eventCode = super.next();
+ if (eventCode == XMLEvent.START_ELEMENT && "portlet-app".equals(getLocalName()))
+ {
+ String version = getAttributeValue(null, "version");
+ if ("1.0".equals(version))
+ {
+ adjustedNamespaceURI = "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd";
+ }
+ else if ("2.0".equals(version))
+ {
+ adjustedNamespaceURI = "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd";
+ }
+ }
+ return eventCode;
+ }
+
+ @Override
+ public String getNamespaceURI()
+ {
+ String namespaceURI = super.getNamespaceURI();
+ return (namespaceURI != null ? namespaceURI : adjustedNamespaceURI);
+ }
+ };
+
Unmarshaller u = jc.createUnmarshaller();
u.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
- app = (JAXBElement) u.unmarshal(in);
- }catch (JAXBException jaxbEx){
+ app = (JAXBElement) u.unmarshal(delegatingStreamReader);
+ }
+ catch (JAXBException jaxbEx)
+ {
final IOException ioe = new IOException(jaxbEx.getMessage());
ioe.initCause(jaxbEx);
throw ioe;
}
- catch(Exception me) {
+ catch(Exception me)
+ {
final IOException ioe = new IOException(me.getLocalizedMessage());
ioe.initCause(me);
throw new IOException(me.getLocalizedMessage());
}
+
PortletApplicationDefinition pad = null;
+
if (app.getValue() instanceof org.apache.pluto.container.om.portlet10.impl.PortletAppType)
{
pad = ((org.apache.pluto.container.om.portlet10.impl.PortletAppType)app.getValue()).upgrade();
@@ -172,8 +219,10 @@
{
pad = (PortletApplicationDefinition)app.getValue();
}
+
pad.setName(name);
pad.setContextPath(contextPath);
+
return pad;
}
@@ -285,4 +334,34 @@
throw new IOException(me.getLocalizedMessage());
}
}
+
+ /*
+ * Because this service can be invoked from a portlet servlet with its own context class loader,
+ * <CODE>XMLInputFactory</CODE> should be created while the class loader switched to the container's.
+ */
+ private XMLInputFactory getXMLInputFactory(ClassLoader classLoader)
+ {
+ XMLInputFactory inputFactory = null;
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+
+ try
+ {
+ if (contextClassLoader != classLoader)
+ {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ }
+
+ inputFactory = XMLInputFactory.newInstance();
+ }
+ finally
+ {
+ if (contextClassLoader != classLoader)
+ {
+ Thread.currentThread().setContextClassLoader(contextClassLoader);
+ }
+ }
+
+ return inputFactory;
+ }
+
}
Modified: portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java?rev=823509&r1=823508&r2=823509&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java (original)
+++ portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/container/impl/JaxBDescriptorServiceImplTest.java Fri Oct 9 10:34:34 2009
@@ -20,15 +20,17 @@
import java.io.IOException;
import java.io.InputStream;
+import javax.xml.parsers.ParserConfigurationException;
+
import junit.framework.Assert;
import junit.framework.TestCase;
-import org.apache.pluto.container.impl.PortletAppDescriptorServiceImpl;
import org.apache.pluto.container.om.portlet.PortletApplicationDefinition;
import org.apache.pluto.container.om.portlet.PortletDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.xml.sax.SAXException;
/**
* Testclass for testing jaxb xml 2 java binding (portlet.xml)
@@ -37,11 +39,13 @@
public class JaxBDescriptorServiceImplTest extends TestCase{
private StringBuffer xmlBegin286 = new StringBuffer();
+ private StringBuffer xmlBegin286NoNamespaceURI = new StringBuffer();
private StringBuffer portlet286 = new StringBuffer();
private StringBuffer attrs286 = new StringBuffer();
private StringBuffer xmlEnd = new StringBuffer();
private StringBuffer xmlBegin168 = new StringBuffer();
+ private StringBuffer xmlBegin168NoNamespaceURI = new StringBuffer();
private StringBuffer portlet168 = new StringBuffer();
private StringBuffer attrs168 = new StringBuffer();
@@ -64,6 +68,9 @@
"xmlns:portlet=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\"\n" +
"xsi:schemaLocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\n" +
"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\">\n ");
+ xmlBegin286NoNamespaceURI.append("" +
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+ "<portlet-app id=\"id1\" version=\"2.0\">");
portlet286.append(""+
"<portlet id=\"id2\">\n" +
"<description xml:lang=\"de\">description</description>\n" +
@@ -184,6 +191,9 @@
"xmlns:portlet=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\"\n" +
"xsi:schemaLocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\n" +
"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\">\n ");
+ xmlBegin168NoNamespaceURI.append("" +
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+ "<portlet-app id=\"id1\" version=\"1.0\">");
portlet168.append(""+
"<portlet id=\"id2\">\n" +
"<description xml:lang=\"de\">description</description>\n" +
@@ -313,66 +323,109 @@
in = new ByteArrayInputStream(xml168.toString().getBytes());
PortletApplicationDefinition portletApp168 = jaxb.read("test", "/test", in);
- // test if portlet has the right params:
-
- // test jsr168 compliant portlets
- PortletDefinition portlet168 = (PortletDefinition)portletApp168.getPortlets().get(0);
- assertTrue(portlet168.getExpirationCache()==50);
- assertEquals(portlet168.getPortletName(),"portlet168");
- assertEquals(portlet168.getInitParams().get(0).getParamValue(),"value");
- assertEquals(portlet168.getSecurityRoleRefs().get(0).getRoleLink(), "role-link");
- assertEquals(portletApp168.getCustomPortletModes().get(0).getPortletMode(), "portlet-mode");
- assertEquals(portletApp168.getCustomWindowStates().get(0).getWindowState(), "window-state");
- assertEquals(portletApp168.getUserAttributes().get(0).getName(), "name" );
- assertEquals(portletApp168.getSecurityConstraints().get(0).getPortletNames().get(0), "portlet-name");
- assertEquals(portlet168.getExpirationCache(), 50);
- // id (isn't supported yet)
-// assertFalse(portletApp.getId().equals("id2"));
-// assertTrue(portletApp.getId().equals("id1"));
-
- jaxb.write(portletApp168, System.out);
- // portlet id
- PortletDefinition portlet286 = (PortletDefinition)portletApp286.getPortlets().get(0);
-// assertTrue(portlet1.getId().equals("id2"));
-
- // portlet class
- assertTrue(portlet286.getPortletClass().equals("portlet-class"));
-
- // portlet info
- // id isn't supported yet
-// assertTrue(portlet1.getPortletInfo().getId().equals("info1"));
- assertTrue(portlet286.getPortletInfo().getTitle().equals("title"));
-
- assertEquals("supports size should be 3", 3, portlet286.getSupports().size());
-
- assertEquals(portletApp286.getCustomPortletModes().get(0).getPortletMode(), "portlet-mode");
- assertEquals(portletApp286.getCustomWindowStates().get(0).getWindowState(), "window-state");
- assertEquals(portletApp286.getUserAttributes().get(0).getName(), "name" );
- assertEquals(portletApp286.getSecurityConstraints().get(0).getPortletNames().get(0), "portlet-name");
- assertEquals(portletApp286.getEventDefinitions().get(0).getValueType(), "java-class");
-// assertEquals(portletApp286.getRender().get(0).getName(), "QName");
- assertEquals(portletApp286.getFilters().get(0).getLifecycles().get(0), "lifecycle");
- assertEquals(portletApp286.getFilterMappings().get(0).getPortletNames().get(0), "portlet-name");
- assertEquals(portletApp286.getResourceBundle(), "resource-bundle");
- assertEquals(portletApp286.getVersion(), "2.0");
-
- // test container runtime options
- assertEquals(portletApp286.getContainerRuntimeOptions().size(),1);
- assertEquals(portletApp286.getContainerRuntimeOptions().get(0).getName(),"Runtime-Option-Portlet-App");
- assertEquals(portletApp286.getContainerRuntimeOptions().get(0).getValues().get(0),"false");
-
- assertEquals(portlet286.getContainerRuntimeOptions().size(),1);
- assertEquals(portlet286.getContainerRuntimeOptions().get(0).getName(),"Runtime-Option");
- assertEquals(portlet286.getContainerRuntimeOptions().get(0).getValues().get(0),"true");
- assertEquals(portlet286.getExpirationCache(), 100);
-
- jaxb.write(portletApp286, System.out);
+ validatePortletApp168AndPortletApp286(portletApp168, portletApp286);
} catch (IOException e) {
- fail("exception was thrown");
+ fail("exception was thrown: " + e);
}
+
}
+ /**
+ * Test method for {@link org.apache.pluto.descriptors.services.jaxb.JaxBDescriptorServiceImpl#read(java.net.URL)}
+ * with descriptors not having namespace.
+ * @throws ParserConfigurationException
+ * @throws SAXException
+ */
+ @Test
+ public void testReadNoNamespaceURI() throws IOException, ParserConfigurationException, SAXException {
+
+ try {
+
+ StringBuffer xml286NoNamespaceURI = new StringBuffer()
+ .append(xmlBegin286NoNamespaceURI).append(portlet286)
+ .append(attrs286).append(xmlEnd);
+
+ StringBuffer xml168NoNamespaceURI = new StringBuffer()
+ .append(xmlBegin168NoNamespaceURI).append(portlet168)
+ .append(attrs168).append(xmlEnd);
+
+ InputStream in = new ByteArrayInputStream(xml286NoNamespaceURI.toString().getBytes());
+
+ PortletApplicationDefinition portletApp286NoNamespaceURI = jaxb.read("test", "/test", in);
+
+ in = new ByteArrayInputStream(xml168NoNamespaceURI.toString().getBytes());
+ PortletApplicationDefinition portletApp168NoNamespaceURI = jaxb.read("test", "/test", in);
+
+ validatePortletApp168AndPortletApp286(portletApp168NoNamespaceURI, portletApp286NoNamespaceURI);
+
+ } catch (IOException e) {
+ fail("exception was thrown: " + e);
+ }
+
+ }
+
+ /**
+ * Validates if portlet has the right params for {@link #testRead()} and {@link #testReadNoNamespaceURI()}.
+ * @param portletApp168
+ * @param portletApp286
+ * @throws IOException
+ */
+ private void validatePortletApp168AndPortletApp286(PortletApplicationDefinition portletApp168, PortletApplicationDefinition portletApp286) throws IOException {
+ // test jsr168 compliant portlets
+ PortletDefinition portlet168 = (PortletDefinition)portletApp168.getPortlets().get(0);
+ assertTrue(portlet168.getExpirationCache()==50);
+ assertEquals(portlet168.getPortletName(),"portlet168");
+ assertEquals(portlet168.getInitParams().get(0).getParamValue(),"value");
+ assertEquals(portlet168.getSecurityRoleRefs().get(0).getRoleLink(), "role-link");
+ assertEquals(portletApp168.getCustomPortletModes().get(0).getPortletMode(), "portlet-mode");
+ assertEquals(portletApp168.getCustomWindowStates().get(0).getWindowState(), "window-state");
+ assertEquals(portletApp168.getUserAttributes().get(0).getName(), "name" );
+ assertEquals(portletApp168.getSecurityConstraints().get(0).getPortletNames().get(0), "portlet-name");
+ assertEquals(portlet168.getExpirationCache(), 50);
+ // id (isn't supported yet)
+// assertFalse(portletApp.getId().equals("id2"));
+// assertTrue(portletApp.getId().equals("id1"));
+
+ jaxb.write(portletApp168, System.out);
+ // portlet id
+ PortletDefinition portlet286 = (PortletDefinition)portletApp286.getPortlets().get(0);
+// assertTrue(portlet1.getId().equals("id2"));
+
+ // portlet class
+ assertTrue(portlet286.getPortletClass().equals("portlet-class"));
+
+ // portlet info
+ // id isn't supported yet
+// assertTrue(portlet1.getPortletInfo().getId().equals("info1"));
+ assertTrue(portlet286.getPortletInfo().getTitle().equals("title"));
+
+ assertEquals("supports size should be 3", 3, portlet286.getSupports().size());
+
+ assertEquals(portletApp286.getCustomPortletModes().get(0).getPortletMode(), "portlet-mode");
+ assertEquals(portletApp286.getCustomWindowStates().get(0).getWindowState(), "window-state");
+ assertEquals(portletApp286.getUserAttributes().get(0).getName(), "name" );
+ assertEquals(portletApp286.getSecurityConstraints().get(0).getPortletNames().get(0), "portlet-name");
+ assertEquals(portletApp286.getEventDefinitions().get(0).getValueType(), "java-class");
+// assertEquals(portletApp286.getRender().get(0).getName(), "QName");
+ assertEquals(portletApp286.getFilters().get(0).getLifecycles().get(0), "lifecycle");
+ assertEquals(portletApp286.getFilterMappings().get(0).getPortletNames().get(0), "portlet-name");
+ assertEquals(portletApp286.getResourceBundle(), "resource-bundle");
+ assertEquals(portletApp286.getVersion(), "2.0");
+
+ // test container runtime options
+ assertEquals(portletApp286.getContainerRuntimeOptions().size(),1);
+ assertEquals(portletApp286.getContainerRuntimeOptions().get(0).getName(),"Runtime-Option-Portlet-App");
+ assertEquals(portletApp286.getContainerRuntimeOptions().get(0).getValues().get(0),"false");
+
+ assertEquals(portlet286.getContainerRuntimeOptions().size(),1);
+ assertEquals(portlet286.getContainerRuntimeOptions().get(0).getName(),"Runtime-Option");
+ assertEquals(portlet286.getContainerRuntimeOptions().get(0).getValues().get(0),"true");
+ assertEquals(portlet286.getExpirationCache(), 100);
+
+ jaxb.write(portletApp286, System.out);
+ }
+
/**
* Test method for {@link org.apache.pluto.descriptors.services.jaxb.JaxBDescriptorServiceImpl#writePortletApp(com.sun.java.xml.ns.portlet.portlet_app_2_0_xsd.PortletAppType, java.io.OutputStream)}.
*/
@@ -411,5 +464,5 @@
PortletDefinition pd = (PortletDefinition) portletDD.getPortlets().get( 0 );
assertEquals( pd.getExpirationCache(), 0 );
}
-
+
}