title: Best way to detect whether code is running in an application server java
body: <p>For a J2EE bean I am reusing code that was developed for a java swing application. <code>JOptionPane.showMessageDialog()</code> is unfortunately commonly used. Most occurences luckily in code sections that are not reused by the J2EE application, but in some cases lower levels of the code has instances of <code>JOptionPane.showMessageDialog()</code>. Obviously this it results in dialog boxes popping up on the server, which is what I want to avoid.</p>
  
  <p>As a first step I'd like to somehow assure that no dialog boxes will ever occur on the server.</p>
  
  <p>Someone suggested peeking in some event or paint queue (I do not recall which one):
  That would be:</p>
  
  <pre><code>// old code: JOptionPane.showMessageDialog(msg);
  if ( someEventQueue.size() == 0 ) // &lt;== consider this pseudo-code
    Log.log(msg); // I am running on a server. Tell the log.
  else
    JOptionPane.showMessageDialog(msg); // I have a user made of meat. Tell him!
  </code></pre>
  
  <p>I never <em>really</em> got that working. What would you do? </p>