Enum on steroids – java

Hi was reviewing some java code this week and came across this wonderful way of establishing structured enums.

Code

//Declaration
private enum ServerType {
        DEV("
https://server1:8001", "customservicesuser", "xzy"),
        TEST("
http://server2:8001", "customservicesuser", "xzy"),
        PROD("
http://server3:8001", "customservicesuser", "xzy"),
        LOCALHOST("
http://localhost:8001", "customservicesuser", "xzy");

        private String serverUrl;
        private String username;
        private String password;

        ServerType(String serverUrl, String username, String password) {
            this.serverUrl = serverUrl;
            this.username = username;
            this.password = password;
        }
}

// Usage
        try {
            serverType = ServerType.valueOf(server);
        } catch (Exception e) {
            System.out.println("Unable to get the server info, options are: 
                DEV, TEST, PROD, LOCALHOST");
        }

Enjoy!

Garbage Collection in the JVM (1.6)

Hi guys,

I’ve found myself discussing garbage collecting in the JVM a few times this week and though I’d share this information with everyone that ever wondered how all this works.

Automatic Garbage Collection

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and deleting the unused objects. An in-use object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.

In a programming language like C++, allocating and deallocating memory is a manual process. In other languages e.g. .net and Java, the process of deallocating memory is handled automatically by the garbage collector.

Basic Process

Marking

The first step in the process is called marking. This is the activity of marking what parts of memory have references and which have not.

Deletion

The second part is removing unreferenced objects, The deletion can be normal or can also include compacting.

Stop the world

When a gc happens all the threads in the application are stopped in what is referenced to as a “Stop the world” event.

JVM Generations

Having to mark and compact all objects in the JVM is inefficient, as more and more objects are allocated the list of objects grows and grows leading to a longer GC time. Empirical analysis of applications has shown that most objects are short lived.

Generations

The Hotspot JDK is broken into the following generations.

  • Young
  • Old
  • Permanent

image

 

 

 

Note not all JDK’s have the same structureimage

In this post I’m referring the the SUN JDK6 only. I’ve not really used the JDK7 that much except for personal projects, but even that is different with the G1 (Garbage First) and JDK8 will be a game changer again…

Sun Hotspot

The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.

The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection.

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.

GC Process

In the simplest of terms:

  • New objects are allocated into the eden section of the heap. after the first GC if they are still referenced they are aged and moved to the S0.
  • On the next GC, any referenced objects in eden are moved into S1, also the objects in S0 if still referenced are aged and also moved to S1.
  • The process repeats alternating between S0 and S1 until the objects are aged enough to become eligible for the old age pension and are promoted into the tentured generation.

 

Show me the memory!

So enough talking, show me the money as Cuba Gooding once said ;-)

The simplest way to visualize the GC in operation is to open the %JAVA_HOME%\bin\jvisualvm

When you run it for the first time it will ask you to click OK to calibrate so do this.

You will need to go to Tools/Plugins/Available Plugins and choose VisualGC (i’ve it already installed hence you see in a different tab)

image 

You can then double click on the java application you wish to profile (I chose Weblogic Application Server) and you can see the GC minor collections in progress.

Shows the WLS server starting up and the Eden space starting to fill.image

Shows the Survivorimage 1 getting the first aged references

Simagehows the minor GC’s and the alternating between S0 and S1

Shows aged survivors getting their old age pensionimage

You can look at the monitor tab to get some more diagnostics,
Number of threads / Classes etc

image You can see what types of objects are holding memoryimage

NewRelic

Another tool I recommend when instrumenting applications is NewRelic.com not only will you get JVM stats, but you’ll get so, so, so, sooo much more, you just have to use it to believe it.

BOOM! Bye bye iPhone, again…

So I’ve splashed out the 600euro and got me a SIM free Samsung Galaxy S4 Android phone.

Why?

Well I find myself doing more and more JAVA these days (as if I wasn’t busy enough keeping up to speed on .NET and iOS too) and I’ll admit it’s starting to win a place in my heart, not the language as such as it’s IMO inferior to C#, but the framework has lots little gems, there are some pretty nice external frameworks e.g. Drools, JBPM etc, and the application servers are interesting (I'm focusing mainly on JBoss AS7 these days); what I’m trying to say is that I’m starting to feel that Android may be a home away from home.

Flirtations

Well I’ve previously two timed my iPhone with a Windows Phone 7, this lasted for about 10 months before I moved back to iPhone, I guess one of the biggest reasons for doing this was I’d invested in a few iOS apps like the very expensive TomTom Worldwide and I kept finding a need for it (I travel with work and don’t always have roaming data). Also the lack of apps was disappointing, even though I contributed to the ecosystem with a WP7 app for Grass Management. I know the app situation is improving especially with Microsoft buying them up but I see too many complaints on social feeds to make me think they are a little shaky (is it the fault of the developers? … maybe!). When I moved back from WP7 to iPhone I complained for a few days over little nuisances, but I stayed there. Until now….

Android a week on

I’ll admit I was a little apprehensive at first thinking I’d like only the big screen and the  ability to write and deploy some droid apps on a real device (i don’t include the Samsung Galaxy Y that’s lying about the office as a real device :-) )

The good

But I’ve been very surprised, the Android apps appear to be better, the experience appears to be better, notifications are better. Why do i think this is? well maybe because the API allows the developers to do more, iOS does really lock things down which has it merits (we’ve all heard about the apps that silently call premium numbers).

  • The screen is large and beautiful
  • Camera is fantastic
  • Have all the apps I had on iPhone (not yet paid for TomTom again but will)

The bad

  • I don’t have the ability to have widgets on the lock screen when I have a PIN active.
  • The screen is too large! Contradiction I know, but I’ve hands like shovels and I need two of them for this device, apple has a great argument but it won’t win out.

 

Am I going back

Well never say never, but apple needs to up it’s game with a bigger screen, I always thought it was fine until I look back on my iPhone now and think it’s tiny

C++ ACE

Overview

I’ve been looking at a c++ application that uses ACE library, now I’ve never used this library myself, I used to love BOOST when I was 100% c++ (way back in the day sniff sniff). C++ 11 has drastically improved life sure but it’s not got everything I need.

I decided to do a bit of googling to see what I’ll be dealing with and below was the first post I came across on StackOverflow…. oh dear! not a great start for my motivation :-(

Post

Do yourself a favor and steer clear of ACE. It's a horrible, horrible library that should never have been written, if you ask me. I've worked (or rather HAD to work with it) for 3 years and I tell you it's a poorly designed, poorly documented, poorly implemented piece of junk using archaic C++ and built on completely brain-dead design decisions ... calling ACE "C with classes" is actually doing it a favor. If you look into the internal implementations of some of its constructs you'll often have a hard time suppressing your gag reflex. Also, I can't stress the "poor documentation" aspect enough. Usually, ACE's notion of documenting a function consists of simply printing the function's signature. As to the meaning of its arguments, its return value and its general behavior, well you're usually left to figure that out on your own. I'm sick and tired of having to guess which exceptions a function may throw, which return value denotes success, which arguments I have to pass to make the function do what I need it to do or whether a function / class is thread-safe or not.

Boost on the other hand, is simple to use, modern C++, extremely well documented, and it just WORKS! Boost is the way to go, down with ACE!

Using MVC5 Owin Security with EF Code First

Good evening, I’d like to share my solution for using MVC5 Owin security with VS2013 Update 1.
I wanted to have my own DBContext contain both my own DBSets and also the security tables.

Now I could find zero documentation on this and I actually walked away from the bleeding edge last week and reverted to MVC4. Tonight I had some free time and like a dog with a bone I wanted to figure this out. I’m not saying I came up with the nicest or correct solution, but alas I came up with something that appears to be working for me.
Please feel free to give me any feedback I’d appreciate it.

The problem

I wanted one single database containing my own entities and also the security entities.
I wanted to use the latest MVC5 built on the OWIN (open web interface for .net).
I wanted to use code first migrations to keep my database up to date.
I could find no documentation..

The Solution

  • Create my own entity class
    image

 

 

  • Create my own DBContext and IdentityStoreContext (the secret sauce) 

image The SiteCertDbContext derives from the IdentityDbContext used by default in MVC5 Update1.

  • Tell the account controller use this new DbContext

image

 

That’s pretty much it,  hope it helps someone else out you can now open the Nuget package manager and Enable-Migrations.

Parameterized Unit Tests with JUnit

I’ve been shown a lovely little utility for testing with JUnit 4.11 whereby one can have a list of items performed as part of the single unit test.

First set add the annotations you see below and set your inputs and expected outputs. (Arrange and Assert)
image

 

 

 

 

 

Then create a constructor and some static variables to store each Tuple

image

 

 

 

 

Then define your single unit test (the business logic is your Action)

image

 

 

 

 

That’s it, a really nice way to have a single unit test that can be run multiple times with different parameters.

image

 

 

 

 

 

If you would like to do the same sort of testing in .net you have some choices to make.

you could use NUnit and use the TestCaseAttribute

image

 

 

 

 

Of if like me you prefer MSTest then you can opt for a data driven unit test.
See this vs2013 page (has worked at least since vs2010) :
http://msdn.microsoft.com/en-US/library/ms182527(v=vs.120)

image

Xamarin a few weeks on

  • I’ve gotten my hands dirty,
  • I’ve published v1.0 to the store
  • I’ve gotten the best support I’ve ever seen from Xamarin
  • I’ve gone back to Objective-C, BOOM!

Bindings drove me daft

The reason I walked away was that I found some nice Objective-C widgets I wanted to use. In order to use these in Xamarin I needed to either

  • Port the source to c# (estimated 1 day)
  • Create a library in XCode and a component binding in Xamarin Studio

The first option seemed like an approach I could achieve but did I really want to do this every time I found a nice new control.

The second option proved painful, there were some tools that didn’t work (beta tools of course), I wasn’t quite clear about the multi targeted library I was building etc. It was the straw that broke this camel’s back.

Now I’ve previously indicated that I’m not a seasoned ObjectiveC developer, but I’ve written a few app and some of them even made it to the app store, so for me it didn’t seem daunting…

I had a few obstacles to overcome:

Basic Auth: I’ve posted before when I started my Xamarin rants that it was a little painful to do Basic Auth, I wanted to use blocks as I like this approach and not the delegate approach, but this was a little painfully or at least it meant that I had to write my own classes, what I did in the end was revert to my old friend   AFNetworking that supported exactly what I needed.

KeyStore: The Obj-C code is bloated and verbose and not intuitive, the Xamarin approach is so much easier.

Lessons learnt

Too many options or being a polyglot can be a PIA (and i’m not talking primary interop assembly), If I didn’t know objectiveC then I would have just figured out the Bindings faster and been happy.

Recommendations

What would I recommend to my fellow C# programmers…. Use Xamarin! I haven’t, but I’ve already invested many hours/days/weeks the language and 3rd party libraries,  the mono stack gives you a lot of
this for free and you will be familiar with it from your .net background.

Also the app I’m currently working on is just collecting Json and displaying it, so it’s not very code intensive on the client. If when an Android version is done it will be in Java because I’ve programmed in Java for years too (hey I’m looking for an excuse (and the time) to try this new Android Studio).
Given a different use case I we may well have an even more compelling reason for using C# for both platforms with something like MVVM Cross and achieve a more DRY approach.

Xamarin UIDeviceFamily

I was releasing an app to the the Apple App store tonight (actually two apps, I've done an Atley Hunter ;-) )

One of my apps encountered a problem when I tried to upload my archive:

"This bundle is invalid. The UIDeviceFamily key must be present when requiring a MinimumOSVersion of at least 3.2."

This did confuse me for a while, I did a bit of research and this information is supposed to be set automatically with XCode when I set the target platform.

Given I was using Xamarin Studio I was a little unsure where I stood so i took a shot in the dark.
I changed the platform in Xamarin to Universal and then back to iPhone/iPod problem solved,

hope this tip helps someone

To Xammer or not to Xammer

Xamarin vs XCode

Anyone that reads this blog regularly or knows me, will know that I’m a C#/Java guy primarily, but I do love all other languages, especially javascript and objective-c  (my objective-c is just about passable, mainly because I’ve only written 3 iOS applications). I was in London this week and I got an early preview of a new service, it looked pretty good so I was thinking of how I could write a few of my own clients for this. I’ve the luxury of being in a position to take a few approaches .

Client options

  • Html5 Desktop Client
  • iOS native client
  • Droid native client (I’ve never strictly speaking done one, unless you include my hello world post two years back)
  • PhoneGap/KendoUi iPad/Droid apps (I’m doing the phone apps for my Expenses service with these technologies).

Decisions tree

My first instinct was to go the PhoneGap with KendoUI, I actually did the initial layout and had a look in the Android Emulator and it was pretty nice (as I was in London I didn’t have my MacBookAir with me and I didn’t have the Visual Studio Phone Tools installed either so eclipse/android it was with that painfully slow emulator). The problem I encountered with this approach was that CORS was not enabled by default and I use visual studio to develop design debug (PhoneGap can do CORS once the site is white listed).

My next option was to write the native clients, when I arrived back home I quickly ensured I could connect to the server with basic authentication, I used objective-C firstly, I didn’t use any 3rd party libraries and the end result appears a little verbose (a better iOS developer will probably cry when they look at these screenshots and tell me use AFNetworking or blocks etc (which I used in my BrianKeating.net companion app).

XCode Version

image

Make the initial request and delegate to the view class.

 

image

Here i set the username and password for the request when/if challenged.

Screen Shot 2013-05-04 at 22.15.41

Here i allocate a place to store the response if it’s was a success and save to data into urlData.

image

Here I list all the key value pairs in the JSON returned (something I  don’t actually show in the c# version.

The attraction of XCode/Objective-C to me is that it’s a different toy to play with.

MonoDevelop Version

C# is just a fantastic language it’s RAD and Xamarin have done a fantastic job of bringing it to the iOS and Android Platforms. This  screen shot shows the same request to the server as done in XCode, the difference is that this took me about 1 minute where the objective-c version took me about 25 (although i did reuse the storyboard).

image

   

You said Decision?

So how will I proceed? I’m going to ditch the objective-C approach for sure:

Reasons:

  • It’s so much faster! I love C++ but can’t get myself to use it for app dev these days simply because C# kicks it to touch for rapid application development, same goes for Objective-C
  • Because I don’t expect to continue this POC myself and will have to hand it over to someone, and I don’t know anyone else on the team that knows objective-C.

I’m actually still leaning towards the PhoneGap/KendoUI option, I just need to get my grubby hands on the server so i can add the CORS headers (again because I don’t want to do the bulk of my debugging with Phone gap but rather IE/Chrome and I quite like my browser stack (jQuery/Knockout/Breeze etc) but the main advantage is that the the same source can then be used for the Android platform. Sure Facebook/LinkedIn etc have all been moving away from html5 because of the tooling, but I feels it’s the best solution for getting to both markets quickly.

So that is my reasoning, I expect everyone with be faced with much the same decisions and will have to weight the pros and cons themselves.

File Upload MCV4 Web API, Knockout.js

 

I wish to follow up on my previous post Uploading a file in MVC4 C#5 .NET 4.5

I promised a few things here, an Ajax client, WinRT, iOs, Droid, This post addresses the ajax upload.

First some background, I’m working on an expense tracking system at the moment, the core technologies involved in this Single Page Application are:

  • ASP MVC4 WebApi
  • Html5 SPA
  • Knockout.js

A fundamental part of this system is the ability to upload receipts.
 image

When the user browses to an image file, it gets converted to base64 and uploaded via a MVC4 Api controller.

Here are the important parts:

Html

image

First we create an image where we can display either the previously selected image or the newly selected image.
We only display this image if it’s in the javascript model.

Secondly we bind the html5 input file with a knockout binding.

Javascript

Model

image

The important parts are the image and imageType properties, there also exists a computed property that joins these two so it can be displayed in an image tag. The reason i keep these separated is that I can’t post the source as is without further encoding.

Knockout Bindings

In knockout.js you are not limited to the built in bindings like, click and value, you can create your own,
I’ve taken https://github.com/khayrov/khayrov.github.com/blob/master/jsfiddle/knockout-fileapi/ko_file.js as my start point, this pretty much does what I want, however I made a slight tweak in that i wanted base64. (basically because I’ve written some of the objective-C iOS app already and didn’t fancy changing it).

 

image

WebApi

image

ExpenseDto

image

All source can be viewed @ https://github.com/brianbruff/Expenses

Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation

Calendar

<<  October 2018  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Month List