Wednesday, December 11, 2013

How to edit the hosts file in Mac OS

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.

The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.

Editing the hosts file
Editing the hosts file in Mac OS, is a pretty easy task, especially if you are familiar with the terminal. Just follow these steps:
  1. Open the Terminal
  2. Open the hosts file by typing on the Terminal that you have just opened:
    $ sudo nano /private/etc/hosts
    

    Type your user password when prompted.
  3. Edit the hosts file:
    The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost). Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing! You can navigate the file using the arrow keys.
  4. Save the hosts file
    When done editing the hosts file, press control-o to save the file. Press enter on the filename prompt, and control-x to exit the editor.
  5. Flush the DNS cache (Optional)
    On Leopard you can issue a simple Terminal command to flush the DNS cache, and have your host file changes to take immediate effect:
    $ dscacheutil -flushcache
    
You can now test your new mapping on the browser!


Friday, November 1, 2013

How to merge (and not replace) folders when copying on the Mac?

Problem:
If I try to copy or move a folder to somewhere it already exists, it asks to replace it. That would result in deleting the target. Rather I want to merge :(


With Windows is really simple, however on Mac, well, it works a little bit different!

Rather than using the GUI I'd rather recommend to use the command line:

$ditto from_folder destination_folder

Infallible!!


This is why

ditto -- copy directory hierarchies, create and extract archives.

If the destination directory does not exist it will be created before the first source is copied. If the destination directory already exists then the source directories are merged with the previous contents of the destination.

Friday, October 18, 2013

How does Facebook set cross-domain cookies for iFrames on canvas pages?

I was browsing Facebook's documentation reading about canvas applications and I came across an example application: http://developers.facebook.com/docs/samples/canvas. As I read through their example, however, I got very confused about their use of cookies in the iframe application.

A little backstory...

I had already played around with using iframes for embeddable widgets (unrelated to Facebook) and I found out a few browsers (Chrome, Safari, etc.) have strict cookie policies and don't allow cross-domain cookies set in iframes (Firefox, on the other hand, allows iframes to set cross-domain cookies in iframes). For example, if foo.com has an iframe with src="http://bar.com/widget" the iframe widget will not be able to set any cookies for bar.com and therefore will have trouble persisting state within the iframe: bar.com will interpret every request (including ajax requests) from the widget as a fresh request without an established session. I struggled, and found a way around this by using JSONP and javascript to set cookies for foo.com instead...

... and so?

Well, I was looking at the example canvas iframe Facebook application and I noticed that their application (hosted on runwithfriends.appspot.com) is able to set a cookie, u, with the current user's id along with a few other parameters for the runwithfriends.appspot.com domain. It sends this cookie with every request... and it works in both Chrome and Firefox! WTF? How does Facebook get around the cross-domain cookie restrictions on Chrome?

(I already know the answer now, but I thought this might be helpful for anyone struggling to figure out the same thing)

So the iFrame isn't actually setting the u cookie for the runwithfriends.appspot.com domain. What Facebook does is it creates a form, and uses javascript to submit the form on page load. Since the form's target is the iframe, it doesn't reload the page... it just loads the iframe with the POST's response. Apparently even Chrome and other browsers with strict cookie policies set cookies for cross domain requests if they are POST requests...


As you can see the answer is simple, and could be implemented by just creating a handler (could be a Servlet or anything that can catch some parameterts and transform them into cookies), like this:

Thursday, August 8, 2013

Modify iframe contents with Javascript

Conventionally, you are not allowed to play around with the contents of an iframe of external url with javascript. But with the use of javascript injections, you can do it very easily.
For example, you want to modify the values of text fields in the web page open in an iframe, but the web page is from an external url. What you would have tried first would have been:

But this does not work when you open the web page in a browser. So what to do? Let’s explore the possibilities of doing this with JavaScript Injections.
We know that we have the full permissions to modify the url of an iframe. So what we do is,
And viola! The username is changed!

Applications:
Possible applications of such injections are:
  • Automatic form filling Modifying the contents of iframe with
  • Automatic login by using
  • Automatic posting/commenting on sites like facebook, wordpress blogs, etc.
  • Or you can go further and insert a full javascript file into the external web page with:


Examples: Anything else you can imagine of!


Thursday, July 11, 2013

Setting Environment Variables in OS X

Have you ever asked yourself What is the proper way to modify environment variables like PATH in OS X?
You could use:
/etc/paths
~/.profile
~/.tcshrc

OR you can try by playing around with
/etc/launchd.conf

Please note that environment.plist does not work for applications launched via Spotlight. This is documented by Steve Sexton here
  1. Open a terminal prompt
  2. Type sudo vi /etc/launchd.conf (note: this file might not yet exist)
  3. Put contents like the following into the file
    	#setenv JAVA_VERSION 1.6
    	setenv JAVA_HOME /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    
    	setenv ANT_HOME /Applications/java/tools/apache-ant-1.8.2
    	setenv ANT_OPTS -Xmx512M
    
    	setenv MAVEN_OPTS -Xmx1024M
    	setenv M2_HOME /Applications/java/tools/apache-maven-3.0.2
    
    	setenv ATG_HOME /Applications/java/tools/Dynamo5.6.1
    
    	setenv DYNAMO_HOME /Applications/java/tools/Dynamo5.6.1/home
    
    	setenv ORACLE_HOME /opt/app/oracle/product/817 
     
  4. Save your changes in VI and reboot your Mac.
  5. Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ and other GUI apps you launch via Spotlight.

Friday, June 14, 2013

How to speed up Eclipse on Mac OS X

Or Pimp my Eclipse


If you’ve been experiencing horrendous performance of Eclipse on OS X, or maybe you even did not know that Eclipse actually can run very fast, you’d be surprised to learn that apparently it is very easy to make Eclipse run 4-5 times faster on OS X with proper settings to your eclipse.ini file.

Working with Eclipse on OS X felt horrendously slow! I had already noticed this nuance in the past and I thought that the Eclipse is just not meant to fly on Mac's (oh well…), especially when there are so many different distributions for OS X, it signals that the project is in a shifting phase basically (it is shifting from Carbon to Cocoa) and that you should expect all kinds of problems.

I bet many people not having experience with Eclipse on other platforms would think this is normal. It’s Java, so it is supposed to be slow and they just go and hate it and sing how X-Code is great. The thing is, they probably never experienced the full potential of Eclipse – a pity. Hopefully this post can change your mind and help you experience the wonderful world of Eclipse (ok, I am being a little dramatic here). Ok, let’s dive into that.

Since I am pretty heavy user of Eclipse the slowness of Eclipse quickly grew on me and I started looking for ways to try and do something about it.

In the past while experiencing slowness with Eclipse on Windows, I played with the memory allocation within it to increase the memory cap and this usually helped a bit. Being desperate, I decided to try that and off I went to try that. But, that was not so easy. I wandered a bit around the eclipse folder looking for the location of the eclipse.ini but it was nowhere to be found. So I tried searching for it – nop, no luck there as well.

Apparently eclipse.ini is packaged inside the eclipse app so what you need to do is to get inside the eclipse.app package


OR you can use this command:
vi $ECLIPSE_HOME/Eclipse.app/Contents/MacOS/eclipse.ini

after that edit eclipse.ini which you will find in Contents->MacOS folder and increase the memory limits for the following directives (on my case I used these values):
--launcher.XXMaxPermSize
2048m
...
-vmargs
-Xdock:icon=../Resources/Eclipse.icns
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Dosgi.requiredJavaVersion=1.6
-Xms512m
-Xmx850m
-XX:PermSize=512m
-XX:MaxPermSize=1024m
While this is good, this is NOT what will actually make your Eclipse fly. The most important part here is the:
-Dosgi.requiredJavaVersion=1.5

now, you have to switch that to 1.6 and make it
-Dosgi.requiredJavaVersion=1.6


Now THAT will make your Eclipse really shine! Just make sure you actually HAVE the JDK 1.6 installed. You can validate this by running “java -version” in the terminal. If you got 10.6 – Snow Leopard though, you are most likely covered.

Also I still have two more tricks that might be useful:
-Xverify:none

Which will skip the class verification stage during class loading. Using -Xverify:none disables Java class verification, which can provide a 10-15% improvement in startup time. However corrupted or invalid class data is not detected when this option is specified. If corrupt class data is loaded, the Java Virtual Machine (JVM) might behave in an unexpected manner, or the JVM might fail.
-XX:+UseParallelGC

Which is known as The parallel scavenge collector. Which is just an improved version of the parallel copying collector (Enabled using -XX:+UseParNewGC), but the algorithm is tuned for gigabyte heaps (over 10GB) on multi-CPU machines.

If you already had that in the eclipse.ini file – well, unfortunately this already has been optimized for you and the only thing you can try from there that we know of is to play with the memmory limits.

Also, I've discovered other options, which I'm not 100% sure how they work together (because I haven't researched them too deep :P), however they might help you out:
-XX:CompileThreshold=5
-XX:MaxGCPauseMillis=10
-XX:MaxHeapFreeRatio=70
-XX:+UseFastAccessorMethods
-XX:+CMSIncrementalPacing
-XX:+UseG1GC
-XX:+UseFastAccessorMethods

Just don't mix them with -XX:+UseParallelGC

Thursday, May 9, 2013

Interesting Picture

This picture was taken during a Physics Conference during 1927 and shows a lot of great scientific minds such as Planck, Pauli, Bohr, Einstein, Curie, Dirac, Schroedinger and somebody else, I hope you can spot him :)

Saturday, April 13, 2013

Getting column info from Oracle table you don't own (without using describe)

How would one get columns information on table which he doesn't own, but has select granted? This is, without using DESCRIBE table_name

My answer is:

select owner, column_name from all_tab_columns where table_name = 'STUDENT';

Saturday, March 16, 2013

Creating a branch in Perforce with P4V

I recently wanted to create a private branch to experiment with some stuff in a Perforce repository. Coming from a SVN history, I had to figure out what 'branching' means in Perforce.

Perforce uses the word "integrate" for most branching activities. What's a little strange is that you 'integrate' to create the branch, then you 'integrate' to merge the branch later.

I'm still not completely sure what that means

First, create a new branch spec. Use File > New > Branch Mapping. (To edit it later, use View > Branch Mappings). In it, I created "CLOUD_CRAZE" as the name of my branch, then gave it a branch spec of
//depot/kaptest.com/jboss_apps/kaptest_3.1/DEV/... //depot/kaptest.com/jboss_apps/kaptest_3.1/branches/CLOUD_CRAZE/DEV/...

Next, you need to actually execute the branch creation, which is done with "Integrate..."

Choose the "CLOUD_CRAZE" entry in the Workspace that I currently have sync'd. Context Menu > Integrate ... "Use Branch Specification"

Select my branch mapping "CLOUD_CRAZE"

Use "Preview" to indicate that it seems to have the right mapping together

Use "Integrate" to actually start the branch/copy.

It churns for a while and appears to be creating my copy. NOTE: It appears that P4 does client-side copying and branching, so this takes a LOOOOONG time if you have big files.

When finished, I know have files in ~/MyCodeProjects/juan_rtcap/kaptest.com/jboss_apps/kaptest_3.1/branches/CLOUD_CRAZE/DEV

Finally, you must submit this into perforce in order to see it in your Depot and Voila!!!

You're all set!


Thursday, February 28, 2013

MSSQL Restore locked database

Below is a short snippet that will allow you to restore your database by putting it in single user mode.

After restoring (if without errors) it will be in multi user mode again. If it is not you can still run the
alter database DATABASE_NAME set multi_user
command to put it back in original mode.

USE master;
 
ALTER DATABASE [DATABASE_NAME] SET single_user WITH ROLLBACK immediate;
 
restore DATABASE [DATABASE_NAME] FROM disk = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\YOUR_AWESOME_BACKUP.bak';

Monday, February 4, 2013

New Baby

I know, this is not technical stuff, but I'm really happy and excited about it.



Soon (between this week and the next one) I will become a Dad!!!

Thursday, January 31, 2013

Getting INVALID_SESSION_ID using Salesforce's SOAP based API

For those of you consuming Salesforce SOAP API.

The more calls you make, the more errors you'll get (if you're using the same user to perform this calls)

This is a brief description of what happened to me:
I implemented a process to perform Realtime user provisioning by using Salesforce SOAP based API calls. In a few words this is my process:
  1. Login
  2. Generate User Account (Using Connection.upsert)
  3. Verify if User already exists (query User) to determine if next step will be Update or Insert
  4. Upsert (Insert or Update) User
  5. Upsert (Using Connection.upsert) other related objects (custom)
  6. Logout

This process gets invoked when performing SSO, therefore I get as many executions of this process as users try to access my portal.

There is no pattern on this issue.

This is the error I see in my PingFederate logs: [UnexpectedErrorFault [ApiFault exceptionCode='INVALID_SESSION_ID' exceptionMessage='Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key' ...

After login is been performed (could be before executing any step before Logout or during Logout) This error is generating incomplete user/accounts in Salesforce (Some objects are not created due to this error)


After a few calls and emails with salesforce Developer Support, I got a clear and simple answer:

"There is no implication by calling only login and no logout()"
"Client applications do not need to explicitly log out to end a session. Sessions expire automatically after a predetermined length of inactivity".

Then, instead of over-thingking this problem and design some complex solutions, just like: Implementing a connection pooling mechanism or making singleton my Connection object. The answer was easier than I thought. Don't call Logout and Login as many times as you need. You'll get the same Session_Id (your session will be refreshed on Salesforce and will remain active), letting Salesforce kill it when I'm not using it.

Cool!

Friday, January 4, 2013

How to recursively remove .DS_Store files on Mac OS X

The following command will remove all .DS_Store files in a directory where it is executed as well as that particular directory’s subdirectories:
find . -name '*.DS_Store' -type f -delete

Why would you want to remove .DS_Store files? The .DS_Store files store Finder-specific preferences concerning the display of each of your folders. Removing these files is an easy way to reset your display preferences. Also, if you’re moving files between your Mac and another operating system such as Windows or Linux these files can cause unexpected problems and should be removed prior to transfer.
This command works on Mac OS X (both 10.4 Tiger and 10.5 Leopard) and Unix-type operating systems such as Linux. What follows is a step-by-step guide on how to properly use this command on Mac OS X.

Directions on how to recursively remove .DS_Store files on Mac OS X
  1. Open up the Terminal application (/Applications/Utilities/Terminal.app).
  2. Find the directory/folder that you wish to recursively delete the .DS_Store files from. For this tutorial let’s pretend you want to delete all the .DS_Store files in your Pictures folder and every folder inside that folder. Type this command into Terminal (without pressing the return key):
    cd
  3. Then drag and drop your Pictures folder onto the Terminal window. The cd command should now look something like this–where ariadoss is your Mac’s “short name” (i.e. your username):
     cd /Users/jcruz/Pictures
  4. Press the return/enter key on your keyboard.
  5. Type the following command into Terminal and press return to execute it:
    find . -name '*.DS_Store' -type f -delete

Congratulations the .DS_Store files have been deleted! Please note that this command will just delete existing .DS_Store files; your Mac will continue to create them

If you wish to disable the creation of .DS_Store files on external volumes type the following command into Terminal and hit enter (you may need to logout or restart your computer after executing this command):
defaults write com.apple.desktopservices DSDontWriteNetworkStores true