In the community commons you have the Java action GetApplicationUrl. We use a contains action to check the string for localhost, test, accp etc. to do all kinds of automatic settings. This way you never ever have to think about it again.
The easiest way is to just to create a constant and set your environment there. Environment = D/T/A/P or something similar.
Then you can check the Environment constant in an exclusive split and handle your logic from there.
You can use DTAP Module in the Appstore for finding the current environment.Its for synchronizing data between environments but you can use for finding the current environment using creating the DTAP constant and set the environment.