1 / 28

Library Example

Library Example. eric.gerlofsma@hu.nl February 2010 – August 2010 www.ericgerlofsma.nl. Contents. Creating a Library Session Bean Current situation Desired new situation IF project for connecting the front-end EJB project for business logic JAAS project for authentication

dai-chan
Download Presentation

Library Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Library Example eric.gerlofsma@hu.nl February 2010 – August 2010 www.ericgerlofsma.nl

  2. Contents • Creating a Library Session Bean • Current situation • Desired new situation • IF project for connecting the front-end • EJB project for business logic • JAAS project for authentication • Client project for testing the EJB • JSF project for deploying the ABC-Library

  3. Current Situation UML(1) public interface LibraryManagerIF HashSet<Customer> allCustomers(); HashSet<Book> allBooks(); HashSet<Book> allBooks(String name) String addCredits(String name, int credits) String lend(String title, String name) String newBook(String title) String newCustomer(String name) String removeBook(String title) String removeCustomer(String name) String unlend(String title) implements public interface LibraryManager

  4. Current Situation UML(2) public class Book private static HashMap<String, Book> books = new HashMap<String, Book>(); static Book findBook(String title) static void removeBook(String title) static HashSet<Book> allBooks() private String title = null; private Customer lendTo = null; Book(String newTitle) public String getTitle() Customer getLendTo() void setLendTo(Customer customer) public class Customer private static HashMap<String, Customer> customers = new HashMap<String, Customer>(); static Customer findCustomer(String name) static void removeCustomer(String name) static HashSet<Customer> allCustomers() private String name = null; private int credits = 0; private HashSet<Book> books = new HashSet<Book>(); Customer(String newName) public String getName() intgetCredits() void setCredits(intnewCredits) HashSet<Book> getBooks() void addBook(Book book) void removeBook(Book book)

  5. Current Situation UML(3) public class LibraryAdmin public LibraryAdmin() public static String[] check(String usr, String pwd) class LibraryPrincipal public static final String MANAGER = "manager"; public static final String ADMINISTRATOR = "administrator"; public static final String CASHIER = "cashier"; private static HashMap<String, LibraryPrincipal> principals = new HashMap<String, LibraryPrincipal>(); static LibraryPrincipalfindLibraryPrincipal(String name) private String name = null; private String password = null; private String[] roles = null; LibraryPrincipal(String newName, String newPassword, String[] newRoles) String getName() String getPassword() String[] getRoles()

  6. Current Situation • The given ABC-Library-Model has no real database! • It’s “java-database” is implemented by HashMap attributes in the Book & Customer classes. • The 6 Library classes are packed in a jar-file and becomes an integral part of the JBoss lib. • This package is to be used by a MVC (JSF) frontend. • There is no Authentication and Confidentiality yet! • No separate logging! • ABC-Library should be a separate module in Jboss and not part of JBoss? • Adding a database is a JBoss Service. C:\jboss-5.1.0.GA\server\default conf data deploy deployers lib log tmp work ABC-Library.jar

  7. Desired New Situation UML ABC-LibraryIF.jar ABC-Library-JAAS BookDAO LibraryManagerIF LoginModule CustomerDAO implements ABC-Library-EJB.jar ABC-Library-Client Log Book Main LibraryManagerBean Customer LibraryAdminBean LibraryPrincipal C:\jboss-5.1.0.GA\server\default conf data deploy deployers lib log tmp work ABC-Library-JAAS.sar ABC-Library-EJB.jar ABC-Library-IF.jar

  8. New Situation • Create InteraceprojectEJB means creating a Stateless Session Bean. • It’s convenient to make 5 projects: • IF project • EJB project • JAAS project • Client project • JSF project • The IF project is part of all other projects. • The minimum testable configuration is IF, EJB and JAAS. • After a successful test we create(copy) the front-end project, • remove the model ABC-Library.jar and make a connection to the enterprise back-end.

  9. IF Project. • Create in eclipse a new java-project • Create a new directories “src” • Set project properties: • Java Build Path • Source = “src” • Default output folder = “classes” • Copy from model to “src”: LibraryManagerIF • Change Book→BookDAO and Customer → CustomerDAO • Create serializable data classes BookDAO and CustomerDAO • Update a copy an old build,xml file for deploying IF

  10. IF Project. • Create in eclipse a new java-project • Create a new directory “src” • Set project properties: • Java Build Path • Source = “src” • Default output folder = “classes” • Copy from model to “src”: LibraryManagerIF • Create a new interface LibraryAdminIF • Change Book→BookDAO and Customer → CustomerDAO • Create serializable data classes BookDAO and CustomerDAO • Update a copy an old build,xml file for deploying IF

  11. Bean Interfaces. package efg.library.IF; import javax.ejb.Local; @Local public interface LibraryAdminIF { String[] check(String usr, String pwd); } package efg.library.IF; import java.security.Principal; import java.util.HashSet; import javax.ejb.Remote; @Remote public interface LibraryManagerIF { Principal getPrincipal(); HashSet<CustomerDAO> allCustomers(); HashSet<BookDAO> allBooks(); HashSet<String> allBooks(String name) throws Exception; String addCredits(String name, int credits) throws Exception; String lend(String title, String name) throws Exception; String newBook(String title) throws Exception; String newCustomer(String name) throws Exception; String removeBook(String title) throws Exception; String removeCustomer(String name) throws Exception; String unlend(String title) throws Exception; } By default all methods are public

  12. DAO Data Access Object. public class BookDAO implements Serializable { private static final long serialVersionUID = 1847273866441754398L; private String title = null; private String lendTo = null; public void setTitle (String newTitle) { title = newTitle; } public void setLendTo(String newLendTo){ lendTo = newLendTo; } public String getTitle() { return title; } public String getLendTo(){ return lendTo; } } public class CustomerDAO implements Serializable { private static final long serialVersionUID = 2403140518716476898L; private String name = null; private int credits = 0; private HashSet<String> titles = new HashSet<String>(); public void setName( String newName) { name = newName; } public void setCredits(int newCredits) { credits = newCredits; } public void addTitle (String newTitle) { titles.add(newTitle); } public void setBooks (HashSet<String> newTitles) { titles = newTitles; } public String getName() { return name; } public int getCredits() { return credits;} public HashSet<String> getBooks() { return titles; } }

  13. build.xml IF Project. <project name="P1-ABC-Library-IF" basedir="." default="package" > <property environment="env"/> <property name="jboss.home" value="${env.JBOSS_HOME}"/> <property name="jboss.server" value="${jboss.home}/server/default"/> <property name="jboss.deploy" value="${jboss.server}/deploy/efg"/> <target name="package"> <delete file="${ant.project.name}.jar"/> <jar destfile="${ant.project.name}.jar" basedir="jar"/> <copy file="${ant.project.name}.jar" todir="${jboss.deploy}"/> </target> </project>

  14. EJB Project. • Create in eclipse a new java-project • Create a new directory: “src” • Set project properties: • Java Build Path • Source = “src” • Default output folder = “classes” • Project = Interface project • External jar = jboss-ejb3x.jar • Copy from model to src: LibraryManager →LibaryManagerBean • Add Annotations to LibraryManagerBean • Add one new public method: Principal getPrincipal() • Update a copy an old build.xml file for deploying

  15. LibraryManagerBean @Stateless @Interceptors(efg.library.Log.class) @SecurityDomain("ABC-Library-Security-Domain") public class LibraryManagerBean implements LibraryManagerIF { @Resource private EJBContext context = null; @RolesAllowed({"cashier","administrator","manager"}) public Principal getPrincipal() { return context.getCallerPrincipal(); } @RolesAllowed({"cashier","administrator","manager"}) public HashSet<BookDAO> allBooks() { return Book.allBooks(); } . . . etc @RolesAllowed before every method

  16. LibraryAdminBean package efg.library; import javax.ejb.Stateless; import efg.library.IF.LibraryAdminIF; @Stateless public class LibraryAdminBean implements LibraryAdminIF { public LibraryAdminBean() { System.out.println("LibraryAdmin()"); } public String[] check(String usr, String pwd) { System.out.println("LibraryAdmin.check("+usr+", "+pwd+")"); LibraryPrincipal principal = LibraryPrincipal.findLibraryPrincipal(usr); if (principal != null && pwd.equals(principal.getPassword())) { return principal.getRoles(); } return null; } }

  17. EJB Project - build.xml <project name="P1-ABC-Library-EJB" basedir="." default="package" > <property environment="env"/> <property name="jboss.home" value="${env.JBOSS_HOME}"/> <property name="jboss.server" value="${jboss.home}/server/default"/> <property name="jboss.deploy" value="${jboss.server}/deploy/efg"/> <target name="package"> <delete file="${ant.project.name}.jar"/> <jar destfile="${ant.project.name}.jar"> <fileset dir="classes"/> <fileset dir="jar"/> </jar> <copy file="${ant.project.name}.jar" todir="${jboss.deploy}"/> </target> </project>

  18. EJB JNDI view java:comp namespace of the component jboss.j2ee:jar=P1-ABC-Library-EJB.jar,name=LibraryAdminBean,service=EJB3 : +- EJBContext (class: javax.ejb.EJBContext) +- TransactionSynchronizationRegistry[link -> java:TransactionSynchronizationRegistry] (class: javax.naming.LinkRef) +- UserTransaction (class: org.jboss.ejb3.tx.UserTransactionImpl) +- env (class: org.jnp.interfaces.NamingContext) +- ORB[link -> java:/JBossCorbaORB] (class: javax.naming.LinkRef) Global JNDI Namespace +- LibraryAdminBean (class: org.jnp.interfaces.NamingContext) | +- local (class: Proxy for: efg.library.IF.LibraryAdminIF) | +- local-efg.library.IF.LibraryAdminIF (class: Proxy for: efg.library.IF.LibraryAdminIF) +- LibraryManagerBean (class: org.jnp.interfaces.NamingContext) | +- remote (class: Proxy for: efg.library.IF.LibraryManagerIF) | +- remote-efg.library.IF.LibraryManagerIF (class: Proxy for: efg.library.IF.LibraryManagerIF)

  19. JAAS Project public boolean login() throws LoginException { try { InitialContextctx = new InitialContext(); LibraryAdminIFlibraryAdmin = (LibraryAdminIF)ctx.lookup("LibraryAdmin/local"); String[] myroles = libraryAdmin.check(username, password); if (myroles != null) { roles = new MyGroup("Roles"); for (inti=0; i<myroles.length; i++) { roles.addMember(new MyPrincipal(myroles[i])); } callerPrincipal = new MyGroup("CallerPrincipal"); callerPrincipal.addMember(new MyPrincipal(username)); ret = succeeded = true; } } catch (NamingException ne) { ret = succeeded = false; } return ret; }

  20. Client Project (1) import java.util.HashSet; import efg.library.IF.BookDAO; import efg.library.IF.CustomerDAO; import efg.library.IF.LibraryManagerIF; import javax.naming.InitialContext; import org.jboss.security.client.SecurityClient; import org.jboss.security.client.SecurityClientFactory; public class Main { public static void main(String[] args) throws Exception { InitialContext ctx = new InitialContext(); LibraryManagerIF manager = (LibraryManagerIF)ctx.lookup("LibraryManager/remote"); System.out.println("librarymanager="+manager); SecurityClient securityClient = SecurityClientFactory.getSecurityClient(); securityClient.setSimple("henk", "geheim"); securityClient.login(); printBooks(manager); printCustomers(manager); manager.addCredits("Eric", 1); printCustomers(manager); securityClient.logout();

  21. Client Project (2) securityClient.setSimple("john", "geheim"); securityClient.login(); printBooks(manager); manager.newBook("aapje"); printBooks(manager); printCustomers(manager); manager.newCustomer("aapje"); printCustomers(manager); manager.removeCustomer("aapje"); printCustomers(manager); securityClient.logout(); securityClient.setSimple("fred", "geheim"); securityClient.login(); printCustomers(manager); manager.lend("aapje", "Eric"); printCustomers(manager); securityClient.logout(); securityClient.setSimple("john", "geheim"); securityClient.login(); printBooks(manager); manager.removeBook("aapje"); printBooks(manager); securityClient.logout(); }

  22. Client Project (3) private static void printBooks(LibraryManagerIF manager) { HashSet<BookDAO> books = manager.allBooks(); for (BookDAO book : books) { System.out.println(book.getTitle()); } System.out.println("----------------------------------------------"); } private static void printCustomers(LibraryManagerIF manager) { HashSet<CustomerDAO> customers = manager.allCustomers(); for (CustomerDAO customer : customers) { System.out.print(customer.getName()+", "+customer.getCredits()); HashSet<String> books = customer.getBooks(); for (String title : books) { System.out.print(", "+title); } System.out.println(); } System.out.println("----------------------------------------------"); } }

  23. Do TESTING for first milestone. Deploy IF Deploy JAAS Deploy EJB do Client-test

  24. JSF Project • Inject a connector to EJB Backend • @EJB(mappedName="LibraryManager/remote") • private LibraryManagerIF libraryManager = null;

  25. JSF Project LibraryAccessBean public String doLogin() { boolean[] render = { . . . } session.setAttribute("render", render); try { SecurityClient securityClient = SecurityClientFactory.getSecurityClient(); securityClient.setSimple(usr, pwd); securityClient.login(); Principal principal = libraryManager.getPrincipal(); String name = principal.getName(); securityClient.logout(); boolean[] menu = null; if (name.equals("eric")) { menu = . . . } else if (name.equals("fred")) { menu = . . . } else if (name.equals("john")) { menu = . . . } else if (name.equals("henk")) { menu = . . . } session.setAttribute("menu", menu); } catch (Exception e) { setException(e.getMessage()); return "LibraryAccess"; } return "LibraryManager"; }

  26. JSF Project LibraryManagerBean public void doNewBook(ActionEvent ae) { if (title.equals("")) { FacesContext ctx = FacesContext.getCurrentInstance(); ctx.addMessage( "subview-choice:subview-new-book:form-new-book:input-new-book“ , new FacesMessage(" title is a required field")); return; } try { securityClient.setSimple(usr, pwd); securityClient.login(); setMessage(libraryManager.newBook(title)); title=""; securityClient.logout(); } catch (Exception e) { setException(e.getMessage()); } }

  27. Do TESTING for second milestone. Deploy JSF do Browser-test

  28. More steps to do. • Use the Jboss database service

More Related