Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements. See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership. The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.marmotta.splash.systray;
 
 import static org.apache.marmotta.splash.common.MarmottaStartupHelper.getServerName;
 import static org.apache.marmotta.splash.common.MarmottaStartupHelper.getServerPort;
 
 import java.awt.Image;
 import java.net.URI;
 import java.util.Map;
 import java.util.Set;
 
 
Add file description here!

Author: Sebastian Schaffert

 
 public class SystrayListener implements LifecycleListener {
 
     protected static Log log = LogFactory.getLog(SystrayListener.class);
 
     private static Set<MarmottaContext>       contexts = new HashSet<MarmottaContext>();
 
     private TrayIcon                         icon;
 
     private Map<StringString>              demoLinks;
 
     private Map<StringString>              adminLinks;
 
     public static void addServletContext(MarmottaContext context) {
         .add(context);
     }
 
     public SystrayListener() {
         super();
     }

    
Register the systray menu when the application server has successfully started.

Parameters:
event LifecycleEvent that has occurred
 
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
         if(event.getType().equals(.) && SystemTray.isSupported()) {
             initContextLinks();
             initSysTray();
         }
     }

    
Check all servlet contexts for the presence of the systray.admin and systray.demo attributes and add the link definitions to the menu link maps
 
     private void initContextLinks() {
          = new HashMap<StringString>();
          = new HashMap<StringString>();
 
         for(MarmottaContext ctx : ) {
             if(ctx.getServletContext() != null) {
                Object ctxAdminAttr = ctx.getServletContext().getAttribute("systray.admin");
                if(ctxAdminAttr != null && ctxAdminAttr instanceof Map) {
                    @SuppressWarnings("unchecked")
					Map<String,StringctxAdminLinks = (Map<String,String>) ctxAdminAttr;
                    .putAll(ctxAdminLinks);
                }
                Object ctxDemoAttr = ctx.getServletContext().getAttribute("systray.demo");
                if(ctxDemoAttr != null && ctxDemoAttr instanceof Map) {
                    @SuppressWarnings("unchecked")
					Map<String,StringctxDemoLinks = (Map<String,String>) ctxDemoAttr;
                    .putAll(ctxDemoLinks);
                }
            } else {
                .error("could not register systray links because servlet context is not yet initialised");
            }
        }
    }
    private void initSysTray() {
        if (SystemTray.isSupported()) {
            // allow proper shutdown
            System.setProperty("org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES","false");
            SystemTray tray = SystemTray.getSystemTray();
            // create menu
            PopupMenu popup = new PopupMenu();
            //MenuItem mainPage = createMenuItem("Start Page", "http://"+getServerName()+":"+getServerPort()+"/");
            //popup.add(mainPage);
            //popup.addSeparator();
            // launch browser action
            MenuItem admin = createMenuItem("Administration","http://"+getServerName()+":"+getServerPort()+"/");
            popup.add(admin);
            // admin links
            for(final Map.Entry<String,StringlinkEntry : .entrySet()) {
                MenuItem entry = createMenuItem(linkEntry.getKey(),linkEntry.getValue());
                popup.add(entry);
            }
            // shutdown action
            MenuItem shutdown = new MenuItem("Shutdown");
            try {
                Class.forName("org.apache.catalina.mbeans.MBeanUtils");
                ActionListener stopListener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        performServerShutdown();
                    }
                };
                shutdown.addActionListener(stopListener);
            } catch (ClassNotFoundException e) {
                shutdown.setEnabled(false);
            }
            popup.add(shutdown);
            
            if (!.isEmpty()) {
                popup.addSeparator();
            }
            for(final Map.Entry<String,StringlinkEntry : .entrySet()) {
                boolean containsEntry = false;
                for(int i = 0; i < popup.getItemCount(); i++) {
                    MenuItem item = popup.getItem(i);
                    if(item.getLabel().equals(linkEntry.getKey())) {
                        containsEntry = true;
                        break;
                    }
                }
                if(!containsEntry) {
                    MenuItem entry = createMenuItem(linkEntry.getKey(),linkEntry.getValue());
                    popup.add(entry);
                }
            }
            popup.addSeparator();
            MenuItem about = new MenuItem("About");
            about.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MessageDialog.show("Apache Marmotta",
                            "About Apache Marmotta \n",
                            "(c)2014 The Apache Software Foundation \n" +
                            "Visit http://marmotta.apache.org for further details");
                }
            });
            popup.add(about);
            MenuItem issues = createMenuItem("Issues Reports""https://issues.apache.org/jira/browse/MARMOTTA");
            popup.add(issues);
            MenuItem homepage = createMenuItem("Project Homepage""http://marmotta.apache.org");
            popup.add(homepage);
            // load icon image
            try {
                Image image = ImageIO.read(SystrayListener.class.getResource("systray.png"));
                 = new TrayIcon(image,"Apache Marmotta",popup);
                .setImageAutoSize(true);
                tray.add();
            } catch (IOException e) {
                .error("SYSTRAY: could not load the logo for system tray",e);
            } catch (AWTException e) {
                .error("SYSTRAY: tray icon could not be added");
            }
        }
    }
    private MenuItem createMenuItem(final String labelfinal String uriString) {
        MenuItem entry = new MenuItem(label);
        if (Desktop.isDesktopSupported()) {
            final Desktop desktop = Desktop.getDesktop();
            if(desktop.isSupported(..)) {
                ActionListener adminBrowserListener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            URI uri = new URI(uriString);
                            desktop.browse(uri);
                        } catch (IOException e1) {
                            .error("SYSTRAY: could not access system browser, access to "+label+" disabled");
                        } catch (URISyntaxException e1) {
                            .error("SYSTRAY: could not build URI to administration service, access to "+label+" disabled");
                        }
                    }
                };
                entry.addActionListener(adminBrowserListener);
            } else {
                entry.setEnabled(false);
            }
        } else {
            entry.setEnabled(false);
        }
        return entry;
    }
    private void performServerShutdown() {
        try{
            MBeanServer server = (MBeanServer)Class.forName("org.apache.catalina.mbeans.MBeanUtils").getMethod("createServer").invoke(null);
            ObjectName name;
            if(isTomcat6()) {
                // Tomcat 6.x
                name = new ObjectName("Catalina:type=Service,serviceName=Catalina");
                server.invoke(name"stop"new Object[0], new String[0]);
                .warn("shutting down Apache Tomcat server on user request");
            } else if(isTomcat7()) {
                // Tomcat 7.x
                name = new ObjectName("Catalina""type""Service");
                server.invoke(name"stop"new Object[0], new String[0]);
                .warn("shutting down Apache Tomcat server on user request");
            }
        } catch (Exception ex) {
            .error("shutting down other servers than Apache Tomcat is not supported",ex);
        }
        // ensure complete shutdown
        System.exit(0);
    }


    
Return true if Tomcat 6.x is detected; tests for presence of class org.apache.catalina.ServerFactory

Returns:
    private boolean isTomcat6() {
        try {
            Class.forName("org.apache.catalina.ServerFactory");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }


    
Return true if Tomcat 7.x is detected; tests for presence of class org.apache.catalina.CatalinaFactory

Returns:
    private boolean isTomcat7() {
        try {
            Class.forName("org.apache.catalina.CatalinaFactory");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
New to GrepCode? Check out our FAQ X