Coverage Report - argos.naming.NamingService
 
Classes in this File Line Coverage Branch Coverage Complexity
NamingService
0%
0/112
0%
0/46
0
 
 1  
 /*
 2  
 Copyright (c) 2006, University of Tromsø
 3  
 All rights reserved.
 4  
 
 5  
 Redistribution and use in source and binary forms, with or without 
 6  
 modification, are permitted provided that the following conditions are met:
 7  
 
 8  
  * Redistributions of source code must retain the above copyright notice, this list 
 9  
    of conditions and the following disclaimer.
 10  
 
 11  
  * Redistributions in binary form must reproduce the above copyright notice, this 
 12  
    list of conditions and the following disclaimer in the documentation and/or other 
 13  
    materials provided with the distribution.
 14  
 
 15  
  * Neither the name of the University of Tromsø nor the names of its contributors may 
 16  
    be used to endorse or promote products derived from this software without specific 
 17  
    prior written permission.
 18  
 
 19  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 20  
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 21  
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 22  
 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 23  
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 24  
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 25  
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 26  
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 28  
 DAMAGE.
 29  
 */
 30  
 
 31  
 package argos.naming;
 32  
 
 33  
 import java.io.File;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Collections;
 36  
 import java.util.HashMap;
 37  
 import java.util.List;
 38  
 import java.util.Map;
 39  
 import java.util.logging.Logger;
 40  
 import argos.annotation.Description;
 41  
 import argos.annotation.Impact;
 42  
 import argos.annotation.RemoveInstrumentation;
 43  
 import argos.deploy.ComponentManager;
 44  
 import argos.deploy.HotDeployer;
 45  
 import argos.logging.Console;
 46  
 import argos.metadata.ComponentMetaInfo;
 47  
 import argos.metadata.ServiceMetaInfo;
 48  
 import argos.proxy.NotificationProxy;
 49  
 import argos.util.ComponentComparator;
 50  
 
 51  
 /**
 52  
  * Created on 04.jul.2006
 53  
  * 
 54  
  * @author Dan Peder Eriksen
 55  
  */
 56  
 @Description("This component used used to see what components are availible in the system")
 57  
 public class NamingService {
 58  0
         private static final Logger logger = Logger.getLogger(NamingService.class.getName());
 59  0
         private static NamingService singelton = new NamingService();
 60  
         
 61  
         public static NamingService getInstance() {
 62  0
                 return singelton;
 63  
         }
 64  
         private Map<ComponentMetaInfo, Object> componentMetaToComponent;
 65  
         private Map<Object, ComponentMetaInfo> componentToComponentMeta;
 66  
         private Map<File, ServiceMetaInfo> fileToService;
 67  
         private Map<File, Long> timestamps;
 68  
         private List<NotificationProxy> proxies;
 69  
         private Map<String, ComponentMetaInfo> stringToComponentMeta;
 70  
         private Map<String, ComponentMetaInfo> mbeanToComponentMeta;
 71  
         private Map<String, ServiceMetaInfo> stringToService;
 72  
         private List<File> loadedFiles;
 73  
         
 74  
         private NamingService() {
 75  0
                 super();
 76  0
                 componentMetaToComponent = new HashMap<ComponentMetaInfo, Object>();
 77  0
                 stringToComponentMeta = new HashMap<String, ComponentMetaInfo>();
 78  0
                 mbeanToComponentMeta = new HashMap<String, ComponentMetaInfo>();
 79  0
                 stringToService = new HashMap<String, ServiceMetaInfo>();
 80  0
                 componentToComponentMeta = new HashMap<Object, ComponentMetaInfo>();
 81  0
                 fileToService = new HashMap<File, ServiceMetaInfo>();
 82  0
                 timestamps = new HashMap<File, Long>();
 83  0
                 proxies = new ArrayList<NotificationProxy>();
 84  0
                 loadedFiles = new ArrayList<File>();
 85  0
         }
 86  
         
 87  
         @Impact(Impact.ACTION)
 88  
         @Description("Adds a component")
 89  
         public synchronized boolean addComponent(ComponentMetaInfo meta, Object mbean) {
 90  0
                 String name = meta.getName();
 91  0
                 if(componentMetaToComponent.get(meta) == null) {
 92  0
                         componentMetaToComponent.put(meta, mbean);
 93  0
                         componentToComponentMeta.put(mbean, meta);
 94  0
                         stringToComponentMeta.put(name, meta);
 95  0
                         mbeanToComponentMeta.put(meta.getMbeanName(), meta);
 96  0
                         logger.finest("Component added to naming service, " + name);
 97  0
                         return true;
 98  
                 }
 99  
                 else {
 100  0
                         logger.severe("Didnt add component " + name + " to naming service, already exists.");
 101  0
                         return false;
 102  
                 }
 103  
         }
 104  
         
 105  
         @Impact(Impact.ACTION)
 106  
         @Description("Adds a notifcation proxy")
 107  
         public synchronized boolean addNotificationProxy(NotificationProxy proxy) {
 108  0
                 if(proxies.contains(proxy)) {
 109  0
                         logger.severe("Didnt add proxy " + proxy + " to naming service, already exists.");
 110  0
                         return false;
 111  
                 }
 112  
                 else {
 113  0
                         proxies.add(proxy);
 114  0
                         return true;
 115  
                 }
 116  
         }
 117  
         
 118  
         @RemoveInstrumentation
 119  
         public synchronized void addService(ServiceMetaInfo service) {
 120  0
                 stringToService.put(service.getName(), service);
 121  0
                 fileToService.put(service.getFile(), service);
 122  0
                 timestamps.put(service.getFile(), service.getFile().lastModified());
 123  0
                 loadedFiles.add(service.getFile());
 124  0
         }
 125  
         
 126  
         @Impact(Impact.INFO)
 127  
         @Description("Gets all the components metadata")
 128  
         public List<ComponentMetaInfo> getAllComponentMetaData() {
 129  0
                 List<ComponentMetaInfo> list = new ArrayList<ComponentMetaInfo>();
 130  0
                 for(String key : stringToComponentMeta.keySet()) {
 131  0
                         list.add(stringToComponentMeta.get(key));
 132  
                 }
 133  0
                 Collections.sort(list, new ComponentComparator());
 134  0
                 return list;
 135  
         }
 136  
         
 137  
         @RemoveInstrumentation
 138  
         public Object getBangBang(String name) {
 139  0
                 if("!!HotDeployer".equals(name)) {
 140  0
                         return HotDeployer.getInstance();
 141  
                 }
 142  0
                 else if("!!ComponentManager".equals(name)) {
 143  0
                         return ComponentManager.getInstance();
 144  
                 }
 145  0
                 else if("!!Console".equals(name)) {
 146  0
                         return Console.console;
 147  
                 }
 148  
                 else {
 149  0
                         return getComponentByName(name);
 150  
                 }
 151  
         }
 152  
         
 153  
         @Impact(Impact.INFO)
 154  
         @Description("Looks up a component")
 155  
         public Object getComponentByMeta(ComponentMetaInfo meta) {
 156  0
                 Object o = componentMetaToComponent.get(meta);
 157  0
                 if(o == null) {
 158  0
                         logger.warning("Unable to find Component by ComonentMetaInfo: " + meta.getName());
 159  
                 }
 160  0
                 return o; 
 161  
         }
 162  
         
 163  
         @Impact(Impact.INFO)
 164  
         @Description("Looks up a component, by name")
 165  
         public Object getComponentByName(String name) {
 166  0
                 Object o = componentMetaToComponent.get(stringToComponentMeta.get(name));
 167  0
                 if(o == null) {
 168  0
                         logger.warning("Unable to find Component by name: " + name);
 169  
                 }
 170  0
                 return o; 
 171  
         }
 172  
         
 173  
         @Impact(Impact.INFO)
 174  
         @Description("Looks up a components metadata, by name")
 175  
         public ComponentMetaInfo getComponentMetaByName(String name) {
 176  0
                 return getComponentMetaByName(name, true);
 177  
         }
 178  
         
 179  
         @Impact(Impact.INFO)
 180  
         @Description("Looks up a components metadata, by name")
 181  
         public ComponentMetaInfo getComponentMetaByName(String name, boolean verbose) {
 182  0
                 ComponentMetaInfo meta = stringToComponentMeta.get(name);
 183  0
                 if(verbose && meta == null) {
 184  0
                         logger.warning("Unable to find component meta data by name: " + name);
 185  
                 }
 186  0
                 return meta;
 187  
         }
 188  
         
 189  
         @Impact(Impact.INFO)
 190  
         @Description("Looks up a components metadata, by component")
 191  
         public ComponentMetaInfo getComponentMetaByComponent(Object component) {
 192  0
                 ComponentMetaInfo meta = componentToComponentMeta.get(component);
 193  0
                 if(meta == null) {
 194  0
                         logger.warning("Unable to find component meta data by component: " + component.getClass().toString());
 195  
                 }
 196  0
                 return meta;
 197  
         }
 198  
         
 199  
 
 200  
         @Impact(Impact.INFO)
 201  
         @Description("Looks up a components metadata, by mbean name")
 202  
         public ComponentMetaInfo getComponentMetaByMBeanName(String mbean) {
 203  0
                 ComponentMetaInfo meta = mbeanToComponentMeta.get(mbean);
 204  0
                 if(meta == null) {
 205  0
                         logger.warning("Unable to find component meta data by mbean name: " + mbean);
 206  
                 }
 207  0
                 return meta;
 208  
         }
 209  
         
 210  
         @Impact(Impact.INFO)
 211  
         @Description("Returns a list of all registered components")
 212  
         public List<ComponentMetaInfo> getComponentMetaDatas() {
 213  0
                 List<ComponentMetaInfo> results = new ArrayList<ComponentMetaInfo>();
 214  0
                 for(ComponentMetaInfo key : componentMetaToComponent.keySet()) {
 215  0
                         results.add(key);
 216  
                 }
 217  0
                 return results;
 218  
         }
 219  
         
 220  
         @Impact(Impact.INFO)
 221  
         @Description("Returns a list of all loaded files")
 222  
         public List<File> getLoadedFiles() {
 223  0
                 return loadedFiles;
 224  
         }
 225  
         
 226  
         @Impact(Impact.INFO)
 227  
         @Description("Looks up the Notification proxy for the component")
 228  
         public NotificationProxy getNotificationProxy(Object object) {
 229  0
                 if(object == HotDeployer.getInstance()) {
 230  0
                         return HotDeployer.getInstance().proxy;
 231  
                 }
 232  0
                 if(object == ComponentManager.getInstance()) {
 233  0
                         return ComponentManager.getInstance().proxy;
 234  
                 }
 235  0
                 for(NotificationProxy np : proxies) {
 236  0
                         if(np.getMbean() == object) {
 237  0
                                 logger.finest("Found NotificationProxy for " + object.toString());
 238  0
                                 return np;
 239  
                         }
 240  
                 }
 241  0
                 logger.severe("Didnt find NotificationProxy for " + object.toString());
 242  0
                 return null;
 243  
         }
 244  
         
 245  
         @Description("Looks up a service by name.")
 246  
         @Impact(Impact.INFO)
 247  
         public ServiceMetaInfo getServiceByName(String name, boolean verbose) {
 248  0
                 ServiceMetaInfo s = stringToService.get(name);
 249  0
                 if(verbose && s == null) {
 250  0
                         logger.warning("Unable to find service by name: " + name);
 251  
                 }
 252  0
                 return s;
 253  
         }
 254  
         
 255  
         @Impact(Impact.INFO)
 256  
         @Description("Gets the service meta data, by name")
 257  
         public ServiceMetaInfo getServiceByName(String name) {
 258  0
                 return getServiceByName(name, true);
 259  
         }
 260  
         
 261  
         @Description("Looks up a service by name.")
 262  
         @Impact(Impact.INFO)
 263  
         public ServiceMetaInfo getServiceByFile(File file) {
 264  0
                 return fileToService.get(file);
 265  
         }
 266  
         
 267  
         @Impact(Impact.INFO)
 268  
         @Description("Gets the meta data of all services loaded.")
 269  
         public List<ServiceMetaInfo> getAllServices() {
 270  0
                 List<ServiceMetaInfo> list = new ArrayList<ServiceMetaInfo>();
 271  0
                 for(String name : stringToService.keySet()) {
 272  0
                         list.add(getServiceByName(name));
 273  
                 }
 274  0
                 return list;
 275  
         }
 276  
         
 277  
         @Impact(Impact.INFO)
 278  
         @Description("Checks if a component is loaded, by meta info")
 279  
         public boolean isComponentLoaded(ComponentMetaInfo meta) {
 280  0
                 return componentMetaToComponent.containsKey(meta);
 281  
         }
 282  
         
 283  
         @Impact(Impact.INFO)
 284  
         @Description("Checks if a component is loaded, by name")
 285  
         public boolean isComponentLoaded(String name) {
 286  0
                 return stringToComponentMeta.containsKey(name);
 287  
         }
 288  
         
 289  
         @Impact(Impact.INFO)
 290  
         @Description("Checks if a service is loaded, by file")
 291  
         public boolean isFileLoaded(File file) {
 292  0
                 return getServiceByFile(file) != null;
 293  
         }
 294  
         
 295  
         @Impact(Impact.ACTION)
 296  
         @Description("Removes a component")
 297  
         public synchronized void removeComponent(ComponentMetaInfo meta) {
 298  0
                 String name = meta.getName();
 299  0
                 Object comp = componentMetaToComponent.remove(meta);
 300  0
                 stringToComponentMeta.remove(name);
 301  0
                 componentToComponentMeta.remove(comp);
 302  0
                 mbeanToComponentMeta.remove(meta.getMbeanName());
 303  0
                 proxies.remove(comp);
 304  0
                 logger.finest("Removing component " + name + " from naming service");
 305  0
         }
 306  
         
 307  
         @RemoveInstrumentation
 308  
         public synchronized void removeService(ServiceMetaInfo service) {
 309  0
                 stringToService.remove(service.getName());
 310  0
                 fileToService.remove(service.getFile());
 311  0
                 timestamps.remove(service.getFile());
 312  0
                 loadedFiles.remove(service.getFile());
 313  0
         }
 314  
         
 315  
 
 316  
         
 317  
         /**
 318  
          * Checks to see if the given file is newer than the one that 
 319  
          * is loaded. Its assumed that the file is already loaded.
 320  
          * 
 321  
          * @param jarFile        The jar file checking is done against.
 322  
          * @return        True if the given jar file is never, false otherwise.
 323  
          */
 324  
         @Description("Checks if a file is newer than the one already loaded")
 325  
         @Impact(Impact.ACTION)
 326  
         public boolean isNewer(File jarFile) {
 327  0
                 Long time = timestamps.get(jarFile);
 328  0
                 if(time == null) {
 329  0
                         return true;
 330  
                 }
 331  
                 else {
 332  0
                         return jarFile.lastModified() > time;
 333  
                 }
 334  
         }
 335  
 }