Pages

Friday, April 29, 2011

Java -=- How to copy file using Apache IOUtils

Here is a code snippet that explain how to copy a file using IOUtils. You can get Apache IOUtils at http://commons.apache.org/io/download_io.cgi

InputStream in = null;
OutputStream out = null;

try {
    // Ensure folder is created
    FileUtils.forceMkdir(new File("/Users/yann/myfolder"));

    in = new FileInputStream(new File("/Users/yann/from.txt"));
    out = new FileOutputStream(new File("/Users/yann/myfolder", "to.txt"));

    IOUtils.copy(in, out);

} finally {
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
}

Wednesday, April 27, 2011

Good site to find free favicons

Here's a good site to find free favicons

http://www.freefavicon.com/

JAD Java Decompiler Download Mirror

Since http://www.kpdus.com/ is not available anymore, JAD is now hard to find.

Here's a mirror containing all the versions of JAD. This article is a copy of Tomas Varaneckas's site (http://www.varaneckas.com/jad).

 

Installation

Unzip jad.zip file into any appropriate directory on your hard drive.
This will create two files:
  • an executable file named 'jad.exe' (Windows 9x/NT/2000) or 'jad' (UNIX)
  • README file 'Readme.txt', which contains the short user's manual
For UNIX users: make 'jad' executable: chmod a+x jad
No further setup is required.

Disclaimer

JAD is Copyright 1997-2001 Pavel Kouznetsov. All rights reserved. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE IN ON-LINE CONTROL OF AIRCRAFT, AIR TRAFFIC, AIRCRAFT NAVIGATION OR AIRCRAFT COMMUNICATIONS; OR IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY. THE AUTHOR DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OFFITNESS FOR SUCH USES. YOU MUST REPRESENT AND WARRANT THAT YOU WILL NOT USE THE SOFTWARE FOR SUCH PURPOSES

Monday, April 18, 2011

[RSA] Install Cicso AnyConnect on Mac OS X Snow Leopard

Hi!

There is a bug with the version of Cisco AnyConnect 2.3 that made impossible to install the VPN client on Mac OS X Snow Leopard.

To be able to install it, we must download a newer version (2.4 and later).

You can get the client by connecting to the Cisco Web Site (http://www.cisco.com/cisco/software/navigator.html) and download it. You need a username / password.

If you do not have an account on the Cisco's Web Site, you can download a copy of the software here:
at  http://unf.net/snowleopard/anyconnect-macosx-i386-2.4.0154-k9-BETA.zip


Download and install the client. After that, connect yourself to the RSA servers. The AnyConnect client will be automatically updated.

You must be ready to use AnyConnect Client with Snow Leopard.

Thursday, April 14, 2011

ExtJS -=- Converting a timestamp value to a valid date when loading a FormPanel (DateField)

I've got a problem today. I was trying  to load a record that contains a timestamp field that comes from a Rails application (with PostgreSQL DB).

The FormPanel refuse to load the date value in my DateField component. I've discovered that it was because the DateField did not recognize the date because it was a timestamp.

In order to be able to set the date's value, we need to format the timestamp as a date when loading the FormPanel.

Here's how to do this.

Generated JSON used to load the FormPanel
{
     success:true, 
     data: {
          start_date: \"Tue Apr 12 20:06:11 UTC 2011\", 
          id: 4, 
          app_id: 2, 
          version: \"1.2.3\", 
          message: \"test msg\", 
          active: true
     }
}


Add this code to your FormPanel
form.getForm().on({
    actioncomplete: function(form, action) {
        if (action.type === 'load') {
            if (action.result.success) {
                var date = new Date(action.result.data.start_date);

                form.setValues({
                    start_date: date.format('Y-m-d')
                });
            }
        }
    }
});

When loading, the value of the date will be converted from a timestamp to a valid date and the DateField will recognize the value.

ExtJS -=- Wait message while loading a store

Hi!

To display a loading message while a store is loading, simply add this line of code...

// Store Definition
var objectAStore =  new Ext.data.JsonStore({
    url: '/objecta/json_dropdown_list',
    root: 'Records',
    totalProperty: 'Total',
    fields:[
    {
        name: 'id'
    },
    {
        name: 'name'
    }
    ]
});

// Loading Mask
new Ext.LoadMask(Ext.getBody(),{msg:'Loading data...', store: objectAStore});

If you want a loading mask for a grid panel, simply set GridPanel's property "loadMask" to true

Monday, April 11, 2011

Mac OS X -=- Installing Java 1.4 on Snow Leopard

By default, Snow Leopard does not include Java 1.4 anymore. All the Java 1.4 softlinks points to the Java 1.6 library. If you want to install Java 1.4, you must install it manually.


In the terminal: 


Get the java 1.4.2 that was included in 10.5 "leopard" and unpack

cd /tmp/
curl -o java.1.4.2-leopard.tar.gz http://icebergsofts.com/static_content/java.1.4.2-leopard.tar.gz
tar -xvzf java.1.4.2-leopard.tar.gz

Move it to your System java folder (password needed)
sudo mv 1.4.2 /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2-leopard

Tell OS X that java 1.4.2 actually is java 1.4.2
cd /System/Library/Frameworks/JavaVM.framework/Versions/
sudo ln -s 1.4.2-leopard 1.4.2
sudo ln -s 1.4.2 1.4

Open Java Preferences
open "/Applications/Utilities/Java Preferences.app"

Change the properties to use Java 1.4.2 32-bit by default:



Credits goes to http://wiki.oneswarm.org/index.php/OS_X_10.6_Snow_Leopard

Mac OS X -=- Installing Java 1.5 on Snow Leopard

By default, Snow Leopard does not include Java 1.5 anymore. All the Java 1.5 softlinks points to the Java 1.6 library. If you want to install Java 1.5, you must install it manually.


In the terminal: 


Get the java 5 that was included in 10.5 "leopard" and unpack

cd /tmp/
curl -o java.1.5.0-leopard.tar.gz http://icebergsofts.com/static_content/java.1.5.0-leopard.tar.gz
tar -xvzf java.1.5.0-leopard.tar.gz

Move it to your System java folder (password needed)
sudo mv 1.5.0 /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0-leopard

Tell OS X that java 5 actually is java 5
cd /System/Library/Frameworks/JavaVM.framework/Versions/
sudo rm 1.5.0
sudo ln -s 1.5.0-leopard 1.5.0
sudo rm 1.5
sudo ln -s 1.5.0 1.5

Open Java Preferences
open "/Applications/Utilities/Java Preferences.app"

Change the properties to use Java 5 32-bit by default:



Credits goes to http://wiki.oneswarm.org/index.php/OS_X_10.6_Snow_Leopard

Friday, April 8, 2011

ExtJS -=- Complete FormPanel Example with ComboBox

Here's a complete example how to use a FormPanel (loading and saving) that contains some combobox binded with JSON Stores. Since calls in AJAX are asynchronous, by the time the form is loaded, the JSON Store are not loaded. So the Combo box shows the index instead of the right value...

This example loads the JSON Stores in sequence, and once the store are loaded, it loads the FormPanel data. So all the data is loaded correctly...



var objectAStore =  new Ext.data.JsonStore({
    url: '/objecta/json_dropdown_list',
    root: 'Records',
    totalProperty: 'Total',
    fields:[
    {
        name: 'id'
    },
    {
        name: 'name'
    }
    ]
});

var objectBStore = new Ext.data.JsonStore({
    url: '/objectb/json_dropdown_list',
    root: 'Records',
    totalProperty: 'Total',
    fields:[
    {
        name: 'id'
    },
    {
        name: 'name'
    }
    ]
});

var myForm = new Ext.form.FormPanel({
    monitorValid: true,
    border: false,
    bodyBorder: false,
    baseCls: 'x-plain',
    labelWidth: 130,

    defaults:{
        width: 270,
        allowBlank: false,
        displayField: 'name',
        valueField: 'id'
    },

    keys: [
    {
        key: [10, 13],
        fn: function() {
            if(myForm.getForm().isValid()){
                myForm.buttons[0].fireEvent('click');
            }
        }
    }
    ],

    buttons: [
    {
        text: 'Save',
        formBind: true,
        handler: function(){
            myForm.getForm().submit({
                method:'POST',
                url: '/objectc/save',
                reset: false,
                waitTitle: "Please Wait",
                waitMsg: 'Saving ...',
                success: function(form, action) {
                    win.close();
                },
                failure: function() {
                //Failure stuff here
                }
            })
        }
    },
    {
        text: 'Close',
        handler: function() {
            win.close();
        }
    }
    ],

    items:[
    {
        xtype: 'hidden',
        name: "id",
        value: objectc_id
    },
    {
        mode: 'remote',
        hiddenName: 'from_objectb',
        fieldLabel: 'FROM OBJECT B',
        xtype: 'combo',
        store: objectBStore
    },
    {
        fieldLabel: 'OBJECT A',
        xtype: 'combo',
        typeAhead: true,
        mode: 'remote',
        minChars: 1,
        hiddenName: 'objecta',
        triggerAction: 'all',
        store:  objectAStore
    },

    {
        fieldLabel: 'TO OBJECT B',
        xtype: 'combo',
        hiddenName: 'to_objectb',
        store: objectBStore
    }
    ]
});


/**
     * If the relationship_id is not empty, so the window is used of updating data
     */
if(!Ext.isEmpty(relationship_id)){
    objectAStore.load({
        params:{
            'objecta': objecta
        },
        callback: function(){
            objectBStore.load({
                params:{
                    'from_objectb': from_objectb,
                    'to_objectb': to_objectb
                },

                callback: function(){
                    myForm.load({
                        url: '/objectc/get_form_data',
                        params: {
                            'objectc_id': objectc_id
                        }
                    });
                }
            });
        }
    });


}

win = new Ext.Window({
    title: 'TITLE HERE',
    height: 163,
    width: 450,
    layout: 'fit',
    border: false,
    bodyStyle: 'padding: 10px',
    bodyBorder: false,
    modal: true,
    items:[
    myForm
    ],
    listeners:{
        beforeshow: function(){
            Ext.MessageBox.hide();
        }
    }
});

win.show();

Mac OS X -=- Prevent Writing .DS_Store on Network Drives

If you want to prevent Mac OS X to write .DS_Store on network drives, simply write this command in a terminal.

defaults write com.apple.desktopservices DSDontWriteNetworkStores true

After restart, OS X won't create these files anymore on shared drives.

Thursday, April 7, 2011

PHP - Getting details of a variable

var_dumpDumps information about a variable

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
In PHP 5 all public, private and protected properties of objects will be returned in the output.


For example, here's an output of a variable

 array(3) { [0]=> object(stdClass)#17 (3) { ["id"]=> string(1) "1" ["news_date"]=> string(10) "2011-04-07" ["news"]=> string(15) "TEst nouvelle 1" } [1]=> object(stdClass)#18 (3) { ["id"]=> string(1) "2" ["news_date"]=> string(10) "2011-04-07" ["news"]=> string(16) "Test nouvelles 2" } [2]=> object(stdClass)#19 (3) { ["id"]=> string(1) "3" ["news_date"]=> string(10) "2011-04-07" ["news"]=> string(16) "Test nouvelles 3" } }

Ruby on Rails -=- EZ_Where plugin

Here's a good plugin used to build where conditions for Rails without SQL. It is called ez_where plugin for rails.

You can download it at https://rubyforge.org/projects/ez-where/

Here's the description of the project as shown on one of the creator's blog (see: http://brainspl.at/articles/tag/conditions)

This plugin is meant to be used as a nice ruby like syntax for creating the :conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method. This method takes a block to simplify single and multi table queries.

articles = Article.ez_find(:all, :include => :author) do |article, author|
  article.title =~ "%Foo Title%"
  author.any do
    name == 'Ezra'
    name == 'Fab'
  end 
end
This will produce :conditions => ["article.title LIKE ? AND
(authors.name = ? OR authors.name = ?)",
"%Foo Title%", "Ezra", "Fab"]

Basically here is the breakdown of how we map ruby operators
to SQL operators:

foo == 'bar'           #=> ["foo = ?", 'bar']
foo =~ '%bar'          #=> ["foo LIKE ?", '%bar']
foo <=> (1..5)         #=> ["foo BETWEEN ? AND ?", 1, 5]
id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]]
<, >, >=, <= et all will just work like you expect.

There is also the ability to create the conditions in stages so
you can build up a query:

cond = Caboose::EZ::Condition.new do
  foo == 'bar'
  baz <=> (1..5)
  id === [1, 2, 3, 5, 8]
end
 
@result = Model.find(:all, :conditions=> cond.to_sql)


#=> ["foo = ? AND baz BETWEEN ? AND ? AND id IN (?)",
"bar", 1, 5, [1, 2, 3, 5, 8]]


You can even do nested sub conditions. condition will use AND
by default in the sub condition:
cond = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  baz <=> (1..5)
  id === [1, 2, 3, 5, 8]
  condition :my_other_table do
    fiz =~ '%faz%'
  end
end

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["my_table.foo = ? AND my_table.baz BETWEEN ? AND ?
AND my_table.id IN (?) AND (my_other_table.fiz LIKE ?)",
"bar", 1, 5, [1, 2, 3, 5, 8], "%faz%"]

You can also build multiple Condition objects and join
them together for one monster find:

cond_a = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  condition :my_other_table do
    id === [1, 3, 8]
    foo == 'other bar'
    fiz =~ '%faz%'
  end
end

#=> ["my_table.foo = ? AND (my_other_table.id IN (?) AND my_other_table.foo = ?
AND my_other_table.fiz LIKE ?)", "bar", [1, 3, 8], "other bar", "%faz%"]

cond_b = Caboose::EZ::Condition.new :my_table do
  active == true
  archived == false
end

#=> ["my_table.active = ? AND my_table.archived = ?", true, false]

composed_cond = Caboose::EZ::Condition.new
composed_cond << cond_a
composed_cond << cond_b
composed_cond << 'fuzz IS NULL'

@result = Model.find(:all, :conditions => composed_cond.to_sql)

#=> ["(my_table.foo = ? AND (my_other_table.id IN (?) AND my_other_table.foo = ?
AND my_other_table.fiz LIKE ?)) AND (my_table.active = ? AND my_table.archived = ?)
AND fuzz IS NULL", "bar", [1, 3, 8], "other bar", "%faz%", true, false]

You can compose a new condition from different sources:

ar_instance = Author.find(1)

other_cond = Caboose::EZ::Condition.new :my_table do 
  foo == 'bar'; baz == 'buzz'
end

cond = Caboose::EZ::Condition.new
# another Condition
cond.append other_cond
# an array in AR condition format
cond.append ['baz = ? AND bar IS NOT NULL', 'fuzz'], :or
# a raw SQL string
cond.append 'biz IS NULL'
# an Active Record instance from DB or as Value Object
cond.append ar_instance

#(append is aliased to << because of syntax issues 
involving multiple args like :or)

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["(my_table.foo = ? AND my_table.baz = ?) OR (baz = ? AND bar IS NOT NULL)
AND biz IS NULL AND authors.id = ?", "bar", "buzz", "fuzz", 1]

OK there is also other options for doing subconditions. OR is
aliased to any and any creates a subcondition that uses OR to
join the sub conditions:

cond = Caboose::EZ::Condition.new :my_table do
  foo == 'bar'
  any :my_other_table do
    baz === ['fizz', 'fuzz']
    biz == 'boz'
  end
end

@result = Model.find(:all, :conditions=> cond.to_sql)

#=> ["my_table.foo = ? AND (my_other_table.baz IN (?)
OR my_other_table.biz = ?)",
"bar", ["fizz", "fuzz"], "boz"]

OK lets look a bit more at ez_find with a few more complex queries:

# all articles written by Ezra. Here you can use a normal AR object
# in the conditions
# session[:user_id] = 2
ezra = Author.find(session[:user_id])    
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author << ezra # use AR instance to add condition; uses PK value if set: author.id = ezra.id
end 

#=>["(authors.id = ?)", 2]

# all articles written by Ezra, where he himself responds in comments
@articles = Article.ez_find(:all, :include => [:author, :comments]) do |article, author, comment|
  article.author_id == ezra.id
  comment.author_id == ezra.id   
end

#=>["(articles.author_id = ?) AND (comments.author_id = ?)", 2, 2]

# any articles written by Fab or Ezra
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author.name === ['Fab', 'Ezra']   
end
#=>["(authors.name IN (?))", ["Fab", "Ezra"]]

# any articles written by Fab or Ezra, using subcondition
@articles = Article.ez_find(:all, :include => :author) do |article, author|
  author.any do
    name == 'Ezra'
    name == 'Fab'
  end  
end

#=>["(authors.name = ? OR authors.name = ?)", "Ezra", "Fab"]

# any articles written by or commented on by Fab, using subcondition
@articles = Article.ez_find(:all, :include => [:author, :comments]) do |article, author, comment|
  article.sub { author_id == 1 }
  comment.outer = :or # set :outer for the comment condition, since it defaults to :and
  comment.sub { author_id == 1 }       
end

#=>["(articles.author_id = ?) OR (comments.author_id = ?)", 1, 1]

@articles = Article.ez_find(:all, :include => [:author, :comments],
                           :outer => { :comments => :or }, 
                           :inner => { :article => :or}) do |article, author, comment|
  article.sub { author_id == 1; author_id == 2 }
  comment.sub { author_id == 1 } 
end

["(articles.author_id = ? OR articles.author_id = ?) OR (comments.author_id = ?)", 1, 2, 1]

And finally you can use any and all with ez_condition like this:

cond = Article.ez_condition { active == true; archived == false }
cond.all { body =~ '%intro%'; body =~ '%demo%' }
cond.any { title =~ '%article%'; title =~ '%first%' }

#=> ["articles.active = ? AND articles.archived = ?
AND (articles.body LIKE ? AND articles.body LIKE ?)
AND (articles.title LIKE ? OR articles.title LIKE ?)",
true, false, "%intro%", "%demo%", "%article%", "%first%"]

ExtJS -=- Items auto width in FormPanel

When you use a FormPanel with ExtJS 2.3, you cannot specify the layout type as "fit". If you specify "fit" you will lost all the labels of the items of your FormPanel.

If you want your items filling all the width of the container, you must use the anchor properties.

Here's a small example that shows a combobox using the anchor property and the width of the combo is 100% of the container...

{
     fieldLabel: 'Type',
     anchor: "100%",
     xtype: 'combo',
     typeAhead: true,
     mode: 'remote',
     minChars: 1,
     hiddenName: 'relationship[type_id]',
     triggerAction: 'all',
     store:  relationshipTypeStore
}

Really Good B2B Integration Tool

Last week, I visited the EXTOL's company to have more details about the EXTOL Business Integrator software. EXTOL Business Integrator (EBI) is platform-independent business integration middleware, with seamless support for “lights-out” business-to-business (B2B), Software as a Service (SaaS), internal application-to-application (A2A) and data integration.

It is designed to replace – and bridge the gaps between – conventional EDI translators, data integration tools, business process automation tools, web services toolkits, XML transformation toolkits and spreadsheet integrators. EBI spans this broad range of capabilities by providing a flexible set of integration services that can be combined and configured to suit a variety of business purposes, all without programming.

With EBI, you can handle B2B, A2A, SaaS and data integration problems individually or in any combination required by your business. EBI can implement simple, point-to-point solutions that automate parts of larger business processes, or to implement longer, more complex business processes that connect applications, trading partners, services and business data resources.


EBI includes a powerful and highly flexible set of business integration services, including process automation, data transformation, messaging and communications, event management, scheduling, and adapter-based connections to internal and external sources and targets, as well process monitoring, auditing, and governance tools. The solution seamlessly integrates with your existing applications and business processes through business events, schedules, application actions or explicit process invocations.

For conclusion, EBI is a really great product. It is insanely easy to use and it has a lot of features!

+1 for the guys of EXTOL!

ExtJS 4 Beta 2 Released

The Sencha team has launched a new version of the well known AJAX library ExtJS. You can download it at ExtJS 4 Beta 2.

You can see the changes / improvements by visiting http://www.sencha.com/products/extjs4-beta/

I will give a try this evening of this library!

Monday, April 4, 2011

Ruby -=- Using environment variable

Hi

With Ruby, You can use environment variable by using the function.

ENV['JAVA_HOME']

The variable must be defined in the operating system.


Ruby have many places to define environment variable within the code. You can put environment variable common across environment in [RubyProject]/config/environment.rb

Also, if you want to define an environment variable for a specific environment, you can set the variable in [RubyProject]/config/environments/production.rb or test.rb or development.rb...

The variable defined in the project can be use directly. For example, if I put MY_VAR = 3 in the file environment.rb, I will use the variable directly by calling MY_VAR in the code. For example


p MY_VAR

How to Tie a Tie

Hi!

Here's a simple tutorial how to tie a tie.

http://www.noeud-de-cravate.com/noeud-simple.html