esercizi webservices wsdl
This commit is contained in:
74
exercise2_ws/Book.java
Normal file
74
exercise2_ws/Book.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
|
||||
@Entity(name="Bookws")
|
||||
@XmlRootElement
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findbyisbnws", query = "SELECT b FROM Bookws b WHERE b.isbn =?1"),
|
||||
@NamedQuery(name = "findallws", query = "SELECT b FROM Bookws b")
|
||||
})
|
||||
public class Book implements Serializable{
|
||||
|
||||
@GeneratedValue @Id
|
||||
private Long id ;
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
@NotNull
|
||||
private Float price;
|
||||
@NotNull
|
||||
private String isbn;
|
||||
|
||||
|
||||
public Book(){}
|
||||
|
||||
public Book(@NotNull String in_isbn,@NotNull String in_title,@NotNull String in_author, @NotNull @DecimalMin("0.0") Float in_price){
|
||||
title = in_title;
|
||||
author = in_author;
|
||||
price = in_price;
|
||||
isbn = in_isbn;
|
||||
}
|
||||
|
||||
public @NotNull String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(@NotNull String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public @NotNull String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(@NotNull String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public @NotNull String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor( @NotNull String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public @NotNull @DecimalMin("0.0") Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(@NotNull @DecimalMin("0.0") Float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
||||
26
exercise2_ws/BookMessage.java
Normal file
26
exercise2_ws/BookMessage.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
public class BookMessage implements Serializable{
|
||||
|
||||
public BookMessage (String in_isbn, Float in_price){
|
||||
isbn = in_isbn;
|
||||
price = in_price;
|
||||
}
|
||||
|
||||
|
||||
private String isbn;
|
||||
private Float price;
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
18
exercise2_ws/BookWebService.java
Normal file
18
exercise2_ws/BookWebService.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercise2_ws;
|
||||
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 13/06/2015.
|
||||
*/
|
||||
|
||||
@WebService(endpointInterface ="exercise2_ws.wsinterface")
|
||||
public class BookWebService implements wsinterface {
|
||||
|
||||
public boolean checkbook(Book b) {
|
||||
if (b.getIsbn() == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
49
exercise2_ws/EJBBook.java
Normal file
49
exercise2_ws/EJBBook.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package exercise2_ws;
|
||||
|
||||
|
||||
|
||||
import javax.ejb.LocalBean;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Stateless
|
||||
@LocalBean
|
||||
@ws
|
||||
public class EJBBook implements RemoteInterface {
|
||||
|
||||
@PersistenceContext(unitName = "exercise2ws")
|
||||
EntityManager em ;
|
||||
|
||||
public void create_book (@NotNull Book s) {
|
||||
em.persist(s);
|
||||
}
|
||||
public void update_book (@NotNull Book s) {
|
||||
em.merge(s);
|
||||
em.persist(s);
|
||||
}
|
||||
public void delete_book (@NotNull Book s) {
|
||||
em.merge(s);
|
||||
em.remove(s);
|
||||
}
|
||||
|
||||
public @NotNull List<Book> findbyisbn (@NotNull String in_isbn) {
|
||||
Query Q = em.createNamedQuery("findbyisbnws");
|
||||
Q.setParameter(1, in_isbn);
|
||||
return (List<Book>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
public List<Book> findall () {
|
||||
Query Q = em.createNamedQuery("findallws");
|
||||
return (List<Book>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
43
exercise2_ws/InitializeDB.java
Normal file
43
exercise2_ws/InitializeDB.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package exercise2_ws;
|
||||
|
||||
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
public class InitializeDB {
|
||||
|
||||
private Book b1,b2;
|
||||
|
||||
@Inject @ws
|
||||
private EJBBook e;
|
||||
|
||||
@PostConstruct
|
||||
void pupulate (){
|
||||
b1 = new Book("0001","Titolo1","Author1",10f);
|
||||
b2 = new Book("0010","Titolo2","Author2",30f);
|
||||
e.create_book(b1);
|
||||
e.create_book(b2);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void delete () {
|
||||
e.delete_book(b1);
|
||||
e.delete_book(b2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
40
exercise2_ws/MDBBook.java
Normal file
40
exercise2_ws/MDBBook.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.inject.Inject;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
@MessageDriven(mappedName = "jms/javaee7/Topic")
|
||||
public class MDBBook implements MessageListener {
|
||||
|
||||
@Inject
|
||||
Logger l;
|
||||
|
||||
@Inject @ws
|
||||
EJBBook e;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
exercise2_ws.BookMessage m = message.getBody(exercise2_ws.BookMessage.class);
|
||||
l.info("Messaggio: " + m.getIsbn() + " " + m.getPrice());
|
||||
|
||||
List<exercise2_ws.Book> lb = e.findbyisbn(m.getIsbn());
|
||||
Iterator<exercise2_ws.Book>ib = lb.iterator();
|
||||
if (ib.hasNext()){
|
||||
Book b = ib.next();
|
||||
b.setPrice(m.getPrice());
|
||||
e.update_book(b);
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
16
exercise2_ws/RemoteInterface.java
Normal file
16
exercise2_ws/RemoteInterface.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Remote
|
||||
public interface RemoteInterface {
|
||||
|
||||
@NotNull List<exercise2_ws.Book> findbyisbn(@NotNull String in_isbn);
|
||||
List<exercise2_ws.Book> findall() ;
|
||||
|
||||
}
|
||||
18
exercise2_ws/ws.java
Normal file
18
exercise2_ws/ws.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import javax.inject.Qualifier;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 26/04/2015.
|
||||
*/
|
||||
|
||||
@Qualifier
|
||||
@Retention(RUNTIME)
|
||||
@Target({FIELD, TYPE, METHOD})
|
||||
public @interface ws {
|
||||
}
|
||||
11
exercise2_ws/wsinterface.java
Normal file
11
exercise2_ws/wsinterface.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package exercise2_ws;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 13/06/2015.
|
||||
*/
|
||||
@WebService
|
||||
public interface wsinterface {
|
||||
boolean checkbook(Book b);
|
||||
}
|
||||
Reference in New Issue
Block a user