Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 2 Unofficial Field Trip  Computer History Museum in Mt. View http://www.computerhistory.org/  Saturday, November 8, 11:30 – closing time

3 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 3 Legacy Software  Contains necessary functionality.  May be difficult to use. Often the user interface is the command line.  May be difficult to rewrite. Original programmers long gone. May contain many modifications made over time.  Made by different programmers with varying levels of competence. Written in an archaic programming language that no one knows any more. Nobody dares touch the code lest it break.

4 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 4 Reuse  Lower development effort and cost. Don’t reinvent the wheel.  Lower risk. It’s always worked!  Less training. May be difficult to use, but the concepts are familiar.  Take advantage of past expertise and policies. Overcome the NIH (Not Invented Here) syndrome.

5 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 5 Rewrite  Too difficult or costly to understand the old code.  The old code has become obsolete or dangerous. Example: The Y2K crisis.  New code will be easier to maintain.  Reuse vs. rewrite? Similar to the classic Make vs. Buy decision.

6 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 6 What to Do with Legacy Software  An old command-line program. Keyboard input (standard in) Terminal output (standard out) Still has useful functionality. Nobody wants (or dares) to rewrite it.  Example: An old FORTRAN IV program.

7 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak What to Do with Legacy Software, cont’d 7 C SQUARE ROOT PROGRAM C DOUBLE PRECISION NUMBER, ROOT C C READ NUMBER C READ (5,100) NUMBER 100 FORMAT (F5.0) C C COMPUTE SQUARE ROOT C ROOT = DSQRT(NUMBER) C C WRITE SQUARE ROOT C WRITE (6,200) NUMBER, ROOT 200 FORMAT ('THE SQUARE ROOT OF ', F5.1, ' IS ', F15.10) C STOP END

8 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 8 What to Do with Legacy Software  One solution: Wrap it inside a modern Java program.  If the Java program is a servlet, you can deploy the legacy app to the web! Use a web form to supply input to the app. Display the app's output on a web page. LegacyDemo Java servlet process Legacy application process standard out standard in

9 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 9 Shortcomings of JDBC  Java programmers must understand relational databases. The database is very visible. The Java programmer must know SQL.  JDBC is not fully object-oriented. A Java program accesses field values individually from a result set. You need statements to create Java objects from the result set values.

10 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 10 Object-Relational Mapping  Goal: Overcome the paradigm mismatch between relational data from an RDBMS and object-oriented programming.  Object-oriented languages such as Java would much rather deal with classes and objects. Java programmers do not want to write SQL. Java programmers do not want to want to deal with database concepts such as one-to-many, one-to-many, many-to-many, etc. Java programmers do not want to understand normalization.

11 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 11 Object-Relational Mapping  Solution: Add a layer of software between the database and the Java application that does object-relational mapping.  Relational data being read from the database are automatically converted to objects.  Objects being persisted (saved) to the database are automatically converted to relational data.

12 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 12 Object-Relational Mapping  The Java application manipulates objects (create, update, search, delete) and these operations are automatically translated into the corresponding database operations.  The Java programmer can mostly forget that there is an underlying database.  Database platform independence.

13 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 13 Hibernate  A very popular open-source object-relational persistence and query service for Java. Download from http://www.hibernate.org/http://www.hibernate.org/  Replace the JDBC API with the Hibernate API.  Java annotations describe in detail the mapping between the Java classes and the relational database tables. _

14 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 14 Hibernate, cont’d  Import Java packages: Hibernate Hibernate annotations Log4J import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Column; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;

15 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 15 hibernate.cfg.xml  Hibernate configuration file Modify as necessary. Must be in your application’s classpath. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> com.mysql.jdbc.Driver jdbc:mysql://localhost/school_demo root sesame org.hibernate.dialect.MySQL5Dialect true

16 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 16 Hibernate: Student @Entity public class Student { private long id; private String firstName; private String lastName; public Student() {} public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }... } The annotations provide metadata for the Java compiler and the Java run time.

17 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 17 Hibernate: Student @Entity public class Student {... @Id @GeneratedValue @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; } @Column(name="first_name") public String getFirstName() { return firstName; } public void setFirstName(String name) { this.firstName = name; } @Column(name="last_name") public String getLastName() { return lastName; } public void setLastName(String name) { this.lastName = name; }... }

18 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 18 Student.java  Main method (for testing) public static void main(String args[]) { // Configure Hibernate and add the Student class. Configuration config = new Configuration(); config.addAnnotatedClass(Student.class); config.configure(); // Create the database table. (new SchemaExport(config)).create(true, true); // Create a session. SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession();... }

19 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 19 Student.java public static void main(String args[]) {... // Create a session. SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); // Load the Student table in a transaction. Transaction tx = session.beginTransaction(); { session.save(new Student("Mary", "Jane")); session.save(new Student("Kim", "Smith")); session.save(new Student("John", "Doe")); session.save(new Student("Tim", "Novak")); session.save(new Student("Leslie", "Klein")); } tx.commit(); session.close(); } SchoolDemo1

20 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 20 HibernateContext.java  Goal: Move all the Hibernate bookkeeping code into a shared class.

21 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 21 HibernateContext.java import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null; /** * Set the configuration if it is null. */ private static void setConfiguration() { if (config == null) { config = new Configuration(); config.configure(); } }... }

22 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 22 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Set the factory if it is null. */ private static void setFactory() { if (factory == null) { setConfiguration(); factory = config.buildSessionFactory(); }... }

23 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 23 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Open a session from the factory. * @return a session. */ public static Session getSession() { setFactory(); return factory.openSession(); }... }

24 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 24 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Create a new schema (database) from the configuration. */ public static void createSchema() { setConfiguration(); (new SchemaExport(config)).create(true, true); }... }

25 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 25 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Add a new class object to the database. * @param klass the class object. */ public static void addClass(Class klass) { setConfiguration(); config.addAnnotatedClass(klass); }... }

26 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 26 HibernateContext.java public class HibernateContext { public static Configuration config = null; public static SessionFactory factory = null;... /** * Add a list of class objects to the database. * @param klasses the list of class objects. */ public static void addClasses(Class klasses[]) { for (Class klass : klasses) { addClass(klass); }

27 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 27 Student.java @Entity public class Student {... public static Student find(long id) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where id = :idvar"); query.setLong("idvar", id); Student student = (Student) query.uniqueResult(); session.close(); return student; }... } Hibernate Query Language (HQL)

28 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 28 Student.java @Entity public class Student {... public static Student find(String lastName) { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where lastName = :name"); query.setString("name", lastName); Student student = (Student) query.uniqueResult(); session.close(); return student; }... }

29 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 29 Teacher.java @Entity public class Teacher { private long id; private String firstName; private String lastName; public Teacher () {} public Teacher(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }... } Similar to Student.java

30 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 30 Teacher.java @Entity public class Teacher {... @Id @GeneratedValue @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; } @Column(name="first_name") public String getFirstName() { return firstName; } public void setFirstName(String name) { this.firstName = name; } @Column(name="last_name") public String getLastName() { return lastName; } public void setLastName(String name) { this.lastName = name; }... }

31 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 31 Teacher.java @Entity public class Teacher {... /** * Load the Teacher table. */ public static void load() { Session session = HibernateContext.getSession(); Transaction tx = session.beginTransaction(); { session.save(new Teacher("Tom", "Rogers")); session.save(new Teacher("Art", "Thompson")); session.save(new Teacher("John", "Lane")); session.save(new Teacher("Mabel", "Flynn")); } tx.commit(); session.close(); System.out.println("Teacher table loaded."); }... }

32 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 32 Teacher.java @Entity public class Teacher {... /** * List all the teachers. */ public static void list() { Session session = HibernateContext.getSession(); Query query = session.createQuery("from Teacher"); System.out.println("All teachers:"); for (Teacher teacher : (List ) query.list()) { teacher.print(); } session.close(); } }

33 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 33 Teacher.java @Entity public class Teacher {... /** * Print teacher attributes. */ public void print() { System.out.printf("%d: %s %s\n", id, firstName, lastName); }... }

34 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 34 SchoolDemo.java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SchoolDemo { private static final String HELP_MESSAGE = "*** Commands: create, load, find, students, teachers, quit"; public static void main(String args[]) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String command; Class klasses[] = {Student.class, Teacher.class}; HibernateContext.addClasses(klasses);... } }

35 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 35 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do { System.out.print("\nCommand? "); try { command = stdin.readLine(); } catch (java.io.IOException ex) { command = "?"; } String parts[] = command.split(" ");... } while (!command.equalsIgnoreCase("quit")); }

36 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 36 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... if (command.equalsIgnoreCase("create")) { HibernateContext.createSchema(); } else if (command.equalsIgnoreCase("load")) { Student.load(); Teacher.load(); }... } while (!command.equalsIgnoreCase("quit")); } }

37 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 37 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... else if (command.equalsIgnoreCase("teachers")) { Teacher.list(); } else if (command.equalsIgnoreCase("students")) { Student.list(); }... } while (!command.equalsIgnoreCase("quit")); }

38 Computer Science Dept. Fall 2014: November 5 CS 160: Software Engineering © R. Mak 38 SchoolDemo.java public class SchoolDemo {... public static void main(String args[]) {... do {... else if (parts[0].equalsIgnoreCase("find") && (parts.length >= 2)) { long id = Long.parseLong(parts[1]); Student student = Student.find(id); if (student != null) { student.print(); } else { System.out.printf("*** No student with id %d\n", id); }... } while (!command.equalsIgnoreCase("quit")); } SchoolDemo2


Download ppt "CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google