File Source: Service.java

         /* 
    P/P   *  Method: com.dmdirc.plugins.Service__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  package com.dmdirc.plugins;
    23  
    24  import java.util.List;
    25  import java.util.ArrayList;
    26  
    27  /**
    28   * Defines a service provided by a ServiceProvider.
    29   */
    30  public class Service {
    31  	/** Service Type. */
    32  	private final String type;
    33  	
    34  	/** Service Name. */
    35  	private final String name;
    36  	
    37  	/** List of ServiceProviders that implement this service. */
    38  	private List<ServiceProvider> serviceproviders = new ArrayList<ServiceProvider>();
    39  	
    40  	/**
    41  	 * Create a new Service
    42  	 *
    43  	 * @param type Type of this service
    44  	 * @param name Name of this service
    45  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.plugins.Service(String, String)
        	  * 
        	  *  Postconditions:
        	  *    this.name == name
        	  *    init'ed(this.name)
        	  *    this.serviceproviders == &amp;new ArrayList(Service#1)
        	  *    this.type == type
        	  *    init'ed(this.type)
        	  *    new ArrayList(Service#1) num objects == 1
        	  */
    46  	protected Service(final String type, final String name) {
    47  		this.type = type;
    48  		this.name = name;
    49  	}
    50  	
    51  	/**
    52  	 * Get the name of this service
    53  	 *
    54  	 * @return The name of this service
    55  	 */
    56  	public String getName() {
        		 /* 
    P/P 		  *  Method: String getName()
        		  * 
        		  *  Postconditions:
        		  *    return_value == this.name
        		  *    init'ed(return_value)
        		  */
    57  		return name;
    58  	}
    59  	
    60  	/**
    61  	 * Get the type of this service
    62  	 *
    63  	 * @return The type of this service
    64  	 */
    65  	public String getType() {
        		 /* 
    P/P 		  *  Method: String getType()
        		  * 
        		  *  Postconditions:
        		  *    return_value == this.type
        		  *    init'ed(return_value)
        		  */
    66  		return type;
    67  	}
    68  	
    69  	/**
    70  	 * Add the given ServiceProvider as a provider for this service.
    71  	 *
    72  	 * @param provider ServiceProvider that provides this service
    73  	 */
    74  	public void addProvider(final ServiceProvider provider) {
        		 /* 
    P/P 		  *  Method: void addProvider(ServiceProvider)
        		  * 
        		  *  Preconditions:
        		  *    this.serviceproviders != null
        		  */
    75  		serviceproviders.add(provider);
    76  	}
    77  	
    78  	/**
    79  	 * Remove the given ServiceProvider as a provider for this service.
    80  	 *
    81  	 * @param provider ServiceProvider that no longer provides this service
    82  	 */
    83  	public void delProvider(final ServiceProvider provider) {
        		 /* 
    P/P 		  *  Method: void delProvider(ServiceProvider)
        		  * 
        		  *  Preconditions:
        		  *    this.serviceproviders != null
        		  */
    84  		serviceproviders.remove(provider);
    85  	}
    86  	
    87  	/**
    88  	 * Get a list of ServiceProviders for this service
    89  	 *
    90  	 * @return List of ServiceProvider that provide this service
    91  	 */
    92  	public List<ServiceProvider> getProviders() {
        		 /* 
    P/P 		  *  Method: List getProviders()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.serviceproviders)
        		  * 
        		  *  Postconditions:
        		  *    return_value == &amp;new ArrayList(getProviders#1)
        		  *    new ArrayList(getProviders#1) num objects == 1
        		  */
    93  		return new ArrayList<ServiceProvider>(serviceproviders);
    94  	}
    95  	
    96  	/**
    97  	 * Get the first active ServiceProvider for this service, or null.
    98  	 *
    99  	 * @return First active ServiceProvider for this service, or null.
   100  	 */
   101  	public ServiceProvider getActiveProvider() {
        		 /* 
    P/P 		  *  Method: ServiceProvider getActiveProvider()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.serviceproviders)
        		  * 
        		  *  Presumptions:
        		  *    java.util.Iterator:next(...)@102 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@102: {0}, {1}
        		  */
   102  		for (ServiceProvider provider : getProviders()) {
   103  			if (provider.isActive()) {
   104  				return provider;
   105  			}
   106  		}
   107  		
   108  		return null;
   109  	}
   110  	
   111  	/**
   112  	 * Check if this service is active.
   113  	 *
   114  	 * @return True if this service is currently active.
   115  	 */
   116  	public boolean isActive() {
   117  		// Service is known, check that at least 1 plugin that provides it is loaded
        		 /* 
    P/P 		  *  Method: bool isActive()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(this.serviceproviders)
        		  * 
        		  *  Presumptions:
        		  *    java.util.Iterator:next(...)@118 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@118: {0}, {1}
        		  */
   118  		for (ServiceProvider provider : getProviders()) {
   119  			if (provider.isActive()) {
   120  				return true;
   121  			}
   122  		}
   123  		
   124  		return false;
   125  	}
   126  		
   127  	
   128  	/**
   129  	 * Activate this service.
   130  	 *
   131  	 * @return True if this service is already active, or if it was activated.
   132  	 */
   133  	public boolean activate() {
        		 /* 
    P/P 		  *  Method: bool activate()
        		  * 
        		  *  Preconditions:
        		  *    (soft) init'ed(this.serviceproviders)
        		  * 
        		  *  Presumptions:
        		  *    java.util.Iterator:next(...)@138 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    java.util.Iterator:hasNext(...)@138: {0}, {1}
        		  *    provider.plugin@138: Inverse{null}, Addr_Set{null}
        		  */
   134  		if (isActive()) { return true; }
   135  		
   136  		// If none of the plugins that provide the service are loaded, load the
   137  		// first one that registered itself as the provider
   138  		for (ServiceProvider provider : getProviders()) {
   139  			if (!provider.isActive()) {
   140  				provider.activateServices();
   141  				if (provider.isActive()) {
   142  					return true;
   143  				}
   144  			}
   145  		}
   146  		
   147  		return false;
   148  	}
   149  	
   150  	/**
   151  	 * Get this service as a String
   152  	 *
   153  	 * @return String representation of this service
   154  	 */
   155  	public String toString() {
        		 /* 
    P/P 		  *  Method: String toString()
        		  * 
        		  *  Preconditions:
        		  *    this.serviceproviders != null
        		  * 
        		  *  Postconditions:
        		  *    java.lang.StringBuilder:toString(...)._tainted == 0
        		  *    return_value == &amp;java.lang.StringBuilder:toString(...)
        		  */
   156  		return "Service: "+type+"->"+name+" (Providers: "+serviceproviders.size()+")";
   157  	}
   158  }








SofCheck Inspector Build Version : 2.17854
Service.java 2009-Jun-25 01:54:24
Service.class 2009-Sep-02 17:04:13