Presentation is loading. Please wait.

Presentation is loading. Please wait.

Library Example February 2010 – August 2010

Similar presentations


Presentation on theme: "Library Example February 2010 – August 2010"— Presentation transcript:

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

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

3 Current Situation UML(1) public interface LibraryManagerIF HashSet allCustomers(); HashSet allBooks(); HashSet 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) public interface LibraryManager implements

4 Current Situation UML(2) public class Book private static HashMap books = new HashMap (); static Book findBook(String title) static void removeBook(String title) static HashSet 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 customers = new HashMap (); static Customer findCustomer(String name) static void removeCustomer(String name) static HashSet allCustomers() private String name = null; private int credits = 0; private HashSet books = new HashSet (); Customer(String newName) public String getName() int getCredits() void setCredits(int newCredits) HashSet 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 principals = new HashMap (); static LibraryPrincipal findLibraryPrincipal(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. conf C:\jboss-5.1.0.GA\server\default data deploy deployers lib log tmp work ABC-Library.jar

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

8 New Situation Create Interace projectEJB means creating a Stateless Session Bean. It’s convenient to make 5 projects: 1.IF project 2.EJB project 3.JAAS project 4.Client project 5.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. 1.Create in eclipse a new java-project 2.Create a new directories “src” 3.Set project properties: 1.Java Build Path 2.Source = “src” 3.Default output folder = “classes” 4.Copy from model to “src”: LibraryManagerIF 5.Change Book→BookDAO and Customer → CustomerDAO 6.Create serializable data classes BookDAO and CustomerDAO 7.Update a copy an old build,xml file for deploying IF

10 IF Project. 1.Create in eclipse a new java-project 2.Create a new directory “src” 3.Set project properties: 1.Java Build Path 2.Source = “src” 3.Default output folder = “classes” 4.Copy from model to “src”: LibraryManagerIF 5.Create a new interface LibraryAdminIF 6.Change Book→BookDAO and Customer → CustomerDAO 7.Create serializable data classes BookDAO and CustomerDAO 8.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 allCustomers(); HashSet allBooks(); HashSet 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 titles = new HashSet (); 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 newTitles) { titles = newTitles; } public String getName() { return name; } public int getCredits() { return credits;} public HashSet getBooks() { return titles; } }

13 build.xml IF Project.

14 EJB Project. 1.Create in eclipse a new java-project 2.Create a new directory: “src” 3.Set project properties: 1.Java Build Path 2.Source = “src” 3.Default output folder = “classes” 4.Project = Interface project 5.External jar = jboss-ejb3x.jar 4.Copy from model to src: LibraryManager →LibaryManagerBean 5.Add Annotations to LibraryManagerBean 6.Add one new public method: Principal getPrincipal() 7.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 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

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 { InitialContext ctx = new InitialContext(); LibraryAdminIF libraryAdmin = (LibraryAdminIF)ctx.lookup("LibraryAdmin/local"); String[] myroles = libraryAdmin.check(username, password); if (myroles != null) { roles = new MyGroup("Roles"); for (int i=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 books = manager.allBooks(); for (BookDAO book : books) { System.out.println(book.getTitle()); } System.out.println("----------------------------------------------"); } private static void printCustomers(LibraryManagerIF manager) { HashSet customers = manager.allCustomers(); for (CustomerDAO customer : customers) { System.out.print(customer.getName()+", "+customer.getCredits()); HashSet 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. 1.Use the Jboss database service


Download ppt "Library Example February 2010 – August 2010"

Similar presentations


Ads by Google