Java/EJB3/Open EJB

Материал из Java эксперт
Перейти к: навигация, поиск

component interfaces (Open EJB)

custom injection (Open EJB)

EJB security testing

injection of datasource

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.Stateful; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; import java.util.ArrayList; @Stateful(name = "Movies") public class MoviesImpl implements Movies {

   /**
    * The field name "movieDatabase" matches the DataSource we
    * configure in the TestCase via :
    * p.put("movieDatabase", "new://Resource?type=DataSource");
    *
    * This would also match an equivalent delcaration in an openejb.xml:
    * <Resource id="movieDatabase" type="DataSource"/>
    *
    * If you"d like the freedom to change the field name without
    * impact on your configuration you can set the "name" attribute
    * of the @Resource annotation to "movieDatabase" instead.
    */
   @Resource
   private DataSource movieDatabase;
   @PostConstruct
   private void construct() throws Exception {
       Connection connection = movieDatabase.getConnection();
       try {
           PreparedStatement stmt = connection.prepareStatement("CREATE TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)");
           stmt.execute();
       } finally {
           connection.close();
       }
   }
   public void addMovie(Movie movie) throws Exception {
       Connection conn = movieDatabase.getConnection();
       try {
           PreparedStatement sql = conn.prepareStatement("INSERT into movie (director, title, year) values (?, ?, ?)");
           sql.setString(1, movie.getDirector());
           sql.setString(2, movie.getTitle());
           sql.setInt(3, movie.getYear());
           sql.execute();
       } finally {
           conn.close();
       }
   }
   public void deleteMovie(Movie movie) throws Exception {
       Connection conn = movieDatabase.getConnection();
       try {
           PreparedStatement sql = conn.prepareStatement("DELETE from movie where director = ? AND title = ? AND year = ?");
           sql.setString(1, movie.getDirector());
           sql.setString(2, movie.getTitle());
           sql.setInt(3, movie.getYear());
           sql.execute();
       } finally {
           conn.close();
       }
   }
   public List<Movie> getMovies() throws Exception {
       ArrayList<Movie> movies = new ArrayList<Movie>();
       Connection conn = movieDatabase.getConnection();
       try {
           PreparedStatement sql = conn.prepareStatement("SELECT director, title, year from movie");
           ResultSet set = sql.executeQuery();
           while ( set.next() ) {
               Movie movie = new Movie();
               movie.setDirector(set.getString("director"));
               movie.setTitle(set.getString("title"));
               movie.setYear(set.getInt("year"));
               movies.add( movie );
           }
       } finally {
           conn.close();
       }
       return movies;
   }

}


 </source>
   
  
 
  



injection of ejbs

injection of entitymanager and entity class

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.jpa; import org.superbiz.injection.jpa.Movie; import org.superbiz.injection.jpa.Movies; import javax.ejb.Stateless; import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.persistence.PersistenceContextType; import java.util.List; @Stateful(name = "Movies") public class MoviesImpl implements Movies {

   @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
   private EntityManager entityManager;
   public void addMovie(Movie movie) throws Exception {
       entityManager.persist(movie);
   }
   public void deleteMovie(Movie movie) throws Exception {
       entityManager.remove(movie);
   }
   public List<Movie> getMovies() throws Exception {
       Query query = entityManager.createQuery("SELECT m from Movie as m");
       return query.getResultList();
   }

} /////////////////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.jpa; import org.superbiz.injection.jpa.Movie; import java.util.List; /**

* @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
*/

public interface Movies {

   void addMovie(Movie movie) throws Exception ;
   void deleteMovie(Movie movie) throws Exception ;
   List<Movie> getMovies() throws Exception ;

}

/////////////////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.jpa; import javax.persistence.Entity; @Entity public class Movie {

   private String director;
   private String title;
   private int year;
   public Movie() {
   }
   public Movie(String director, String title, int year) {
       this.director = director;
       this.title = title;
       this.year = year;
   }
   public String getDirector() {
       return director;
   }
   public void setDirector(String director) {
       this.director = director;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }

}


 </source>
   
  
 
  



injection of env entry

jpa hibernate (Open EJB)

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.h3jpa; import javax.persistence.*; @Entity public class Movie {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;
   private String director;
   private String title;
   private int year;
   public Movie() {
   }
   public Movie(String director, String title, int year) {
       this.director = director;
       this.title = title;
       this.year = year;
   }
   public String getDirector() {
       return director;
   }
   public void setDirector(String director) {
       this.director = director;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }

} //////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.h3jpa; import org.superbiz.injection.h3jpa.Movie; import java.util.List; /**

* @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
*/

public interface Movies {

   void addMovie(Movie movie) throws Exception ;
   void deleteMovie(Movie movie) throws Exception ;
   List<Movie> getMovies() throws Exception ;

}

//////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.h3jpa; import org.superbiz.injection.h3jpa.Movie; import org.superbiz.injection.h3jpa.Movies; import javax.ejb.Stateful; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.persistence.PersistenceContextType; import java.util.List; @Stateful(name = "Movies") public class MoviesImpl implements Movies {

   @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
   private EntityManager entityManager;
   public void addMovie(Movie movie) throws Exception {
       entityManager.persist(movie);
   }
   public void deleteMovie(Movie movie) throws Exception {
       entityManager.remove(movie);
   }
   public List<Movie> getMovies() throws Exception {
       Query query = entityManager.createQuery("SELECT m from Movie as m");
       return query.getResultList();
   }

}


 </source>
   
  
 
  



LocalHome annotation (Open EJB)

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.hello; import javax.ejb.EJBLocalHome; import javax.ejb.CreateException; /**

* @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
*/

public interface HelloEjbLocalHome extends EJBLocalHome {

   HelloEjbLocal create() throws CreateException;

} /////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.hello; import javax.ejb.EJBLocalObject; /**

* @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
*/

public interface HelloEjbLocal extends EJBLocalObject {

   String sayHello();

}

/////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.hello; import javax.ejb.LocalHome; import javax.ejb.Stateless; /**

* @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
*/

@Stateless @LocalHome(HelloEjbLocalHome.class) public class HelloBean {

   public String sayHello() {
       return "Hello, World!";
   }

}


 </source>
   
  
 
  



Remote stab

   <source lang="java">
 

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

//START SNIPPET: code package org.superbiz.telephone; import javax.ejb.Remote; import javax.ejb.Stateful; import java.util.ArrayList; import java.util.List; @Remote @Stateful public class TelephoneBean implements Telephone {

   private static final String[] answers = {
           "How nice.",
           "Oh, of course.",
           "Interesting.",
           "Really?",
           "No.",
           "Definitely.",
           "I wondered about that.",
           "Good idea.",
           "You don"t say!",
   };
   private List<String> conversation = new ArrayList<String>();
   public void speak(String words) {
       conversation.add(words);
   }
   public String listen() {
       if (conversation.size() == 0) {
           return "Nothing has been said";
       }
       String lastThingSaid = conversation.get(conversation.size() - 1);
       return answers[Math.abs(lastThingSaid.hashCode()) % answers.length];
   }

} //END SNIPPET: code

 </source>
   
  
 
  



Stateful session bean simple

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.counter; import javax.ejb.Stateful; /**

* This is an EJB 3 style pojo stateful session bean
* Every stateful session bean implementation must be annotated
* using the annotation @Stateful
* This EJB has 2 business interfaces: CounterRemote, a remote business
* interface, and CounterLocal, a local business interface
*
* Per EJB3 rules when the @Remote or @Local annotation isn"t present
* in the bean class (this class), all interfaces are considered
* local unless explicitly annotated otherwise.  If you look
* in the CounterRemote interface, you"ll notice it uses the @Remote
* annotation while the CounterLocal interface is not annotated relying
* on the EJB3 default rules to make it a local interface.
*/

//START SNIPPET: code @Stateful public class CounterImpl implements CounterLocal, CounterRemote {

   private int count = 0;
   public int increment() {
       return ++count;
   }
   public int reset() {
       return (count = 0);
   }

} //END SNIPPET: code

////////////////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.counter; import javax.ejb.Remote; /**

* This is an EJB 3 remote business interface
* A remote business interface must be annotated with the @Remote
* annotation
*/

//START SNIPPET: code @Remote public interface CounterRemote {

   public int increment();
   public int reset();

} //END SNIPPET: code

////////////////////////////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.counter; /**

* This is an EJB 3 local business interface
* A local business interface may be annotated with the @Local
* annotation, but it"s optional. A business interface which is
* not annotated with @Local or @Remote is assumed to be Local
*/

//START SNIPPET: code public interface CounterLocal {

   public int increment();
   public int reset();

} //END SNIPPET: code


 </source>
   
  
 
  



stateless EJB for calculation

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.calculator; import javax.ejb.Stateless; /**

* This is an EJB 3 style pojo stateless session bean
* Every stateless session bean implementation must be annotated
* using the annotation @Stateless
* This EJB has 2 business interfaces: CalculatorRemote, a remote business
* interface, and CalculatorLocal, a local business interface
* 
*/

//START SNIPPET: code @Stateless public class CalculatorImpl implements CalculatorRemote, CalculatorLocal {

 public int sum(int add1, int add2) {
   return add1+add2;
 }
 public int multiply(int mul1, int mul2) {
   return mul1*mul2;
 }

} //END SNIPPET: code /////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.calculator; import javax.ejb.Remote; /**

* This is an EJB 3 remote business interface
* A remote business interface must be annotated with the @Remote
* annotation
*/

//START SNIPPET: code @Remote public interface CalculatorRemote {

 public int sum(int add1, int add2);
 
 public int multiply(int mul1, int mul2);

} //END SNIPPET: code

/////////////// /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.calculator; /**

* This is an EJB 3 local business interface
* A local business interface may be annotated with the @Local
* annotation, but it"s optional. A business interface which is 
* not annotated with @Local or @Remote is assumed to be Local
*/

//START SNIPPET: code public interface CalculatorLocal {

 public int sum(int add1, int add2);
 
 public int multiply(int mul1, int mul2);

} //END SNIPPET: code


 </source>
   
  
 
  



Transaction in EJB

   <source lang="java">
 

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.injection.tx; import javax.ejb.Stateful; import javax.ejb.TransactionAttribute; import static javax.ejb.TransactionAttributeType.MANDATORY; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType; import javax.persistence.Query; import java.util.List; @Stateful(name = "Movies") @TransactionAttribute(MANDATORY) public class MoviesImpl implements Movies {

   @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.TRANSACTION)
   private EntityManager entityManager;
   public void addMovie(Movie movie) throws Exception {
       entityManager.persist(movie);
   }
   public void deleteMovie(Movie movie) throws Exception {
       entityManager.remove(movie);
   }
   public List<Movie> getMovies() throws Exception {
       Query query = entityManager.createQuery("SELECT m from Movie as m");
       return query.getResultList();
   }

}

 </source>
   
  
 
  



Use interceptors to inspect EJB

Web application based on EJB

web service and Stateless session bean (Open EJB)

   <source lang="java">

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.calculator; import javax.jws.WebService; //END SNIPPET: code /**

* This is an EJB 3 webservice interface
* A webservice interface must be annotated with the @Local
* annotation.
*/

//START SNIPPET: code @WebService(targetNamespace="http://superbiz.org/wsdl" ) public interface CalculatorWs {

 public int sum(int add1, int add2);
 public int multiply(int mul1, int mul2);

} //END SNIPPET: code

////////////////

/**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

package org.superbiz.calculator; import javax.ejb.Stateless; import javax.jws.WebService; /**

* This is an EJB 3 style pojo stateless session bean
* Every stateless session bean implementation must be annotated
* using the annotation @Stateless
* This EJB has a single interface: CalculatorWs a webservice interface.
*/

//START SNIPPET: code @Stateless @WebService(

       portName = "CalculatorPort",
       serviceName = "CalculatorWsService",
       targetNamespace = "http://superbiz.org/wsdl",
       endpointInterface = "org.superbiz.calculator.CalculatorWs")

public class CalculatorImpl implements CalculatorWs {

   public int sum(int add1, int add2) {
       return add1 + add2;
   }
   public int multiply(int mul1, int mul2) {
       return mul1 * mul2;
   }

} //END SNIPPET: code


 </source>