留言板

ClassCastException: PortletPreferencesImpl and Wrapper

thumbnail
nicolas saubi,修改在8 年前。

ClassCastException: PortletPreferencesImpl and Wrapper

Junior Member 帖子: 43 加入日期: 09-10-28 最近的帖子
Env : Linux Debian
Liferay : 6.2 GA4 (CE)
Framework : Struts 1.2.9
Builder : Maven

Hi there,
we have an issue after migrating from L6.1.2 to L6.2.

Exception is :
Caused by: java.lang.ClassCastException: com.liferay.portlet.PortletPreferencesImpl cannot be cast to com.liferay.portlet.PortletPreferencesWrapper
        at com.liferay.portal.util.PortalImpl.getPreferences(PortalImpl.java:4392)
        at com.liferay.portal.util.PortalUtil.getPreferences(PortalUtil.java:1294)
        at fr.mgpat.espaceadherent.struts.RedirectionAction.execute(RedirectionAction.java:96)
        at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)


This occurs as soon as we try to use Portlet Preferences, following this tutorial :
https://www.liferay.com/fr/documentation/liferay-portal/6.2/development/-/ai/implementing-configurable-portlet-prefer-liferay-portal-6-2-dev-guide-03-en

Here is the pom or our portlet :

<!--?xml version="1.0"?-->
<project xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelversion>4.0.0</modelversion>
  <parent>
    <groupid>fr.xxxxxx.utils</groupid>
    <artifactid>commun</artifactid>
    <version>2.0.0-SNAPSHOT</version>
    <relativepath>../commun/pom.xml</relativepath>
  </parent>
  <groupid>fr.xxxxxx.portlet</groupid>
  <artifactid>espaceAdherents-portlet</artifactid>
  <version>2.0.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Espace Adherents Portlet</name>
  <distributionmanagement>
    <repository>
      <id>releases</id>
      <url>http://xxxxxxxxxxx/nexus/content/repositories/releases</url>
    </repository>
    <snapshotrepository>
      <id>snapshots</id>
      <name>Internal Snapshots</name>
      <url>http://xxxxxxxxxxxx/nexus/content/repositories/snapshots</url>
    </snapshotrepository>
  </distributionmanagement>
  <properties>
    <liferay.app.server.lib.global.dir>C:\developpement\portal62\liferay-portal-6.2\tomcat-7.0.40\lib\ext</liferay.app.server.lib.global.dir>
    <jboss.server>default</jboss.server>
    <jboss.home>C:\developpement\jboss-4.2.2.GA</jboss.home>
    <liferay.maven.plugin.version>6.1.30</liferay.maven.plugin.version>
    <liferay.version>6.2.3</liferay.version>
    <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>
    <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
    <liferay.app.server.portal.dir>C:\developpement\portal62\liferay-portal-6.2\tomcat-7.0.40\webapps\ROOT</liferay.app.server.portal.dir>
    <liferay.app.server.deploy.dir>C:\developpement\portal62\liferay-portal-6.2\tomcat-7.0.40\webapps</liferay.app.server.deploy.dir>
  </properties>
  <dependencies>
    <dependency>
      <groupid>displaytag</groupid>
      <artifactid>displaytag</artifactid>
      <version>1.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>log4j</groupid>
      <artifactid>log4j</artifactid>
      <version>1.2.14</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>fr.xxxxxx.ear</groupid>
      <artifactid>xxxxxInternetEJB</artifactid>
      <version>2.0.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>fr.xxxxxx.utils</groupid>
      <artifactid>xxxxxxMailingServicesUtils</artifactid>
      <version>2.0.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>fr.xxxxxx.utils</groupid>
      <artifactid>commonServiceUtils</artifactid>
      <version>2.0.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>fr.xxxxxx.utils</groupid>
      <artifactid>commonPortailAdherentUtils</artifactid>
      <version>2.0.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>jboss</groupid>
      <artifactid>jbossall-client</artifactid>
      <version>4.2.2.GA</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>javax</groupid>
      <artifactid>javaee-api</artifactid>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>com.liferay.portal</groupid>
      <artifactid>portal-service</artifactid>
      <version>6.2.3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>com.liferay.portal</groupid>
      <artifactid>util-bridges</artifactid>
      <version>6.2.3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>com.liferay.portal</groupid>
      <artifactid>util-taglib</artifactid>
      <version>6.2.3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>com.liferay.portal</groupid>
      <artifactid>util-java</artifactid>
      <version>6.2.3</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>fr.liferay.xxxxxx</groupid>
      <artifactid>portlet</artifactid>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>servlet-api</artifactid>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet.jsp</groupid>
      <artifactid>jsp-api</artifactid>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>com.liferay.portal</groupid>
      <artifactid>portal-client</artifactid>
      <version>6.2.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>jstl</artifactid>
      <version>1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>javax.servlet.jsp.jstl</groupid>
      <artifactid>jstl-api</artifactid>
      <version>1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>displaytag</groupid>
      <artifactid>displaytag-portlet</artifactid>
      <version>1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>fr.xxxxxx.utils</groupid>
      <artifactid>portal-bridge</artifactid>
      <version>6.2.0-RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>struts</groupid>
      <artifactid>struts</artifactid>
      <version>1.2.9</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupid>javax.xml</groupid>
      <artifactid>jaxrpc</artifactid>
      <version>1.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
        <updatepolicy>always</updatepolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatepolicy>always</updatepolicy>
      </snapshots>
      <id>central</id>
      <url>http://central</url>
    </repository>
  </repositories>
  <pluginrepositories>
    <pluginrepository>
      <releases>
        <enabled>true</enabled>
        <updatepolicy>always</updatepolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatepolicy>always</updatepolicy>
      </snapshots>
      <id>central</id>
      <url>http://central</url>
    </pluginrepository>
  </pluginrepositories>
  <build>
    <sourcedirectory>C:\workspaces_maven\espaceAdherents-portlet\src\main\java</sourcedirectory>
    <scriptsourcedirectory>C:\workspaces_maven\espaceAdherents-portlet\src\main\scripts</scriptsourcedirectory>
    <testsourcedirectory>C:\workspaces_maven\espaceAdherents-portlet\src\test\java</testsourcedirectory>
    <outputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\classes</outputdirectory>
    <testoutputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\test-classes</testoutputdirectory>
    <resources>
      <resource>
        <directory>C:\workspaces_maven\espaceAdherents-portlet\src\main\resources</directory>
      </resource>
    </resources>
    <testresources>
      <testresource>
        <directory>C:\workspaces_maven\espaceAdherents-portlet\src\test\resources</directory>
      </testresource>
    </testresources>
    <directory>C:\workspaces_maven\espaceAdherents-portlet\target</directory>
    <finalname>new_espaceAdherents-portlet</finalname>
    <pluginmanagement>
      <plugins>
        <plugin>
          <artifactid>maven-antrun-plugin</artifactid>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactid>maven-assembly-plugin</artifactid>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactid>maven-dependency-plugin</artifactid>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactid>maven-release-plugin</artifactid>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginmanagement>
    <plugins>
      <plugin>
        <artifactid>maven-compiler-plugin</artifactid>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <encoding>UTF-8</encoding>
              <source>1.6
              <target>1.6</target>
            </configuration>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <encoding>UTF-8</encoding>
              <source>1.6
              <target>1.6</target>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <encoding>UTF-8</encoding>
          <source>1.6
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactid>maven-resources-plugin</artifactid>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <artifactid>maven-clean-plugin</artifactid>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactid>maven-install-plugin</artifactid>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactid>maven-surefire-plugin</artifactid>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactid>maven-war-plugin</artifactid>
        <version>2.2</version>
        <executions>
          <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactid>maven-deploy-plugin</artifactid>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactid>maven-site-plugin</artifactid>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\site</outputdirectory>
              <reportplugins>
                <reportplugin>
                  <groupid>org.apache.maven.plugins</groupid>
                  <artifactid>maven-project-info-reports-plugin</artifactid>
                </reportplugin>
              </reportplugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\site</outputdirectory>
              <reportplugins>
                <reportplugin>
                  <groupid>org.apache.maven.plugins</groupid>
                  <artifactid>maven-project-info-reports-plugin</artifactid>
                </reportplugin>
              </reportplugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\site</outputdirectory>
          <reportplugins>
            <reportplugin>
              <groupid>org.apache.maven.plugins</groupid>
              <artifactid>maven-project-info-reports-plugin</artifactid>
            </reportplugin>
          </reportplugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputdirectory>C:\workspaces_maven\espaceAdherents-portlet\target\site</outputdirectory>
  </reporting>
</project>



And here is our class generating the exception :

package fr.xxxx;

imports [...]
import com.liferay.portal.util.PortalUtil;

public class RedirectionAction extends CustomAction {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

        //this is rising the exception
        String preferenceParameter = PortalUtil.getPreferences(request).getValue("preferenceParameter", "default");
    }
}



We badly need help here... emoticon

Thanks
thumbnail
Andrew Jardine,修改在8 年前。

RE: ClassCastException: PortletPreferencesImpl and Wrapper

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Hi Nicolas,

One thing I would try if I were you is to remove the dependency from the pom.xml and try using the liferay-plugin-package.properties to pick the struts jars that are in the portal's WEB-INF/lib. I'm guessing that what you have sis a conflict between the one you are providing and the one that is already there. This happened to me pretty recently.

I also had this issue while using JRebel recently as well (it was not bumping the cache on me so I had to disable it entirely) -- just in case you are also a JRebel user.

Let me know if that helps.
thumbnail
nicolas saubi,修改在8 年前。

RE: ClassCastException: PortletPreferencesImpl and Wrapper

Junior Member 帖子: 43 加入日期: 09-10-28 最近的帖子
Hi Andrew and thanks for your reply,

unfortunately we have the same error with your system.

We are now trying to get the preferences not from the request but from the PreferencesLocalServiceUtil. I bet that if we can somehow find the preferencesId our portlet uses, we'll be able to get the preferences object from the service and avoid the ClassCastException of PortletPreferencesWrapper (as we won't call PortalUtils.getPreferences(request)).

Keeping the thread alive with whatever we find.
thumbnail
Andrew Jardine,修改在8 年前。

RE: ClassCastException: PortletPreferencesImpl and Wrapper

Liferay Legend 帖子: 2416 加入日期: 10-12-22 最近的帖子
Can you share your code with us?
thumbnail
nicolas saubi,修改在8 年前。

RE: ClassCastException: PortletPreferencesImpl and Wrapper

Junior Member 帖子: 43 加入日期: 09-10-28 最近的帖子
Hi,

here is what we use. We still encounter some problems but no more ClassCastExceptions.


private String getValue(String key, HttpServletRequest request) throws SystemException {
             String portletId = PortalUtil.getPortletId(request);

             long companyId = PortalUtil.getCompanyId(request);
             RenderRequest renderRequest = (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
             long plid = Long.parseLong(renderRequest.getWindowID().split(LiferayPortletSession.LAYOUT_SEPARATOR)[1]);
             log.info("getValue "+key+" plid "+plid+" portletId "+portletId+" WindowID "+renderRequest.getWindowID());
             javax.portlet.PortletPreferences portletPreferences=PortletPreferencesLocalServiceUtil.fetchPreferences(companyId, 0, 3, plid, portletId);
             return portletPreferences.getValue(key, null);
}


We use getValue to access the value of a preference key.