Avatar of judsonV
judsonV
 asked on

Ignoring IP's in a java properties file

I have an application that reads incoming packets from specific IP address. Testing is changing and adding IP's to the boxes and they are providing me with a list of IP's they want me to code into the app to ignore.

The existing code look like this :
if (localIP != null && localIP != "142.139.24.87" && localIP != "142.139.24.12" && (localIP.equals(WebUtil.getDevIPAddress()) || localIP.equals(WebUtil.getUaIPAddress()) || localIP.equals(WebUtil.getProdIPAddress())))

As you can see I hard coded the first two IP's they provided to me to ignore. But now they are saying there could be up to 20 IP's which would make an huge if statement. I'm normally a 'net guy and this is java app so i'm a little out of my element.

I know I could make a properties file that could contain all the ip's (they are stored as strings currently thankfully enough) and just basically in that if statement tell the app to ignore everything in that properties file.  

I have created the properties file which looks like:
# Redundant IP's in the test/UA environment as of 2011-08-10
ClientChooser.IP=149.139.24.12  
PaymentHub.IP=149.139.24.87

Now in the code how do call that and tell the app to ignore every IP in that file?

Thanks
SoftwareJSP

Avatar of undefined
Last Comment
for_yan

8/22/2022 - Mon
for_yan


You can first read your properties from file and then do something like that:

Properties p = new Properties();
p.load(new FileInputStream("filename.properties"));
Set s = p.keySet();
boolean isLocalIP = false;
Iterator itr = s.interator();

while(itr.hasNext()){
String str = (String) itr.next();
String ipAdd = (String) p.getProperty(str);
if(localIP.equals(ipAdd)){
isLocalIP = true;
break;
}

}

Open in new window


and then in your code you just make one condition
  && !isLocalIP &&


Mind that you should never  compare Strings in Java in this way:
localIP != "142.139.24.12"

you need to use equals method

   !(localIP.equals("142.139.24.12"))

otherwise there may be really bad mistakes



cmalakar

>>Now in the code how do call that and tell the app to ignore every IP in that file?

You can just use "containsValue()" method of Hashtable, which is extended by java.util.Properties, to check if the ip is existing.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html#containsValue%28java.lang.Object%29

Properties p = new Properties();
p.load(new FileInputStream("filename.properties"));
if(p.containsValue()){
//ip is existing in the file, do something
}else{
//do something else.
}

Open in new window

judsonV

ASKER
for_yan.... So the example of a properties file that looks like below is a bad idea?:
# Redundant IP's in the test/UA environment as of 2011-08-10
ClientChooser.IP=149.139.24.12  
PaymentHub.IP=149.139.24.87

Should it look like this?->
# Redundant IP's in the test/UA environment as of 2011-08-10
#ClientChooser.IP
149.139.24.12
#PaymentHub.IP
149.139.24.87
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
judsonV

ASKER
As well it is saying "Set" cannot be resolved to a type
cmalakar

You don't need Set, that will make it complex.

Just do as I said.

And you properties file is more meaningful, it will help anyone in identifying which box ip is excluded.
neosdone

This should make it more clear

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class TestProperties {
      public static void main(String[] args) throws Exception, IOException {
            Properties p = new Properties();
            p.load(new FileInputStream(new File("ip.properties")));
            
            System.out.println(p.keySet().contains("190.1.1.1"));
      }
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
neosdone

Please note that I have just made what cmalakar@ said more clear!
for_yan

Your initial format of property file with equal sign is not a bad idea - this is how property file should look like with name of property before the equal sign and value after the equal sign - just as you originally suggested.

Set should work, but containsValuie()as suggested by cmalarkar is shorter.
So just after you load properties you can use your original condition but instead of naming all individual IP's you just have one clause :

...&& !p.containsValue() && ...

And it would replace all your If's
for_yan

Sorry I meant of course

... && !p.containsValue(localIP) && ...

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
for_yan

But the most important stuff of this whole thread is:
Never use == or != when comparing Strings in Java:
even if you have twenty correct IF's they will still work - there is no limit on those - but comparing strings in such way will sooner or later lead you to very bad mistakes.
judsonV

ASKER
I think I got it working with reading the properties file but ran into another issue. The IP variable set by calliung in external function like this: "String localIP = InetAddress.getLocalHost().getHostAddress();"
So its getting it's ip from an external function, I need to loop untill it gets one of the proper IP's. I've got some code but I still a little fuzzy around the placement of the logic... for example:

new Properties().load(ListenerUtil.class.getResourceAsStream("redundantIPs.properties"));
String localIP = InetAddress.getLocalHost().getHostAddress();
Properties p = new Properties();
p.load(new FileInputStream("redundantIPs.properties"));

if (localIP != null && !p.containsValue(localIP) &&(localIP.equals(WebUtil.getDevIPAddress()) || localIP.equals(WebUtil.getUaIPAddress()) || localIP.equals(WebUtil.getProdIPAddress()))) {
code to start listener etc.....

I want it to ignore the IP's in the properites file but not start the listners untill it gets one of the 3 ip's that are in the "if" statement. So I should probably do a loop before i hit, something along the lines of:
                        
InetAddress  in = InetAddress.getLocalHost();
InetAddress[] all = InetAddress.getAllByName(in.getHostName());
      for(int i =0; i<all.length; i++){


Does that makes sense?                        
            
for_yan

1) The very first statement in your code starting with "new Properties()..." does not make sense, as you don't have a variable defined for these priperties. Further down the code you read properties in a reasonable way if your file is in the default folder of your process
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
for_yan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question