How to determine if account running java application is 'SYSTEM' - java

How can I check whether my Java application is running as "SYSTEM"/"Local System" (as seen on Windows Service list)?
I tried using this:
System.out.println("Running with user: " + System.getenv().get("USERDOMAIN") + "\\" + System.getenv().get("USERNAME"));
... but it seems to return DOMAIN\COMPUTERNAME according where the program is run. So it can be like DOMAIN1\COMPUTER1 and somewhere else it is FOO\SERVER451 and both still means "SYSTEM" account.
For background information, my Java application is wrapped to a Windows Service with 'Apache Commons Daemon Service Runner' and by default it will run as "Local System" (same way as in example image).
I really would want to simplify my code to print either SYSTEM or MYDOMAIN\JackTheUser depending on user type... Is there a way to do it with Java?
EDIT 20/12/02:
This is what I have done meanwhile the SO army working to find the correct answer:
Main:
String username = System.getenv().get("USERNAME");
String userdomain = System.getenv().get("USERDOMAIN");
String servername = getComputerName();
if (username.equalsIgnoreCase((servername + "$"))) {
System.out.println("Running with user: 'Local System'("
+ userdomain + "\\" + username + ")");
} else {
System.out.println("Running with user: '" + userdomain + "\\"
+ username + "'");
}
Methods:
private static String getComputerName() {
Map<String, String> env = System.getenv();
if (env.containsKey("COMPUTERNAME"))
return env.get("COMPUTERNAME");
else if (env.containsKey("HOSTNAME"))
return env.get("HOSTNAME");
else
return "Unknown Host name";
}
Prints:
Running with user: 'MYDOMAIN\jokkeri' or Running with user: 'Local System'(MYSERVER\SERVER_1$)
(not a perfect solution and I'm sure there are many occasions where it won't work but it's a starting point)
EDIT2 20/12/02:
Some good information about SYSTEM account was found from this thread from superuser: https://superuser.com/questions/265216/windows-account-ending-with

That’s the best I can come up so far
private static final String APP_NAME = "Some App";
private static final Configuration CONFIG = new Configuration() {
public #Override AppConfigurationEntry[] getAppConfigurationEntry(String name) {
return name.equals(APP_NAME)?
new AppConfigurationEntry[] { new AppConfigurationEntry(
"com.sun.security.auth.module.NTLoginModule",
LoginModuleControlFlag.REQUIRED, Collections.emptyMap())}:
null;
}
};
static final boolean DEBUG = true;
public static void main(String[] args) throws LoginException {
LoginContext lc = new LoginContext(APP_NAME, null, null, CONFIG);
lc.login();
final Subject subject=lc.getSubject();
boolean isSystem = false;
try {
for(Principal p: subject.getPrincipals()) {
if(DEBUG) System.out.println(p);
if(p.toString().equals("NTSidUserPrincipal: S-1-5-18")) {
isSystem = true;
if(DEBUG) System.out.println("\tit's SYSTEM");
}
}
}
finally { lc.logout(); }
}
As explained in this answer, SYSTEM is a set of permissions that can be attached to different accounts. The code iterates over all principals associated with the current account and tests for the well known SYSTEM.
But if you’re only interested in a printable user name, you may check for the NTUserPrincipal.
LoginContext lc = new LoginContext(APP_NAME, null, null, CONFIG);
lc.login();
final Subject subject=lc.getSubject();
try {
String name = System.getProperty("user.name"); // just a fall-back
for(Principal p: subject.getPrincipals()) {
if(p.toString().startsWith("NTUserPrincipal: ")) {
name = p.getName();
break;
}
}
System.out.println("Hello " + name);
}
finally { lc.logout(); }
If you can live with a direct dependency to the com.sun.security.auth package (or jdk.security.auth module in Java 9+), you can use the specific principal types directly
LoginContext lc = new LoginContext(APP_NAME, null, null, CONFIG);
lc.login();
final Subject subject=lc.getSubject();
try {
boolean system = false;
for(NTSidUserPrincipal p: subject.getPrincipals(NTSidUserPrincipal.class)) {
if(p.getName().equals("S-1-5-18")) {
system = true;
break;
}
}
Set<NTUserPrincipal> up = subject.getPrincipals(NTUserPrincipal.class);
String name = up.isEmpty()?
System.getProperty("user.name"): up.iterator().next().getName();
System.out.println("Hello " + name+(system? " *": ""));
}
finally { lc.logout(); }

Related

Can't configure node_auto_index with InProcessServer() SDN 4

I'm using the following java configuration for my Neo4j (using SDN 4.0.0.RELEASE) based application, with unit tests:
...
#Bean
public Neo4jServer neo4jServer() {
return new InProcessServer();
}
#Bean
public SessionFactory getSessionFactory() {
Neo4jRequest<String> neo4jRequest = new DefaultRequest(httpClient);
String json = "{" + "\"name\" : \"node_auto_index\", " + "\"config\" : {" + "\"type\" : \"fulltext\", "
+ "\"provider\" : \"lucene\"" + "}" + "}";
neo4jRequest.execute(neo4jServer().url() + "db/data/index/node/", json);
return new SessionFactory("org.myproject.domain");
}
...
I created on getSessionFactory() a full-text node_auto_index. I'm actually missing how to configure my current in-memory istance of Neo4j, because I need to set those properties:
node_auto_indexing=true
node_keys_indexable=title
I read on "Good Relationships: The Spring Data Neo4j Guide Book" that
InProcessServer is useful for test and development environments, but is not recommended for production use. This implementation will start a new instance of CommunityNeoServer running on an available local port and return the URL needed to connect to it.
Do I need to use CommunityNeoServer instead? Should I use it even if it's deprecated? In this case, how can I configure it for an in-memory database that will support node auto indexing?
If you want to supply additional configuration, you can provide your own implementation of Neo4jServer, like this:
public class AutoIndexTestServer implements Neo4jServer {
final String uri;
public AutoIndexTestServer() {
try {
ServerControls controls = TestServerBuilders.newInProcessBuilder()
.withConfig("dbms.security.auth_enabled", "false")
.withConfig("node_auto_indexing", "true")
.withConfig("node_keys_indexable", "title")
.newServer();
uri = controls.httpURI().toString();
}
catch (Exception e) {
throw new RuntimeException("Could not start inprocess server",e);
}
}
#Override
public String url() {
return uri;
}
#Override
public String username() {
return null;
}
#Override
public String password() {
return null;
}
}
and use it
#Bean
public Neo4jServer neo4jServer() {
return new AutoIndexTestServer();
}

Some informations of how handle the main() args in Java [duplicate]

This question already has answers here:
How to make IntelliJ prompt me for command line arguments
(3 answers)
Closed 8 years ago.
I have to develop a command line Java application in which the main() method accept 2 String parameters named respetivelly partitaIVA and nomePDF.
So, as starting point, I created this simple Main class:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
}
}
I think that I can perform this minimalistic application from the Windows console and that I can perform my application passion these parameters to it doing something like this in the Windows console (or in the Linux shell):
java Main 123456789 myDocument.pdf
and I think that I can retrieve it inside my application modifying the original code in this way:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
}
}
So now I have 2 doubts about this topic:
1) I know that I can perform this application specifying my 2 parameters using the Windows command line or the Linux shell but can I do the same thing into my IDE console? Specifically in the Run tab of IntelliJ?
2) Can I specify in some way that the parameters that the user can specify are only 2?
1) There is something called run/debug configuration https://www.jetbrains.com/idea/help/creating-and-editing-run-debug-configurations.html (here are also sone details about the specific options you have: https://www.jetbrains.com/idea/help/creating-and-editing-run-debug-configurations.html#d1628194e152)
2) No, you can only print an error and guide the user
You should invest the time in learning a modern CLI argument parser:
I prefer JewelCli
<dependency>
<groupId>com.lexicalscope.jewelcli</groupId>
<artifactId>jewelcli</artifactId>
<version>0.8.9</version>
</dependency>
Here is an example that can be used as a base class:
public class Main
{
private static final Logger LOG;
static
{
LOG = LoggerFactory.getLogger(Main.class);
}
private static Args init(#Nonnull final String[] args)
{
final Cli<Args> cli = CliFactory.createCli(Args.class);
try
{
return cli.parseArguments(args);
}
catch (final ArgumentValidationException e)
{
for (final ValidationFailure vf : e.getValidationFailures())
{
LOG.error(vf.getMessage());
}
LOG.info(cli.getHelpMessage());
System.exit(2); // Bash standard for arg parsing errors
return null; // This is to make the compiler happy!
}
}
private static List<String> parseKey(#Nonnull final String key)
{
return new ArrayList<String>(Arrays.asList(key.toLowerCase().split("\\.")));
}
#SuppressWarnings("unchecked")
private static Map<String, Object> addNode(#Nonnull Map<String, Object> node, #Nonnull final List<String> keys, #Nonnull final String value)
{
if (keys.isEmpty())
{
return node;
}
else if (keys.size() == 1)
{
node.put(keys.remove(0), value.trim());
return node;
}
else if (node.containsKey(keys.get(0)))
{
return addNode((Map<String, Object>) node.get(keys.remove(0)), keys, value);
}
else
{
final Map<String, Object> map = new HashMap<String, Object>();
node.put(keys.remove(0), map);
return addNode(map, keys, value);
}
}
public static void main(final String[] args)
{
try
{
final Args a = init(args);
final Properties p = new Properties();
p.load(new FileInputStream(a.getInputFile()));
final HashMap<String, Object> root = new HashMap<String, Object>();
for (final String key : p.stringPropertyNames())
{
addNode(root, parseKey(key), p.getProperty(key));
}
switch (a.getFormat().toLowerCase().charAt(0))
{
case 'j': LOG.info(mapToJson(root)); break;
case 'b' : LOG.info(Strings.bytesToHex(mapToCbor(root))); break;
case 'x' : LOG.error("XML not implemented at this time!"); break;
default : LOG.error("Invalid format {}", a.getFormat());
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
interface Args
{
#Option(shortName = "i", longName = "input", description = "Properties file to read from.")
File getInputFile();
#Option(shortName = "o", longName = "output", description = "JSON file to output to.")
File getOutputFile();
#Option(shortName = "f", longName = "format", description = "Format of output Json|Binary|Xml")
String getFormat();
#Option(helpRequest = true, description = "Display Help", shortName = "h")
boolean getHelp();
}
}
In Intellij (Linux) you do:
Press Alt + Shift + F10 (the run shortcut)
Press right key
Go down to Edit
Then press Tab to go to "Program arguments".
This is where you pass the arugments in IntelliJ. After that just hit run.

Java Properties Save Temporary

I have some issues using properties in java. I have this software that uses properties.
What i want to do is run some code right before the Java software open so i have the following
MessageBox.infoBox("Are you ready to be amazed?", "XXX");
String macAdress = MCSystem.getMacAd();
MessageBox.infoBox(macAdress, "This is your MacAddress");
String sAct = config.getProperty("user.activationkey");
MessageBox.infoBox("You have no Activation or invalid Key","first ACT");
if(sAct != null)
{
MessageBox.infoBox(sAct,"second ACT");
}
else
{
String retAct = MessageBox.messageDialog("Please Enter Your Activation Key : ");
MessageBox.infoBox(retAct, "third ACT");
config.setProperty("user.activationkey", retAct);
String testin = config.getProperty("user.activationkey");
MessageBox.infoBox(testin, "fourth ACT");
}
As far as this goes everything works fine, outputs are good and right after that the software opens fine.
Now i would like to display the "user.activationkey" inside the software.
I'm trying to do it like below but i see no result. It's empty.
ActivationKey.setText(config.getProperty("user.activationkey"));
Any help would be appreciated.
EDIT:
private void init(File configfile) {
this.configfile = configfile;
m_propsconfig = new Properties();
logger.log(Level.INFO, "Reading configuration file: {0}", configfile.getAbsolutePath());
}
...
public String getProperty(String sKey) {
return m_propsconfig.getProperty(sKey);
}
...
public void setProperty(String sKey, String sValue) {
if (sValue == null) {
m_propsconfig.remove(sKey);
} else {
m_propsconfig.setProperty(sKey, sValue);
}
}

Speeding up Java application

My application listens on some directory and it's sub directories. For listening on directory I use JNotify. When new file is created on directory application checks the files and processes it in some way. Below is code:
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
public class JNotifyDemo {
public void sample() throws Exception {
// path to watch
//String path = System.getProperty("user.home");
String path = "/folder";
System.out.println(path);
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED
| JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED
| JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
//check file whether it is xml or not
//validate xml
//do some internal processing of file
// and do other jobs like inserting into database
}
void print(String msg) {
System.err.println(msg);
}
}
public static void main(String[] args) throws Exception {
new JNotifyDemo().sample();
}
}
As you can see from code, application processes one file per time. Any advice speeding up this application like using threading or anything else?
I'd recomand you to use java.nio.file.Path which extends Watchable interface, it may be registered with a watch service so that it can be watched for changes and events.
This event-driven approach speed-up your application. Take a look of example.
Path and Watchable both included in java 7.
how much processing is actually going on? keep in mind that IO is very slow, and, unless your processing takes considerable amount of time, multithreaded solution will be bottlenecked on reading the file.
anyways, quick way to implement parallel processing is to start an ExecutorService and submit it a Runnable with file path as a parameter.

error exception access violation in JNotify

I am trying to implement JNotify. but I am getting a bit weird error messages when I compiled the program. I get the sample code from this site ttp://jnotify.sourceforge.net/sample.html
as an info, JNotify is used for directory monitoring and this is how my source code looks like.
this is the content of the class watching.java
import net.contentobjects.jnotify.JNotifyListener;
import net.contentobjects.jnotify.JNotify;
public class watching{
public void watching(String s) throws Exception {
// path to watch
String path = System.getProperty(s);
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
}
then this is the main class that named nowwatch.java
public class nowwatch
{
public static void main(String[] args) throws Exception
{
System.out.println("Hello World!");
watching hello = new watching();
hello.watching("C:/Users/Raden/Documents/Downloads");
}
}
but why did the error went like this? I had screenshot the error so that you can see it by clicking on this link
has any of you ever experience this type of error? any help would be appreciated though.
thanks
JNotify surely uses JNI to interface with the OS-dependent notification APIs. Looks like there's a bug in JNotify. Have you tried asking on the JNotify forum on SourceForge?
We had the same problems. Because we used JNA anyways, we just used the FileMonitor example from this framework. Works like a charm.
it ask for jNotify.dll file, make sure that you have placed that file to the window or in jre/bin or jdk/bin. and then try it will start working.

Categories

Resources