15 December 2007

Correcting blank image and link dialogs in Joomla

I was going nuts! When I worked on my friend's Joomla site from home, the Image and Link dialogs appear just fine:


But when I tried it from work or when my friend did it from her house, the dialogs came up blank:


I tried every combination of browser setting and browser (Firefox, Opera, IE). Nothing. What was so magical about my machine at home?

I found the answer on Alex Le's blog. It turns out that my site home (as defined by $mosConfig_live_site) was set to www.site_name.com while I was firing up the administration console with http://site_name.com/administrator (notice the lack of www). Once I launched from http://www.site_name.com/administrator, the edit dialogs are dandy again, just like they are from home. Thanks, Alex!

Getting JRuby gem utility to work

I've been cursing my brains out trying to get the JRuby version of gem to work. After much hunting around, I found that a Java memory size issue was causing the problem. Things have been working fine since I started installing gems with:

jruby -J-Xmx512M -S gem install

My apologies for having lost the original reference to this information. This bug entry might have been where I first found the solution.

JRuby as a system programming language

At work, we are starting to refactor some of our system utilities. The scripts interact with these basic components:
  • Postgres databases
  • FTP servers
  • Files on various servers
  • Java applications
  • Specific system calls
My boss, being from an "older generation" ;-), favors Unix scripting (sed, bash, etc.) . Being a Ruby/JRuby enthusiast myself, I wanted to show my team how easy these tasks are in JRuby. While sed might take fewer characters in some cases, I think JRuby strikes an ideal match between terseness and readability.

Note that the following code will not run on your computer, at least not without setting up a Postgres database and changing the FTP server info, etc. But it will give you a good flavor for system programming in JRuby.


#

# Copyright 2007 Effectiveqa.com. All rights reserved

#



def postgres

# Accessing Postgres from JRuby

# http://ruby.scripting.ca/postgres/rdoc/



$LOAD_PATH << 'C:/jruby-1.0.1/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib'

require 'postgres'

con = PGconn.connect('localhost', 5432, nil, nil, 'foo', 'postgres', 'password'

res = con.query("select * from bar")

puts res[0][0]

end



def ftp

# FTP Access

# http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/index.html

require 'net/ftp'

Net::FTP.open('effectiveqa.com') do |ftp|

ftp.login('username', 'password')

ftp.chdir('public_html')

ftp.list('*.css').each {|file| puts file}

ftp.gettextfile('main.css', 'local_main.css')

end

end



def ftp_password

# Show how easily JRuby can handle password problem

require 'net/ftp'

begin

ftp = Net::FTP.open('effectiveqa.com')

ftp.login('effectiv', 'BADPASSWORD')

rescue

puts "Here is where I fix the password"

ftp.login('username', 'password')

ftp.chdir('public_html')

ftp.list('*.css').each {|file| puts file}
end

end



def file_tricks

# Different file/directory manipulations

# http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/index.html

require 'fileutils'

include FileUtils



begin cd('dfsdf') rescue puts 'Oops, had a problem...' end

chmod(0777, 'local_main.css')

puts "Don't match!" if ! cmp('local_main.css', 'local_main_1.css')

cp('local_main.css', 'local_main_copy.css')

puts "Do match!" if cmp('local_main.css', 'local_main_copy.css')

puts pwd

rm('local_main_copy.css')

begin rm('DOESNT_EXIST') rescue puts 'Caught one' end

end



def file_tests

# Very nice tests

# http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_filetest.html



puts FileTest.directory?('example.zip') # false

puts FileTest.size('example.zip') # 1723

puts FileTest.file?('.') # false

puts FileTest.file?('example.zip') # true

puts FileTest.readable?('example.zip') # true

puts FileTest.writable?('example.zip') # true



include FileTest

puts directory?('.') # true; needs include

puts file?('.') # false

end

def use_java

# Show off how easy it is to use Java from JRuby

include Java



zf = java.util.zip.ZipFile.new('example.zip')

puts "There are #{zf.size} files in the zip."

zf.entries.each {|entry| puts " file: #{entry}"}

end



def split_files

# Easy processing of DB dump files



lines = IO.readlines('pipeFile.txt')

lines.each {|line| puts line.split('|')[1]}

end



def system_calls

# JRuby has full access to OS calls

lines = `df`.split("\n")

lines.shift # Read off header line

lines.each do |line|

fields = line.split

next unless fields[0] =~ /C:$/ # Only care about the C: drive

percent = fields[4].to_i

puts "Holy cow, disk almost full" if percent > 95

puts "Disk > 50% full" if percent > 50

end

end



# Some interesting Ruby libraries

# http://beaver.net/slides/ruby/10-easy-pieces.html

# Library doc: http://www.ruby-doc.org/stdlib/

# Pathnames: http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html

# MD5, sha1: http://www.ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html

# Waking trees: http://ruby-doc.org/stdlib/libdoc/find/rdoc/index.html



puts "\n**** I can access postgres\n"

postgres

puts "\n**** I can access FTP\n"

ftp

puts "\n**** I can tell if an FTP password failed\n"

ftp_password

puts "\n**** I have all kinds of *nix file commands\n"

file_tricks

puts "\n**** I also have all kinds of cool file tests\n"

file_tests

puts "\n**** I can easily use Java code\n"

use_java

puts "\n**** I can split up Dave's db dump files\n"

split_files

puts "\n**** I can make OS calls with ease\n"

system_calls



The only shortcoming I could find with JRuby is that it doesn't support all the command line options that Ruby does. So you can see that JRuby falls behind in the terseness of its one-liner to rip a particular field out of a pipe-delimited dump file, though I would argue it is more readable.

#> ruby -n -a -F"\|" -e "puts $F[1]" pipeFile.txt

#> perl -n -a -F"\|" -e 'print "$F[1]\n"' pipeFile.txt

#> jruby -e "IO.readlines('pipeFile.txt').each {|ln| puts ln.split('|')[1]}"


05 December 2007

Using Subversion svn+ssh protocol without typing passwords

I have been having more and more problems using the svn: protocol for Subversion. So I've moved to using the svn+ssh: protocol. The only frustration was having to constantly enter my password (sometimes many times) for each svn command. It turns out that svn+ssh: is using standard SSH protocols, so SSH key authentication solves the problem.

I found a nice write-up on Aaron Toponce's blog on how to set it up.

It is a little more complicated for Windows. I will address that in a future installment.