ApplicationRegistry.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.ui;
022 
023 import java.util.HashMap;
024 import java.util.List;
025 import java.util.concurrent.CopyOnWriteArrayList;
026 
027 import org.eclipse.jface.resource.FontRegistry;
028 import org.eclipse.jface.resource.ImageRegistry;
029 import org.eclipse.swt.SWT;
030 import org.eclipse.swt.graphics.Font;
031 import org.eclipse.swt.graphics.FontData;
032 import org.eclipse.swt.graphics.Image;
033 import org.eclipse.ui.ISharedImages;
034 import org.eclipse.ui.PlatformUI;
035 
036 /**
037  * Main Application Registry, which contains shared resources and map to all connected servers.
038  @author Bhupendra Bhardwaj
039  */
040 public abstract class ApplicationRegistry
041 {
042     private static ImageRegistry imageRegistry = new ImageRegistry();
043     private static FontRegistry fontRegistry = new FontRegistry();
044     public static final boolean debug = Boolean.getBoolean("eclipse.consoleLog");
045     public static final long timeout = Long.parseLong(System.getProperty("timeout""5000"));
046     
047     static
048     {
049         imageRegistry.put(Constants.CONSOLE_IMAGE, 
050                 org.apache.qpid.management.ui.Activator.getImageDescriptor("/icons/qpidmc.gif"));
051         imageRegistry.put(Constants.CLOSED_FOLDER_IMAGE, 
052                 org.apache.qpid.management.ui.Activator.getImageDescriptor("/icons/icon_ClosedFolder.gif"));
053         imageRegistry.put(Constants.OPEN_FOLDER_IMAGE,
054                 PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
055         imageRegistry.put(Constants.MBEAN_IMAGE,
056                 PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
057         imageRegistry.put(Constants.NOTIFICATION_IMAGE,
058                 org.apache.qpid.management.ui.Activator.getImageDescriptor("/icons/notifications.gif"));
059     }
060     
061     static
062     {
063         fontRegistry.put(Constants.FONT_BUTTON, new FontData[]{new FontData("Arial"8, SWT.BOLD)} );
064         fontRegistry.put(Constants.FONT_BOLD, new FontData[]{new FontData("Bold"9, SWT.BOLD)} );
065         fontRegistry.put(Constants.FONT_ITALIC, new FontData[]{new FontData("Italic"9, SWT.ITALIC)} );
066         fontRegistry.put(Constants.FONT_TABLE_CELL, new FontData[]{new FontData("Tablecell"8, SWT.NORMAL)} );
067         fontRegistry.put(Constants.FONT_NORMAL, new FontData[]{new FontData("Normal"9, SWT.NORMAL)} );
068     }
069     
070     /*
071      * This maps all the managed servers to the respective server registry.
072      * Server can be JMX MBeanServer or a C++ server
073      */
074     private static HashMap<ManagedServer, ServerRegistry> _serverRegistryMap = new HashMap<ManagedServer, ServerRegistry>();
075     
076     // This map gets updated when a server connection closes.
077     private static List<ManagedServer> _closedServerList = new CopyOnWriteArrayList<ManagedServer>();    
078     
079     public static Image getImage(String key)
080     {
081         return imageRegistry.get(key);
082     }
083     
084     public static Font getFont(String key)
085     {
086         return fontRegistry.get(key);
087     }
088     
089     public static void addServer(ManagedServer server, ServerRegistry registry)
090     {
091         _serverRegistryMap.put(server, registry);
092     }
093     
094     public static void removeServer(ManagedServer server)
095     {
096         _serverRegistryMap.remove(server);
097     }
098     
099     public static ServerRegistry getServerRegistry(ManagedServer server)
100     {
101         return _serverRegistryMap.get(server);
102     }
103     
104     public static ServerRegistry getServerRegistry(ManagedBean mbean)
105     {
106         ManagedServer server = mbean.getServer();
107         return getServerRegistry(server);
108     }
109     
110     public static boolean isServerConnected(ManagedServer server)
111     {
112         return _serverRegistryMap.containsKey(server);
113     }
114     
115     // remove the server from the registry
116     public static void serverConnectionClosed(ManagedServer server)
117     {
118         _closedServerList.add(server);
119         removeServer(server);
120     }
121     
122     /*
123      * Returns the lis of closed servers. The Thread in GUI, which keeps checking for closed connection
124      * will check this and will remove the server links from the GUI.
125      */
126     public static List<ManagedServer> getClosedServers()
127     {
128         if (_closedServerList.isEmpty())
129             return null;
130         
131         List<ManagedServer> list = new CopyOnWriteArrayList<ManagedServer>(_closedServerList);
132         _closedServerList.clear();
133         return list;
134     }
135 
136 }