Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing.

Slides:



Advertisements
Similar presentations
Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Advertisements

Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
JavaBeans: Introspection and Customization Umer Farooq CS6704: Design Patterns & Component FrameworksJanuary 30, 2002.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
By Dr. Jiang B. Liu 12. The Java Beans. Java Beans n JavaBeans is a portable, platform-independent software component model written in Java. It enables.
JAVA BEANS By Madhuri Kakumanu. What is a Java Bean? “ A Java Bean is a reusable software component that can be visually manipulated in builder tools.”
M.Sc. Course, Dept. of Informatics and Telecommunications, University of Athens S.Hadjiefthymiades “Web Application Servers” Basics on WAS WAS are necessary.
INTRODUCTION TO JAVA PROGRAMMING Chapter 1. What is Computer Programming?
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
Introducing JavaBeans Identify the features of a JavaBean Create and add a bean to an application Manipulate bean properties using accessor and mutator.
Java Beans.  Java offers software component development through java Beans  Java Beans are based on a software component model for java.  The model.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
Component-Based Software Engineering Introduction to Java Beans Paul Krause and Sotiris Moschoyiannis.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Java Beans.
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
Comparing JavaBeans and OSGi Towards an Integration of Two Complementary Component Models HUMBERTO CERVANTES JEAN-MARIE FAVRE 09/02.
Component frameworks Roy Kensmil. Historical trens in software development. ABSTRACT INTERACTIONS COMPONENT BUS COMPONENT GLUE THIRD-PARTY BINDING.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
Introduction to Java Beans CIS 421 Web-based Java Programming.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Getting Started with the Open Services Gateway Initiative (OSGi) CNT 5517 Dr. Sumi Helal, Ph.D. Professor Computer & Information Science & Engineering.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment –COM.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.
Introduction to Java Beans From Anders Børjesson.
Introduction to Software Components: the JavaBeans specs Babak Esfandiari.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
Java Beans THETOPPERSWAY.COM. Contents What is a Java Bean? Advantages Properties Bean Developers Kit (BDK) What makes Bean possible? Support for Java.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Java Beans - Basics CIS 421 Web-based Java Programming.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 32 JavaBeans and Bean Events
Chapter 36 JavaBeans and Bean Events
Chapter 11 Object-Oriented Design
Java Beans Sagun Dhakhwa.
JavaBeans and JSP CS-422.
Knowledge Byte In this section, you will learn about:
Component-Based Software Engineering
Java Programming: Advanced Topics
CMSC 202 Exceptions.
Presentation transcript:

Introduction to Java 120 February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing School of EPS February 2004

Introduction to Java 220 February 2004Sifei HE © 2004 Beans Baked haricot beans, typically cooked in tomato sauce and tinned? An edible seed growing in long pods on certain plants. The hard seed of a coffee or cocoa plant.

Introduction to Java 320 February 2004Sifei HE © 2004 Java Bean “A Java Bean is a reusable software component that can be manipulated visually in a builder tool” Sun Microsystems Sun’s tutorial on Java Beans Package: java.beans

Introduction to Java 420 February 2004Sifei HE © 2004 Components Components are self-contained, reusable software units that can be visually composed into composite components, applets, applications, and servlets using visual application builder tools. JavaBean components are known as Beans.

Introduction to Java 520 February 2004Sifei HE © 2004 JavaBeans, a component model JavaBeans is Java’s component model The model is made up of an architecture and an API The API makes it possible to write component software in Java The architecture provides the framework (services and rules) that allows components to participate properly

Introduction to Java 620 February 2004Sifei HE © 2004 Some Distinctions Features that distinguish a Java Bean from Java objects are: –Support for introspection –Support for customization –Support for events –Support for properties –Support for persistence Bean requires a zero-argument constructor. Bean does not have public member variables. –Access through pre-defined methods

Introduction to Java 720 February 2004Sifei HE © 2004 Sources of Builder Tools Bean Builder from Sun: – NetBeans: – JBuilder: – Note, you also need to have the Java SDK installed.

Introduction to Java 820 February 2004Sifei HE © 2004

Introduction to Java 920 February 2004Sifei HE © 2004

Introduction to Java 1020 February 2004Sifei HE © 2004

Introduction to Java 1120 February 2004Sifei HE © 2004 Bean Books Developing Java Beans Robert Englander O’ReillyRecommended JavaBeans Programming J. O’Neil and H. Schildt McGraw HillGood. Contains many examples. JavaBeans by Example Henri JubinPrentice HallSimpler than the above, but with useful examples NetBeans – the Definitive Guide Boudreau, Glick, Greene, Spurlin, Woehr O’ReillyNot so good on Beans. More about the IDE.

Introduction to Java 1220 February 2004Sifei HE © 2004 The Component Model Discovery and Registration Raising and Handling of Events Persistence Visual Presentation Support for Visual Programming

Introduction to Java 1320 February 2004Sifei HE © 2004 The Component Model Raising and Handling of Events –Beans (or JavaBeans components) use events to communicate with other Beans –A Bean that wants to receive events (a listener Bean) registers its interest with the Bean that triggers the event (a source Bean)

Introduction to Java 1420 February 2004Sifei HE © 2004 The Component Model Persistence –Persistence enables Beans to save and restore their state –JavaBeans uses Java Object Serialization to support persistence –implements java.io.Serializable

Introduction to Java 1520 February 2004Sifei HE © 2004 The Component Model Visual Presentation –The Bean is free to choose its own visual presentation (fonts, colours, shape, etc) –Many of these characteristics will be properties of the Bean (some might be persistent too) Note: Not all beans are visual beans. Beans need not to be visual !

Introduction to Java 1620 February 2004Sifei HE © 2004 The Component Model Support of Visual Programming –User can select a component from the toolbox and place it into a container –Properties of the component can then be edited to create the desired behaviour

Introduction to Java 1720 February 2004Sifei HE © 2004 Properties, Methods & Events A property is a subset of a component’s state. –Allow the user to control the behavior of that component so it operates as desired. Methods can be invoked to execute code in a component. An event is a notification that is generated by a component when there is some change in its state.

Introduction to Java 1820 February 2004Sifei HE © 2004 Properties Named attributes that can be read or set by other beans Accessibility of a bean’s properties is only via its get and set methods In the Java Beans model, properties will only be recognised if appropriate get and (or) set methods are defined –not necessarily both

Introduction to Java 1920 February 2004Sifei HE © 2004 A Pepper Bean public class Pepper { private String iKind; private int iHeatLevel; public Pepper( ) { } public String getKind( ) { return iKind; } public void setKind(String aKind) { iKind = aKind; }

Introduction to Java 2020 February 2004Sifei HE © 2004 Introspection and Properties The process by which builder tools discover a Bean’s features. By following specific naming conventions, the properties of a Bean that are “revealed” to the world can be identified: public class SignalGenerator { private double frequency; // … constructor methods here public double getFrequency( ) { return frequency; } public void setFrequency(double frequency) { this.frequency = frequency;} }

Introduction to Java 2120 February 2004Sifei HE © 2004 Conventions for Access Methods Simple Properties: –For a property of type Type and name Name: –public Type getName( ); –public void setName(Type value); Boolean Properties: –public boolean isName( ); –public void setName(boolean value); –public boolean getName(); Indexed Properties: –public Type getName(int index); –public Type[] getName(); –public void setName(int index, Type value); –public void setName(Type values[]);

Introduction to Java 2220 February 2004Sifei HE © 2004 Methods A Bean may be implemented by a Java Class That Class contains a number of methods that may be used to access and control the component These are generally all the public methods of the Class that implements the Bean

Introduction to Java 2320 February 2004Sifei HE © 2004 Events JavaBeans components interact by generating “Events” Several components may register an interest in an Event that is generated by a specific component Occurrence of the Event triggers methods to be called in all the components that are “listening” for it public void add ( listener); public void remove ( listener);

Introduction to Java 2420 February 2004Sifei HE © 2004 Quote Log Graph

Introduction to Java 2520 February 2004Sifei HE © 2004 Reacting to Change In general, a change in the value of a property of a bean will trigger some activity Could code this directly into the get and set methods, but this “hard-wires” application specific dependencies into the methods More flexible (improving reusability) if events are generated when the properties are accessed

Introduction to Java 2620 February 2004Sifei HE © 2004 e.g. setting Kind public void setKind(String aKind) throws java.lang.Exception { if( aKind == “Strawberry” ) { Exception ex = new Exception(“Not a chili pepper!”); throw ex; } else iKind = aKind; }

Introduction to Java 2720 February 2004Sifei HE © 2004 Application Code Pepper pepper = new Pepper( ); try { pepper.setKind(“Strawberry”); } catch (Exception ex) { System.out.println(“Exception - message is: ” + ex.getMessage( ) }; }

Introduction to Java 2820 February 2004Sifei HE © 2004 Introspection The process by which builder tools discover a Bean’s features. Beans support introspection in two ways: By adhering to specific rules (design patterns) By adhering to specific rules (design patterns) when naming properties, methods and events when naming properties, methods and events By explicitly providing property, method and By explicitly providing property, method and event info within a Bean Information class event info within a Bean Information class

Introduction to Java 2920 February 2004Sifei HE © 2004 Introspection Low-level reflection: –Follow Bean coding style (we have seen) –Analysis of the Bean’s class can then reveal properties and methods Revealing complex properties: –Implement a “BeanInfo” class

Introduction to Java 3020 February 2004Sifei HE © 2004 Introspection But the used within a visual development tool, a Bean must expose its: –properties, –methods –events Java reflection mechanism may not be sufficient

Introduction to Java 3120 February 2004Sifei HE © 2004 The BeanInfo Interface Sometimes use of the design patterns: –reveals insufficient information –reveals to much information –forces inappropriate design choices Sometimes the information we wish to expose cannot be represented by a design pattern –e.g. choice of a graphic icon to represent the Bean on a toolbar in a visual development tool

Introduction to Java 3220 February 2004Sifei HE © 2004 java.beans.BeanInfo MethodDescription getAdditionalBeanInfo( )Returns any additional BeanInfo objects that are relevant getBeanDescriptor( )Returns the Bean descriptor object getDefaultEventIndex( )Returns the default event index getDefaultPropertyIndex( )Returns the default property index getEventSetDescriptors( )Returns the event set descriptors getIcon( )Returns the specified icon for the Bean getMethodDescriptors( )Returns the method descriptors getPropertyDescriptors( )Returns the property descriptors

Introduction to Java 3320 February 2004Sifei HE © 2004 Implementing BeanInfo You do not have to provide a detailed implementation If any of the methods in the interface returns null, then low-level reflection will be used to gather all the information that is available If a non-null value is returned, then that is all the information on that aspect that is made available

Introduction to Java 3420 February 2004Sifei HE © 2004 Hiding Information public class Pepper implements java.io.Serializable { private String iKind; private int iHeatLevel; public Pepper() {} public String getKind() { return iKind; } public void setKind(String aKind) { iKind = aKind; } public int getHeatLevel() { return iHeatLevel; } public void setHeatLevel(int aHeatLevel) { iHeatLevel = aHeatLevel; } }

Introduction to Java 3520 February 2004Sifei HE © 2004 PepperBeanInfo.java import java.beans.*; import java.beans.SimpleBeanInfo; public class PepperBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { PropertyDescriptor[] pdArray = new PropertyDescriptor[1]; try { pdArray[0] = new PropertyDescriptor("heatLevel", Pepper.class); } catch (IntrospectionException e) { System.out.println("Error creating a PropertyDescriptor"); } return pdArray; }

Introduction to Java 3620 February 2004Sifei HE © 2004 Explanation SimpleBeanInfo implements BeanInfo It contains default implementations that return null Inheriting the default implementation tells the caller to use reflection Overriding the default implementation enables the developer to hide information by only providing descriptors for those features they wish to expose

Introduction to Java 3720 February 2004Sifei HE © 2004 Reflection vs. Introspection Reflection analyses the Bean itself to discover properties, methods and events Introspection relies (mostly) on the presence of a “meta-object” to provide information about the Bean (an instance of a BeanInfo class) What are the benefits of Introspection? What are the weaknesses?

Introduction to Java 3820 February 2004Sifei HE © 2004 Introspector java.beans.Introspector –seeks a BeanInfo for a Bean –if any descriptors in BeanInfo return null, then use reflection –if no BeanInfo, then use reflection The application always receives a BeanInfo object from the Introspector –(unless an exception occurs) The Introspector also “introspects” superclasses

Introduction to Java 3920 February 2004Sifei HE © 2004 Customisation Simple properties –Development tool will build property sheets dynamically –User may then edit the properties to customise the Bean For the Advanced User –Create a specific customiser for a Bean –This is kept separate to the Bean Class, as with a BeanInfo Class

Introduction to Java 4020 February 2004Sifei HE © 2004 Further Features Visibility –It is not necessary for a Bean to be visible at run-time (e.g. Bean controlling access to a device or data feed) –It is necessary however for a Bean to support the visual builder tool. Even an ‘invisible’ run- time Bean shall be shown on the builder tool

Introduction to Java 4120 February 2004Sifei HE © 2004 Further Features Multithreading –Always assume your code will be used in a multithreaded environment –Make sure that your Beans are thread-safe –Multithreading in JavaBeans is no different than multithreading in Java

Introduction to Java 4220 February 2004Sifei HE © 2004 Further Features Security –By default assume that your Beans are running in a non-trusted applet –Apply security restrictions such as Allow no access to the local file system Limit socket connections to the host system