2

Considering a scenario where there are three applications. A, B and C

A gets loaded using Class.LoadFrom by B. A can also get loaded from C A can also start as a standalone app

The question is, can A find who loaded A or did it start all by itself.

I tried to find the Process name, but that didnt help. Tried to find using Class Loader if it could tell me. Didnt help either. Tried using StackTraces which i want to avoid, but its not at all a good practice. Setting up properties file is something i want to avoid since it requires some manual effort.

in .Net/C# it's very easy :(

Edit:

Take a look at this, is this possible in Java ?

System.Reflection.Assembly.GetEntryAssembly

f_puras
  • 2,521
  • 4
  • 33
  • 38
Basit Anwer
  • 6,742
  • 7
  • 45
  • 88

2 Answers2

2

You can use Thread name to identify which class initiate the process, here is an example. A ticketing system it has own access.

public class TicketingSystem {

    public TicketingSystem() {
        System.out.println("Accessing by : " + Thread.currentThread().getName());
    }
    public void bookATicket() {
        // some process
    }
    public static void main(String[] args) {
        Thread.currentThread().setName("T.System");
        TicketingSystem ticketingSystem = new TicketingSystem();
    }
}

An agent accessing TicketingSystem

   public class TicketAgent {
      public static void main(String[] args) {
          Thread.currentThread().setName("Agent");
          TicketingSystem ts = new TicketingSystem();
          ts.bookATicket();
      }
   }

A customer accessing TicketingSystem

 public class Customer {
        public static void main(String[] args) {
            Thread.currentThread().setName("Customer");
            TicketingSystem ts = new  TicketingSystem();
            ts.bookATicket();
        }
}

Hope this answer will help you.

vels4j
  • 11,208
  • 5
  • 38
  • 63
2

Thread.getAllStackTraces() may work. - Or walk up the ThreadGroup hierarchy to the top and get all threads from the root ThreadGroup (getParent() == null).

Then examining the stack traces for the main() method and figuring out its package/class name may help you.

Detecting the 'stand-alone' mode is easy: When your main() method is run before your other code you're stand-alone.

public class MyMainClass {

  private static boolean standalone = false;

  public static boolean isStandalone() {
    return standalone;
  }

  public static void main(String[] args) {
    standalone = true;

    // Run as usual...

  }

}

Then any of your code can call MyMainClass.isStandalone() to figure out if it's running on its own or not.

Determining which application is running your code, when it's not in stand-alone mode, is somewhat harder and probably cannot be done without stack traces, which is not always fully reliable but rather some kind of heuristics.

If you know which classes are present in application B but not in C and vice versa, you can also try to locate one of them via Class.forName(); if that call fails with a ClassNotFoundException you know the class in question is not available in the current runtime environment, and may be able to deduce which application is running.

It would certainly be a better idea to define some kind of application-global property for each application which can then be evaluated by your code.

JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • Guess this is a problem in Java then :( – Basit Anwer Dec 23 '12 at 19:51
  • I'm wondering what your use case is. Can you give some details about it? – JimmyB Dec 23 '12 at 23:32
  • Well, a friend of mine in QA was developing an application that is self runnable by itself and also contains implemented interfaces used to be loaded by other applications being tested. My point to him was to keep them separated but he insisted on keeping it this way. Also since many QA engg will be using this. He was also trying to avoid any manual input to any property file. Hope you get my point :) – Basit Anwer Dec 24 '12 at 06:14
  • 1
    I'm not totally clear on how the alternate interfaces are used, but it sounds like there are different entry points for each way it is loaded. If that is the case, can't you simply add a static class variable based on an enum and set it depending on the entry point? – ajh158 Dec 28 '12 at 16:29