Posts Tagged ‘Java’

Derek Santos

5 Cool Things About CoffeeScript

Monday, October 22nd, 2012

By Derek Santos – consultant

I’ve been experimenting with CoffeeScript lately and wanted to pass along some things I really like about it.

1. It writes very good JavaScript

CoffeeScript compiles to JavaScript that follows best practices and sticks to JavaScript’s good parts. For example:

  • == always turns into ===
  • Code automatically gets wrapped with a self calling function to protect its scope
  • var declarations always get moved to the top

2. It’s readable

Whitespace is meaningful in CoffeeScript (which can be hard to get used to at first), but consider the following example:

JavaScript

if (devices !== null && devices !== undefined) {
    devices.push({
	name : 'iPhone 6',
	manufacturer : 'Apple'
    });
}

CoffeeScript

if devices?
    devices.push
	name : 'iPhone 6'
	manufacturer : 'Apple'

It’s a simple example, but you can see how CoffeeScript removes the unnecessary syntax and reads more like a sentence.

3. String Interpolation

This is one thing I will admit is tedious in JavaScript (and some other languages). We’ve all had to do this before.

html = '<div>';
html += '<h2>' + title + '</h2>';
html += '<p>' + body + '</p>';
html += '<span>' + footer + '</span>';
html += '</div>';

In CoffeeScript, this would be accomplished with:

html =
"""
<div>
	<h2>#{title}</h2>
	<p>#{body}</p>
	<span>#{footer}</span>
</div>
"""

A templating engine could be used to give the same result, but that also adds extra weight to your project. This is not so in CoffeeScript since it just gets compiled into pure JavaScript and makes it much cleaner.

4. Classes and Inheritance

If you are coming from a classical programming language such as Java, CoffeeScript allows you to structure your code in a way that is familiar to you. CoffeeScript also has some helpful nuggets like using => instead of -> to bind a particular scope to your functions.

5. It’s fantastic for writing unit tests

If you are reluctant to try CoffeeScript because you are afraid of how it will affect your project, I strongly urge you to try writing your unit tests in CoffeeScript. This way, you’ll be learning it in a risk-free environment while still providing value to your projects. You’ll find that your tests will be incredibly easy to read and author using CoffeeScript.

Here is a sample Jasmine test written in CoffeeScript:

describe 'controller', ->
    target = app.mycontroller

    it 'should call a service and return a response', ->
        target.makeRequest (result) ->
            expect(result).toBe('success!')
Ryan R

Getting Started with BB10 Development

Tuesday, July 17th, 2012

BB10 is expected to power the next-generation of RIM handsets and tablets. With four different development approaches supported, many people may wonder which approach is right for them.

The development options avaliable to BB10 developers each have pros and cons, but many developers will want to jump into development as fast as possible. To get up and running quickly, you may want to choose a path that is the most similar to your current style of development.

 

What type of developer are you?

 

 Web Developers

Web developers will feel right at home with WebWorks. WebWorks is a runtime which bundles HTML, JavaScript and (eventually) native plug-ins into a native BB10 application. Along with the provided WebWorks JavaScript API, you are free to use most of the web technologies you are already comfortable with.

 Adventurous Web Developers

If you don’t want to feel too comfortable in your new development environment, consider jumping right into Cascades. While the initial learning curve may be steeper for those without C++ experience, the payoff will be big when you realize how transferable your JavaScript skills are. Cascade applications can be created completed in QML (a declarative language used to construct UI) and JavaScript, with minimal C++ code to glue it all together.

Flash Developers

Like the PlayBook, BB10 can run Adobe AIR applications. ActionScript developers can use the same Adobe tools they are comfortable with and publish their AIR apps right onto a BB10 device with minimal effort.

Java Developers

Java Developers may find themselves torn when migrating to BB10. The only supported path to write in Java is by taking advantage of the Android Runtime bundled into BB10. However, this path should only be considered for Java developers with extensive Android experience as it has some  big drawkbacks. The biggest one being that the Android Runtime is limited to to apps built on the Android 2.3 SDK. The Android 2.3 SDK was built first and formost for Android phones, and is now almost two years old.

In my opinion, the best path forward for a Java developer wanting to get started on BB10 is to jump headfirst into Cascades development. As with the others who aren’t as comfortable with C++ there will be a learning curve, but you’ll find a lot to like about Qt (which Cascades is built ontop of) and QML.

C/C++ Developers

Anyone who’s already familiar with C or C++ should definitely jump into native development on BB10. Those comfortable with low-level C may even want to stick with it by using the NDK, especially for Game Development. Otherwise, Cascades will development will be very easy to get into, and those with Qt experience will have an even easier time. The biggest task for developers familiar with C or C++ will be exploring all the components provided by the Cascades UI library.

Porting Tips

For those lucky (or unlucky) enough to have a pre-existing code base they intend to port, the development path will depend on the original platform. Below are some additional tips for developers looking to bring over various types of projects to BB10.

WebWorks for PlayBook for BB7 (and below)

WebWorks applications built for BB7 or Playbook OS will require minimal changes to prepare for BB10. The most important change is that neither Java and ActionScript plug-ins are supported on BB10, and will need to be rewritten as (C/C++) native plug-ins.

WebOS Applications

Developers with exisiting WebOS projects can migrate them successfully to WebWorks with some reworking. WebWorks uses pure HTML and CSS to style its UI and doesn’t rely on any native elements, to reproduce the look and feel of a Cascades application in WebWorks check out the BBUI project: https://github.com/blackberry/bbUI.js/ Most functionality proviced by Enyo is provided by the blackberry object in JavaScript. 3rd party libraries, like jQuery, are recommended to fill in the missing pieces, more informaiton can be found here: https://developer.blackberry.com/html5/documentation/ww_developing/porting_webos_apps_1952251_11.html

 OpenGL-based Games

With BB10, the NDK is provided to write code completely in low level C. This, with BB10′s support for all POSIX compliant libraries, makes BB10 a great target for porting OpenGL games written in C or C++.

C Libraries

Similarly C libraries should be very easy to get compiling on BB10.

Qt Applications

Qt developers will find that QtQuick has been replaced. Existing applications will have to be adapted to the components and architecture of Cascades UI. Currently drawing shapes is not supported and only Cascades UI elements can be drawn onto the screen. Luckily Cascades supports a component called ForeignView, which can be used to drop down to the use of OpenGL drawing operations.

Android Applications

For those that do have existing Android 2.3 applications targeting the Android Runtime should take a minimal amount of effort. This is provided that your application isn’t heavily reliant on the Google Play store, or a few sensor APIs which the Android Runtime for BB10 does not support. See this API support guide for more information: https://developer.blackberry.com/android/apisupport/ .

 Everything Else

Finally, many multi-platform development toolsets like Unity and Appcelerator have begun to announce support for BB10. With these plethora of options avaliable to developers, BB10 is promising to be one of the most flexible and developer friendly mobile platforms to develop for.

 

alaint

Android SDK Install: Java SE Development Kit (JDK) not found.

Tuesday, August 30th, 2011

By Alain Thibodeau – Consultant

If you are installing the exe version of the Android SDK you might get a warning message. This message is warning you about not being able to find the JDK. You’ll end up double checking your JDK setup for nothing and wasting time.  I’ve seen this happen on Windows XP and Windows 7.

Below is a screen cap of the error you will see:

 

If you do get this message, just hit the back button and then next button, et voilà

alaint

Generate a Certificate with ADT and ANT

Wednesday, August 10th, 2011

By Alain Thibodeau – Consultant

AIR and mobile applications need to be signed. Most of the time we use the same self-signed certificate to package our applications. There are times when you may want to use a different self-signed certificate or create one on the fly. This is possible using ADT and ANT.

Adobe’s documentation states the ADT certificate command use like this:

adt -certificate -cn name -ou orgUnit -o orgName -c country -validityPeriod years key-type output password

To incorporate with ANT we would do something like this:

Properties file:

cert.keystore=mycertificate.p12
cert.name=SelfSignedCertificate
cert.orgunit=Software Development
cert.orgname=newyyz
cert.country=CA
cert.keytype=2048-RSA
cert.password=mypassword

Build file:

<target name=”generate.certificate”>
<exec executable=”${ADT}”>
<arg value=”-certificate” />
<arg value=”-cn” />
<arg value=”${cert.name}” />
<arg value=”-ou” />
<arg value=”${cert.orgunit}” />
<arg value=”-o” />
<arg value=”${cert.orgname}” />
<arg value=”-c” />
<arg value=”${cert.country}” />
<arg value=”${cert.keytype}” />
<arg value=”${cert.keystore}” />
<arg value=”${cert.password}” />
</exec>
</target>

You can find more information here: http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac1e63e3d128fc240122-7ffc.html

Derek Santos

Hello World with the BlackBerry Mobile Platform

Saturday, May 1st, 2010

By Derek Santos – Consultant

The Mobile Market is extremely competitive these days, and it’s important for customers to deploy their applications across multiple mobile platforms. The BlackBerry platform, like Android, is Java based. This lends itself nicely to existing Java and/or Android developers.  In this article, we’ll take a look at creating a HelloWorld application using the BlackBerry Mobile Platform.

Downloading and installing the necessary tools

First, you will need to download the BlackBerry Eclipse Plug-in here.

It is important to note that versions of the BlackBerry platform 4.x and earlier are considered legacy and use what’s called the “BlackBerry JDE(Java Development Environment)”. Versions 5.0 and later now use the BlackBerry Eclipse Plug-in. This article assumes you have Eclipse 3.5 installed.

Once you’ve downloaded the installer, simply run it and point it to your existing eclipse installation.

Creating your first BlackBerry project

To create your project, go to New -> Project.

Then select BlackBerry Project.
BlackBerry Project Selection

Click “Next”. Give your project a name then click “Finish”.

Configuring your project

You now have a BlackBerry project setup. You should notice that an application descriptor file was created for you. This descriptor file contains the Meta information about your project. It can be found under the root of your project.
BlackBerry Application Descriptor

Open this file and enter in information about your project.

BlackBerry Descriptor Configuration

Creating your main application class

Now that your project is configured, you’ll want to create a new Java class and have it extend UIApplication. The UIApplication class is your main entry point into the application. This class will also have your static main() function.

BlackBerry Descriptor Configuration

Once you’ve created your class, add the code to your static main() method as shown below. The enterEventDispatcher() method indicates that your main application thread will execute any drawing and event-handling code.

public static void main(String[] args) {
    HelloWorld application = new HelloWorld();
    application.enterEventDispatcher();
}

At this point, you will have an empty BlackBerry Application.

Creating your applications main screen

BlackBerry applications draw screens on a stack. To initialize this stack, we need to create a class that extends the “MainScreen” class. Create a new Java class and configure it as seen below.

BlackBerry Descriptor Configuration

After creating the class, create a default constructor and add the following code to it.

public SampleMainScreen() {
    super();
    long options = LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH;

    LabelField title = new LabelField("HelloWorld Sample", options);
    setTitle(title);

    RichTextField richTextField = new RichTextField("Hello World!");
    add(richTextField);
}

Now that you’ve created your main screen, you are ready to add it to your application.

Adding your main screen to the application

To have your application display your main screen, you need to “push” the screen onto the applications stack. To do this, add this line of code to the HelloWorld applications constructor. Also make sure you are calling super().

public HelloWorld() {
    super();
    pushScreen(new SampleMainScreen());
}

Running your application

To run your BlackBerry application, simply click the Run dropdown, go to Run As -> BlackBerry Simulator.

Run As BlackBerry Simulator

The first run will take some time as the Simulator initializes. Once the Simulator is loaded up, you need to navigate to the application. You can do this by click the BlackBerry menu button then going to Downloads -> My First Application.

BlackBerry Downloads Folder

Once you click on “My First Application”, your should see “HelloWorld” printed on the screen.

Conclusion

This concludes creating a simple application using the BlackBerry Mobile Platform. For further reading, please see the BlackBerry Developer Zone.

Thanks for reading!

Derek Santos

XMLSocket Benchmarking with Flex and Java

Wednesday, January 13th, 2010

By: Derek Santos

Socket driven applications have several benefits. The benefits include real-time data interchange between the Client and Server, and giving the developer more control over how the data is sent.  This post will cover some basics about sending xml data over sockets, and how the socket performs with large data sets.

Setting Up a Socket Server

The first thing you need to do is create a socket server.  This can be done with Java via the ServerSocket class. Please refer to the attachments to see how this is done. In this example, we will be connecting to a socket on port 5678.  It’s important to note that since Flash Player 9.0.124.0, a policy file is required to be served by the socket.  You can serve a policy file on port 843, which will allow any subsequent socket connections for any port on that host. A Policy File server is also provided in the attachments. In this example, the Java Socket Server simply responds to a request and returns “n” number of xml rows back to the client. The Socket Server will return the data in intervals, rather than all at once. The purpose of this example is to determine at what interval you can achieve the best performance.

Below is the xml we will be expecting from the server.

<person>
    <id>1</id>
    <name>Derek Santos</name>
    <company>New Toronto Group</company>
</person>

Once our Socket Server is up, we can make requests to the server from the Client.  To do this, you can use the XMLSocket class.

var socket:XMLSocket = new XMLSocket();
socket.connect(‘localhost’, 5678);

This will an attempt to connect to the socket server we have created.  We also need to listen for the connect event of the socket, so that we can begin sending requests.

socket.addEventListener(Event.CONNECT, connectHandler);
private function connectHandler(event:Event):void {
socket.send(“my first socket request”);
}

Although it is an XMLSocket, you are not required to send data in XML format.  In fact, the server does not know the difference between an xml socket and a regular socket connection.  You can handle results from the server by adding an event listener on the socket for the DataEvent.

socket.addEventListener(DataEvent.DATA, dataHandler);

XMLSocket Test Results

The screenshot below shows a simple Flex application which asks the server for XML data.  First it asks for 10,000 rows, then 100,000 and then 1,000,000 rows.  The chart below represents the time it took to retrieve all of the rows(y axis), and how many rows at a time were returned(x axis).

Benchmarking Results

This test was run on dual-core processor with 2GB of ram.  Interestingly, it looks like the optimal setting is around 32 rows per result.

You can download all of the source for this example below.

Download Source Code