Java process id via java.lang.management

While working on a project recently I need to find out the current process of the active running Java process (for tracing/auditing), however I never found a 100% perfect solution but did come across an acceptable solution to use the management classes to query its name, which happens to have encoding in it, so here is the quick solution:


import java.lang.management.ManagementFactory;

public class getpid
{
public static void main(String args[]) throws Exception
{
System.out.println("Process id : "+getProcessId());
}

public static long getProcessId()
{
String name = ManagementFactory.getRuntimeMXBean().getName();
String[] nameBits = name.split("@");

return nameBits == null ? -1 : Long.valueOf(nameBits[0]);
}
}


$ java getpid
Process id : 377

Posted in Java, JVM, Tips | Tagged , , | Comments Off on Java process id via java.lang.management

Design Patterns and COBOL

As part of my working life I happy to say I use COBOL and for better or worse it is here to stay. With this in mind it annoys me I here/see saying things such as

COBOL is a old language that naturally prohibits you from using modern design patterns.

rubbish I say… COBOL can be used in good ways and bad ways.

I will try and show you that COBOL can be used in a good way… lets take the “Singleton pattern“, as the first example.

First.. lets start off my creating a singleton class in csharp… so here it is:


using System.Collections;

public sealed class MySingleton {
private static readonly Hashtable sharedHashtable = new Hashtable();

// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static MySingleton() {
}

private MySingleton() {
}

public static Hashtable Singleton {
get {
return sharedHashtable;
}
}
}

Not too shabby.. but lets see what we can do in COBOL…


$set ilusing"System.Collections"
class-id. "MySingleton".

01 shared-hashtable type "Hashtable"
static property as "Singleton" with no set.

method-id. "New" static.
set shared-hashtable to new type "Hashtable"
end method "New".

What… COBOL is smaller… that can’t be true… sorry but it is…

To complete the example… lets use it…


*> Add two items to the single hashtable
invoke type "MySingleton"::"Singleton"::
"Add"("01234567","Ian")

invoke type "MySingleton"::"Singleton"::
"Add"("987654321","Stephen")

*> Now get one of the items of the singleton
display "Account 01234567 - Contains: "
type "MySingleton"::"Singleton"::"Item"("01234567")

And I am sure some people… will say sure… this is really true… it is… here is it running..


c:temp> cobol MySingleton.cbl ilgen(sub);
c:temp> cobol UseSingleton.cbl ilgen ilref"MySingleton.dll";
c:temp> UseSingleton.exe
Account 01234567 - Contains: Ian
c:temp> csc MySingleton.cs /target:library
c:temp> UseSingleton.exe
Account 01234567 - Contains: Ian

Now it seems to me that COBOL is being under rated… perhaps someone should shout about it! 🙂

[ad#Google Adsense-3]

Posted in CLR, COBOL, Tips | Tagged , , | Comments Off on Design Patterns and COBOL

Valliant ecoTEC – Fault F.22

Our boiler at home today decided to display a “F22 0.2” message, at the same time I noticed it also had a low water pressure. Which I suspect was caused by myself bleeding the radiators of air the previous night, thus reducing the overall pressure.

After sometime search the web.. I found a common solution… thanks google

The solution was to open the filling loop until the boiler pressure and hey presto problem solved and I didn’t have to call the gas man and look like a silly bugger. Yeh… it seems easy in hindsight but at the time I had not a clue.

[ad#Google Adsense-3]

Posted in Uncategorized | Tagged , , , | Comments Off on Valliant ecoTEC – Fault F.22

VIM and GO

Getting started with GO and vim

As a developer who lives most of his life on my Mac with vi/vim, I was happy to see the GO distribution includes vim colourisation for .go, files ..but it was installed.. so..

To setup this up, first copy $GOROOT/misc/vim/go.vim to your vim/gvim syntax directory or $HOME/.vim/syntax (see comment below for the mac). Then all that is required is to setup the file association in runtime/filetype.vim, the following lines did it for me..


" Go
au BufNewFile,BufRead *.go setf go

If like me, you are using Vim.app on the mac, the runtime vim location will be Vim.app/Contents/Resources/vim/runtime with the syntax directory being below this too.

If all goes well, it should look like:

GVIM and GO

Posted in go, vim | Tagged , | Comments Off on VIM and GO

El Parador and the Choocolate Line

Dear Webblog Diary

Last friday night we spent the night in London and we found a really good little Spanish Tapas restaurant and I thought I would share it… just because we enjoyed it soo much..

The place is called : El Parador – 245 Eversholt Street, London NW1 1BA.

The next day we headed for Brugge via the Eurostar and true to form Maria (my partner) found the the best Chocolate shop in the country, which we believe is ‘The Chocolate Line‘.

foodies enjoy!

Posted in Tips | Tagged , , | Comments Off on El Parador and the Choocolate Line

GO Baby GO..

As someone who works with ‘C’, MSIL and Java bytecode to write system level software for our compilers and runtime, I was nicely surprised to see that someone has produced a new language aim at ‘System Level Programming’. Let me introduce you to ‘GO’ Googles new language and of course it is open source.

So why has google created the language? Well it is better to let Google themselves say why? (taken from their FAQ..)

No major systems language has emerged in over a decade, but over that time the computing landscape has changed tremendously. There are several trends:

  • Computers are enormously quicker but software development is not faster.
  • Dependency management is a big part of software development today but the “header files” of languages in the C tradition are antithetical to clean dependency analysis—and fast compilation.
  • There is a growing rebellion against cumbersome type systems like those of Java and C++, pushing people towards dynamically typed languages such as Python and JavaScript.
  • Some fundamental concepts such as garbage collection and parallel computation are not well supported by popular systems languages.
  • The emergence of multicore computers has generated worry and confusion.
  • Enough quotes.. I hear you say.. so what next if you want read more.. drive over to http://golang.org/.. now lets get practical and it up and running, as it is open-source and very young its a d.i.y. job… still it looks easy enough…

    They says its simple build… so lets put it to the test.. let my diary begin…

    Firstly… I’ll be up front and say these instructions are for the Mac OSX and tested on my little MacBook running 10.6 with xcode pre-installed..

    1) First download Mercurial source control:

    http://mercurial.selenic.com/downloads/

    2) Add the following to $HOME/.bash_profile using vim


    export PATH=$HOME/bin:$PATH
    export GOROOT=$HOME/go
    export GOARCH=amd64
    export GOOS=darwin

    Tip: if you don’t have a directory called bin, do a quick “mkdir $HOME/bin”

    3) Startup a new terminal session and check to see if the environment have been set with:


    env | grep "^GO"

    If all goes well you should see:


    GOARCH=amd64
    GOROOT=/Users/spg/go
    GOOS=darwin

    4) Okay, lets download the latest source code for Go!


    hg clone -r release https://go.googlecode.com/hg/ $GOROOT

    The ‘hg’ command gets the source code and hopefully your screen should be something like!


    requesting all changes
    adding changesets
    adding manifests
    adding file changes
    added 4109 changesets with 17125 changes to 2968 files
    updating to branch default
    1676 files updated, 0 files merged, 0 files removed, 0 files unresolved

    5) Now lets build it…


    cd $GOROOT/src
    ./all.bash


    wow.... A lot goes bye.... and then finally... it ends with......
    k-nucleotide
    mandelbrot
    meteor-contest
    pidigits
    threadring
    chameneosredux

    --- cd ../test
    1 known bugs; 0 unexpected bugs

    Wow… I appear to have something installed… it took me… less than fifteen minutes..

    So lets try some of the samples out…


    stephen-gennards-macbook:src spg$ cat hello.go
    package main

    import "fmt"

    func main() {
    fmt.Printf("hello, worldn")
    }
    stephen-gennards-macbook:src spg$ 6g hello.go
    stephen-gennards-macbook:src spg$ 6l hello.6
    stephen-gennards-macbook:src spg$ ./6.out
    hello, world

    Ohhh… my that was too easy… its time to leave the blog and start playing…

    What next… guess its time to watch a video about Go!


    [ad#Google Adsense-3]

    Posted in go, Google | Tagged , , , | Comments Off on GO Baby GO..

    Type Safety and COBOL

    Having read a recent bog about COBOL and type-safety, I though I would jot down some comments.

    ANS85 COBOL is naturally is type-unsafe due as every data item being part of one memory region (or storage area), because of this it can make is difficult to talk to type-safe language such as Java or the CLR.

    However, just like any computer language it does not stay still. Object oriented features were added to COBOL, at this point in time you we were given the verbs to create 100% type safe application.

    The key to writing type safe code is the use OBJECT-REFERENCE will a TYPE and using them on in your methods and using the INVOKE verb.

    For example:


    IDENTIFICATION DIVISION.
    PROGRAM-ID. "TypeSafeExample".
    ENVIRONMENT DIVISION.
    CONFIGURATION SECTION.
    REPOSITORY.
    CLASS SYS-STRING AS "System.String".
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 hello-world-string OBJECT REFERENCE SYS-STRING.

    PROCEDURE DIVISION.
    SET hello-world-string TO "Hello World"
    DISPLAY hello-world-string
    STOP RUN.

    The use of COBOL pictures can still be used, however for easy interop with other type-safe languages you really have to stick to the right types for your target environment when exposing your COBOL program to the other languages, this means if you are creating classes, the method to be consumed by other languages should use standard types, for .Net/CLR applications the use of CLS-COMPLIANT types is the right approach. For JVM applications, keep to the “core” java.lang types.

    Keep to these simple rules and COBOL will happy interop with type-safe languages. 🙂

    [ad#Google Adsense-3]

    Posted in CLR, COBOL, JVM, TypeSafety | Tagged , | Comments Off on Type Safety and COBOL

    Base Class Library, Arrays, Queues and Stacks

    Continuing the series of blogs about COBOL and the .Net base class library…

    The .Net base class library has a wealth classes and an huge of amount of methods/properties.

    The .Net base class library has a key handy namespace that contains are the lovely classes, which is System.Collections.

    As you can see from the above link, it does have quite a lot, so for this blob entry I will look at:

  • arrays of numbers
  • queues
  • stacks
  • Lets start the first example by using an array of numbers… just to demonstrate its not just strings you can assign to the arrays using “values”.


    01 numbers binary-long occurs 10
    values 10 20 30 40 50 10 20 30 40 50.

    01 num binary-long value 0.
    01 average binary-double value 0.

    procedure division.
    perform varying num through numbers
    add num to average
    end-perform
    divide average by numbers::"Length" giving average
    display "Average is : " average

    Running the above code gives us:


    Average is : +00000000000000000030

    The next class that is helpful, is the Queue class, this allows us to “Enqueue” and “Dequeue” item using the same methods, the queue is again displayed using the “perform through” syntax.


    $set ilusing"System"
    $set ilusing"System.Collections"

    01 alphabetQueue type "Queue".
    01 item object reference.

    set alphabetQueue to new type "Queue"()
    invoke alphabetQueue::"Enqueue"("A item")
    invoke alphabetQueue::"Enqueue"("B item")
    invoke alphabetQueue::"Enqueue"("C item")

    display "The initial queue is: "
    perform varying item through alphabetQueue
    display " " item
    end-perform

    display "".
    display "De-Queue an item: " alphabetQueue::"Dequeue"()

    display "".
    display "Afterwards is: "
    perform varying item through alphabetQueue
    display " " item
    end-perform

    Running the above queue example gives:


    The initial queue is:
    A item
    B item
    C item

    De-Queue an item: A item

    Afterwards is:
    B item
    C item

    Next, lets look at the ‘Stack’ class, this is very similar, expect it uses the “Push”, “Pop” methods and we iterate through the Stack using the ‘perform through’ syntax.


    $set ilusing"System"
    $set ilusing"System.Collections"

    01 alphabetStack type "Stack".
    01 item object reference.

    set alphabetStack to new type "Stack"()
    invoke alphabetStack::"Push"("A item")
    invoke alphabetStack::"Push"("B item")
    invoke alphabetStack::"Push"("C item")

    display "The initial stack is: "
    perform varying item through alphabetStack
    display " " item
    end-perform

    display "".
    display "Pop item: " alphabetStack::"Pop"()

    display "".
    display "Afterwards is: "
    perform varying item through alphabetStack
    display " " item
    end-perform

    Running the above stack example gives:


    The initial stack is:
    C item
    B item
    A item

    De-Stack an item: C item

    Afterwards is:
    B item
    A item

    As you can see using the base class library collection classes with COBOL is just as easy as using C#… so if you are already using COBOL… continue and try out the .Net base class library.. it really is very easy to use and it will make you more productive.. so please explore and enjoy .Net and its Base Class library.

    Posted in CLR, COBOL | Tagged , , | Comments Off on Base Class Library, Arrays, Queues and Stacks

    Detecting the use Mono CLR dynamically

    While developing something that could be used on Mono on Windows, Mono on Unix and on Windows with Microsoft’s CLR, I needed to be sensitive to the environment but didn’t want to conditionally compile my code different. So I put together a quick class to help.. Below is the C# code with pics of it running on Windows/Mac…


    using System;
    using System.Reflection;

    namespace Gennard.Net
    {
    public class CLRUtils
    {
    private static readonly bool isMono= Type.GetType("Mono.Runtime") == null ? false : true;

    private static readonly int eOSp = (int)Environment.OSVersion.Platform;
    private static readonly bool isUnix = (eOSp == 4) || (eOSp == 128);

    /* Class Properties */
    public static bool IsMono { get { return isMono; } }
    public static bool IsUnix { get { return isUnix; } }

    public static void Main()
    {
    Console.WriteLine("Are we using Mono? : "+IsMono);
    Console.WriteLine("Are we using Unix? : "+IsUnix);
    }
    }
    }

    The taste of the pudding mix.. is in the eating.. so lets see it working…

    On Windows....

    On the Mac, we get....

    Posted in CLR, CSharp, Mono, Tips | Tagged , , | Comments Off on Detecting the use Mono CLR dynamically

    Arrays and the .Net Base Cass Library

    Carrying on from the previous blog, the user of iterators in .Net and especially .Net on COBOL can be very useful.

    When CLR v2.0 was introduced a few new methods in System.IO.File for block reading/writing files were introduced, these works on arrays aka “OCCURS ANY” fields. Using these APIs instead of using traditional line sequentials is a breath of fresh air, especially if you just want to do some code something quickly without the need of records/group items.

    So lets.. have a little play around, lets read a “MonthNames” from the CLR, write them to disk, read them back, reverse and sort it, while display them…


    01 Months-Array type "System.Array".

    01 Months string occurs 12.
    01 Month string.

    *> Note: the MonthsNames has 13 elements and the last element is ""
    *> and I'm not interested it it so I'll drop it by doing a "ConstrainedCopy"
    *> with just elements I'm interested in.
    *> http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.monthnames.aspx
    set Months-Array to type "System.Globalization.DateTimeFormatInfo"::"CurrentInfo"::"MonthNames"
    invoke type "System.Array"::"ConstrainedCopy"(Months-Array, 0, Months, 0, Months::"Length")

    display Months::"Length"
    perform varying Month through Months
    display "Normal -> " Month
    end-perform

    *> Write the array to disk
    invoke type "System.IO.File"::"WriteAllLines"("MyMonths.txt", Months)

    set Months to null

    *> Read it back
    set Months to type "System.IO.File"::"ReadAllLines"("MyMonths.txt")

    *> Reverse it
    invoke type "System.Array"::"Reverse"(Months as Type "System.Array")

    *> Display it
    perform varying Month through Months
    display "Reverse -> " Month
    end-perform

    *> Sort it
    invoke type "System.Array"::"Sort"(Months as type "System.Array")

    *> Display it
    perform varying Month through Months
    display "Sorted -> " Month
    end-perform

    Which when executed gives us:


    12
    Normal -> January
    Normal -> February
    Normal -> March
    Normal -> April
    Normal -> May
    Normal -> June
    Normal -> July
    Normal -> August
    Normal -> September
    Normal -> October
    Normal -> November
    Normal -> December
    Reverse -> December
    Reverse -> November
    Reverse -> October
    Reverse -> September
    Reverse -> August
    Reverse -> July
    Reverse -> June
    Reverse -> May
    Reverse -> April
    Reverse -> March
    Reverse -> February
    Reverse -> January
    Sorted -> April
    Sorted -> August
    Sorted -> December
    Sorted -> February
    Sorted -> January
    Sorted -> July
    Sorted -> June
    Sorted -> March
    Sorted -> May
    Sorted -> November
    Sorted -> October
    Sorted -> September

    That was pretty easy… and the code looks okay too… can’t be bad…

    References : System.Globalization.DatetimeFormat

    Posted in CLR, COBOL, Tips | 4 Comments