Micha Roon
Micha Roon

Reputation: 4007

access a @SessionScoped bean from @WebService annotated EJB

One of my partners needs a SOAP interface and so I thought: lets move to an Application server. I chose Glassfish and it works great out of the box. There is just one thing I can not make it do: inject a @SessionScoped ManagedBean into the @Webservice annotated EJB.

The only way I can acces the EJB is over JNDI. I read about it here

My question is: is there a bug in Glassfish and it will work in a future version or should it work and I did something wrong.

I would like to be able to inject the property DataAccess session with the @EJB annotation but it does not work.

Below are all the files in the test project

Here is my WebService class:

@LocalBean
@Stateless
@WebService()
public class MySOAP implements Serializable {

   private DataAccess session;

   @WebMethod
   public String getUsername() {
      javax.naming.Context ctx = null;
      try {
         ctx = new javax.naming.InitialContext();
         session = ( DataAccess ) ctx.lookup( "java:comp/env/DataAccess" );
         return "user is " + session.getData();
      } catch ( NamingException e ) {
         e.printStackTrace();
      }
      return "exception occured";
   }
}

The EJB I would like to inject:

public abstract class AbstractDataBean {
   @Inject /* this just returns some text*/
   private MySessionBean session;

   public MySessionBean getSession() {
      return session;
   }
}

@LocalBean
@Stateless
public class DataAccess extends AbstractDataBean implements Serializable {

   public String getData() {
      return " data " + getSession();
   }

   @Override
   public String toString() {
      return getData();
   }
}

@Named
@SessionScoped
public class MySessionBean implements Serializable {
   static private int classCounter = 0;
   private String user;

   @PostConstruct
   public void initMySessionBean( ) {
      user = "Micha " + (++classCounter) ; //to check how many times it was called
   }

   public String getUser() {
      return user;
   }

   public void setUser( String user ) {
      this.user = user;
   }

   @Override
   public String toString() {
      return user;
   }
}

I have a web.xml to define the JNDI:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

   <session-config>
      <session-timeout>1</session-timeout>
   </session-config>

   <ejb-local-ref>
      <ejb-ref-name>DataAccess</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <local>ch.sertal.server.services.ejb.DataAccess</local>
   </ejb-local-ref>

</web-app>

a sun-jaxws.xml to define the SOAP WebService:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>

   <endpoint
               name='Mysoap'
               implementation='ch.sertal.server.services.MySOAP'
               url-pattern='/soap/Mysoap '/>
</endpoints>

and an empty beans.xml in order to have CDI functioning:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

and finally the pom.xml It is very long because I copied it from the existing project. But there should be nothing missing.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>ch.sertal</groupId>
   <artifactId>VisionWeb</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>VisionWeb Jersey Webapp</name>
   <build>
      <finalName>VisionWeb</finalName>
      <resources>
         <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <targetPath>META-INF</targetPath>
            <includes>
               <include>*.xml</include>
            </includes>
         </resource>

         <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <targetPath>.</targetPath>
            <includes>
               <include>*.properties</include>
            </includes>
         </resource>

         <resource>
            <filtering>true</filtering>
            <directory>src/main/resources/i18n</directory>
            <targetPath>.</targetPath>
            <includes>
               <include>*.properties</include>
            </includes>
         </resource>
      </resources>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <inherited>true</inherited>
            <configuration>
               <source>1.6</source>
               <target>1.6</target>
            </configuration>
         </plugin>

         <plugin>
            <groupId>org.glassfish.maven.plugin</groupId>
            <artifactId>maven-glassfish-plugin</artifactId>
            <version>2.1</version>
            <configuration>
               <glassfishDirectory>/Development/glassfish3</glassfishDirectory>
               <user>admin</user>
               <adminPassword>U36c9AqVf5Ppk4DX</adminPassword>
               <autoCreate>true</autoCreate>
               <debug>true</debug>
               <echo>false</echo>
               <terse>true</terse>
               <skip>false</skip>
               <domain>
                  <host>${glassfish.host}</host>>
                  <name>domain1</name>
                  <adminPort>4848</adminPort>
                  <httpPort>9080</httpPort>
                  <httpsPort>8181</httpsPort>
                  <iiopPort>3700</iiopPort>
                  <jmsPort>7676</jmsPort>
               </domain>
               <components>
                  <component>
                     <name>VisionWeb</name>
                     <artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
                  </component>
               </components>
            </configuration>
         </plugin>

         <!--surefire-->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
               <parallel>test</parallel>
               <threadCount>1</threadCount>
               <groups>${test.groups}</groups>
            </configuration>
         </plugin>

      </plugins>
   </build>
   <dependencies>

      <dependency>
         <groupId>org.jboss.weld</groupId>
         <artifactId>weld-api</artifactId>
         <version>1.1.Final</version>
      </dependency>

      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <version>3.0.1</version>
         <scope>provided</scope>
      </dependency>

      <dependency>
         <groupId>javax</groupId>
         <artifactId>javaee-api</artifactId>
         <version>6.0</version>
      </dependency>

      <!--jersey for RESTful services-->
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-server</artifactId>
         <version>${jersey-version}</version>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-json</artifactId>
         <version>${jersey-version}</version>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-client</artifactId>
         <version>${jersey-version}</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey.contribs</groupId>
         <artifactId>jersey-multipart</artifactId>
         <version>${jersey-version}</version>
      </dependency>

      <!--GlassFish libraries-->
      <dependency>
         <groupId>org.glassfish.distributions</groupId>
         <artifactId>web-all</artifactId>
         <version>10.0-build-20080430</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.glassfish.embedded</groupId>
         <artifactId>gf-embedded-api</artifactId>
         <version>1.0-alpha-4</version>
         <scope>test</scope>
      </dependency>

      <!--peristence & database-->

      <!-- hsqldb -->
      <dependency>
         <groupId>org.hsqldb</groupId>
         <artifactId>hsqldb</artifactId>
         <version>2.2.6</version>
      </dependency>

      <!-- persistence -->
      <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>eclipselink</artifactId>
         <version>${eclipselink.version}</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>javax.persistence</artifactId>
         <version>${eclipselink.jpa.version}</version>
         <scope>compile</scope>
      </dependency>

      <!--SHIRO Authentication-->
      <dependency>
         <groupId>org.apache.shiro</groupId>
         <artifactId>shiro-core</artifactId>
         <version>1.1.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.shiro</groupId>
         <artifactId>shiro-web</artifactId>
         <version>1.1.0</version>
      </dependency>

      <!--POI components-->
      <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi</artifactId>
         <version>3.7</version>
      </dependency>

      <!--PDF Box-->
      <dependency>
         <groupId>org.apache.pdfbox</groupId>
         <artifactId>pdfbox</artifactId>
         <version>1.6.0</version>
      </dependency>

      <!--XML processing-->
      <dependency>
         <groupId>dom4j</groupId>
         <artifactId>dom4j</artifactId>
         <version>1.6.1</version>
      </dependency>

      <!--testing-->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.8.2</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>6.0.1</version>
         <scope>test</scope>
      </dependency>

   </dependencies>

   <!--properties-->
   <properties>
      <jersey-version>1.8</jersey-version>
      <jpa.unit>hsqldb.server</jpa.unit>
      <test.jpa.unit>hsqldb.testr</test.jpa.unit>

      <eclipselink.version>2.3.0</eclipselink.version>
      <eclipselink.jpa.version>2.0.3</eclipselink.jpa.version>
   </properties>

   <!--profiles-->
   <profiles>
      <profile>
         <id>mode.alpha.dev</id>
         <activation>
            <activeByDefault>false</activeByDefault>
            <property>
               <name>environment</name>
               <value>server</value>
            </property>
         </activation>
         <properties>
            <delete.files>true</delete.files>

            <log.level>FINER</log.level>
            <jersey.log.level>WARNING</jersey.log.level>
            <jpa.log.level>INFO</jpa.log.level>
            <test.groups>server-tests</test.groups>
            <db.server.name>localhost</db.server.name>
            <img-basedir>/opt/sertal/data</img-basedir>

            <!--the url for tomcat 7 has changed. this is why /html has been appended to the below URL-->
            <tomcat.manager>http://dev.sertal.net:7070/manager/html</tomcat.manager>
            <tomcat.username>sertaladmin</tomcat.username>
            <tomcat.password>29VeK0Ul</tomcat.password>
            <tomcat.context>/VisionWeb</tomcat.context>

            <test.groups>none</test.groups>

            <!-- the date at the end marks the version of the data model -->
            <hsqldb.data.path>/opt/sertal/data/hsqldb-dev-20110813</hsqldb.data.path>
         </properties>
      </profile>
   </profiles>

   <!--repositories and plugin repos-->
   <repositories>
      <!--glass fish-->
      <repository>
         <id>glassfish.java.net</id>
         <name>GlassFish Maven Repository</name>
         <url>http://download.java.net/maven/glassfish</url>
         <layout>default</layout>
      </repository>
      <repository>
         <id>m2.java.net</id>
         <name>Java.net Maven 2 Repository</name>
         <url>http://download.java.net/maven/2</url>
         <layout>default</layout>
      </repository>
      <!--eclipse link-->
      <repository>
         <id>eclipselink.repo</id>
         <name>eclipselink maven repository</name>
         <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
         <snapshots>
            <enabled>true</enabled>
         </snapshots>
      </repository>
   </repositories>
   <pluginRepositories>
      <pluginRepository>
         <id>m2.java.net</id>
         <name>Java.net Maven 2 Repository</name>
         <url>http://download.java.net/maven/2</url>
         <layout>default</layout>
      </pluginRepository>

      <pluginRepository>
         <id>jboss-public-repository-group</id>
         <name>JBoss Public Maven Repository Group</name>
         <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
         <layout>default</layout>
         <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
         </releases>
         <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
         </snapshots>
      </pluginRepository>
   </pluginRepositories>
</project>

Spare yourself the advice, I will not switch to JBoss :-)

Upvotes: 0

Views: 1796

Answers (1)

LightGuard
LightGuard

Reputation: 5378

The Session Scope isn't active for web derives per section 6.7.2 of JSR 299, it's not a bug. If you think about it, there isn't much point as the session doesn't really follow from one web service request to the next.

Also, you said you won't switch to JBoss, out of curiosity, what are your reasons?

Upvotes: 5

Related Questions