Showing posts with label how to. Show all posts
Showing posts with label how to. Show all posts

Saturday, December 03, 2011

configure boxee to work as a second monitor with sound over hdmi

My HTPC setup is simple:
I ran a 40 foot HDMI cable from my computer room into my bedroom through the basement. I hooked up my TV in the bedroom to be the 2nd monitor on my PC and set boxee to run on that monitor.

The problem:
I wanted to have Boxee use sound over hdmi, but also be able to listen to to music and play games on my computer as usual without the sound coming from the bedroom TV.

Solution:
In your windows sound prefs set:
  • "Speakers" as your default audio device
  • "HDMI digital output" as your default communication device
In boxee sound config set:
  • primary soundcard: HDMI digial sound
restart boxee. Now you should be able to hear HDMI sound only on the TV, and all other sound will come from your speakers. 

Friday, July 08, 2011

Python: remove some entries from a dict

Problem:
Remove a bunch of entries from a dict in an elegant way

Solution 1:

map(lambda name: dict.pop(name, None), ['some_key', 'some_other_key', 'bleee'])


Solution 2:
My coworker Dave came up with an even better way to do this thing:

map(dict.pop, ['some_key', 'some_other_key', 'bleee'], [])

Wednesday, April 06, 2011

Grails: Securing CKEditor URLs

Problem:
I am using the grails CKEditor plugin and i wanted to secure the file manager urls to be accessed only by a site admin. I am using the Spring Security Core plugin for site security, but i was already using annotations elsewhere on my site, so using a InterceptUrlMap was not an option.

So, how to keep using annotations and secure a plugin's urls?

Solution:
The trick is properly using the controllerAnnotations.staticRules (which really isnt that hard if you pay attention to the docs).
Here is the mapping that works for ANY url mapping you define to CKEditor:



Sunday, February 06, 2011

Multiple domains with tomcat behind apache

This is how you would host multiple domains/webapps on a single tomcat behind apache via mod-jk.

first setup tomcat behind apache

I am setting up a new domain called dogself.com:

in server.xml add this under last host:

<Host name="dogself.com"  appBase="webapps-dogself"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false">
<alias>www.dogself.com<alias>
</Host> 
 
and also add another connector port:
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> 

create a webapps-dogself directory next to webapps and give it the same permissions

in your virtualhost apache config:
ServerName dogself.com 
JkMount  /* dogself 
 
in workers.properties add dogself worker to list:
worker.list=another,dogself

# Define dogself
worker.dogself.port=8010
worker.dogself.host=dogself.com
worker.dogself.type=ajp13 

If you want the manager app to work on the domain you need to:
mkdir /etc/tomcat6/Catalina/dogself.com 

and copy the manager.xml into it from /etc/tomcat6/Catalina/localhost

in /var/lib/tomcat6 create a webapps-dogself dir and give it 0775 perms and chown it to tomcat:tomcat

restart tomcat and apache

Troubleshooting:
if you get error:
worker dogself can't resolve tomcat address dogself.com
Check that your dns a/aaaa records point to your ip. Comment out the worker in workers.properties and see if you can get to the apache vhost.

if any of the domains seems to point to another domain, ie the war that is running on one domain is incorrect. check if you are in the www.domain and if you have aliased the www both in tomcat and in apache. if you dont alias the www correctly tomcat will send you the the defaultDomain as defined in server.xml

Thursday, February 03, 2011

Linode: how to install mod_jk, tomcat behind apache

This "tutorial" is for Ubuntu 10.04 LTS (Lucid).
This is how you would set up Tomcat6 behind Apache2 via mod_jk

I am assuming that you have apache2 installed and tomcat6 installed. I also assume you have deployed and can find your tomcat app possibly on port 8080 of your linode.

  1. Install mod_jk:
    apt-get install libapache2-mod-jk
  2. Create workers.properties in /etc/apache2/ and add the following into it:
    # this lives in /etc/apache2/workers.properties
    # workers.properties - ajp13
    #
    # List workers
    worker.list=worker1
    
    # Define worker1
    worker.worker1.port=8009
    worker.worker1.host=localhost
    worker.worker1.type=ajp13
    
  3.  Edit your apache2 config in /etc/apache2/apache2.conf and add this to the end:
    # tomcat connector stuff:
    JkWorkersFile /etc/apache2/workers.properties
    # Where to put jk shared memory
    JkShmFile     /var/log/apache2/mod_jk.shm
    # Where to put jk logs
    JkLogFile     /var/log/apache2/mod_jk.log
    # Set the jk log level [debug/error/info]
    JkLogLevel    info
    # Select the timestamp log format
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] 
    
  4. In your /etc/apache2/sites-enabled/ dir find the vhost you want to use tomcat and edit it, at the end of the vhost declaration put:
    #Everything under root goes to tomcat
    JkMount  /* worker1
    #html files should be served by apache2
    JkUnMount /*.html worker1
    
  5. Edit /etc/tomcat6/server.xml and make sure that the ajp connector is uncommented:
    <connector port="8009" protocol="AJP/1.3" redirectport="8443" />
    
  6. restart tomcat: /etc/init.d/tomcat6 restart
  7. restart apache: /etc/init.d/apache2 restart
  8. navigate to yourdomain.com/ and you should be all tomcatted

Wednesday, January 26, 2011

How to make your bed for engineers

Being an engineer I am always looking for more efficient ways to do all manner of household tasks.
Making the bed has never been my strong point. My wife makes the bed faster than me, and it always looks better. I have long given up trying to figure out her technique, she probably just has many more years of experience than me. She is a principal bed maker while i am just a bed maker intern.

Yesterday a friend of mine shared a bed making technique with me that has improved the tidiness and speed of bed making. I would like to share this technique with you!

The secret to making large beds efficiently is to make the bed while you are in it! Being in the middle of the bed gives you maximum reach to both sides, where you can straighten out the covers. You no longer need to make several passes from one side of the bed to the other. After you get the bed looking nice, carefully roll out. Chances are that you will need just one more pass to fix the wrinkles in the covers. 



Saturday, December 04, 2010

posting with syntaxhighlighter

I always forget the markup for posting syntax highlighted code. here it is:

<script class="brush: java" type="syntaxhighlighter">

thank me later

Tuesday, March 16, 2010

jQuery: Detect Windows XP Theme


I started building a jquery dropdown widget, and i ran into a problem where i was not styling the dropdown arrow correctly because i developed the widget in the classic theme, but the xp theme styled widgets differently.

Clearly i needed to be able to assign different classes to my widget depending on the user's theme if i wanted the widget to look natural to that user.

The problem is that the user's theme setting is not something available through a javascript api call, nor is it in the user agent string. I used a javascript technique called feature detection to get this information. Here is how it works:

We create a button element and assign it background color called 'ButtonFace'. This is a special system color that is different depending on what theme you use. Next we need to get the computed color of the button element, and remove the element from the DOM. The idea is that a user in XP theme will have a slightly different colored button than a user in the classic theme.

I used some jQuery to make my life easier, but it would be easy to port this code to any other framework, or do it in plain javascript.


The code above plugin will create a new jquery.support object $.support.windowsTheme which contains a name member that will be set to 'xp' if user is using the XP windows theme, 'classic' if they are using the classic theme, and undefined if they are using a custom theme, or another operating system.

To see this code in action look at this demo page

For further updates to this code please visit the jquery plugin page

Caveats:
  • This code does not check if the user is actually using windows, you would need to add that check yourself.
  • I have tested this code in firefox 3.6 and IE6,7 on windows XP. This code should really be also tested on Vista and Windows 7.
See also:
It has come to my attention that someone has already done this before

Friday, August 28, 2009

Javascript: insert table rows / table body

If you write lots of javascript, there will someday come a time when you need to do one of the following DOM manipulations:
  • Insert a new table body, discarding the old one
  • Insert some number of table rows after some given row
This can be easily accomplished using a javascript library like jQuery, but there is a major downside to this: very bad performance! (for big chunks of html)

The reason for this is that a library like jQuery must preform all sorts of checks for all sorts of corner cases, such as checking if the html you are inserting contains script blocks and running them if your browser does not support script eval (IE6 - 7). It must clean each string before turning it into DOM objects.

For this reason, it is better to use plain javascript to write all the table manipulation functions, and make assumptions about the html you will be inserting (so you dont have to clean it). Here is a collection of such functions that i have used in production code. They are on average 2 or 3 times faster than using jQuery for the same insert.

Inserting table rows after a row

Example Usage:

You can insert 1 or more rows this way.

Replacing a table body with a new table body
Note: html does not include the actual tbody tag, just the rows and columns.



Another function that I often use is a helper function for replacing the table body to be used with jQuery's ajax functions:


Caveats of using these functions:
  • On browsers that dont support scriptEval, scripts that are inserted into the table body will not be executed upon insertion. You will need to deal with them yourself. I usually include them in a hidden input with a predefined ID, and then eval the value of that input after insertion.
  • Leading and trailing whitespace counts as a DOM node in IE, and will have weird effects if the html used in these methods contains it. If you are in charge of creating the html you insert, make sure it doesnt have unneeded whitespace between and around nodes.
  • I dont know if liveQuery plugin will know about these dom modifications.

Friday, April 03, 2009

Trim a string in perl

problem:
I am a perl noob and i need trim a string.

solution:
1) if i can modify the actual string:
sub trim {$_[0] =~s/^\s+|\s+$//g}

2)if i don't want it to modify:
sub trim {my $s = $_[0]; $s=~s/^\s+|\s+$//g;return $s;}

Wednesday, March 18, 2009

Multidimensional hashes in javascript

When i first started out with javascript i had no idea how to make a multidimensional array or a hash. Then, for a while i did not know how to use it properly after creating it. Now that i _think_ i know what i am doing i want share this very useful knowledge with the world.

First things first. A hash is the corner stone of all javascript objects and it is easy to create:

Each of the above is a perfectly valid way to create and access a hash. Quotes around the name allows you to use reserved words as the name, such as "function".

One thing to note here, is that the name must be either a string or a number, if its a number, you are better off using an array (if all your other keys are numbers too). This means that the name cannot be an object.

Now lets create a hash with more than one dimension. The function below is used to both init and add to a 3D hash.

We make sure not to access any elements that are undefined / not an object.

Now all we have left is a function to get values from our hash. This is not as straight forward as it may seem
The first statement is wrong, because if any of the 3 values passed in does not exist in the hash, you will try to access a property of undefined, causing an error. The second statement makes sure that each dimension exists before trying to access it, and finally returns either the value you want, or undefined.

Thats it!

Monday, February 23, 2009

Functional Testing with Tellurium

I have recently discovered a new toolset for functional testing. Its called Tellurium and it runs on top of Selenium. Tellurium brings some cutting edge technologies to the table:
  • It's built fully in Groovy and takes advantage of DSLs, closures and other syntax sugar, while letting you code in java if you prefer.
  • It supports JUnit and TestNG unit testing frameworks
  • Instead of relying on xpath to describe the UI, it uses a UI mapping DSL
  • Comes with a firefox plugin for generating UI modules called TrUMP
Even though Tellurium is a fairly young project, it has very good support on the project forums. I have requested a few bug fixes and patches, and had the fixes committed within days. Anyway, the biggest advantage in using Tellurium vs Selenium is the UI mapping. Let's look at what a simple Selenium script would look like versus that same script written for Tellurium.

In this simple script we will click around the Tellurium project page. This is the Selenium version:



And now, the same test in Tellurium. Tellurium uses UI modules to describe the UI in a class that is separate from the test. Here is the UI module DSL:



And this is the Tellurium TestNG test case that uses the above module:




As you can see, the code looks very different. You will notice that the Selenium test is much shorter, but does not re-use UI elements like the Tellurium test does. Tellurium's UI mapping maps UI elements to very short element ids, which support hierarchical nesting, which can greatly simplify large tests.
Another big and obvious advantage to UI mapping is that if your underlying html changes, you would only need to modify the UI mapping DSL, and not need to worry about the actual tests. This is where Tellurium really shines for me.
Modifing the UI maps is really simple using the TrUMP Firefox plugin. TrUMP lets you point and click on the page, and builds out the nested UI module DSL for you.

What you will need in order to use Tellurium for your project
Useful resources for working with Tellurium and TrUMP firefox plugin
The best place to go to find support is the tellurium user group. Questions are answered very fast, and it is the main reason I was able to integrate Tellurium into my functional testing strategy at work.

Also, the Tellurium team has created two videos to help you get started with the project:
Tellurium Demo Video Part I: Tellurium project and TrUMP IDE
Tellurium Demo Video Part II: Create Tellurium Test cases

Friday, December 12, 2008

jQuery: Conflict free events

Problem:
I had to validate an input, both onBlur and when the user pressed enter. If there was an error, in both cases, an alert would pop up. The problem was that when the user clicked 'ok' on the alert, the field would lose focus and cause the blur event to fire, and thus cause another alert to pop up. I only wanted one alert per error, so i made this little plugin to solve this problem.

Solution:
To use this plugin, you can pass in a hash with 2 keys: {blur: function(){}, enter: function(){}}, or a single function that you want run both onBlur and onEnter.



Example usage:

Saturday, November 15, 2008

How to make a pie crust from scratch


Making a pie crust is harder than programming, i have decided. It is one of the hardest things a man can undertake. Anyway, i am a pie crust making beginner. I just made a great pie crust and i need to remember how to do it.

You will need:
a food processor with a metal blade
1 1/4 cups white flour
2 teaspoons sugar
1/8 teaspoon salt
1 stick (1/2 cup) of cold butter, diced
1 egg, beaten to a pulp
1 table spoon of water

First, the butter needs to be pretty cold, think, freezer cold. The colder, the better.

  • combine flour, surgar and salt in the food processor and turn it on, let it do its thing while you do the next step
  • dice your stick of butter, and put it in the food processor, leave it on until the mixture looks like corn meal, make sure that all butter gets mixed in well.
  • beat the egg and add that into the food processor. at this point the dough should start forming into a ball.
  • add 1 table spoon of water and let it get mixed in.
  • remove the dough from the food processor, and form it into a ball. refrigerate the ball for about 15 mins
  • get some clear plastic wrap, lay it on your counter. put the ball on the wrap and start forming into a circle, about 1/8 inch thick. (it has to be bigger then your pie pan by about 1 inch)
  • after you make the circle, cover it with some wrap, and put it on a pizza pan, and freeze it for 5 mins
  • take dough out of the freezer, it should be hard, and easily broken. DO NOT BREAK IT. put it on top of your pie pan and remove the plastic wrap from under it. let it rest there for 3-4 minutes to warm up
  • when dough warms up it will be more pliable. form the dough into the pie pan. if it breaks, uses peices to patch up cracks. you might have extra dough on the sides. use food scissors to cut off excess. you can make something cool with the extra on top of the pie.
  • freeze the dough in the pie pan for 5 minutes, covered.
  • put a piece of foil in the pie plate, and form it around the dough, add pie weights (i dont have these, so i use a pan top)
  • bake at 400 deg for 20 minutes
  • remove foil and bake at 350 deg for 5 minutes or until crust is golden.

Wednesday, November 12, 2008

How to debug UI display issues

i spent far too much time tracking down stupid UI issues in IE. This is an attempt to write down some techniques that yielded results.

  • If the issue is on a screen with a javascript UI widget:
    • Does the issue occur oh pages without the widget? no:
    • Does returning early from the widget rendering function fix it? yes:
    • Move the return statement down the render function, until the UI breaks.

Tuesday, October 14, 2008

Use Selenium RC in Firefox 3

How to use Selenium RC in Firefox 3

For a while now i have been told that selenium RC does not work in firefox 3. This is a huge bummer for anyone who wants to run functional tests and has to downgrade to firefox 2.

Edit 1/12/2009:
A new version (1.0 beta 2) of selenium RC was released today that fixes this issue once and for all!
get it here. You shouldnt need to do this hack now, unless you want to use the old non-beta version. If you do need to use the old version of selenium, here is how to get it to run firefox 3:

Edit 2/8/2009:
Today i was told that there is a firefox extension that allows you to turn of extension version checking. Also, you can do it by editing about:config.

Today i found a workaround to this problem, which works, and is really simple.

Here is what you need to do:
1. Open selenium-server.jar using winrar
2. locate 2 dirs: customProfileDirCUSTFFCHROME and customProfileDirCUSTFF
3. recursively explore each of those dirs, and when you find a file called install.rdf drag it to some temp location, and edit the following line:
<em:maxVersion>2.0.0.*</em:maxVersion>
change it to:
<em:maxVersion>4.0.0.*</em:maxVersion>
4. drag the install.rdf back into the archive and overwrite the old one.
5. do this for all the install.rdf files in those 2 dirs.


you can now use "*firefox" in selenium RC. "*chrome" was giving me problems though, complaining about a malformed rdf file, i might have made a mistake in one of them. Either way, i use *firefox, so it does not matter to me.

if you want, you can get my modified selenium jar here - modified to run in firefox3.
the version of selenium that this jar contains are:
selenium.core.version=0.8.3
selenium.rc.version=0.9.2
selenium.rc.revision=2006
selenium.core.revision=1879

also, there is a 1.0-beta1 compatible modified jar here

Edit 12/10/2008:
Some people have been asking for help with selenium errors in the comments. I cannot help you with those, they are probably specific to what you are doing. If you have question about selenium RC errors, you should try asking them on the Selenium RC User Forums.

Thursday, January 24, 2008

Java: reading / writing international characters

It is actually very easy to support international characters in java. The only trick is knowing the charset that the text is encoded in. Most of the time is a safe bet to try UTF-8 if you dont know. If you use Apache's HTTPClient it has methods to get the charset of the stream you are reading from.


Friday, December 14, 2007

Unix: find largest files / folder to free up space

Unix boxes like to fill up with log files, and when they get really full, stuff starts to break. These commands will tell you where the big log files sit so you can delete them or gzip them.

first, in root run
df -k
/dev/md/dsk/d3 4129290 4020939 67059 99% /var

then go to /var and find what it taking up all the space:
du -sk * | sort -m | head
2855134 mail
..
193102 tmp

so mail is taking up space, now lets look for files bigger then 100MB:
find . -size +100000000c -ls
22615 483984 -rw-r--r-- 1 root other 495339455 Dec 14 15:09 ./mail/access_log
113593 209784 -rw-r--r-- 1 root other 214696853 Dec 14 15:08 ./mail/servletd.log
208492 354768 -rw-rw-rw- 1 admin mail 363091751 Dec 14 14:29 ./mail/admin

now you can see what is steal your megabytes.

Friday, November 16, 2007

parse xml with JDOM and Xpath

every now and again its good to have your config in an xml file, but i am usually to lazy to remember to how to do it properly and just use java.util.Properties. Well no more of that!

This is a short self contained code sample on how to parse some xml using jdom and jaxen

you will need:
jdom lib
jaxen lib (which may be old now)