Tell me please
How can i get active directory domain name from java
I tried this
System.out.println(System.getenv("USERDOMAIN"));
but I only get the name of the computer
======================
I did so
InetAddress inet = InetAddress.getLocalHost();
InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
usernameId.setText(System.getProperty("user.name"));
if (ips != null) {
for (int i = 0; i < ips.length; i++) {
String[] str = ips[i].toString().split("/");
if (!(str[1].startsWith("169") || str[1].contains(":")))
System.out.println("Computer name: " + str[0] + "\nIp address: " + str[1]);
computernameId.setText(str[0]);
And i get ip address and computername.domainname
Try using
System.out.println(System.getenv("USERDNSDOMAIN"));
If that does not work, you can (as James Tanner said) try parsing through your system variables to find the one you want:
Map<String, String> envMap = System.getenv();
Iterator iter = envMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> pair = (Map.Entry<String, String>)iter.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
From this article, try checking the DomainName environment variable.
Or, from this question, try the LOGONSERVERvariable.
If that doesn't work, I'd recommend taking a look at your environment variables directly (directions vary depending on which version of Windows you're running) to find the one that actually contains the information you're looking for, then use that one.
Related
I was surprised to find that there doesn't seem to be a way to pull Beam configuration from Environment Variables (for the direct runner). In case it helps anyone, I've create the code snippet below as an answer - but I was wondering if there was a better (or more official) variant? This definitely feels hacky...
private static String[] AddArgsFromEnvironmentVariables(String[] args) {
ArrayList<String> argsWithEnvVariables = new ArrayList<String>(Arrays.asList(args));
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
if (envName.startsWith("BEAM_")) {
String argName = envName.substring(5);
argsWithEnvVariables.add(0, "--" + argName + "=" + env.get(envName));
}
}
return argsWithEnvVariables.toArray(args);
}
How to use:
PipelineOptionsFactory.fromArgs(AddArgsFromEnvironmentVariables(args))
I have a List<UserVO>
Each UserVO has a getCountry()
I want to group the List<UserVO> based on its getCountry()
I can do it via streams but I have to do it in Java6
This is in Java8. I want this in Java6
Map<String, List<UserVO>> studentsByCountry
= resultList.stream().collect(Collectors.groupingBy(UserVO::getCountry));
for (Map.Entry<String, List<UserVO>> entry: studentsByCountry.entrySet())
System.out.println("Student with country = " + entry.getKey() + " value are " + entry.getValue());
I want output like a Map<String, List<UserVO>>:
CountryA - UserA, UserB, UserC
CountryB - UserM, User
CountryC - UserX, UserY
Edit: Can I further reschuffle this Map so that I display according to the displayOrder of the countries. Display order is countryC=1, countryB=2 & countryA=3
For example I want to display
CountryC - UserX, UserY
CountryB - UserM, User
CountryA - UserA, UserB, UserC
This is how you do it with plain Java. Please note that Java 6 doesn't support the diamond operator so you have use <String, List<UserVO>> explicitly all the time.
Map<String, List<UserVO>> studentsByCountry = new HashMap<String, List<UserVO>>();
for (UserVO student: resultList) {
String country = student.getCountry();
List<UserVO> studentsOfCountry = studentsByCountry.get(country);
if (studentsOfCountry == null) {
studentsOfCountry = new ArrayList<UserVO>();
studentsByCountry.put(country, studentsOfCountry);
}
studentsOfCountry.add(student);
}
It's shorter with streams, right? So try to upgrade to Java 8!
To have a specific order based on the reversed alphabetical String, as mentioned in the comments, you can replace the first line with the following:
Map<String,List<UserVO>> studentsByCountry = new TreeMap<String,List<UserVO>>(Collections.reverseOrder());
I am trying to query using the google public dns server (8.8.8.8) to get the IP address of some known URL. However, it seems like I am not able to get that using the following code? I am using the dnsjava java library. This is my current code
The results
Lookup lookup = new Lookup("stackoverflow.com", Type.NS);
SimpleResolver resolver=new SimpleResolver("8.8.8.8");
lookup.setDefaultResolver(resolver);
lookup.setResolver(resolver);
Record [] records = lookup.run();
for (int i = 0; i < records.length; i++) {
Record r = (Record ) records[i];
System.out.println(r.getName()+","+r.getAdditionalName());
}
}
catch ( Exception ex) {
ex.printStackTrace();
logger.error(ex.getMessage(),ex);
}
Results:
stackoverflow.com.,ns-1033.awsdns-01.org.
stackoverflow.com.,ns-cloud-e1.googledomains.com.
stackoverflow.com.,ns-cloud-e2.googledomains.com.
stackoverflow.com.,ns-358.awsdns-44.com.
You don’t need a DNS library just to look up an IP address. You can simply use JNDI:
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.dns.DnsContextFactory");
env.setProperty(Context.PROVIDER_URL, "dns://8.8.8.8");
DirContext context = new InitialDirContext(env);
Attributes list = context.getAttributes("stackoverflow.com",
new String[] { "A" });
NamingEnumeration<? extends Attribute> records = list.getAll();
while (records.hasMore()) {
Attribute record = records.next();
String name = record.get().toString();
System.out.println(name);
}
If you insist on using the dnsjava library, you need to use Type.A (as your code was originally doing, before your edit).
Looking at the documentation for the Record class, notice the long list under Direct Known Subclasses. You need to cast each Record to the appropriate subclass, which in this case is ARecord.
Once you’ve done that cast, you have an additional method available, getAddress:
for (int i = 0; i < records.length; i++) {
ARecord r = (ARecord) records[i];
System.out.println(r.getName() + "," + r.getAdditionalName()
+ " => " + r.getAddress());
}
I have List<HashMap> of mail addressess, along with users ids (each entry in list looks like: id: 123, mail: "john#doe.com").
I want to make a HashMap in which every key is a domain name of e-mail address, and value is list of e-mails from that domain:
"foo.com":
[1]
id: 123,
mail: a#foo.com
[2]
id: 345
mail: b#foo.com
"bar.com":
[1]
id: 787,
mail: a#bar.com
[2]
id: 456
mail: b#bar.com
To achieve that, I do what's below. Problem is, when I try to add new list entry to list existing on domain entry, java adds new record to sortedAddresses instead of using present one. My prediction is containsKey() method always returns false.
HashMap<String, List> sortedAddresses = new HashMap<String, List>();
for(HashMap<String, String> r : this.lightUsersList){
String mail = r.get("email");
Integer uid = Integer.parseInt(r.get("id"));
try{
String[] mailSplit = mail.split("#");
String domain = mailSplit[1];
//if domain key doesn't exist, add it to hashmap
if(!sortedAddresses.containsKey(domain)){
List<HashMap> domainAddr = new ArrayList<HashMap>();
sortedAddresses.put(domain, domainAddr);
}
List<HashMap> domainAddr = sortedAddresses.get(domain);
sortedAddresses.remove(domain);
domainAddr.add(r);
sortedAddresses.put(domain, domainAddr);
}
catch(Exception e){
//to be implemented
System.out.println("Nie udalo sie dodac adresu " + mail + " do tablicy domenowej (" + e.getMessage() + ")");
}
//displaying hashmap summary (source from another SO thread)
Iterator it = sortedAddresses.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + sortedAddresses.get(pairs.getKey()).size());
it.remove(); // avoids a ConcurrentModificationException
}
}
Output I get:
foo = 1
bar = 1
foo = 1
bar = 1
Should be:
foo = 2
bar = 2
Okay, it seems that I see where I made a mistake. Of course Iterator part should be after for loop. My bad. Hate mondays.
I have a blackberry Application. It is downloaded from a web page which provides dynamic JAD file content. The JSP prints those :
out.println("Appid: " + appid);
out.println("Ip: " + user.getIp());
out.println("Servicename: " + service);
out.println("MIDlet-Version: 1.0.0");
out.println("MIDlet-Jar-URL: MyApp.jar");
out.println("MIDlet-Jar-Size: 91633");
out.println("MicroEdition-Profile: MIDP-2.0");
(and other attributes goes on like that..)
I need to get my custom attributes like "Appid" but it sometimes gets null values. User can download and run the app, but some of them cannot get my custom attributes. I dont know it is about the phone model or the current state of OS, but according to my logs, this problem appears mostly on those devices :
9800 with OS 6.0.0.546
9300 with OS 6.0.0.570
9300 with OS 6.0.0.668
9320 with OS 7.1.0.398
My code to get attributes :
CodeModuleGroup cmg = null;
CodeModuleGroup[] allGroups = CodeModuleGroupManager.loadAll();
String moduleName = ApplicationDescriptor
.currentApplicationDescriptor().getModuleName();
for (int i = 0; i < allGroups.length; i++) {
if (allGroups[i].containsModule(moduleName)) {
cmg = allGroups[i];
break;
}
}
if (cmg != null) {
AppData.firstPageURL = cmg.getProperty("Firstpage");
AppData.appId = cmg.getProperty("Appid");
AppData.firstIp = cmg.getProperty("Ip");
AppData.firstSubServiceName = cmg.getProperty("Servicename");
for (Enumeration e = cmg.getPropertyNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
String value = cmg.getProperty(name);
AppData.errorStep += "-" + name + ":" + value + "-";
}
}
By the way, I determined that the code in the for loop above never runs in these cases.
Any idea ?
Sometimes, ApplicationDescriptor.currentApplicationDescriptor().getModuleName() gives the name of the sibling cod file instead of the main cod file. So, if your module name is MyApp, the function may return MyApp-1.
To solve this, you have to strip out the number after the hyphen.
String moduleName = ApplicationDescriptor.currentApplicationDescriptor()
.getModuleName();
if(moduleName.indexOf('-') > 0) {
moduleName = moduleName.substring(0, moduleName.indexOf('-');
}