Thursday, April 15, 2010

Workshop 4 - Storing persistent data

Storing Persistent Data

In a blackberry application, if an objects needs to be saved and then retrieved after the blackberry device is restarted, the object needs to implement the Persistable interface. In this workshop, I will create a blackberry project that saves data and then retrieve data from the application.

Note: Sub-classes of the class implementing Persistable interface do not automatically implement the interface.


Setting up a new project:

In previous workshops, I have shown how to create a Blackberry project using Eclipse. I will create another project "Bookstore" similarly and add a class called "Bookstore.java". The class will extend UiApplication and contain some EditFields, a java.util.Vector variable and a PersistentObject variable. The class will like below.



Menu Items:

I will add menu items to Save and Get item. The Save menu will save the data that is entered and display a success message. The Get menu will retrieve the data from the saved object and populates the EditFields. The codes for the menu items are shown below.




StoreInfo class:



The StoreInfo class implements the Persistable interface. The class declares a Vector variable and some constants. Constants are used to make sure that the data is read from and saved at the same location. The class also contains methods to set and get data from the Vector variable. The code for this class is shown below.


How persistence works:

The code to how persistence works is shown below.



As can be seen, persistence occurs by calling the getPersistentObject(long id) method. This method requires us to pass a unique id that is used to identify the persistent object. The setContents(Object obj) method is used to set the contents that will be saved in the device. The commit() method is called to save the object to the Blackberry device. Data can be retrieved from the device by calling the getContents() method (it returns an Object). In Eclipse, these methods are marked with a padlock. This means that the application has to be signed before it works - however, this will fine within the simulator.

Tuesday, April 13, 2010

Workshop 3 - Localization

Localizing an application really means to make it international. That is, the content of the application can appear in multiple languages. In blackberry, the net.rim.device.api.i18n package is used to enable localization.

For localized application, Blackberry automatically opens the application in the local language. For example, if the default locale settings are set to British English, the application will start up in British English if this Locale is available.

I will create a new Blackberry project as shown in the previous workshops and name it "Local". I have then added a file called Local.java. The class is shown below.


I have then added another class "LocalScreen". This class extends MainScreen and implements LocalDemoResource class. The LocalDemoResource class is relevant for setting up localization.


To add a new resource, create a new File of type Other and choose "Blackberry Resource File". The file is named as LocalDemo.rrh. The filename is important as the resource filename (not including extension) + "Resource" becomes the class name that the LocalScreen class implements. Then add keys to the .rrh file. To add contents to the resource files, edit the *.rrc file and add values for the keys to it. Add more .rrc files as needed to add more resources. The screen shots below shows how to add the resource files.



In this workshop, I have added 4 resource files in total and have added keys to resource files. The keys that are added can be called directly from the LocalScreen class.

Then I have added the code for creating the menu similar to the previous workshop, added a OnClose method to close the application and a refresh method to update values on the screen.

Then, running the applciation produced the following screen shots.



References:

Monday, April 12, 2010

Workshop 2 - Creating user interface and adding menu items

Blackberry UI API provides UI components that provide default layout and behaviours which are consistent with the core Blackberry device applications.


Setting Up a New Blackberry Project

In the previous workshop, I created a new project "HelloWorld". In this workshop, I will create a new project and add a new class "UserInterface" that extends UiApplication. The class is looking like below.




Then, another class "UserInterfaceScreen" is added that extends the MainScreen class.
 Variables of various types are declared. The class is looking like below.



Title

The title of the application is setup as shown below.



Managers

The screen is divided into three zones: top, middle and bottom. Horizontal and vertical managers are used to divide the application. The managers aligns up into rows or columns depending in their orientation. This is similar to setting up a components inside a JPanel class in a Java Swing application.

The managers are added in the order they will be displayed on the screen. A vertical manager is built-in on the screen. The code for setting up the managers is below.



Edit and Label Fields

Labels and Edit fields ae added to the middle manager. The code is shown below.



Buttons

Five buttons have been added with different country names. FieldChangedListener is added to the buttons and the fieldChanged method is implemented.

This event performs action when a button is clicked. The code for creating the button and adding FieldChangeListener is shown below.



The buttons are then added to the FieldManager as shown below.



Menu Items

Two menu items are added to the menu - Close and Change Capital. Close shows a dialog as shown in workshop 1 and exits the application. Change Capital allows the user to edit the capital of the selected country. The method for adding menu items is shown below.



Running the application

When the application is run, the following screens are displayed.



As can be seen from the above screen shots of the simulator, the button for India did not appear and the text on Australia button is concatenated.This is because Blackberry screen does not have enough space to display the button in the same row and does not automatically add a new row.

Therefore, to solve this problem, a new FieldManager will need to be added with the buttons added in it.

References: 
http://na.blackberry.com/developers/resources/A11_User_Interface_V3.pdf

Sunday, April 11, 2010

Workshop 1 - Part 2 - HelloWorld for Blackberry

Creating a HelloWorld Blackberry application

Whenever we start to learn any programming language, we start with a HelloWorld application that prints the text "Hello World!" in the screen, browser or the console. So, in this tutorial, I will start by creating a HelloWorld application for Blackberry.


Creating a Blackberry project in Eclipse

To create a new Blackberry project

Click on File >> New >> Project
Choose Blackberry >> Blackberry Project as shown below



Click on Next and enter Project name as shown below.



Click on Next and the setting will appear like below.




The project is now set up. A screen will appear showing the "Blackberry Application Descriptor". I have added a title and a decription for the application as shown above.


Adding a new class:


To add a new class - Click on New and choose Class. A prompt will appear. Add a Package name and a class name as shown below and click Finish. The class will then be created.




UiApplication


Blackberry applications that have a user interface must extend the UiApplication class. A UI application contains a stack of screen objects. When it adds new screen on to the stack, it drwas them on top of other screens already on the stack. Only the screen on the top of the stack receives input.

To create a UiApplication, the HelloWorld class needs to extend the net.rim.device.api.ui.UiApplication class. The code will like below.




Now, I will add a main method to the class and initiate a new object of type HelloWorld. In the constructor, I will add a new Screen object and display it. The code is shown below.




MainScreen

net.rim.device.api.ui.container.MainScreen class provides a working acrea where data from HelloWorld application can be displayed. I will nowcreate the HelloWorldScreen class shown in the code previously and extend the MainScreen class.

In the constructor, I will add a net.rim.device.api.ui.component.LabelField to set the title of the application and add a net.rim.device.api.ui.component.RichTextField to display the "Hello World!" message. Then, I will add confirmation dialog that displays a message before closing the application. I will need to overwrite onClose() method for this. The code for this class is  below.





Running the application

Running the application is quite simple. Just click on the Debug button and the Blackberry simulator will start. Click on the Applications menu and then Donwloads menu. The HelloWorld application will appear as shown below.



Clicking on HelloWorld application will show the screen as shown below.



Trying to move to any other screen will show the dialog for "closing the application" as shown below.




Workshop 1 - Part 1 - Choosing the development tool

Choosing the tool:

The Blackberry platform is based on Java and there are various tools like NetBeans and Eclipse that can be used to build Java applications. As part of this workshop, I will be using Eclipse to develop Blackberry applications. The reason behind choosing Eclipse is that it is a freeware and has the necessary plug-in to develop Blackberry applications.


System Requirements:
  • Computer monitor with resolution 1024 x 768 or higher
  • Intel® Pentium® 4 Processor (minimum 3 GHz)
  • 1.5 GB Hard drive
  • 1 GB RAM
  • Microsoft® Windows Vista™, or Windows® XP
  • Java® SE Development Kit (JDK) version 5 or version 6 Version 6 is required if you are using BlackBerry MDS-CS for debugging.

The Blackberry plug-in can also be installed on an existing Eclipse installation.


Downloads:

The tools and the necessary tools can be downloaded from the following location.
Java Development Kit: http://java.sun.com/javase/downloads/index.jsp .
Eclipse: http://www.eclipse.org/downloads/
Blackberry Plug-in and JDE: http://na.blackberry.com/eng/developers/resources/devtools.jsp


Installation:

Once all the tools have been downloaded, they will need to be installed. The process of installing JDK and Eclipse is quite simple and can be accomplished by clicking the install icon. Administrative previlege will be required before installation can continue. Once JDK and Eclipse have been installed, the Blackberry plug-in will need to be installed.

Double-click on the installation file and the following screen will appear.


It may take a while (around 2 minutes in my system), and then click on the 'Next' button few times. Provide the location of the Eclipse installation when prompted and continue installation.

Saturday, April 3, 2010

Exercise 13 - Topic 10

Develop the class diagram for the following shopping cart specifications:
A shoppingCart object is associated with only one creditCard and customer and to items in itemToBuy object. Persistent customer information such as name, billing address, delivery address, email address and credit rating is stored in the customer object. The credit card object is associated with a frequentShopper discount object, if the credit rating for the customer is good. The customer can make or cancel orders as well as add and delete items to the shopping cart product. The credit card object contains the secure method for checking that the charge is authentic.


Exercise 12 - Topic 9

Examine the Use Case in figure 9.2, page 60, and explain the MVC architecture of the online bookstore (the Model, the View and Controllers) needed to Lookup Books and Add to Shopping Cart.

Lookup books:
  1. View: A customer looks up or searches for book from a web page.
  2. Controller: The controller takes in the request, makes a call to the database and loads the data into memory which is the model.
  3. Model: The model is the object in memory that holds the data that is returned in step 2.
  4. Controller: The controller retrieves the data from the model and pass it to the view.
  5. View: The page is displayed that contains data returned in step 4.
Add to shopping cart:
  1. View: A customer add a book to the shopping cart.
  2. Controller: The controller takes the request, creates the model object and the request to the model.
  3. Model: The model is the object in memory that contains the book add to shopping cart in step 1.
  4. Controller: The controller gets the message from step 2 on whether the book has been successfully added or not.
  5. View: The controller passes the message from step 4 to customer that the book is successfully added or could not be added.

Exercise 11 - Topic 8

1. Give a description in your own words of the ACID properties of a transaction.

ACID (atomicity, consistency, isolation, durability) is a set of properties that guarantee that database transactions are processed reliably. This really refers to what can be described of a single logical operation called transaction. For example, in a sql server database, I may choose to update a customer records in two tables. Usually, I would update the record in one table first followed by the next table. But it may result in such that only one table is updated due to some errors. Using transaction, it would mean that either both the table is updated or none is updated. Once the transaction is completed, the database will be in a consistent state and the changes made by the transaction is permanently stored in the database.


2. Describe a TP monitor environment. How can a TP monitor stop an operating system being overwhelmed?

A TP monitor is a control program that manages the transfer of data between multiple local and remote terminals and the application programs that serve them. It provides integrity by making sure that transactions do not get lost. It has the ability to update multiple different databases in a single transaction and has the ability to attach priority to transactions and provide security.

With the TP monitor technology, a client connects to the TP monitor instead of the database server. The TP monitor accepts the transaction, queues it and manages it till completion. This helps the operating system from being overwhelmed.


References:
http://en.wikipedia.org/wiki/ACID
http://www.peterindia.net/N-tierApplicationsView.html

Exercise 10 - Topic 8

1. Find definitions for wigth terms and concepts used in threaded programming:
i.   Thread Synchronisation
ii.  Locks
iii. Deadlock
iv.  Semaphores
v.   Mutex (mutual exclusion)
vi.  Thread
vii. Event
viii.Waitable timer.

Thread Synchronisation is the mechanism to ensure that two concurrently running threads do not execute specific sections of the program simultaneously.

Locks make sure that no two or more threads can access a particular resource at the same time.

Deadlock is the state no further progress on the program can be made.

A semaphore is a protected variable or abstract data type that constitutes a classic method of controlling access by several processes to a common resource in a parallel programming environment.

Mutual exclusion (mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.

A thread is the unit of execution within a process.

An event (also called event semaphore) is a type of synchronization mechanism that is used to indicate to waiting processes when a particular condition has become true.

A waitable timer object is a synchronization object whose state is set to signaled when the specified due time arrives.

References:

http://en.wikipedia.org/wiki/Mutex
http://en.wikipedia.org/wiki/Semaphore_(programming)
http://en.wikipedia.org/wiki/Event_%28synchronization_primitive%29
http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx


2. A simple demonstration of the threading module in Python (threddemo.py) that uses both a lock and semaphore to control concurrency is by Ted Herman at the University of Iowa. The code and sample output below are worth a loo. Report your findings.

The code is an example of how threads are run in Python. In the code, the numtasks variable is used to determine the max number of tasks. The variable sema is used to create a semaphore which determine how many threads can be run at a time.  The mutex variable is used to create a lock.
In the code, the threads are started and the semaphore is acquired. Once acquired, only 3 threads could run at a time. The first 3 threads ran and when each completed, a new thread started executing until all the threads completed to run.

Exercise 9 - Topic 7

1. Find out about SET and the use of RSA 128-bit encryption for e-commerce.

The SET ENCRYPTION PASSWORD states that the default password and hint will be used for encryption and decryption. The password is not related to authentication but to the actual encryption and decryption process.

RSA is an algorithm for public key cryptography. It involves public key and a private key. Messages can be encrypted using the public key and can only be decrypted using the private key.

2. What can you find out about network and host-based intrusion detection systems?

Network-based intrusion detection systems use raw network packets as the data source. It monitors and analyzes all traffic in real-time as it travels through the network. It uses the following 4 techniques to recognise an attack.

i.  Pattern, expression or bytecode matching,
ii. Frequency or threshold crossing
iii.Correlation of lesser events
iv. Statistical anomaly detection

Host-based intrusion detection systems work best in simple environments. It relies on auditing logs for suspicious activities.


3. What is 'phishing'?

Phishing is the process of attempting to acquire sensitive information like usernames, password, credit card details, etc by pretending to be trustworthy entity in an electronic communication like email.


4. What is SET and how does it compare with SSL as a platform for secure electronic transaction? Is SET in common use?

As described previously, SET is related to the enecryption and decryption process.
SSL is the standard security technology for establishing an encrypted link between a web server and a browser. This ensures all data passed between the client and the server is authenticated. To create a SSL connection, the web server needs to have a SSL certificate. This needs to be obtained from a Certification Authority who validates all the server's details before issuing the certificate. The web server will match the SSL certificate to the private key.

SET is not commonly used. SSL is the industry standard and is widely used.


5. What are cookies and how are they used to improve security? Can the cookies be a security risk?

A cookie is a small text file (less than 4KB) that is sent by the web server and stored in the client's computer. The cookie can contain information that can be later used by the server for client identification. Cookies contain attributes to determine which host names, URI path can access the cookie and may also contain a expiry date.

Cookies give the impression of a session and can be used by the server to check if the client is validated or not. Cookies can also contain access control information after a user is validated. This reduces the server resources by not needing to validate the client each time the request is sent.

However, this also leads to a security issue. For example, an eavesdropper armed with a packet sniffer could simply inercept the cookie as it passes from the browser to the server using it to obtain free access to the site.

That's why, cookies should contain as little private information as possible.


6. What makes a firewall a good security investment? Accessing the internet, find two or three firewall vendors. Do they provide hardware, software or both.

A firewall is a part of computer system or network that is designed to authorize each request to the server. It is a device or a set devices configured to permit or deny requests based on rules and other criteria. This makes it a good investment since it can be used to authorize clients.

Firewalls can be implemented in either hardware or software or both.


7. What measures should e-commerce provide to create trust among their potential customers? What measures can be verified by the customer?

E-commerce websites should be created such that the potential customer is aware that it is secure to pass on credit card details and other personal details and should be made aware of any privacy statements. The following are some other measures that can be verified by the customer.
i. All transactions from the site should be secure - that is, use https and not http.
ii. A session should time-out after a definite time interval. (system)
iii. Customer should be always aware about the amount they are paying.
iv. Customer should have the option to update orders before it is completed.
v. Custome should be made aware where the payment is going and should receive necessary information regarding the purchase.


8. Get the latest PGP information from http://en.wikipedia.org/wiki/Pretty_Good_privacy.
The use of digital certificates and passports are just two examples of many tools for validating legitimate users and avoiding consequences such as identity theft. What others exist?

i. SSL is used to authenticate users on the web.
ii. Firewall is used to authorize users before giving them access to any resource.
iii. Anti-virus programs exist to identify and eliminate viruses and malware.  


References:

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/db2/rbafzmstsetep.htm
http://en.wikipedia.org/wiki/RSA
http://documents.iss.net/whitepapers/nvh_ids.pdf
http://en.wikipedia.org/wiki/Phishing
http://info.ssl.com/article.aspx?id=10241
http://www.w3.org/Security/Faq/wwwsf2.html
http://en.wikipedia.org/wiki/Firewall_%28computing%29

Exercise 8 - Topic 5

Create an XML document for an online catalogue of cars where each car has the child elements of make, model, year, colour, engine, number_of_doors, transmission_type, and accesories. The engine has the child element called number_of_cylinders and fuel_system.


Exercise 7. Topic 4

1. Why is the perception getting stronger that integration will become a critical factor in coming days?

At present, there is a number application frameworks out there built in a number of programming languages. Many software and web sites are creating services that rely on services created by different software and integrate multiple services from different sources to create new services. This is a challenge for developers to integrate various services together.

One of the ways to face this challenge to create web services and make it available to necessary clients. Data can be passed to the necessary clients via xml.


2. What is the relationship of AJAX to JQuery (jquery.com) and the lightweight Web 2.0 javascript framework called MooTools (mootools.net) within the enterprise software architecture?

JQuery and MooTools are open-source javascript frameworks. MooTools is an extension of JavaScript and improves various JavaScript functions. JQuery on the other hand, focuses on DOM and simplifies traversing HTML documents and handling events. These two frameworks serve different purpose and both offers reusability and provides wrapper classes to work with Ajax simpler.

AJAX - asynchronous JavaScript and XML - is a web development technique to create interactive web applications. Ajax supports partial page updates - that is, retrieves information from the web server without causing the page to reload completely. JavaScript is used to pass the call to the server to retrieve the data from the server. This call is passed and data is retrieved using the XMLHttpRequest object. JQuery provides an ajax api to call methods. MooTools provide wrapper object - Request class - to support Ajax.  


3. What are the similarities between the object-oriented development using model-view-controller (MVC) in Ruby on Rails 2.0 and Action Script 2.0 (Flash animations)?

Model view controller is a development framework where teh application is divided into model, view and controller from development perspective. The view, the user interface, is where a user would interact with the application. The interaction would be processed by controller which will either retrieve or update data to the model.

Ruby on Rails uses the MVC architecture pattern to organise application programming. It includes in-built development tasks that can automatically create models and views needed for a basic website.

ActionScript uses an open-source framework - PureMVC - to create MVC applications.


4. What does it mean to develop RESTful practices into our web applications?

RESTful (Representational State Transfer (REST)) is a software architecture designed for World Wide Web. RESTful means to conform to REST constraints.

RESTful architecture consists of clients and servers. Clients send requests to the server and the server returns a response. For a web application to be RESTful, it needs to follow the contraints of REST architecture. The following are the constraints.

i. Client-server: Client and server are separated by a uniform interface. This means that the client is concerned only with user interface and user state while the server is concerned with data storage. This means that the client code can be improved and the server architecture made more scalable.

ii. Stateless: The client-server communication is stateless. That is, the client needs to send all the information necessary to service a request. The state is only stored in the client.   

iii. Cacheable: This means that responses from the server must be cacheable. This will improve scalability and performance.

iv. Layered System: A client is not aware if it is connected to the end server or not. Inermediary server can improve system scalability by enabling load-balancing and by providing shared caches.

v. Code on demand (optional): Servers can customize the functionality of a client by transferring logic to that it can execute. JavaScript is an example of this.

vi. Uniform interface: The uniform interface of client-server simplifies the architecture and enables each part to be changed independently.


References:

http://api.jquery.com/jQuery.ajax/
http://mootools.net/
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
http://en.wikipedia.org/wiki/PureMVC